ariful islam arif
ariful islam arif

Reputation: 207

Elasticsearch filter result ignoring search keyword

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.

Upvotes: 0

Views: 285

Answers (1)

Sahil Gupta
Sahil Gupta

Reputation: 2176

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);

Upvotes: 1

Related Questions