ElasticSearch:如何匹配映射类型中的所有字段

ElasticSearch: How to Match all fields in Mapping Type

问题 我正在尝试编写一个匹配映射类型 (MyFirstMappingType) 的所有属性的 ElasticSearch 查询。如果一个 属性 不匹配,则不应 return 该项目。他们不应该混合比赛来获得比赛。

映射

  "mappings": {
    "item": { 
      "_all":       { "enabled": "false"  },
      "properties": { 
        "MyFirstMappingType": {
          "properties": {
            "field1":  { "type": "keyword" },
            "field2":  { "type": "keyword" }, 
            "field3":  { "type": "text", "index": "false" }
          }
        }
      }
    }
  }

当项目与每个字段匹配时,此查询 returns 项目。但是当一个项目有多个“MyFirstMappingType”时,它将混合和匹配。例如,此查询仍将 return 此项。

GET myfirst/item/_search
{
  "from": 0,
  "size": 38,
  "_source": [
    "MyFirstMappingType"
  ],
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "field1": "foo"
          }
        },
        {
          "term": {
            "field2": "bar"
          }
        },
        {
          "term": {
            "field3": "world"
          }
        }
        }
      ]
    }
  }
          "MyFirstMappingType" : [
            {
              "field1" : "foo",
              "field2" : "bar",
              "field3" : "hello"
            },
            {
              "field1" : "foo",
              "field2" : "world",
              "field3" : "foo"
            },
            {
              "field1" : "foo",
              "field2" : "foo",
              "field3" : "world"
            }
          ]

查询每个array object independently, you need to define MyFirstMappingType to be of nested type

添加带有索引映射和搜索查询的工作示例

索引映射:

{
  "mappings": {
    "properties": {
      "MyFirstMappingType": {
        "type": "nested"
      }
    }
  }
}

搜索查询:

{
  "query": {
    "nested": {
      "path": "MyFirstMappingType",
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "MyFirstMappingType.field1": "foo"
              }
            },
            {
              "term": {
                "MyFirstMappingType.field2": "bar"
              }
            },
            {
              "term": {
                "MyFirstMappingType.field3": "world"
              }
            }
          ]
        }
      }
    }
  }
}