10

如何转换这种查询。

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

使用 QueryBuilder 到 Java 客户端查询

4

1 回答 1

16

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

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
于 2014-07-12T09:46:43.760 回答