太阳黑子`LIKE`查询 [英] Sunspot `LIKE` query
问题描述
我正在使用 sunspot
.如何运行 LIKE
查询 (LIKE %q%
)?我想做这样的事情:
I'm using sunspot
. How can I run a LIKE
query (LIKE %q%
)? I would like to do something like this:
@search = Sunspot.search(User) do |q|
q.text_fields { with(:company_name).like(params[:q]) }
end.results
代替:
@search = Sunspot.search(User) do |q|
q.text_fields { with(:company_name).starting_with(params[:q]) }
end.results
这部分对我有用.查看sunspot
代码,我发现了这段代码:
which partially works for me. Reviewing the sunspot
code, I found this piece of code:
class StartingWith < Base
private
def to_solr_conditional
"#{solr_value(@value)}*"
end
end
它基本上生成以下太阳黑子搜索哈希:
It basically generates the following sunspot search hash:
Sunspot.search(User) do |q|
q.text_fields { with(:company_name).starting_with("sta")} }
end
=> Sunspot::Search:{:q=>"*:*", :fq=>["type:User", "company_name_text:sta*"]}
如果没有更简单的方法来实现 LIKE %query%
,我应该如何使用 to_solr_conditional
方法创建一个新类 Like
生成 SOLR 逻辑?
In case there's no simpler way of implementing LIKE %query%
, how should I create a new class Like
with the method to_solr_conditional
which generates the SOLR logic?
推荐答案
如果使用标准 DisMax 处理程序,它不支持通配符.您有 2 个选择:
If you use the standard DisMax handler, it does not support wildcards. You have 2 options:
一个.激活 EdgeNGramFilter:
a. Activate EdgeNGramFilter:
<fieldType name="text" class="solr.TextField" omitNorms="false">
<analyzer type="index">
..
<filter class="solr.EdgeNGramFilterFactory" minGramSize="2" maxGramSize="15" side="front"/>
..
</analyzer>
</fieldType>
B.使用夜间构建 Solr 与 EDismax 处理程序.
b. Use nightly build Solr with EDismax Handler.
请参阅关于太阳黑子文档的维基文章或
这篇关于太阳黑子`LIKE`查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!