SearchPattern example


    private EntityRepository $repository;

    protected function setUp(): void
    {
        $this->repository = $this->getContainer()->get('product.repository');
    }

    public function testScoringExtensionExists(): void
    {
        $context = Context::createDefaultContext();
        $pattern = new SearchPattern(new SearchTerm('test'));
        $builder = new EntityScoreQueryBuilder();
        $queries = $builder->buildScoreQueries(
            $pattern,
            $this->getContainer()->get(ProductDefinition::class),
            $this->getContainer()->get(ProductDefinition::class)->getEntityName(),
            $context
        );

        $criteria = new Criteria();
        $criteria->addQuery(...$queries);

        
private readonly AbstractTokenFilter $tokenFilter
    ) {
    }

    public function interpret(string $word, Context $context): SearchPattern
    {
        $tokens = $this->tokenizer->tokenize($word);

        $tokens = $this->tokenFilter->filter($tokens$context);

        if (empty($tokens)) {
            return new SearchPattern(new SearchTerm(''));
        }

        $tokenSlops = $this->slop($tokens);
        if (!$this->checkSlops(array_values($tokenSlops))) {
            return new SearchPattern(new SearchTerm($word));
        }

        $tokenKeywords = $this->fetchKeywords($context$tokenSlops);
        $matches = array_fill(0, \count($tokens)[]);
        $matches = $this->groupTokenKeywords($matches$tokenKeywords);

        
/** * @internal */
    public function __construct(private readonly TokenizerInterface $tokenizer)
    {
    }

    public function interpret(string $term): SearchPattern
    {
        $terms = $this->tokenizer->tokenize($term);

        $pattern = new SearchPattern(new SearchTerm($term));

        if (\count($terms) === 1) {
            return $pattern;
        }

        foreach ($terms as $part) {
            $percent = mb_strlen($part) / mb_strlen($term);
            $pattern->addTerm(new SearchTerm($part$percent));
        }

        return $pattern;
    }
$this->shouldNotTokenizeDefinition = $this->registerDefinition(ShouldNotTokenizeFieldDefinition::class);
        $this->context = Context::createDefaultContext();
    }

    /** * @dataProvider validDateTerms */
    public function testValidDateTerms(string $dateTerm): void
    {
        $builder = new EntityScoreQueryBuilder();

        $pattern = new SearchPattern(
            new SearchTerm($dateTerm, 1),
            'product'
        );

        $queries = $builder->buildScoreQueries($pattern$this->onlyDateFieldDefinition, 'test', $this->context);

        static::assertEquals(
            [
                new ScoreQuery(new EqualsFilter('test.dateTime', $dateTerm), 100),
                new ScoreQuery(new ContainsFilter('test.dateTime', $dateTerm), 50),
            ],
            
Home | Imprint | This part of the site doesn't use cookies.