有关嵌套对象的elasticsearch复杂查询

时间:2016-11-20 13:10:40

标签: 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中执行此查询?

1 个答案:

答案 0 :(得分:1)

试试这个,两个条件的应用条款

{
    "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"
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}