【问题标题】:Elasticsearch - search across multiple indices with conditional decay functionElasticsearch - 使用条件衰减函数搜索多个索引
【发布时间】:2015-02-18 16:45:19
【问题描述】:

我正在尝试使用一个查询搜索多个索引,但仅将高斯衰减函数应用于其中一个索引上存在的字段。

我正在通过 elasticsearch-api gem 运行它,这部分工作得很好。

这是我在 Marvel 中运行的查询。

GET episodes,shows,keywords/_search?explain
{
"query": {
  "function_score": {
    "query": {
      "multi_match": {
        "query": "AWESOME SAUCE",
        "type": "most_fields",
        "fields": [ "title", "summary", "show_title"]
      }
    },
    "functions": [
      { "boost_factor":  2 },
      {
        "gauss": {
          "published_at": {
            "scale": "4w"
          }
        }
      }
    ],
  "score_mode": "multiply"
  }
},
  "highlight": {
  "pre_tags": ["<span class='highlight'>"],
  "post_tags": ["</span>"],
  "fields": {
    "summary": {},
    "title": {},
    "description": {}
   }
 }
}

该查询非常适用于剧集索引,因为它具有可让高斯函数发挥其魔力的 published_at 字段。但是,当跨所有索引运行时,它对于节目和关键字失败(对于剧集仍然成功)。

如果 published_at 字段存在或在单集索引上,是否可以运行条件高斯衰减函数?

我愿意探索替代方案(即为每个索引运行单独的查询,然后合并结果),但我认为单个查询在性能方面是最好的。

谢谢!

【问题讨论】:

    标签: elasticsearch elasticsearch-api


    【解决方案1】:

    您可以添加一个过滤器以将这些高斯衰减函数仅应用于文档子集:

    {
      "filter": {
        "exists": {
          "field": "published_at"
        }
      }
      "gauss": {
        "published_at": {
          "scale": "4w"
        }
      }
    }
    

    对于没有该字段的文档,您可以返回 0 分:

    {
      "filter": {
        "missing": {
          "field": "published_at"
        }
      }
      "script_score": {
        "script": "0"
      }
    }
    

    【讨论】:

    • 感谢您的回答!我最终改用 indices 查询并根据索引指定了 2 个不同的查询。对这种方法与您建议的解决方案的优缺点有何想法?
    • 如果文档没有published_at 字段会发生什么,即使您希望它有?我打赌它会得到最高分,我认为你不希望这样。
    • 我想我从来没有考虑过这一点,因为在我们的例子中,如果文档没有被发布,就不会被索引......但如果是这样的话,这个答案比我们的实现要好。丹克。
    • 我认为这个解决方案不适用于当前版本的 elasticsearch。如果某些索引的映射中不存在字段,我会收到有关字段不存在的错误。
    【解决方案2】:

    在较新的 elasticsearch 版本中,您必须使用 script score queryfunction score 查询将被弃用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      相关资源
      最近更新 更多