【问题标题】:Elasticsearch Term Filter SlowElasticsearch 词条过滤器慢
【发布时间】:2012-12-13 00:25:26
【问题描述】:

我们目前正在运行一个具有 2 个索引的 2 节点弹性搜索集群,它的性能非常好(750k 文档和 1110 万文档)。

我们现在正尝试添加一个包含 3540 万个文档的新索引,但搜索性能很慢。一个词条过滤器大约需要 2 秒才能返回。

映射:

tire do
  mapping _routing: { required: true, path: :order_id } do
    indexes :id,            type: 'string', index: :not_analyzed
    indexes :order_id,      type: 'string', index: :not_analyzed

    [:first_name, :last_name, :company_name, :title, :email, :city, :state_region_province, :postal_code].each do |attribute|
      indexes attribute, type: 'string', analyzer: 'keyword'
    end

    indexes :metadata,      type: 'string'
    indexes :clicks,        type: 'integer', index: :not_analyzed, include_in_all: false
    indexes :view_count,    type: 'integer', index: :not_analyzed, include_in_all: false
    indexes :sender,        type: 'boolean', index: :not_analyzed, include_in_all: false
    indexes :bounced,       type: 'boolean', index: :not_analyzed, include_in_all: false
    indexes :unsubscribed,  type: 'boolean', index: :not_analyzed, include_in_all: false
  end
end

搜索:

Model.tire.search(load: true, page: page, per_page: per_page, routing: order_id) do |search|
  search.query do
    match :metadata, query, type: 'phrase_prefix', max_expansions: 10
  end if query.present?

  search.filter :term, order_id: order_id
  search.filter :term, sender: false
end

我正在做的搜索只是指定要过滤的 order_id;返回结果大约需要 2 秒。如何加快速度?

编辑: 我现在正在索引 user_id 并将其用作路由路径。我创建了一个包含 30 个分片的新索引来测试过度分配。

编辑 2: 使用 30 个分片时,索引的性能更高,但在第一个查询中返回数据仍然需要一秒钟的时间。我不确定如何加快速度或我做错了什么。

【问题讨论】:

    标签: ruby-on-rails ruby indexing elasticsearch tire


    【解决方案1】:

    如果将order_id 字段的分析切换为:keyword,会发生什么情况?来自:

    indexes :order_id,      type: 'string', index: :not_analyzed
    

    到:

    indexes :order_id,      type: 'string', index: :keyword
    

    The docs说:

    一种类型关键字的分析器,将整个流“标记化”为单个标记。这对于邮政编码、ID 等数据很有用。

    这似乎适用于order_id

    【讨论】:

    • 我做了'not_analyzed',因为术语过滤器特别提到使用它:elasticsearch.org/guide/reference/query-dsl/term-filter.html
    • 老实说,对于数字 id 之类的东西,我不确定 not_analyzed 和关键字之间有什么区别。关键字分析器只是将整个输入作为单个标记吐出,但在这种情况下,数字 id 在不分析时已经是单个标记。
    【解决方案2】:

    如果您的查询不使用构面,我建议将您的查询转换为 filtered query 并将术语过滤器从顶级移动到过滤查询中的过滤器。另见Performance of elastic queries

    【讨论】:

    • 我会试一试并报告。
    • 我试过了,没有任何区别。请注意,在我的搜索中,我没有指定查询字符串,所以这只是测试 2 个术语过滤器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 2014-12-11
    • 2015-09-08
    • 1970-01-01
    相关资源
    最近更新 更多