Home > Net >  Convert elasticsearch bool querry into java
Convert elasticsearch bool querry into java

Time:01-06

GET feeds/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "comment",
            "query": { 
              "match": {
                "comment.c_text": "This is mateen"
              }
            },"inner_hits": {}
          }
        },
        {
          "term": {
            "title.keyword": {
              "value": "This is mateen"
            }
          }
        },
        {
          "term": {
            "body.keyword": {
              "value": "This is mateen"
            }
          }
        }
      ]
    }
  }
}

Mapping is as follow

PUT feeds
{
  "mappings": {
    "properties": {
      "comment":{
        "type": "nested"
      }
    }
  }
}

i am using elasticsearch 7.17.3. For searching all documents of elasticsearch in my springboot i have write following code that gives me exact output


 public  List<feed> searchAllDocuments() throws IOException {
        SearchRequest searchRequest = SearchRequest.of(s -> s.index(indexName));
        SearchResponse searchResponse = elasticsearchClient.search(searchRequest, feed.class);
        List<Hit> hits = searchResponse.hits().hits();
        List<feed> feeds = new ArrayList<>();
        feed f=null;
        for (Hit object : hits) {
            f = (feed) object.source();
           
            feeds.add(f);
            }

        return feeds;
        }

Can anyone help me convert the es querry into springboot application i am new to it need guidence

CodePudding user response:

Here, you can do the following way

    BoolQueryBuilder booleanQuery = QueryBuilders.boolQuery();
    MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("comment.c_text", "This is mateen");
    booleanQuery.should(QueryBuilders.termQuery("title", "This is mateen"));
    booleanQuery.should(QueryBuilders.termQuery("body", "This is mateen"));
    booleanQuery.should(QueryBuilders.nestedQuery("comment", matchQuery, ScoreMode.None).innerHit(new InnerHitBuilder()));

    SearchRequest searchRequest = new SearchRequest("feeds");

    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(booleanQuery);
    searchRequest.source(sourceBuilder);

    SearchResponse searchResponse = elasticsearchClient.search(searchRequest, feed.class);

CodePudding user response:

If you use the new Java Api Client try the code bellow:

Query nestedQuery = NestedQuery.of(nq ->
    nq.path("comment")
        .innerHits(InnerHits.of(ih -> ih))
        .query(MatchQuery.of(mq -> mq.field("comment.c_text").query("This is mateen"))._toQuery())
)._toQuery();

Query termQueryTitle = TermQuery.of(
    tq -> tq.field("title.keyword").value("This is mateen")
)._toQuery();

Query termQueryBody = TermQuery.of(
    tq -> tq.field("body.keyword").value("This is mateen")
)._toQuery();

Query boolQuery = BoolQuery.of(bq -> bq.should(nestedQuery, termQueryBody, termQueryTitle))._toQuery();

SearchRequest searchRequest = SearchRequest.of(
    s -> s.index("idx_name").query(boolQuery)
);

var response = client.search(searchRequest, Feed.class);
  • Related