MultiMatchQuery example

$value = $this->parseValue($definition$filter$filter->getValue());
            $query = new TermQuery($fieldName$value);

            if ($field instanceof TranslatedField) {
                $multiMatchFields = [];

                foreach ($context->getLanguageIdChain() as $languageId) {
                    $multiMatchFields[] = $this->getTranslatedFieldName($fieldName$languageId);
                }

                $query = new MultiMatchQuery($multiMatchFields$value[
                    'type' => 'best_fields',
                ]);
            }

            return $this->createNestedQuery($query$definition$filter->getField());
        }

        $fieldName = $this->buildAccessor($definition$filter->getField()$context);

        if ($filter->getValue() === null) {
            $query = new BoolQuery();
            
$boolQuery->addParameter('minimum_should_match', 1);
        $boolQuery->add($this->getBestFieldQuery($term), BoolQuery::SHOULD);
        foreach ($this->maxExpansions as $field => $maxExpansion) {
            $boolQuery->add($this->getPhrasePrefixQuery($term$field$maxExpansion), BoolQuery::SHOULD);
        }

        return $boolQuery;
    }

    private function getBestFieldQuery(string $term): MultiMatchQuery
    {
        return new MultiMatchQuery(
            [
                'name^7',
                'name.*_analyzer^7',

                'keywords^5',
                'keywords.*_analyzer^5',

                'manufacturer.name^3',
                'manufacturer.name.*_analyzer^3',

                'shortDescription',
                
$this->handleFilter($criteriaPart$criteria$search$context);
    }

    private function addQuery(SimilarProductCondition $criteriaPart, Search $search): void
    {
        $productId = $criteriaPart->getProductId();
        $productName = $criteriaPart->getProductName();
        $categories = $this->getProductCategories($productId);

        $query = new BoolQuery();

        $nameQuery = new MultiMatchQuery(['name', 'keywords']$productName['boost' => 5]);
        $categoriesQuery = new TermsQuery('categoryIds', $categories['boost' => 0.2]);

        $query->add($nameQuery, BoolQuery::SHOULD);
        $query->add($categoriesQuery, BoolQuery::MUST);

        $not = new BoolQuery();
        $not->add(new TermQuery('id', $productId), BoolQuery::MUST_NOT);

        $search->addQuery($not, BoolQuery::FILTER);
        $search->addQuery($query);
    }

    
$multiMatchFields[] = $searchField;
            $matchPhraseFields[] = $searchField;

            if ($config->tokenize()) {
                $ngramField = $this->buildTranslatedFieldName($config$languageId, 'ngram');
                $fuzzyMatchFields[] = $searchField;
                $ngramFields[] = $ngramField;
            }
        }

        $queries = [
            new MultiMatchQuery($multiMatchFields$token[
                'type' => 'best_fields',
                'fuzziness' => 0,
                'boost' => $config->getRanking() * 5,
            ]),
            new MultiMatchQuery($matchPhraseFields$token[
                'type' => 'phrase_prefix',
                'slop' => 5,
                'boost' => $config->getRanking(),
            ]),
        ];

        
Home | Imprint | This part of the site doesn't use cookies.