弹性查询嵌套查询

时间:2016-08-18 11:19:59

标签: json elasticsearch

第1步:

创建弹性搜索索引 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好的 Content-Type:application / json;字符集= UTF-8 内容长度:518

{"铺" {"映射" {"布" {"属性" {"布&# 34;:{"属性" {"属性" {"属性" {"名称" {"属性&# 34;:{"指数" {"类型":"串"}"类型" {"类型&#34 ;:"串"}}}"变异" {"属性" {"属性" {"属性&# 34;:{"颜色" {"属性" {"指数" {"类型":"串&#34 ;}"类型" {"类型":"串"}}}"大小" {"属性&# 34;:{"指数" {"类型":"串"}"类型" {"类型&#34 ;:"串"}}}}}"类型" {"类型":"串"}}}}}}} "名称" {"类型":"串"}"变异" {"属性":{ "颜色" {"类型":"串"}"大小" {"类型":&# 34;串"}}}}}}}}

第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错误请求 Content-Type:application / 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}

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

2 个答案:

答案 0 :(得分: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"
            }
          }
        }
      }
    }
  }
}'

答案 1 :(得分:0)

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