【问题标题】:Elastic Search : General and conditional filters弹性搜索:通用和条件过滤器
【发布时间】:2023-03-31 05:36:01
【问题描述】:

我使用的是 Elastic Search,带有查询 match_all 和过滤。在我的情况下,我想应用一般过滤器并按条件过滤。

这里是伪:

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

请参阅以下代码。如果“组”字段存在,我只想应用“组”过滤器! “exists”过滤器在这种情况下不起作用。

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

【问题讨论】:

    标签: elasticsearch conditional apply exists


    【解决方案1】:

    试试这个

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

    【讨论】:

    • 积极的方法呢? “存在”而不是“缺失”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多