Elastic Search : General and conditional filters

半世苍凉 提交于 2019-12-10 09:50:59

问题


I'm using Elastic Search, with query match_all and filtering. In my situation I want to apply a general filter and filters by condition.

Here in pseudo:

  1. query: match all (works fine)
  2. filter range date between d1 and d2 (works fine without bullet 3)
  3. filter (apply only if field exists, but how?)
  4. etc.

See the following code. I want only apply the "groups" filter if the "groups" field exists! The "exists" filter doesn't take effect in that case.

    "query":
    {
        "filtered":
        {
            "query":
            {
                "match_all": {}
            },
            "filter":
            {
                "bool":
                {
                    "must":
                    {
                        "range":
                        {
                            "date": {
                                "from": "2015-06-01",
                                "to": "2015-06-30"
                            }
                        }

                    },
                    "must_not":
                    {
                        "term":
                        {
                            "e.state": 0
                        }
                    }
                }
            },
            "filter":
            {
                "bool":
                {
                    "must":
                    {
                        "exists": {"field": "groups"},
                        "filter":
                        {
                            "bool":
                            {
                                "must":
                                {
                                    "term": {"groups.sex": "w"}
                                },
                                "should":
                                {
                                    "terms": {"groups.categories.id": [7,10]}
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

回答1:


Try this

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "date": {
                  "from": "2015-06-01",
                  "to": "2015-06-30"
                }
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "missing": {
                      "field": "groups"
                    }
                  },
                  {
                    "bool": {
                      "must": {
                        "term": {
                          "groups.sex": "w"
                        }
                      },
                      "should": {
                        "terms": {"groups.categories.id": [7,10]}
                      }
                    }
                  }
                ]
              }
            }
          ],
          "must_not": {
            "term": {
              "e.state": 0
            }
          }
        }
      }
    }
  }
}


来源:https://stackoverflow.com/questions/30652019/elastic-search-general-and-conditional-filters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!