Skip to content

Commit

Permalink
Fix ES' query string queries, refs #12186
Browse files Browse the repository at this point in the history
Splits multi-field string queries into boolean queries with a string query
clause per field. This allows ES to use the field's analyzer on each clause
instead of the default analyzer and therefore it generates better results,
specially working with stop-words in a multi-culture index.

Additionally:

- Simplify retrieval of all fields by not processing the enabled
  languages until the query generation.
- Modify and simplify boosting process, now required as part of the
  boolean query clauses and not appended to the fields.
- Remove unused parse function from arElasticSearchPlugin class.
  • Loading branch information
jraddaoui authored and sbreker committed Mar 18, 2021
1 parent e55cc29 commit 34adf96
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 366 deletions.
14 changes: 7 additions & 7 deletions apps/qubit/modules/default/actions/moveAction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ public function execute($request)

if (isset($request->query))
{
$query = new \Elastica\Query\QueryString(arElasticSearchPluginUtil::escapeTerm($request->query));
$query->setDefaultOperator('AND');
$query->setFields(array(
'identifier',
'referenceCode',
sprintf('i18n.%s.title', sfContext::getInstance()->user->getCulture())));
$this->queryBool->addMust($query);
$fields = array(
'identifier' => 1,
'referenceCode' => 1,
sprintf('i18n.%s.title', sfContext::getInstance()->user->getCulture()) => 1);
$this->queryBool->addMust(
arElasticSearchPluginUtil::generateBoolQueryString($request->query, $fields)
);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ public function execute($request)
}
else
{
$queryString = new \Elastica\Query\QueryString(arElasticSearchPluginUtil::escapeTerm($request->query));
$queryString->setDefaultOperator('AND');
$fields = array('i18n.'.$culture.'.title.autocomplete' => 1);

// Search for referenceCode or identifier, and title
if (1 == sfConfig::get('app_inherit_code_informationobject', 1))
{
$queryString->setFields(array('i18n.'.$culture.'.title.autocomplete', 'referenceCode.autocomplete'));
$fields['referenceCode.autocomplete'] = 1;

// Change sort order
$this->query->setSort(array(
Expand All @@ -67,10 +66,12 @@ public function execute($request)
}
else
{
$queryString->setFields(array('i18n.'.$culture.'.title.autocomplete', 'identifier'));
$fields['identifier'] = 1;
}

$this->queryBool->addMust($queryString);
$this->queryBool->addMust(
arElasticSearchPluginUtil::generateBoolQueryString($request->query, $fields)
);
}

// Filter results by parent
Expand Down
10 changes: 5 additions & 5 deletions apps/qubit/modules/repository/actions/browseAction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ public function execute($request)
}
else
{
$queryText = new \Elastica\Query\QueryString(arElasticSearchPluginUtil::escapeTerm($request->subquery));
$queryText->setDefaultOperator('OR');
arElasticSearchPluginUtil::setFields($queryText, 'repository');

$this->search->queryBool->addMust($queryText);
$this->search->queryBool->addMust(
arElasticSearchPluginUtil::generateBoolQueryString(
$request->subquery, arElasticSearchPluginUtil::getAllFields('repository')
)
);
}

$i18n = sprintf('i18n.%s.', $this->selectedCulture);
Expand Down
10 changes: 5 additions & 5 deletions apps/qubit/modules/search/actions/indexAction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public function execute($request)
{
parent::execute($request);

$queryText = new \Elastica\Query\QueryString(arElasticSearchPluginUtil::escapeTerm($request->query));
$queryText->setDefaultOperator('AND');
arElasticSearchPluginUtil::setFields($queryText, 'informationObject');

$this->search->queryBool->addMust($queryText);
$this->search->queryBool->addMust(
arElasticSearchPluginUtil::generateBoolQueryString(
$request->query, arElasticSearchPluginUtil::getAllFields('informationObject')
)
);

// Realm filter
if (isset($request->repos) && ctype_digit($request->repos))
Expand Down
15 changes: 6 additions & 9 deletions apps/qubit/modules/taxonomy/actions/indexAction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,33 +170,30 @@ public function execute($request)

if (1 !== preg_match('/^[\s\t\r\n]*$/', $request->subquery))
{
$queryString = new \Elastica\Query\QueryString(arElasticSearchPluginUtil::escapeTerm($request->subquery));

switch ($request->subqueryField)
{
case 'preferredLabel':
$queryString->setFields(arElasticSearchPluginUtil::getI18nFieldNames('i18n.%s.name'));
$fields = array('i18n.%s.name' => 1);

break;

case 'useForLabels':
$queryString->setFields(arElasticSearchPluginUtil::getI18nFieldNames('useFor.i18n.%s.name'));
$fields = array('useFor.i18n.%s.name' => 1);

break;

case 'allLabels':
default:
// Search over preferred label (boosted by five) and "Use for" labels
$fields = array('i18n.%s.name', 'useFor.i18n.%s.name');
$boost = array('i18n.%s.name' => 5);
$queryString->setFields(arElasticSearchPluginUtil::getI18nFieldNames($fields, null, $boost));
$queryString->setDefaultOperator('AND');
$fields = array('i18n.%s.name' => 5, 'useFor.i18n.%s.name' => 1);

break;
}

// Filter results by subquery
$this->queryBool->addMust($queryString);
$this->queryBool->addMust(
arElasticSearchPluginUtil::generateBoolQueryString($request->subquery, $fields)
);
}

// Set query
Expand Down
16 changes: 0 additions & 16 deletions plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,22 +520,6 @@ public function partialUpdate($object, $data)
}
}

/**
* Function helper to parse query strings
*/
public function parse(string $query)
{
if (empty($query))
{
throw new Exception('No search terms specified.');
}

$query = new \Elastica\Query\QueryString(arElasticSearchPluginUtil::escapeTerm($query));
$query->setDefaultOperator('AND');

return $query;
}

// ---------------------------------------------------------------------------

public function delete($object)
Expand Down
Loading

0 comments on commit 34adf96

Please sign in to comment.