【问题标题】:Elastic Query Nested Query弹性查询嵌套查询
【发布时间】:2016-12-25 07:21:26
【问题描述】:

第一步:

在弹性搜索上创建索引 http://localhost:9200/shop 带有下面的 mapping.json

{
  "cloth" : 
  {
      "properties" : 
      {
          "name" : { "type" : "string", "index" : "analyzed" },
          "variation" : 
          {
            "type" : "nested", 
            "properties" : 
            { 
                "size" : 
                { 
                    "type" : "string", "index" : "not_analyzed"
                },
                "color" : 
                {
                    "type" : "string", "index" : "not_analyzed"
                }
            }
        }
    }
  }
}

获取:http://localhost:9200/shop/_mapping/cloth

HTTP/1.1 200 正常 内容类型:应用程序/json;字符集=UTF-8 内容长度:518

{"shop":{"mappings":{"cloth":{"properties":{"cloth":{"properties":{"properties":{"properties":{"name":{"属性":{"index":{"type":"string"},"type":{"type":"string"}}},"variation":{"properties":{"properties":{"属性":{"color":{"properties":{"index":{"type":"string"},"type":{"type":"string"}}},"size":{"属性":{"index":{"type":"string"},"type":{"type":"string"}}}}},"type":{"type":"string"}} }}}}},"name":{"type":"string"},"variation":{"properties":{"color":{"type":"string"},"size":{" type":"string"}}}}}}}}

第 2 步:

使用下面给出的 data.json 插入数据 http://localhost:9200/shop/cloth/?_create

{
"name" : "Test shirt",
"variation" : [
{ "size" : "XXL", "color" : "red" },
{ "size" : "XL", "color" : "black" }
]
}

第 3 步:

尝试使用给定的 query.json 进行搜索

http://localhost:9200/shop/cloth/_search

{
"query" : {
"nested" : {
"path" : "variation",
"query" : {
"bool" : {
"must" : [
{ "term" : { "variation.size" : "XXL" } },
{ "term" : { "variation.color" : "black" } }
]
}
}
}
}
}

出现以下错误

HTTP/1.1 400 错误请求 内容类型:应用程序/json;字符集=UTF-8 内容长度:519

{"error":{"root_cause":[{"type":"query_parsing_exception","reason":"[nested] nested object under path [variation] is not of nested type","index":"shop","line":4,"col":1}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"shop","node":"6U9SA_SDRJKfw1bRxwH8ig","reason":{"type":"query_parsing_exception","reason":"[nested] nested object under path [variation] is not of nested type","index":"shop","line":4,"col":1}}]},"status":400}

嵌套查询的搜索方式是什么?有没有合适的方法将映射文件加载到搜索集群中?

【问题讨论】:

  • 你能用curl -XGET localhost:9200/shop/_mapping/cloth 的输出更新你的问题吗?
  • 我们如何插入映射,就像使用带有 mapping.json 内容的 POST 一样
  • 我的错,对不起,请再次查看我上面的评论。
  • 请运行这个:curl -XGET localhost:9200/shop/_mapping/cloth 我不认为是这样
  • @Val 我有一个简单的问题:如果在映射文件中声明更多字段并在索引数据库上发布更少的列并在索引上执行搜索是否会引发与上述问题相同的错误?跨度>

标签: json elasticsearch


【解决方案1】:

我认为您没有使用 cloth 映射正确创建索引。这样做:

# delete your index first
curl -XDELETE localhost:9200/shop

# create it properly
curl -XPUT localhost:9200/shop -d '{
  "mappings": {
    "cloth": {
      "properties": {
        "name": {
          "type": "string",
          "index": "analyzed"
        },
        "variation": {
          "type": "nested",
          "properties": {
            "size": {
              "type": "string",
              "index": "not_analyzed"
            },
            "color": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    }
  }
}'

【讨论】:

    【解决方案2】:
    {
        "query" : {
            "nested" : {
                "path" : "cloth.variation",
                "query" : {
                    "bool" : {
                        "must" : [
                            { "term" : { "cloth.variation.size" : "XXL" } },
                            { "term" : { "cloth.variation.color" : "black" } }
                        ]
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 你应该添加一些解释。
    猜你喜欢
    • 2021-09-01
    • 2015-09-24
    • 2019-09-12
    • 2017-06-11
    • 2020-07-26
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多