我试图让ElasticSearch在我的盒子上工作。我有以下映射:
{
"sneakers" : {
"mappings" : {
"sneaker" : {
"properties" : {
"brand" : {
"type" : "nested",
"properties" : {
"id" : {
"type" : "integer",
"index" : "no"
},
"title" : {
"type" : "string"
}
}
}
}
}
}
}
}
所以我有一个'运动鞋'索引与运动鞋'类型,品牌'拥有' id'和一个标题'。
检查运动鞋是否存在,运行卷曲-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"
}
}
}, ...
}
但是一旦我开始使用查询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不起作用?我做错了什么?
答案 0 :(得分: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"
,您的查询将会有效。