[英]Elastic Search: Query on multiple fields
我在弹性搜索中有JSON文档如下
{
"animals": [
{
"id": 1,
"name": "cat"
},
{
"id": 2,
"name": "dog"
},
{
"id": 3,
"name": "rabbit"
}
]
}
如果查询只有当所有三只动物都存在时才返回此文件?
这不起作用。
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": []
}
}
}'
为了达到您想要的目的,确保animals
在索引映射中具有嵌套类型:
PUT animals
{
"mappings": {
"properties": {
"animals": {
"type": "nested"
}
}
}
}
然后您的查询需要如下所示:
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.