弹性搜索:通用和条件过滤器 [英] Elastic Search : General and conditional filters

查看:43
本文介绍了弹性搜索:通用和条件过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Elastic Search,带有查询 match_all 和过滤.在我的情况下,我想应用通用过滤器并按条件过滤.

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.

这里是伪:

  1. 查询:匹配所有(工作正常)
  2. 过滤 d1 和 d2 之间的日期范围(没有项目符号 3 也可以正常工作)
  3. 过滤器(仅在字段存在时应用,但如何应用?)

请看下面的代码.如果组"字段存在,我只想应用组"过滤器!在这种情况下,exists"过滤器不起作用.

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

推荐答案

试试这个

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

这篇关于弹性搜索:通用和条件过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆