简体   繁体   中英

ElasticSearch Java API - Query Builder

I'm currently working on elasticsearch where I've created a query:

  "query": {
    "bool": {
      "must": [
        {"match": {
          "companyId": 35
        }},
        {
          "multi_match": {
            "query": "movie",
            "fields": ["name", "description"]
          }
        }
      ]
    }
  }
}

For which I have build QueryBuilder in JAVA:

        SearchRequest searchRequest = new SearchRequest("faqs");//index name
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
                
        QueryBuilder mulMatch = QueryBuilders.multiMatchQuery(name,"name", "description").type("phrase").boost(5);
        QueryBuilder cId = QueryBuilders.matchQuery("companyId", companyId).boost(5);
        QueryBuilder must = QueryBuilders.boolQuery().must(cId).must(mulMatch).boost(5);
        
        searchSourceBuilder.query(must);
        searchRequest.source(searchSourceBuilder);
        System.out.println("SS Query : " + searchSourceBuilder);

        return searchSourceBuilder;

which gives me query created as:

{"query":{"bool":{"must":[{"match":{"companyId":{"query":35,"operator":"OR","prefix_length":0,"max_expansions":50,"fuzzy_transpositions":true,"lenient":false,"zero_terms_query":"NONE","auto_generate_synonyms_phrase_query":true,"boost":5.0}}},{"multi_match":{"query":"movie","fields":["description^1.0","name^1.0"],"type":"phrase","operator":"OR","slop":0,"prefix_length":0,"max_expansions":50,"zero_terms_query":"NONE","auto_generate_synonyms_phrase_query":true,"fuzzy_transpositions":true,"boost":5.0}}],"adjust_pure_negative":true,"boost":5.0}}}

However, I require output such as:

{
          "Id": 1,
          "name": "Controle",
          "description": "Welcome movie",
          "visibility": "2",
          "isarticleEnabled": false,
          "companyId": 35,
          "categoryId": 0        
}

Though I'm getting output as(API Response):

{
    "suggestOnly": false,
    "fragment": false
}

So to get such a response, what should I exactly do? One solution I've got is of using Search Response such as:

SearchResponse res = client.search(searchRequest, RequestOptions.DEFAULT);

But still, this is not actually working with ElasticsearchRestTemplate and RestHighLevelClient.

Note: I've searched for all the relative solutions, ie Search Request, Search Response, but I was unable to get any of the proper solutions.

Maybe they should be merged instead of overriding. searchSourceBuilder.query(QueryBuilders.boolQuery().filter(mulMatch).filter(cId).filter(must));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM