Skip to content

Commit

Permalink
Update advanced search layout and action code for ES
Browse files Browse the repository at this point in the history
  • Loading branch information
sevein committed Sep 29, 2012
1 parent 2284dd9 commit 206711a
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 190 deletions.
254 changes: 134 additions & 120 deletions apps/qubit/modules/search/actions/advancedAction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,9 @@ class SearchAdvancedAction extends SearchIndexAction
'levelOfDescription',
'materialType',
'mediaType',
'repository',
'searchFields'
'repository'
);

public function execute($request)
{
if ('print' == $request->getGetParameter('media'))
{
$this->getResponse()->addStylesheet('print-preview', 'last');
}

$this->form = new sfForm;
$this->form->getValidatorSchema()->setOption('allow_extra_fields', true);

foreach ($this::$NAMES as $name)
{
$this->addField($name);
}

$this->form->bind($request->getRequestParameters() + $request->getGetParameters() + $request->getPostParameters());

if ($this->form->isValid())
{
if (isset($request->searchFields))
{
$this->queryTerms = array();

// we are handling a search request
parent::execute($request);
}
}

}

protected function addField($name)
{
switch ($name)
Expand Down Expand Up @@ -166,133 +135,178 @@ protected function addField($name)
$this->form->setValidator($name, new sfValidatorChoice(array('choices' => array_keys($choices))));
$this->form->setWidget($name, new sfWidgetFormSelect(array('choices' => $choices)));

break;
}
}

protected function processField($field, $queryBool)
{
if (null === $value = $this->form->getValue($field->getName()))
{
return;
}

$this->hasFilters = true;

switch ($field->getName())
{
case 'copyrightStatus':
$query = new Elastica_Query_Term;
$query->setTerm('copyrightStatusId', $value);
$queryBool->addMust($query);
break;

case 'hasDigitalObject':
$query = new Elastica_Query_Term;
$query->setTerm('hasDigitalObject', $value);
$queryBool->addMust($query);

break;

case 'levelOfDescription':
$query = new Elastica_Query_Term;
$query->setTerm('levelOfDescriptionId', $value);
$queryBool->addMust($query);

break;

case 'materialType':
$query = new Elastica_Query_Term;
$query->setTerm('materialTypeId', $value);
$queryBool->addMust($query);

break;

case 'mediaType':
$query = new Elastica_Query_Term;
$query->setTerm('digitalObject.mediaTypeId', $value);
$queryBool->addMust($query);

break;

case 'searchFields':
case 'repository':
$query = new Elastica_Query_Term;
$query->setTerm('repositoryId', $value);
$queryBool->addMust($query);

break;
}
}

public function parseQuery()
protected function parseQuery()
{
QubitSearch::getInstance();
$queryBuilt = new Zend_Search_Lucene_Search_Query_Boolean();
$queryBool = new Elastica_Query_Bool();

if (!isset($this->request->searchFields))
{
throw new Exception('Search is empty');
}

foreach ($this->request->searchFields as $searchField)
// Iterate over search fields
foreach ($this->request->searchFields as $key => $item)
{
// if no terms for this field, skip it
if (empty($searchField['query']))
if (empty($item['query']))
{
continue;
}

// enclose phrase searches in quotes (strip existing ones)
if ('phrase' == $searchField['match'])
{
$term = '"'.str_replace(array('"', "'"), '', strtolower($searchField['query'])).'"';
}
else
{
$term = strtolower($searchField['query']);
}
$hasAnySearchField = true;

$matchString = $term;
$queryText = new Elastica_Query_Text();

// limit to specified field
if (!empty($searchField['field']))
switch ($item['field'])
{
$term = $searchField['field'] . ':' . $term;
}
case 'identifier':
$queryText->setFieldQuery('identifier', $item['query']);

if (!empty($searchField['field']))
{
$field = ucfirst($searchField['field']);
}
else
{
$field = ('phrase' == $searchField['match']) ? $this->context->i18n->__('Phrase') : $this->context->i18n->__('Keyword(s)');
}
break;

$this->queryTerms[] = array('term' => $field.': '.$matchString, 'operator' => $searchField['operator']);
case 'title':
$queryText->setFieldQuery('i18n.title', $item['query']);

// select which boolean operator to use
if (!isset($searchField['operator'])) $searchField['operator'] = null;
switch ($searchField['operator'])
{
case 'not':
$token = false;
break;

case 'or':
$token = null;
case 'scopeAndContent':
$queryText->setFieldQuery('i18n.scopeAndContet', $item['query']);

break;

case 'archivalHistory':
$queryText->setFieldQuery('i18n.archivalHistory', $item['query']);

break;

case 'extentAndMedium':
$queryText->setFieldQuery('i18n.extentAndMedium', $item['query']);

break;

case 'creatorHistory':
$queryText->setFieldQuery('', $item['query']);

break;

case 'subject':
$queryText->setFieldQuery('', $item['query']);

break;

case 'name':
$queryText->setFieldQuery('', $item['query']);

break;

case 'place':
$queryText->setFieldQuery('', $item['query']);

break;

case 'and':
default:
$token = true;
$queryText->setFieldQuery('_all', $item['query']);

break;
}

$queryBuilt->addSubquery(QubitSearch::getInstance()->parse($term), $token);
}

$query = new Zend_Search_Lucene_Search_Query_Boolean();
$query->addSubquery($queryBuilt, true);
if (0 == $key)
{
$item['operator'] == 'add';
}

return $query;
}
switch ($item['operator'])
{
case 'not':
$queryBool->addMustNot($queryText);

public function filterQuery($query)
{
// limit to a repository if selected
if (!empty($this->request->repository))
{
$query->addSubquery(QubitSearch::getInstance()->addTerm($this->request->repository, 'repositoryId'), true);
$this->queryTerms[] = array('term' => $this->context->i18n->__('Repository').': '.QubitRepository::getById($this->request->repository)->__toString(), 'operator' => 'and');
}
break;

// digital object filters
if ('true' == $this->request->hasDigitalObject)
{
$query->addSubquery(QubitSearch::getInstance()->addTerm('true', 'hasDigitalObject'), true);
$this->queryTerms[] = array('term' => $this->context->i18n->__('Digital object is available'), 'operator' => 'and');
}
else if ('false' == $this->request->hasDigitalObject)
{
$query->addSubquery(QubitSearch::getInstance()->addTerm('false', 'hasDigitalObject'), true);
$this->queryTerms[] = array('term' => $this->context->i18n->__('No digital object is available'), 'operator' => 'and');
}
case 'or':
$queryBool->addShould($queryText);

// limit to a media type if selected
if (!empty($this->request->mediaType))
{
$query->addSubquery(QubitSearch::getInstance()->addTerm($this->request->mediaType, 'mediaTypeId'), true);
$this->queryTerms[] = array('term' => 'mediaType: '.QubitTerm::getById($this->request->mediaType)->__toString(), 'operator' => 'and');
}
break;

// limit to a material type if selected
if (!empty($this->request->materialType))
{
$query->addSubquery(QubitSearch::getInstance()->addTerm($this->request->materialType, 'materialTypeId'), true);
$this->queryTerms[] = array('term' => 'materialType: '.QubitTerm::getById($this->request->materialType)->__toString(), 'operator' => 'and');
}
case 'add':
default:
$queryBool->addMust($queryText);

if (!empty($this->request->levelOfDescription))
{
$query->addSubquery(QubitSearch::getInstance()->addTerm($this->request->levelOfDescription, 'levelOfDescriptionId'), true);
$this->queryTerms[] = array('term' => $this->context->i18n->__('Level of description').': '.$this->request->levelOfDescription, 'operator' => 'and');
break;
}
}

// Copyright status
if (!empty($this->request->copyrightStatus))
// Process filters passing $queryBool
$this->hasFilters = false;
foreach ($this->form as $field)
{
$query->addSubquery(QubitSearch::getInstance()->addTerm($this->request->copyrightStatus, 'copyrightStatusId'), true);
$this->queryTerms[] = array('term' => 'copyrightStatus: '.QubitTerm::getById($this->request->copyrightStatus)->__toString(), 'operator' => 'and');
if (isset($this->request[$field->getName()]))
{
$this->processField($field, $queryBool);
}
}

$query = parent::filterQuery($query);
return $queryBool;
}

return $query;
public function execute($request)
{
parent::execute($request);
}
}
14 changes: 5 additions & 9 deletions apps/qubit/modules/search/actions/indexAction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ class SearchIndexAction extends sfAction
'places.id',
'names.id');

protected function addField($name)
{
}

protected function parseQuery()
{
// if querystring is empty, use match_all
Expand Down Expand Up @@ -127,16 +123,16 @@ protected function buildQuery()
$query = $this->facetQuery($query);

$query->setSort(array('_score' => 'desc', 'slug' => 'asc'));
$query->setLimit($request->limit);
$query->setLimit($this->request->limit);

if (!empty($request->page))
if (!empty($this->request->page))
{
$query->setFrom(($request->page - 1) * $request->limit);
$query->setFrom(($this->request->page - 1) * $this->request->limit);
}

if (isset($request->realm) && is_int($request->realm))
if (isset($this->request->realm) && is_int($this->request->realm))
{
$query->setQuery(new Elastica_Query_Term(array('repository._id' => $request->realm)));
$query->setQuery(new Elastica_Query_Term(array('repository._id' => $this->request->realm)));
}

return $query;
Expand Down
Loading

0 comments on commit 206711a

Please sign in to comment.