【问题标题】:Rails - How to filter a string field in searchkick?Rails - 如何在 searchkick 中过滤字符串字段?
【发布时间】:2018-06-08 03:00:41
【问题描述】:

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

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

现在我正在尝试使用字符串字段而不是布尔值,但搜索仍然不起作用。请帮助我解决同样的问题。

我添加了一个类似where: {status: 'approved'} 的条件,根据这个条件,我应该只获得活跃用户(而不是“删除”用户)。目前没有显示搜索数据。

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]这样的过滤器

服务器日志是,

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

【问题讨论】:

  • 您添加的代码的 sn-p 不足以让我们了解您正在尝试什么。添加更多代码。
  • 问题已更新。

标签: ruby-on-rails ruby elasticsearch where


【解决方案1】:

根据您的情况更改scope :search_import并修改should_index?方法,如,

def should_index?
  User.search_import
end

修改后,对任意数据分别调用after_updatereindex

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    相关资源
    最近更新 更多