2013-10-04 76 views
0

我試圖索引嵌套的數據和查詢彈性搜索,但得到以下錯誤一個簡單的嵌套數據:如何映射然後查詢上elasticsearch

"filter":[]}}}]]]; nested: QueryParsingException[[products] [_na] query malformed, no field after start_object]; }] 

我的映射是:

{ 
    "product": { 
    "properties": { 
     "id": { "type": "integer", "store": true }, 
     "description": { "type": "string" }, 
     "kind": { "type": "string" }, 
     "name": { "type": "string", "store": true }, 
     "tags": { 
     "type": "nested", 
     "properties": { 
      "label": { 
      "type": "string", 
      "index": "not_analyzed", 
      "omit_norms": true, 
      "index_options": "docs" 
      }, 
      "slug": { 
      "type": "string", 
      "index": "not_analyzed", 
      "omit_norms": true, 
      "index_options": "docs" 
      } 
     } 
     } 
    } 
    } 
} 

我成功地讓所有的耳機下面的查詢:

{ 
    "query": { 
     "filtered": { 
      "query": { 
       "bool": { 
        "must": { 
         "match": { "kind": "Headphone" } 
        }, 
        "must_not": [], 
        "should": [] 
       } 
      }, 
      "filter": [] 
     } 
    } 
} 

我的問題是什麼是正確的查詢STRU找到「帶XX標記的耳機」或「具有XX和YY標籤的耳機(另一個查詢)」,同時支持方面?

我只是試圖查詢部分合並以下上面的查詢,但我不能找出「正確」的地方(鍵)把它:

{ 
    "nested": { 
     "path": "tags", 
     "filter": { 
      "bool": { 
       "must": { 
        "terms": { 
         "tags.slug": [ "discount", "black"], 
         "minimum_should_match": 1 
        } 
       } 
      } 
     } 
    } 
} 

回答

0

首先,映射沒有按似乎無效。例如,正確的映射應該包含properties而不是mapping。請確保您的映射實際上得到通過運行

curl "localhost:9200/products/product/_mapping?pretty" 

搜索請求上缺少頂層query元素,minimum_should_match不被terms過濾器支持應用。所以,請求應該看起來像這樣:

{ 
    "query": { 
     "nested": { 
      "path": "tags", 
      "filter": { 
       "bool": { 
        "must": { 
         "terms": { 
          "tags.slug": [ "discount", "black"] 
         } 
        } 
       } 
      } 
     } 
    } 
}' 
+0

哦謝謝你的回答。有幾個問題。我只是更新了有問題的映射,現在閱讀了一些關於這些問題的文檔.. – edigu

+0

這裏是一個完整的工作示例,以防您有任何困難 - https://gist.github.com/imotov/eb0f09a58668af4b726f – imotov