【问题标题】:Elasticsearch use filter on index only when index has field仅当索引具有字段时,Elasticsearch 才对索引使用过滤器
【发布时间】:2017-09-14 22:38:57
【问题描述】:

有 2 个索引:categoriesposts

类别

  • 姓名
  • 身体

帖子

  • 姓名
  • 身体
  • publish_at
  • 发布_直到

我想在publish_atpublish_until 上为posts 索引过滤两个索引。

http://localhost:9200/categories,posts/_search

{
      "query": {
          "bool": {
              "must": {
                  "multi_match": {
                      "query": "keyword",
                      "fields": [
                          "name^3",
                          "body"
                      ]
                  }
            },
            "filter": [{
                "bool": {
                    "must": [
                        { 
                            "range": {
                                "publish_at": {
                                    "lte" : "now"
                                }
                            }
                        },
                        { 
                            "range": {
                                "publish_until": {
                                    "gt" : "now"
                                }
                            }
                        }
                    ]
                }
            }]
        }
    }
}

这个查询只给我posts 作为结果。我还希望在我的结果中出现categories

如何将日期范围过滤器仅应用于具有 publish_atpublish_until 字段的索引并跳过其他索引的日期范围过滤器?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    好吧,在玩了一天 bool 之后,我搞定了:

    {
        "query": {
            "bool" : {
                "must" : [
                    {
                        "multi_match": {
                            "query": "keyword",
                            "fields": [
                                "name^3",
                                "body"
                            ]
                        }
                    }, 
                    {
                        "bool": {
                            "should": [
                                {
                                    "bool": {
                                        "must": [
                                            {
                                                 "range": {
                                                    "publish_at": {
                                                        "lte" : "now"
                                                    }
                                                }
                                            },
                                            {
                                                "range": {
                                                    "publish_until": {
                                                        "gt" : "now"
                                                    }
                                                }
                                            }
                                        ]
                                    }
                                },
                                {
                                    "bool": {
                                        "must_not": [
                                            {
                                                "exists": {
                                                    "field": "publish_at"
                                                }
                                            },
                                            {
                                                "exists": {
                                                    "field": "publish_until"
                                                }
                                            }
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-04
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 2017-12-14
      相关资源
      最近更新 更多