如何翻译 java 中的嵌套弹性搜索查询?

how to translate nested elastic search query in java?

下面的查询将进行过滤和聚合,如何将其转换为 java 代码。来自邮递员的查询同样需要使用 java 客户端 api 转换为 java。我正在使用其他高级客户端作为弹性搜索客户端。我尝试使用下面的 java 代码,但生成的查询与下面实际的 java 代码有点不同,我已经尝试过。

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); 

正在运行的搜索查询

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"
                        }
                    }
                }
            }
        }
}

从上面生成的下面的搜索查询 java 我已经提到但不起作用的代码。

{
  "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"
          }
        ]
      }
    }
  }
}

你最里面的查询块是查询字符串,即

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

这是嵌套查询的查询部分,因此我们创建一个嵌套查询并将上述查询分配给它,如下所示,

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

最后将上述查询添加到bool查询的过滤子句中,

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

总而言之,

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