问题描述
在 rails 中,我使用 searchkick
gem 进行搜索.我需要为过滤器添加一个名为 status
的字符串字段.如何添加到此插件?
In rails, I am using searchkick
gem for search. I need to add one string field called status
for filter. How can I add to this plugin?
我已经就这个问题提出了一个问题Rails - 如何在 searchkick 中为过滤器添加更多字段?
Already I have raised one question regarding this issue Rails - How to add more fields for filter in searchkick?
现在我正在尝试使用字符串字段而不是布尔值仍然搜索不起作用.请帮我解决同样的问题.
Now I am trying with string field instead of boolean still search is not working. Please help me regarding the same.
我添加了一个类似 where: {status: 'approved'}
的条件,根据这个条件,我应该只获取活跃用户(而不是已删除"用户).目前没有显示搜索数据.
I have added a condition like where: {status: 'approved'}
, as per this condition I should get only active users(not 'deleted' users). Right now no search data is displaying.
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]
Also I have tried by adding filterable like 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:[email protected]: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:[email protected]:9200/entities-some_index-en/_search response_code=200 return_code=ok total_time=0.251783
推荐答案
根据您的条件更改 scope :search_import
并修改 should_index?
方法,例如,
Change the scope :search_import
based on your condition and modify should_index?
method like,
def should_index?
User.search_import
end
更改后,对任何数据单独调用reindexafter_update
After this changes, call reindex separately after_update
of any data
这篇关于Rails - 如何在searchkick 中过滤字符串字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!