用于查找包含术语X和子项父项的子项的嵌套查询包含术语Y.

时间:2018-01-12 01:47:29

标签: elasticsearch

我有这样的映射

{
    "properties" : {
        "text" : {
            "type" : "nested",
            "properties": {
                "header": {
                    "type" : "text"
                },
                "nodes": {
                    "type": "nested",
                    "properties": {
                        "subheader" : {
                            "type" : "text"
                        },
                        "nodes" : {
                            "type" : "nested"
                        }
                    }
                }
            }
        }
    }
}

我想返回最里面的孩子text.nodes.nodes包含一些术语' X'当且仅当该孩子的父母text.nodes.subheader包含某个词语时,Y'

这是一个reproducible gist,我希望在那里搜索“超级危险”'在孩子身上,以及'限制'在子标题中应该只返回文档2。 它现在返回两个文件。

从主旨来看,我的查询现在看起来像是:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "text.nodes",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "text.nodes.subheader": {
                        "query": "Restrictions"
                      }
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "text.nodes.nodes",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "text.nodes.nodes.content": {
                        "query": "SUPERFRAGILISTIC"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

1 个答案:

答案 0 :(得分:1)

为了找到包含在单个文档的同一json节点中的两个条件的结果,您需要将第二个条件嵌套在第一个条件中。使用此查询,您将只获得document2:

private final UriInfo uriInfo;
private final String LONGITUDE = "lon";
private final String LATITUDE = "lat";
private final @DefaultValue("10000") String RANGE = "range";
private final @DefaultValue("") String EVENT_TYPE = "eType";
private final @DefaultValue("") String PLACE_TYPE = "pType";
private final String SEARCH_COUNT = "count";

public NearByRequestWrapper(UriInfo uriInfo) {
this.uriInfo = uriInfo;
}

@DefaultValue("50") @Size(min = 10, max = 100)
public String searchCount() {
return uriInfo.getQueryParameters().getFirst(SEARCH_COUNT);
}
相关问题