Skip to content

Commit

Permalink
Merge pull request #186 from maximehuran/feature/nested-fields
Browse files Browse the repository at this point in the history
Manage search on nested fields
  • Loading branch information
maximehuran authored Aug 17, 2023
2 parents 1001fa3 + ceff6e8 commit 248cba5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
4 changes: 4 additions & 0 deletions dist/src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ services:
- 'name^5'
- 'description'
- 'short_description' # We add our new value in search
$nestedFieldsToSearch:
- 'main_taxon:name^2' # We add our new nested value in search
tags:
- { name: monsieurbiz.search.request.product_search_filter }

Expand All @@ -43,6 +45,8 @@ services:
- 'description'
- 'name.autocomplete'
- 'short_description' # We add our new value in instant search
$nestedFieldsToSearch:
- 'main_taxon:name^2' # We add our new nested value in instant search
tags:
- { name: monsieurbiz.search.request.product_instant_search_filter }

Expand Down
5 changes: 3 additions & 2 deletions src/Search/Request/QueryFilter/Product/SearchTermFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ final class SearchTermFilter extends BaseSearchTermFilter
public function __construct(
ProductAttributeRepositoryInterface $productAttributeRepository,
ProductOptionRepositoryInterface $productOptionRepository,
array $fieldsToSearch
array $fieldsToSearch,
array $nestedFieldsToSearch = []
) {
parent::__construct($fieldsToSearch);
parent::__construct($fieldsToSearch, $nestedFieldsToSearch);
$this->productAttributeRepository = $productAttributeRepository;
$this->productOptionRepository = $productOptionRepository;
}
Expand Down
44 changes: 43 additions & 1 deletion src/Search/Request/QueryFilter/SearchTermFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ class SearchTermFilter implements QueryFilterInterface
{
protected array $fieldsToSearch;

protected array $nestedFieldsToSearch;

public function __construct(
array $fieldsToSearch
array $fieldsToSearch,
array $nestedFieldsToSearch = []
) {
$this->fieldsToSearch = $fieldsToSearch;
$this->nestedFieldsToSearch = $nestedFieldsToSearch;
}

public function apply(BoolQuery $boolQuery, RequestConfiguration $requestConfiguration): void
Expand All @@ -37,6 +41,7 @@ public function apply(BoolQuery $boolQuery, RequestConfiguration $requestConfigu
$searchQuery = $qb->query()->bool();
$searchQuery->addShould($searchCode);
$this->addFieldsToSearchCondition($searchQuery, $requestConfiguration);
$this->addNestedFieldsToSearchCondition($searchQuery, $requestConfiguration);

$this->addCustomFilters($searchQuery, $requestConfiguration);

Expand All @@ -57,6 +62,43 @@ protected function addFieldsToSearchCondition(BoolQuery $searchQuery, RequestCon
$searchQuery->addShould($nameAndDescriptionQuery);
}

/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function addNestedFieldsToSearchCondition(BoolQuery $searchQuery, RequestConfiguration $requestConfiguration): void
{
if (0 === \count($this->nestedFieldsToSearch)) {
return;
}

$nestedFields = [];

// Group nested fields by nested path
foreach ($this->nestedFieldsToSearch as $nestedFieldToSearch) {
$nestedFieldExpression = explode(':', $nestedFieldToSearch);
if (2 !== \count($nestedFieldExpression)) {
continue;
}

$nestedFields[$nestedFieldExpression[0]][] = str_replace(':', '.', $nestedFieldToSearch);
}

// Create queries by nested path and nested values
$qb = new QueryBuilder();
foreach ($nestedFields as $nestedField => $nestedFieldExpressions) {
$nestedFieldValueQuery = $qb->query()->multi_match();
$nestedFieldValueQuery->setFields($nestedFieldExpressions);
$nestedFieldValueQuery->setQuery($requestConfiguration->getQueryText());
$nestedFieldValueQuery->setType(MultiMatch::TYPE_MOST_FIELDS);
$nestedFieldValueQuery->setFuzziness(MultiMatch::FUZZINESS_AUTO);

$nestedFieldQuery = $qb->query()->nested();
$nestedFieldQuery->setPath($nestedField)->setQuery($nestedFieldValueQuery);

$searchQuery->addShould($nestedFieldQuery);
}
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
Expand Down

0 comments on commit 248cba5

Please sign in to comment.