弹性搜索查询嵌套对象的两个属性的AND条件

时间:2019-01-11 11:29:40

标签: elasticsearch

我有如下所示的post_filter,我试图过滤学校名称为HILL SCHOOL AND 的记录,其中包含嵌套的子对象,其名称为JOY AND

school位于父对象中,该对象持有子对象的嵌套对象列表。

以上所有都是AND条件。

但是查询似乎不起作用。知道为什么吗?有没有办法组合两个嵌套查询?

GET /test_school/_search
{
"query": {
"match_all": {}
},
"post_filter": {
"bool": {
  "must_not": [
    {
      "bool": {
        "must": [
          {
            "term": {
              "schoolname": {
                "value": "HILL SCHOOL"
              }
            }
          },
          {
            "nested": {
              "path": "children",
              "query": {
                "bool": {
                  "must": [
                    {
                      "match": {
                        "name": "JACK"
                      }
                    }
                  ]
                }
              }
            }
          },
          {
            "term": {
              "children.section": {
                "value": "A"
              }
            }
          }
        ]
      }
    }
  ]
}
}
}

架构如下:

PUT /test_school
{
  "mappings": {
"_doc": {
  "properties": {
    "schoolname": {
      "type": "keyword"
    },
    "children": {
      "type": "nested",
      "properties": {
        "name": {
          "type": "keyword",
          "index": true
        },
        "section": {
          "type": "keyword",
          "index": true
        }
      }
    }
  }
}
  }
}

采样数据如下:

POST /test_school/_doc
{
"schoolname":"HILL SCHOOL",
"children":{
"name":"JOY",
"section":"A"
}
}

第二条记录

POST /test_school/_doc
 {
"schoolname":"HILL SCHOOL",
"children":{
"name":"JACK",
"section":"B"
  }
}

1 个答案:

答案 0 :(得分:0)

https://stackoverflow.com/a/17543151/183217建议使用特殊的映射来处理嵌套对象。您似乎对“跨对象匹配”问题不屑一顾。