1

I'm trying to make a bool must match query in java with this structure

GET offers/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "offerLine": "Technology"
          }
        }
      ]
    }
  }
}

I've tried this and I'm getting a null value:

private NativeQueryBuilder prepareElasticQuery(OfferRequest request){
    BoolQuery.Builder bqb = QueryBuilders.bool();
    bqb.must(m->m.match(ma->ma.field("offerLine").query(request.getOfferLine())));
    NativeQueryBuilder nqb= new NativeQueryBuilder().withQuery(bqb.build()._toQuery());
    return nqb;
}

The bool query gets this value :

BoolQuery: {"must":[{"match":{"offerLine":{"query":"Technology"}}}]}

And I can't seem to find an alternative to the value() method since it's deprecation. Any clues as to how I can build it properly?

Edit: That's the proper way. My problem is with further handling the results.

3
  • 1
    "offerLine": "Technology" and "offerLine":{"query":"Technology"} are equivalent Commented Dec 7, 2022 at 8:50
  • You're right. I've found out that I'm getting hits but am not handling them properly so I end up with an empty result list. Thank you Commented Dec 7, 2022 at 8:59
  • 1
    Ok, glad you figured it out Commented Dec 7, 2022 at 9:00

1 Answer 1

2

Try to do that:

public SearchRequest prepareElasticQuery(OfferRequest request) {
  SearchRequest searchRequest = new SearchRequest("offers");
  searchRequest.source(new SearchSourceBuilder()
    .query(
      QueryBuilders.boolQuery()
        .must(
          QueryBuilders.matchQuery("offerLine", request.getOfferLine()
        )
      )
    )
  );
  return searchRequest;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I've figured it out previously and that wasn't my error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.