MatchQuery example


        Feature::triggerDeprecationOrThrow(
            'ES_MULTILINGUAL_INDEX',
            'Will become abstract, implementation should implement their own `buildTermQuery`'
        );

        $bool = new BoolQuery();

        $term = (string) $criteria->getTerm();

        $queries = [
            new MatchQuery('fullTextBoosted', $term['boost' => 10]), // boosted word matches             new MatchQuery('fullText', $term['boost' => 5]), // whole word matches             new MatchQuery('fullText', $term['fuzziness' => 'auto', 'boost' => 3]), // word matches not exactly =>             new MatchPhrasePrefixQuery('fullText', $term['boost' => 1, 'slop' => 5]), // one of the words begins with: "Spachtel" => "Spachtelmasse"             new WildcardQuery('fullText', '*' . mb_strtolower($term) . '*'), // part of a word matches: "masse" => "Spachtelmasse"             new MatchQuery('fullText.ngram', $term),
        ];

        foreach ($queries as $query) {
            $bool->add($query, BoolQuery::SHOULD);
        }

        

    public function buildQuery(array $fields$term)
    {
        $tokens = $this->tokenize($term);

        $combines = $this->combine($tokens);

        $bool = new BoolQuery();
        foreach ($tokens as $token) {
            foreach ($fields as $field => $priority) {
                $bool->add(new MatchQuery($field$token['boost' => $priority]), BoolQuery::SHOULD);
                $bool->add(new WildcardQuery($field, '*' . strtolower($token) . '*'), BoolQuery::SHOULD);
            }
        }

        // use combination for more precision         foreach ($combines as $token) {
            foreach ($fields as $field => $priority) {
                $bool->add(new MatchQuery($field$token['boost' => $priority * 2]), BoolQuery::SHOULD);
            }
        }

        
if (!str_contains($item['field'], 'customFields')) {
                    $searchField = $item['field'] . '.search';
                    $ngramField = $item['field'] . '.ngram';
                } else {
                    $searchField = $item['field'];
                    $ngramField = $item['field'];
                }

                $queries = [];

                $queries[] = new MatchQuery($searchField$token['boost' => 5 * $ranking]);
                $queries[] = new MatchPhrasePrefixQuery($searchField$token['boost' => $ranking, 'slop' => 5]);
                $queries[] = new WildcardQuery($searchField, '*' . $token . '*');

                if ($item['tokenize']) {
                    $queries[] = new MatchQuery($searchField$token['fuzziness' => 'auto', 'boost' => 3 * $ranking]);
                    $queries[] = new MatchQuery($ngramField$token);
                }

                if (str_contains($item['field'], '.') && !str_contains($item['field'], 'customFields')) {
                    $nested = strtok($item['field'], '.');

                    
case ProductAttributeCondition::OPERATOR_GT:
                return new RangeQuery($field['gt' => $value]);

            case ProductAttributeCondition::OPERATOR_GTE:
                return new RangeQuery($field['gte' => $value]);

            case ProductAttributeCondition::OPERATOR_CONTAINS:
                if (!\is_string($value)) {
                    throw new RuntimeException('Invalid value for MatchQuery provided');
                }

                return new MatchQuery($field$value);

            case ProductAttributeCondition::OPERATOR_NOT_IN:
                if (!\is_array($value)) {
                    throw new RuntimeException('Invalid value for TermsQuery provided');
                }

                if ($type === 'string') {
                    $field .= '.raw';
                }
                $filter = new BoolQuery();
                $filter->add(new TermsQuery($field$value), BoolQuery::MUST_NOT);

                
$this->createMock(AbstractProductSearchQueryBuilder::class),
            $this->createMock(EsProductDefinition::class)
        );

        static::assertSame($productDefinition$definition->getEntityDefinition());
    }

    public function testBuildTermQueryUsingSearchQueryBuilder(): void
    {
        $searchQueryBuilder = $this->createMock(AbstractProductSearchQueryBuilder::class);
        $boolQuery = new BoolQuery();
        $boolQuery->add(new MatchQuery('name', 'test'));
        $searchQueryBuilder
            ->method('build')
            ->willReturn($boolQuery);

        $definition = new ElasticsearchProductDefinition(
            $this->createMock(ProductDefinition::class),
            $this->createMock(Connection::class),
            [],
            new EventDispatcher(),
            $searchQueryBuilder,
            $this->createMock(EsProductDefinition::class)
        );
'de' => 'german',
            ]
        );

        static::assertSame($productDefinition$definition->getEntityDefinition());
    }

    public function testBuildTermQueryUsingSearchQueryBuilder(): void
    {
        $searchQueryBuilder = $this->createMock(AbstractProductSearchQueryBuilder::class);
        $boolQuery = new BoolQuery();
        $boolQuery->add(new MatchQuery('name', 'test'));
        $searchQueryBuilder
            ->method('build')
            ->willReturn($boolQuery);

        $definition = new EsProductDefinition(
            $this->createMock(ProductDefinition::class),
            $this->createMock(Connection::class),
            [],
            new EventDispatcher(),
            $searchQueryBuilder,
            [
                
Home | Imprint | This part of the site doesn't use cookies.