Skip to content

No suggestions while using Finder #1952

@bartkibilko

Description

@bartkibilko

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions