2016-08-18 86 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