Elasticsearch 过滤结果忽略搜索关键词

huangapple go评论70阅读模式
英文:

Elasticsearch filter result ignoring search keyword

问题

我使用常规搜索查询获得了良好的结果。

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder queryBuilder = new BoolQueryBuilder();

String keyword = requestBody.getKeyword();

queryBuilder.should(QueryBuilders.matchQuery("fullText", keyword));

searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.from(requestBody.getPage() - 1);
searchSourceBuilder.size(BROWSE_PAGE_DATA_LIMIT);

searchRequest.source(searchSourceBuilder);

try {
    return client.search(searchRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
    throw new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "Error in ES search");
}

但是当我添加过滤条件时,结果忽略了我的搜索关键字。

queryBuilder.filter(QueryBuilders.termsQuery("authorId", filter.getAuthorIds()));

在这里,我试图替代 Solr 的 fq。我的方法有什么问题?

英文:

I am getting good result with normal search query.

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder queryBuilder = new BoolQueryBuilder();

 String keyword = requestBody.getKeyword();

 queryBuilder.should(QueryBuilders.matchQuery("fullText", keyword));

 searchSourceBuilder.query(queryBuilder);
 searchSourceBuilder.from(requestBody.getPage() - 1);
 searchSourceBuilder.size(BROWSE_PAGE_DATA_LIMIT);

 searchRequest.source(searchSourceBuilder);

    try {
        return client.search(searchRequest, RequestOptions.DEFAULT);
    } catch (IOException e) {
        throw new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "Error in ES search");
    }

But when I add filtering with it, the result is ignoring my search keyword.

queryBuilder.filter(QueryBuilders.termsQuery("authorId", filter.getAuthorIds()));

here I am trying to replace fq of solr. What's wrong at my approach.

答案1

得分: 1

来自ES文档的摘录:

如果布尔查询包含至少一个should子句且没有must或filter子句,则默认值为1。否则,默认值为0。

基本上,如果在布尔查询中存在filter和/或must子句,那么should子句将被忽略,直到设置min_should_match为适当的值。

将minShouldMatch设置为1。例如:

queryBuilder.should(QueryBuilders.matchQuery("fullText", keyword)).minimumShouldMatch(1);
英文:

Excerpt from ES Docs
> If the bool query includes at least one should clause and no must or
> filter clauses, the default value is 1. Otherwise, the default value
> is 0.

Basically, if there is a filter or/and must clause with-in a bool query then the should clause is ignored until min_should_match is set to a suitable value.

Set minShouldMatch to 1. e.g:

queryBuilder.should(QueryBuilders.matchQuery("fullText", keyword)).minimumShouldMatch(1);

huangapple
  • 本文由 发表于 2020年10月13日 22:38:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64337375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定