WildcardQuery example



        $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);
        }

        $bool->addParameter('minimum_should_match', 1);

        return $bool;
    }

    
break;

                case '>':
                    $query->add(
                        new RangeQuery($condition['property'][RangeQuery::GT => $condition['value']])
                    );
                    break;

                case 'like':
                    $value = strtolower($condition['value']);
                    $query->add(
                        new WildcardQuery($condition['property'], '*' . $value . '*'),
                        BoolQuery::MUST
                    );
                    break;
            }
        }

        if (empty($query->getQueries())) {
            return;
        }

        $search->addQuery($query);
    }

    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);
            }
        }

        return $bool;
    }
private function parseContainsFilter(ContainsFilter $filter, EntityDefinition $definition, Context $context): BuilderInterface
    {
        $accessor = $this->buildAccessor($definition$filter->getField()$context);

        /** @var string $value */
        $value = $filter->getValue();

        if ($this->keyValueStorage->get(ElasticsearchIndexer::ENABLE_MULTILINGUAL_INDEX_KEY, false)) {
            $field = $this->getField($definition$filter->getField());

            $query = new WildcardQuery($accessor, '*' . $value . '*');

            if ($field instanceof TranslatedField) {
                $query = new DisMaxQuery();
                foreach ($context->getLanguageIdChain() as $languageId) {
                    $fieldName = $this->getTranslatedFieldName($accessor$languageId);
                    $query->addQuery(new WildcardQuery($fieldName, '*' . $value . '*'));
                }
            }

            return $this->createNestedQuery(
                $query,
                
return new PrefixQuery($field$value);

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

                if ($type === 'string') {
                    $field .= '.raw';
                }

                return new WildcardQuery($field, '*' . $value);

            default:
                throw new RuntimeException(sprintf('Operator %s is not supported in elastic search', $criteriaPart->getOperator()));
        }
    }
}
$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'], '.');

                    foreach ($queries as $query) {
                        $tokenBool->add(new NestedQuery($nested$query), BoolQuery::SHOULD);
                    }
Home | Imprint | This part of the site doesn't use cookies.