ElasticSearch Java客户端查询嵌套对象

11

我该如何转换这种查询。

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

使用QueryBuilders进行Java客户端查询

1个回答

17
以下 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

propertValue should be propertyValue - JackYe
如果有超过2个级别,该如何创建?例如,如果它是类似于“xyz.consultations.prescriptions”的东西。 - mirzak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接