FOSElasticaBundle icon indicating copy to clipboard operation
FOSElasticaBundle copied to clipboard

No suggestions while using Finder

Open bartkibilko opened this issue 1 year ago • 0 comments

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

bartkibilko avatar Jul 29 '24 16:07 bartkibilko