2013-10-04 37 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