我的样本数据如下所示:
{"listings":{"title" : "testing 1", "address" : { "line1" : "3rd cross", "line2" : "6th main", "line3" : "", "landmark" : "", "location" : "k r puram", "pincode" : "", "city" : "Bangalore" },"purpose":"rent","published": true, "inActive": false }, {"listings":{"title" : "testing 2", "address" : { "line1" : "3rd cross", "line2" : "6th main", "line3" : "", "landmark" : "", "location" : "banaswadi", "pincode" : "", "city" : "Bangalore" },"purpose":"sale","published": true, "inActive": false }, {"listings":{"title" : "testing 3", "address" : { "line1" : "3rd cross", "line2" : "6th main", "line3" : "", "landmark" : "", "location" : "tin factory", "pincode" : "", "city" : "Bangalore" },"purpose":"rent","published": true, "inActive": false }
我的索引映射如下所示:
curl -X PUT localhost:9200/testing/listings/_mapping -d '{
"listings" : {
"properties" : {
"address" : {
"properties": {
"location": { "type" : "string",
"index" : "not_analyzed"
}
}
},
"suggest" : {
"type" : "completion",
"index_analyzer" : "simple",
"search_analyzer" : "simple",
"payloads" : true
}
}
}
}'
我根据租金或销售等目的 property Value 访问列表对象 . 我可以单独访问租赁和销售对象 . 如何访问租赁和销售 Value 的列表对象 . 我使用下面的查询来获取租金和销售列表对象 .
{"query":{
"filtered": {
"filter": {
"terms": {
"purpose" : ["rent", "sale"]
}
}
},
"bool":{
"must":[
{"match":{"published":true}},
{"match":{"inActive":false}},
{"match":{"address.city": "bangalore"}}
]
}
}
}
请根据需要建议更改 . 提前致谢 .
1 回答
有几件事:
address
应声明为nested object
,以防止将来搜索地址字段时出现错误的搜索结果 . 您可以在这里参考inner object
,nested object
:http://www.elasticsearch.org/blog/managing-relations-inside-elasticsearch/的问题的更多信息使用
nested object
时,映射将如下所示(地址类型:嵌套):"listings" : { "properties" : { "address" : { "type" : "nested", "properties": { "location": { "type" : "string", "index" : "not_analyzed" } } }, "suggest" : { "type" : "completion", "index_analyzer" : "simple", "search_analyzer" : "simple", "payloads" : true } } }
并且查询会改变一点:使用
terms
和execution=and
,并使用nested query
作为地址字段:"query":{ "filtered": { "filter": { "terms": { "execution" : "and", "purpose" : ["rent", "sale"] } }, "query" : { "bool":{ "must":[ {"term":{"published":true}}, {"term":{"inActive":false}}, { "nested": { "path": "address", "query": {"match":{"address.city": "bangalore"}} } } ] } } } }
您应该参考elasticsearch文档以获取每种查询的语法 . 希望它有所帮助