Skip to content

Commit

Permalink
Adding named queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
romainruaud committed Jul 12, 2024
1 parent fa61b9b commit 2934a13
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public function getSearchQuery()
}
}

$queryParams['name'] = $this->asStringRecursive();
$query = $this->queryFactory->create(QueryInterface::TYPE_BOOL, $queryParams);

return $query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function getSearchQuery(ProductCondition $productCondition)
$this->prepareFieldValue($productCondition);
$queryType = QueryInterface::TYPE_TERMS;
$queryParams = $this->getTermsQueryParams($productCondition);
$queryParams['name'] = $productCondition->asString();

if ($productCondition->getInputType() === 'string' && !in_array($productCondition->getOperator(), ['()', '!()', '<=>'])) {
$queryType = QueryInterface::TYPE_MATCH;
Expand Down Expand Up @@ -121,6 +122,7 @@ public function getSearchQuery(ProductCondition $productCondition)
$nestedQueryParams['query'] = $nestedFilter;
}

$nestedQueryParams['name'] = $productCondition->asString();
$query = $this->queryFactory->create(QueryInterface::TYPE_NESTED, $nestedQueryParams);
}
}
Expand Down Expand Up @@ -179,7 +181,12 @@ private function getMatchQueryParams(ProductCondition $productCondition)
$queryText = $productCondition->getValue();
$minimumShouldMatch = "100%";

return ['field' => $fieldName, 'queryText' => $queryText, 'minimumShouldMatch' => $minimumShouldMatch];
return [
'field' => $fieldName,
'queryText' => $queryText,
'minimumShouldMatch' => $minimumShouldMatch,
'name' => $productCondition->asString(),
];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public function buildQuery(QueryInterface $query)
'boost' => $query->getBoost(),
];

$searchQuery = ['match' => [$query->getField() => $searchQueryParams]];

if ($query->getName()) {
$searchQuery['match']['_name'] = $query->getName();
$searchQueryParams['_name'] = $query->getName();
}

$searchQuery = ['match' => [$query->getField() => $searchQueryParams]];

return $searchQuery;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ public function buildQuery(QueryInterface $query)
throw new \InvalidArgumentException("Query builder : invalid query type {$query->getType()}");
}

return [
$searchQuery = [
'span_first' => [
'boost' => $query->getBoost(),
'match' => $this->parentBuilder->buildQuery($query->getMatch()),
'end' => $query->getEnd(),
],
];

if ($query->getName()) {
$searchQuery['span_first']['_name'] = $query->getName();
}

return $searchQuery;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function create(ContainerConfigurationInterface $containerConfig, $queryT
'query' => $this->getWeightedSearchQuery($containerConfig, $queryText),
'filter' => $this->getCutoffFrequencyQuery($containerConfig, $queryText),
'boost' => $boost,
'name' => 'EXACT',
];
$query = $this->queryFactory->create(QueryInterface::TYPE_FILTER, $queryParams);

Expand Down Expand Up @@ -223,6 +224,7 @@ private function getPureStopwordsQuery(ContainerConfigurationInterface $containe
'minimumShouldMatch' => "100%",
'tieBreaker' => $relevanceConfig->getTieBreaker(),
'boost' => $boost,
'name' => 'PURE_STOPWORDS',
];

return $this->queryFactory->create(QueryInterface::TYPE_MULTIMATCH, $queryParams);
Expand Down Expand Up @@ -254,7 +256,7 @@ private function getSpellcheckedQuery(ContainerConfigurationInterface $container
}

if (!empty($queryClauses)) {
$queryParams = ['should' => $queryClauses, 'boost' => $boost];
$queryParams = ['should' => $queryClauses, 'boost' => $boost, 'name' => 'FUZZY'];

if ($spellingType == SpellcheckerInterface::SPELLING_TYPE_MOST_FUZZY) {
$queryParams['must'] = [$this->getWeightedSearchQuery($containerConfig, $queryText)];
Expand Down Expand Up @@ -306,6 +308,7 @@ private function getFuzzyQuery(ContainerConfigurationInterface $containerConfig,
'tieBreaker' => $relevanceConfig->getTieBreaker(),
'fuzzinessConfig' => $relevanceConfig->getFuzzinessConfiguration(),
'cutoffFrequency' => $relevanceConfig->getCutoffFrequency(),
'name' => 'FUZZY',
];

return $this->queryFactory->create(QueryInterface::TYPE_MULTIMATCH, $queryParams);
Expand Down Expand Up @@ -338,6 +341,7 @@ private function getPhoneticQuery(ContainerConfigurationInterface $containerConf
'minimumShouldMatch' => $minimumShouldMatch,
'tieBreaker' => $relevanceConfig->getTieBreaker(),
'cutoffFrequency' => $relevanceConfig->getCutoffFrequency(),
'name' => 'PHONETIC',
];

return $this->queryFactory->create(QueryInterface::TYPE_MULTIMATCH, $queryParams);
Expand Down Expand Up @@ -394,7 +398,7 @@ private function getSpanQuery(ContainerConfigurationInterface $containerConfig,
$spanFieldsFilter = $this->fieldFilters['spannableFieldFilter'];
$spanFields = $containerConfig->getMapping()->getFields();
$spanFields = array_filter($spanFields, [$spanFieldsFilter, 'filterField']);
$spanQueryParams = ['boost' => $boost, 'end' => $wordCount];
$spanQueryParams = ['boost' => $boost, 'end' => $wordCount, 'name' => 'SPAN'];
$spanQueryType = SpanQueryInterface::TYPE_SPAN_FIRST;

if (count($spanFields) > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function getSearchQuery($excludedCategories = []): QueryInterface
}
}

$queryParams['name'] = $this->asStringRecursive();
$query = $this->queryFactory->create(QueryInterface::TYPE_BOOL, $queryParams);

return $query;
Expand Down

0 comments on commit 2934a13

Please sign in to comment.