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

IDBPR-2554 Add whereMoreLikeThis and addFunctionScore #85

Merged
merged 5 commits into from
Feb 11, 2025
Merged

Conversation

egmanoylin
Copy link
Contributor

Задача: поиск подобных товаров с возможность приоритизации по схожим параметрам.

Добавлена механика: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html
для поиска подобных товаров по определенным полям.

Добавлена механика: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
для приоритезации товара по совпадающим полям.

Данные механики позволяют реализовать необходимый кейс:

       $query = new SearchQuery($index);

        $query->whereMoreLikeThis(
            ['name'],
            (new MoreLikeThis())->addId($data->baseProduct->id),
            MoreLikeOptions::make(
                minTermFreq: 1,
                maxQueryTerms: 12,
                minimumShouldMatch: '80%'
            ),
        );

        $functions = [];
        foreach ($data->priorityFactor->items as $item) {
            $functions[] = new FunctionScoreItem(
                weight: $item->weight,
                filter: new Terms(
                    field: $item->field->value,
                    values: $data->baseProduct->getValue($item->field->value),
                ),
            );
        }
        $query->addFunctionScore($functions, FunctionScoreOptions::make(
            scoreMode: ScoreMode::SUM,
            boostMode: BoostMode::SUM
        ));

        return $query->paginate($data->limitProducts);

@MsNatali MsNatali merged commit 204a150 into v8 Feb 11, 2025
9 checks passed
@MsNatali MsNatali deleted the idbpr-2554 branch February 11, 2025 13:03
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

Successfully merging this pull request may close these issues.

2 participants