【问题标题】:Elasticsearch: search/query for multiple strings within same depth of jsonElasticsearch:在相同深度的json中搜索/查询多个字符串
【发布时间】:2021-08-19 19:04:59
【问题描述】:

是否可以查询 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 实现吗?

【问题讨论】:

    标签: elasticsearch elasticsearch-query


    【解决方案1】:

    由于要分别查询每个对象,最好使用nested data type 而不是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"
                }
              ]
            }
          }
        ]
    

    【讨论】:

    • @vicki 请仔细阅读答案,如果这能解决您的问题,请告诉我?
    • 好的,谢谢!但是,是否可以使用对象数据类型?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 2023-01-17
    • 2015-12-04
    • 1970-01-01
    相关资源
    最近更新 更多