我正在学习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": {}
}
答案 0 :(得分:9)
首先,为了搜索嵌套字段,您需要使用nested query:
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
且term query未执行分析。因此,最好将其替换为match query {"match": {"creators.name": "Steve"}}
。