[英]Querying nested document Elasticsearch
我正在學習Elasticsearch所以我不確定這個查詢是否正確。 我已經檢查過數據已編入索引,但我沒有得到任何點擊。 我究竟做錯了什么? 難道這不會受到創造者名字史上的汽車的打擊嗎?
builder
.startObject()
.startObject("car")
.field("type", "nested")
.startObject("properties")
.startObject("creators")
.field("type", "nested")
.endObject()
.endObject()
.endObject()
.endObject();
{
"query": {
"bool": {
"must": [
{
"term": {
"car.creators.name": "Steve"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 50,
"sort": [],
"facets": {}
}
首先,為了搜索嵌套字段,您需要使用嵌套查詢 :
curl -XDELETE localhost:9200/test
curl -XPUT localhost:9200/test -d '{
"settings": {
"index.number_of_shards": 1,
"index.number_of_replicas": 0
},
"mappings": {
"car": {
"properties": {
"creators" : {
"type": "nested",
"properties": {
"name": {"type":"string"}
}
}
}
}
}
}
}
'
curl -XPOST localhost:9200/test/car/1 -d '{
"creators": {
"name": "Steve"
}
}
'
curl -X POST 'http://localhost:9200/test/_refresh'
echo
curl -X GET 'http://localhost:9200/test/car/_search?pretty' -d ' {
"query": {
"nested": {
"path": "creators",
"query": {
"bool": {
"must": [{
"match": {
"creators.name": "Steve"
}
}],
"must_not": [],
"should": []
}
}
}
},
"from": 0,
"size": 50,
"sort": [],
"facets": {}
}
'
如果使用標准分析器對car.creators.name
編制索引,則{"term": {"creators.name": "Steve"}}
將找不到任何內容,因為單詞Steve
被編入索引為steve
且術語查詢不執行分析。 因此,最好用匹配查詢 {"match": {"creators.name": "Steve"}}
替換它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.