我有一个书单,每本书都有嵌套标签:
"hits": [
{
"_index": "",
"_type": "",
"_id": "",
"_score": ,
"_source": {
"name": "book1",
"tags": [
{
"t": "tagA",
"w": 100
},
{
"t": "tagB",
"w": 0
},
],
"active": true,
}
},
{
"_index": "",
"_type": "",
"_id": "",
"_score": ,
"_source": {
"name": "book2",
"tags": [
{
"t": "tagA",
"w": 100
},
{
"t": "tagB",
"w": 0
},
],
"active": true,
}
},
{
"_index": "",
"_type": "",
"_id": "",
"_score": ,
"_source": {
"name": "book3",
"tags": [
{
"t": "tagC",
"w": 100
},
{
"t": "tagB",
"w": 0
},
],
"active": false,
}
}]
首先,我尝试使用特定标签获取所有“有效”图书,这可以通过以下查询获得:
GET /index/type/_search
{
"query": {
"bool": {
"must_not": {"term" : { "active" : false}},
"must":
[
{
"nested": {
"path": "tags",
"query": {
"bool": {
"must": [
{
"match": {
"tags.t": "tagB"
}
}
]
}
}
}
}
]
}
}
}
对于上述内容,book1和book2返回了。
但是我现在想要变得更加复杂。
我正在尝试获取带有特定标签(tagB)的“有效”图书。但是如果'tagC'在簿中,则簿在未激活时也可以返回。
因此对于这个问题,book1,book2,book3将返回。
如何在Elasticsearch中执行此查询?
最佳答案
试试这个,这两个条件都应该使用
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "tags",
"query": {
"bool": {
"must": [
{
"match": {
"tags.t": "tagC"
}
}
]
}
}
}
},
{
"bool": {
"must": [
{
"term": {
"active": true
}
},
{
"nested": {
"path": "tags",
"query": {
"bool": {
"must": [
{
"match": {
"tags.t": "tagB"
}
}
]
}
}
}
}
]
}
}
]
}
}
}
关于elasticsearch - 对嵌套对象的Elasticsearch复杂查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40704375/