简体   繁体   中英

how to translate nested elastic search query in java?

Below query will do filter and aggregation how to translate this to java code. Query works from postman the same needs to be converted into java using java client api. and i am using rest high level client as elastic search client. i tried with the below java code but the generated query is bit different than the the actual below is java code which i have tried.

BoolQueryBuilder booleanQuery = QueryBuilders.boolQuery();
booleanQuery.filter(QueryBuilders
        .queryStringQuery(String.join(" OR ", exactMatchThese))
        .field("events.recommendationData.exceptionId"));
QueryBuilder queryBuilder = QueryBuilders.nestedQuery("events.recommendationData", booleanQuery, ScoreMode.None); 

Search Query which is working

GET <index-name>/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "nested": { --> note
            "path": "events.recommendationData",
            "query": {
              "query_string": {
                "query": "\"1\" OR \"2\"",
                "fields": [
                  "events.recommendationData.exceptionId"
                ],
                "type": "best_fields",
                "default_operator": "or",
                "max_determinized_states": 10000,
                "enable_position_increments": true,
                "fuzziness": "AUTO",
                "fuzzy_prefix_length": 0,
                "fuzzy_max_expansions": 50,
                "phrase_slop": 0,
                "escape": false,
                "auto_generate_synonyms_phrase_query": true,
                "fuzzy_transpositions": true,
                "boost": 1
              }
            }
          }
        }
      ]
    }
  },
  "size": 1,
  "aggs": {
            "genres": {
                "nested": {
                    "path": "events.recommendationData.recommendations"
                },
                "aggs": {
                    "nested_comments_recomms": {
                        "terms": {
                            "field": "events.recommendationData.recommendations.recommendationType"
                        }
                    }
                }
            }
        }
}

Below Search Query Generated from above java code which i have mentioned and is not working.

{
  "query": {
    "nested": {
      "query": {
        "bool": {
          "filter": [
            {
              "query_string": {
                "query": "\"1\" OR \"2\"",
                "fields": [
                  "events.recommendationData.exceptionId^1.0"
                ],
                "type": "best_fields",
                "default_operator": "or",
                "max_determinized_states": 10000,
                "enable_position_increments": true,
                "fuzziness": "AUTO",
                "fuzzy_prefix_length": 0,
                "fuzzy_max_expansions": 50,
                "phrase_slop": 0,
                "escape": false,
                "auto_generate_synonyms_phrase_query": true,
                "fuzzy_transpositions": true,
                "boost": 1
              }
            }
          ],
          "adjust_pure_negative": true,
          "boost": 1
        }
      },
      "path": "events.recommendationData",
      "ignore_unmapped": false,
      "score_mode": "none",
      "boost": 1
    }
  },
  "aggregations": {
    "recommendationTypes": {
      "terms": {
        "field": "events.recommendationData.recommendations.recommendationType",
        "size": 10,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ]
      }
    }
  }
}

Your inner-most query block is query string ie

QueryStringQueryBuilder queryString = QueryBuilders
        .queryStringQuery(String.join(" OR ", exactMatchThese));

This is the query part of nested query hence we create a nested query and assign the above query to it as written below,

NestedQueryBuilder nestedQuery = QueryBuilders
    .nestedQuery("events.recommendationData", queryString, ScoreMode.None);

Finally add the above query to the filter clause of bool query,

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery().filter(nestedQuery);

All together this is,

QueryStringQueryBuilder queryString = QueryBuilders
    .queryStringQuery(String.join(" OR ", exactMatchThese));
NestedQueryBuilder nestedQuery = QueryBuilders
    .nestedQuery("events.recommendationData", queryString, ScoreMode.None);
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery().filter(nestedQuery);

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