elasticsearch - 对嵌套对象的Elasticsearch复杂查询

标签 elasticsearch

我有一个书单,每本书都有嵌套标签:

    "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/

相关文章:

ElasticSearch:preserve_position_increments 不工作

elasticsearch - Elasticsearch 中的精确匹配搜索不起作用

ruby-on-rails-3 - 在rails中制作项目 "Private"

elasticsearch - 如何在Elasticsearch中对同一文档进行最大日期汇总?

logging - 使用哪个 Serilog 接收器发送到 Logstash?

elasticsearch - Elasticsearch查询结果导出到CSV/Excel文件

elasticsearch - 只允许索引模板中的字段

elasticsearch - 从Kibana中的单个查询绘制多行

c# - NEST Elasticsearch查询值在2个字段名之间

elasticsearch - 没有红色状态似乎无法重新启动Elastic Search