ruby-on-rails - Elasticsearch术语过滤器速度慢

标签 ruby-on-rails ruby indexing elasticsearch tire

我们目前正在运行一个带有2个索引的2节点Elasticsearch集群,并且运行良好(750k文档和1,110万文档)。

我们现在正尝试添加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个分片,索引的性能更高,但仍然需要花费一秒钟的时间才能在第一个查询中返回数据。我不确定如何进一步加快速度或我做错了什么。

最佳答案

如果将对order_id字段的分析切换为:keyword会发生什么?从:

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

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

The docs说:

An analyzer of type keyword that “tokenizes” an entire stream as a single token. This is useful for data like zip codes, ids and so on.



似乎这适用于order_id

关于ruby-on-rails - Elasticsearch术语过滤器速度慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14057631/

相关文章:

ruby-on-rails - 思考 sphinx : search across multiple models: best practices?

ruby - 如何使用 ruby​​ 和 unix 服务器截取网页的屏幕截图?

ruby-on-rails - 状态模型设计模式

r - 为什么 data.table 行索引上的 for 循环比 data.frame 慢?

MongoDB 索引大文本字段似乎不会使查询更快?

ruby-on-rails - 消息/文章的路由错误

ruby-on-rails - column_types 在 Rails 4.0.1 中为 nil

ruby-on-rails - 带有远程 : true 的 Rails link_to URL 和 HTML 选项

ruby - 如何使 rspec-mocks 期望 receive.with 急切失败

sql - 不需要的 MySQL 索引