Elasticsearch:search/query 用于 json 相同深度内的多个字符串

Elasticsearch: search/query for multiple strings within same depth of json

是否可以查询 elasticsearch,使其跟随两个查询?

例如,如果我有以下内容,

[{
      "items": [
            { 
                "color" : "blue",
                "shape" : "circle",
            },{ 
                "color" : "yellow",
                "shape" : "square",
            },{ 
                "color" : "yellow",
                "shape" : "square",
            }                           
},{
      "items": [
            { 
                "color" : "blue",
                "shape" : "triangle",
            },{ 
                "color" : "pink",
                "shape" : "circle",
            },{ 
                "color" : "red",
                "shape" : "circle",
            }                   
},{
      "items": [
{ 
                "color" : "red",
                "shape" : "rectangle",
            },{ 
                "color" : "blue",
                "shape" : "circle",
            },{ 
                "color" : "purple",
                "shape" : "oval",
            }                   
}]

我只想搜索颜色(蓝色)和形状(圆形)的项目。只应返回第一个和第三个。因此,响应应该是

[{
      "items": [
            { 
                "color" : "blue",
                "shape" : "circle",
            },{ 
                "color" : "yellow",
                "shape" : "square",
            },{ 
                "color" : "yellow",
                "shape" : "square",
            }                           
},
{
      "items": [
            { 
                "color" : "red",
                "shape" : "rectangle",
            },{ 
                "color" : "blue",
                "shape" : "circle",
            },{ 
                "color" : "purple",
                "shape" : "oval",
            }                   
}]

但是,目前如果我使用下面的调用,所有条目都会被返回,因为它们都有 color:blue 和 shape:circle.

{
    "query": {
        "query_string": {
            "query": "color:blue AND shape:circle"
        }
    }
}

(但我需要将两者作为一个项目包含在一起)...这可以用 Elasticsearch 实现吗?

既然要分别查询每个对象,最好使用nested data type instead of object data type

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

索引映射:

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

搜索查询:

{
  "query": {
    "nested": {
      "path": "items",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "items.color": "blue"
              }
            },
            {
              "match": {
                "items.shape": "circle"
              }
            }
          ]
        }
      }
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "68853344",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.8483298,
        "_source": {
          "items": [
            {
              "color": "blue",
              "shape": "circle"
            },
            {
              "color": "yellow",
              "shape": "square"
            },
            {
              "color": "yellow",
              "shape": "square"
            }
          ]
        }
      },
      {
        "_index": "68853344",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.8483298,
        "_source": {
          "items": [
            {
              "color": "red",
              "shape": "rectangle"
            },
            {
              "color": "blue",
              "shape": "circle"
            },
            {
              "color": "purple",
              "shape": "oval"
            }
          ]
        }
      }
    ]