ruby-on-rails - Rails-如何在searchkick中过滤字符串字段?

标签 ruby-on-rails ruby elasticsearch where

在rails中,我使用searchkick gem进行搜索。我需要添加一个名为status的字符串字段进行过滤。如何添加到此插件?

我已经对此问题Rails - How to add more fields for filter in searchkick?提出了一个问题

现在,我尝试使用字符串字段而不是 bool(boolean) 值,但搜索仍然无法正常工作。请同样帮助我。

我添加了一个类似于where: {status: 'approved'}的条件,根据此条件,我应该仅获得 Activity 用户(而不是“已删除”用户)。目前没有任何搜索数据在显示。

searchkick word_start: [:name]

def initialize(name, limit = User::SUGGESTION_LIMIT, page = nil)
  @name = name
  @limit = limit
  @page = page
  @per_page = limit.to_i
end

query = {
  match: :word_start,
  where: {status: 'approved'},
  fields: [{ emails: "exact" }, "name^5"],
  misspellings: { prefix_length: 2 },
  load: false
}

User.search(name, query).records

我也尝试过通过添加searchkick word_start: [:name], filterable: [:status]这样的filterable

服务器日志是
Processing by UsersController#search as JSON
Parameters: {"query"=>"sal"}
ETHON: Libcurl initialized
ETHON: performed EASY 
effective_url=http://elastic:changeme@14.127.18.141:9200/users-some_index-en/_search response_code=200 return_code=ok total_time=0.498938
User Search (589.3ms)  curl http://14.127.18.141:9200/users-some_index-en/_search?pretty -d '{"query":{"bool":{"must":{"dis_max":{"queries":[{"match":{"emails.true":{"query":"sal","boost":10,"operator":"and","analyzer":"searchkick_autocomplete_search"}}},{"match":{"emails.true":{"query":"sal","boost":1,"operator":"and","analyzer":"searchkick_autocomplete_search","fuzziness":1,"prefix_length":2,"max_expansions":3,"fuzzy_transpositions":true}}},{"bool":{"must":{"bool":{"should":[{"match":{"name.word_start":{"query":"sal","boost":50.0,"operator":"and","analyzer":"searchkick_word_search"}}},{"match":{"name.word_start":{"query":"sal","boost":5.0,"operator":"and","analyzer":"searchkick_word_search","fuzziness":1,"prefix_length":2,"max_expansions":3,"fuzzy_transpositions":true}}}]}},"should":{"match":{"name.analyzed":{"query":"sal","boost":50.0,"operator":"and","analyzer":"searchkick_word_search"}}}}}]}},"filter":[{"term":{"status":"approved"}}]}},"size":5,"from":0,"timeout":"11s"}'
User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
ETHON: performed EASY effective_url=http://elastic:changeme@14.127.18.141:9200/entities-some_index-en/_search response_code=200 return_code=ok total_time=0.251783

最佳答案

根据您的条件更改scope :search_import并修改should_index?方法,例如,

def should_index?
  User.search_import
end

更改之后,请分别调用reindex任意数据的after_update

关于ruby-on-rails - Rails-如何在searchkick中过滤字符串字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47992634/

相关文章:

ruby - Ubuntu 中的命令正在安装 ruby​​ 1.8,而不是最新的 1.9

elasticsearch - Logstash输出日志到Elasticsearch服务器ConfigurationError

elasticsearch - Elastica-多个 bool 查询-子查询

ruby-on-rails - CanCan 在应该阻止访问时却没有阻止

ruby-on-rails - Ruby on Rails,rake 数据库 :seed or db:reset - how do you dictate which mode (development/test/production)?

ruby-on-rails - 如何在 Rails 引擎的 rspec 测试中从父应用程序访问模型?

ruby-on-rails - XPath 或 CSS 解析速度更快(对于 HTML 文件上的 Nokogiri)?

ruby-on-rails - rails : ActiveRecord Association returns a nil relationship

ruby-on-rails - rails-如何根据时间/日期更新属性

elasticsearch - Elastica或ElasticSearch从所有文档中删除字段