2016-10-03 59 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