2016-10-03 64 views
0

我試圖讓ElasticSearch在我的盒子上工作。我有以下映射:查詢DSL elasticsearch不起作用

{ 
    "sneakers" : { 
    "mappings" : { 
     "sneaker" : { 
     "properties" : { 
      "brand" : { 
      "type" : "nested", 
      "properties" : { 
       "id" : { 
       "type" : "integer", 
       "index" : "no" 
       }, 
       "title" : { 
       "type" : "string" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

所以我有一個「鞋」指數以「運動鞋」型,與具有「身份證」和「標題」一個「品牌」屬性。

檢查球鞋存在,運行卷曲-XGET 'http://localhost:9200/sneakers/sneaker/1?pretty',我得到:

{ 
    "_index" : "sneakers", 
    "_type" : "sneaker", 
    "_id" : "1", 
    "_version" : 1, 
    "found" : true, 
    "_source" : { 
    "brand" : { 
     "id" : 1, 
     "title" : "Nike" 
    } 
    } 
} 

現在,runningcurl -XGET 'http://localhost:9200/sneakers/_search?q=brand.title=adidas&pretty' 我得到:

{ 
    "took" : 13, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 1330, 
    "max_score" : 0.42719018, 
    "hits" : [ { 
     "_index" : "sneakers", 
     "_type" : "sneaker", 
     "_id" : "19116", 
     "_score" : 0.42719018, 
     "_source" : { 
     "brand" : { 
      "id" : 2, 
      "title" : "Adidas" 
     } 
     } 
    }, ... 
} 

但只要我開始使用Query DSL:

curl -XGET 'http://localhost:9200/sneakers/_search?pretty' -d '{ 
    "query" : { 
     "term" : { "brand.title" : "adidas" } 
    } 
} 
' 

我得到

{ 
    "took" : 9, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 0, 
    "max_score" : null, 
    "hits" : [ ] 
    } 
} 

不知何故查詢DSL從不返回任何內容,甚至運行最簡單的查詢。我正在運行ES 2.3.1。

任何想法是爲什麼查詢DSL不工作?我究竟做錯了什麼?

回答

1

你映射brand字段作爲nested類型,所以你需要用nested query進行查詢,像這樣:

curl -XGET 'http://localhost:9200/sneakers/_search?pretty' -d '{ 
    "query" : { 
    "nested": { 
     "path": "brand", 
     "query": { 
      "term" : { "brand.title" : "adidas" } 
     } 
    } 
    } 
} 
' 

注意:如果你從你的映射刪除"type": "nested"您的查詢會工作。

+0

從映射中刪除「類型」:「嵌套」,現在完美工作。乾杯。 – Inigo

+0

太棒了,很高興幫助! – Val