Skip to content

Commit

Permalink
Merge pull request Smile-SA#21 from Elastic-Suite/fix-ES-explain-shin…
Browse files Browse the repository at this point in the history
…gle-several-words

Fix regex to display also shingle matches with several words into the…
  • Loading branch information
rbayet authored Mar 22, 2021
2 parents ff9f054 + f1c069b commit 29a1a16
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/module-elasticsuite-explain/Model/Result/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,24 @@ private function getFieldMatches(array $explain)
$description = $explain['description'];

$matches = [];
if (preg_match('/^weight\(([^:]+):([^\s]+) in/', $description, $matches)) {
$field = $matches[1];
$query = $matches[2];
/**
* Example of descriptions that we want to retrieve:
* - weight(search:atom in 618) [PerFieldSimilarity], result of:
* - weight(Synonym(search.shingle:blue search.shingle:blue jacket) in 1318)
* [PerFieldSimilarity], result of:
* - weight(Synonym(name.shingle:atomic name.shingle:atomic endurance
* name.shingle:atomic endurance running) in 618) [PerFieldSimilarity], result of:
*/
if (preg_match(
'/^weight\((?:.*\()?(?:([^:]+):([^\)]*))\)? in/',
$description,
$matches
)) {
$field = $matches[1]; // We retrieve the field, like 'name.shingle'.
$termsMatch = $matches[2]; // We retrieve the part with terms, like 'blue search.shingle:blue jacket'.
$terms = explode("$field:", $termsMatch); // We have here an array like ['blue ', 'blue jacket'].
$terms = array_map("trim", $terms); // We remove unwanted spaces, ['blue', 'blue jacket'].
$query = implode(', ', $terms);
$score = $explain['value'] ?? 0;
$weight = 1;
if (array_key_exists('details', $explain)) {
Expand Down

0 comments on commit 29a1a16

Please # to comment.