[英]Elastic Search: Query on multiple fields
I have JSON document in elastic search as follows 我在弹性搜索中有JSON文档如下
{
"animals": [
{
"id": 1,
"name": "cat"
},
{
"id": 2,
"name": "dog"
},
{
"id": 3,
"name": "rabbit"
}
]
}
How to query return this document only when all the three animals are present? 如果查询只有当所有三只动物都存在时才返回此文件?
This is not working. 这不起作用。
curl -H 'Content-Type: application/json' -XPOST http://localhost:9200/*animals*/_search -d '{
"query": {
"bool": {
"must": [
{
"term": {
"animals.name.keyword": "dog"
}
},
{
"term": {
"animals.name.keyword": "cat"
}
},
{
"term": {
"animals.name.keyword": "rabbit"
}
}
],
"must_not": [],
"should": []
}
}
}'
In order to achieve what you want to need to make sure that animals
is of nested type in your index mapping: 为了达到您想要的目的,确保
animals
在索引映射中具有嵌套类型:
PUT animals
{
"mappings": {
"properties": {
"animals": {
"type": "nested"
}
}
}
}
Then your query needs to look like this: 然后您的查询需要如下所示:
curl -H 'Content-Type: application/json' -XPOST http://localhost:9200/*animals*/_search -d '{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "animals",
"query": {
"term": {
"animals.name.keyword": "dog"
}
}
}
},
{
"nested": {
"path": "animals",
"query": {
"term": {
"animals.name.keyword": "cat"
}
}
}
},
{
"nested": {
"path": "animals",
"query": {
"term": {
"animals.name.keyword": "rabbit"
}
}
}
}
]
}
}
}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.