Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No suggestions while using Finder #1952

Open
bartkibilko opened this issue Jul 29, 2024 · 0 comments
Open

No suggestions while using Finder #1952

bartkibilko opened this issue Jul 29, 2024 · 0 comments

Comments

@bartkibilko
Copy link

bartkibilko commented Jul 29, 2024

Maybe I'm doing sth wrong :), but ...
When using Finder, I can't get suggestions from results.
This line of code is getting specifically only results, not suggestions: https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/src/Finder/TransformedFinder.php#L117
There should a way to call getSuggests() a way more easily ...
There is some of my code below. Plain query works great when calling ES directly
`
public function __construct(
private readonly TransformedFinder $finder,
) {
}

protected function getAnalyzer(): string
{
    return 'simple';
}

protected function getFields(): array
{
    return [
        'productLanguages.name^2',
        'productLanguages.subtitle^3',
        'productLanguages.descriptionPlainText'
    ];
}

protected function getNestedPath(): string
{
    return 'productLanguages';
}

protected function getCompletionField(): string
{
    return 'productLanguages.nameComplete';
}

/**
 * @return array{results: array<Item|Result>, suggestions: array<object>}
 */
public function filterBySingleString(string $searchValue, bool $rawResult = false): array
{
    $boolQuery = new BoolQuery();

    $multiMatch = new MultiMatch();
    $multiMatch->setQuery($searchValue);
    $multiMatch->setFields($this->getFields());
    $multiMatch->setType('best_fields');
    $multiMatch->setFuzziness('AUTO');
    if (!empty($this->getAnalyzer())) {
        $multiMatch->setParam('analyzer', $this->getAnalyzer());
    }

    $nestedQuery = new Nested();
    $nestedQuery->setPath($this->getNestedPath());
    $nestedQuery->setQuery($multiMatch);

    $boolQuery->addShould($nestedQuery);
    $boolQuery->setMinimumShouldMatch(1);

    $completion = new Suggest\Completion('name_suggest', $this->getCompletionField());
    $completion->setPrefix($searchValue);
    $completion->setFuzzy([
        'fuzziness' => 'AUTO'
    ]);
    $completion->setSize(5);
    $suggest = new Suggest($completion);

    if ($rawResult) {
        return [
            'results' => $this->finder->findRaw($boolQuery),
            'suggestions' => $this->finder->findRaw($suggest)
        ];
    }

    return [
        'results' => $this->finder->find($boolQuery),
        'suggestions' => $this->finder->find($suggest)
    ];
}`

suggestions array is always empty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant