Home > front end >  Building a bool query with Spring Data Elasticsearch
Building a bool query with Spring Data Elasticsearch

Time:12-14

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.

CodePudding user response:

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;
}
  • Related