根据 1 个术语值查询嵌套 JSON

Querying Nested JSON based on 1 term value

我已将 JSON 编入索引,格式如下

JSON:

{"work":[{"organization":"abc", end:"present"},{"organization":"edf", end:"old"}]}
{"work":[{"organization":"edf", end:"present"},{"organization":"abc", end:"old"}]}

我想查询组织是"abc",结束是"present"

的记录

但下面的查询不起作用

 work.0.organization: "abc" AND work.0.end:"present"

没有匹配的记录

如果我给出如下查询

 work.organization: "abc" AND work.end:"present"

两条记录都匹配。而我想要的只有第一条记录

匹配的记录应该只有下面的

{"work":[{"organization":"abc", end:"present"},{"organization":"edf", end:"old"}]}

你必须使用 nested_types。第一个地图使用以下映射在弹性中作为嵌套类型工作

PUT index_name_3
{
  "mappings": {
    "document_type" : {
      "properties": {
        "work" : {
          "type": "nested",
          "properties": {
            "organization" : {
              "type" : "text"
            },
            "end" : {
              "type" : "text"
            }
          }
        }
      }
    }
  }
}

使用以下查询进行嵌套过滤器匹配和innerhits

{
    "query": {
        "nested": {
            "path": "work",
            "inner_hits": {},
            "query": {
                "bool": {
                    "must": [{
                            "term": {
                                "work.organization": {
                                    "value": "abc"
                                }
                            }
                        },
                        {
                            "term": {
                                "work.end": {
                                    "value": "present"
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}