2016-08-18 101 views
0

步骤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" 
       } 
      } 
     } 
    } 
    } 
} 

GET:http://localhost:9200/shop/_mapping/cloth

HTTP/1.1 200 OK 内容类型:application/json; charset = UTF-8 Content-Length:518

{“shop”:{“mappings”:{“cloth”:{“properties”:{“cloth”:{“properties”:{“properties” { “属性”:{ “名称”:{ “属性”:{ “索引”:{ “类型”: “串”}, “类型”:{ “类型”: “串”}}}, “变异”: { “属性”:{ “属性”:{ “属性”:{ “颜色”:{ “属性”:{ “索引”:{ “类型”: “串”}, “类型”:{ “类型”:”字符串 “}}},” 尺寸 “:{” 属性 “:{” 索引 “:{” 类型 “:” 串 “},” 类型 “:{” 类型 “:” 串 “}}}}},” 类型“:{” 类型 “:” 串 “}}}}}}},” 姓名 “:{” 类型 “:” 串 “},” 变异 “:{” 属性 “:{” 颜色 “:{” 类型” : “串”}, “尺寸”:{ “类型”: “串”}}}}}}}}

步骤2:

插入的数据与下面给出data.j儿子 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错误的请求搜索 内容类型:application/json;字符集= UTF-8 的Content-Length: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} 

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

+0

可以更新与输出你'形式卷曲-XGET本地主机你的问题:9200 /店/ _mapping/cloth'? – Val

+0

我们如何插入映射,因为我正在使用POST作为mapping.json内容 –

+0

我的不好,抱歉,请再次检查我上面的评论。 – Val

回答

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" 
      } 
      } 
     } 
     } 
    } 
    } 
}' 
+0

这个工作适合你吗? – Val

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

你应该添加一些解释。 – Sam