ElasticSearch Java 客户端查询嵌套对象

如何转换这种查询.

{
  "query": {
    "nested": {
      "path": "consultations",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "consultations.prescriptions": "alfuorism"
              }
            },
            {
              "match": {
                "consultations.Diagnosis": "Fever"
              }
            }
          ]
        }
      }
    }
  }
}

使用 QueryBuilders 到 Java 客户端查询

To a Java Client query using QueryBuilders

推荐答案

以下 Java 代码将生成您的查询

The folowing Java code will generate your query

public NestedQueryBuilder nestedBoolQuery(final Map<String, String> propertyValues, final String nestedPath) {

    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    Iterator<String> iterator = propertyValues.keySet().iterator();

    while (iterator.hasNext()) {
        String propertyName = iterator.next();
        String propertValue = propertyValues.get(propertyName);
        MatchQueryBuilder matchQuery = QueryBuilders.matchQuery(propertyName, propertValue);
        boolQueryBuilder.must(matchQuery);
    }

    return QueryBuilders.nestedQuery(nestedPath, boolQueryBuilder);
}

参数propertyValues为:

Map<String, String> propertyValues = new HashMap<String, String>();
propertyValues.put("consultations.prescriptions", "alfuorism");
propertyValues.put("consultations.Diagnosis", "Fever");

参数nestedPath为:

consultations

相关文章