Elastic Search : General and conditional filters

2019-05-11 15:44发布

问题:

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
            }
          }
        }
      }
    }
  }
}