问题描述
我目前正在使用 sunspot gem 在我的 rails 应用程序中实现全文搜索.通过 Sunspot/Solr 在我的网站上进行的查询正在运行并返回正确的结果.但是,当我尝试使用 Solr 管理页面进行查询时,我很难显示结果.使用查询字符串 *:*
我可以显示索引数据库中包含的所有结果,但我无法进行正确的查询.如果我尝试使用 *:*
以外的字符串进行查询,例如 test
,则不会返回任何结果,只剩下:
I am currently using the sunspot gem to implement full text search in my rails application. Queries on my website through Sunspot/Solr are working and returning the proper results. However, when I attempt to make a query using the Solr admin page, I am having a hard time displaying results. Using the query string *:*
I can display all the results contained in my indexed database, but I cannot make a proper query. If I try to make a query using a string other than *:*
, such as test
, no results are returned and I am left with:
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">21</int>
<lst name="params">
<str name="explainOther"/>
<str name="fl">*,score</str>
<str name="indent">on</str>
<str name="start">0</str>
<str name="q">test</str>
<str name="hl.fl"/>
<str name="qt"/>
<str name="wt"/>
<str name="fq"/>
<str name="version">2.2</str>
<str name="rows">10</str>
</lst>
</lst>
<result name="response" numFound="0" start="0" maxScore="0.0"/>
</response>
使用字符串进行相同的查询:test
在实际的 Rails 应用程序上返回 100 多个结果.
Making the same query using the string: test
on the actual rails application returns over 100 results.
如何在 Solr 管理页面中进行查询返回与在 rails 应用中进行的查询相同的项目?
How can I make queries in the Solr admin page return the same items as the queries made in the rails app?
推荐答案
稍微扩展 Jayendra 的回答(本质上是正确的):
Expanding on Jayendra's answer a bit (which is essentially right):
如果我尝试使用 *:*
以外的字符串进行查询,例如 test
,则不会返回任何结果……
该查询是针对 defaultSearchField
运行的,它在 Sunspot 中默认为 text
.然而,令人困惑的是,Sunspot 并没有在 text
字段中放入任何内容,因此您的搜索结果是正确的.在它们的 text
字段中没有带有术语 test
的文档,因为没有带有 text
字段的文档.
That query is being run against the defaultSearchField
, which is text
by default in Sunspot. Confusingly, however, Sunspot isn't putting anything into that text
field, so your search results are correct. There are no documents with the term test
in their text
field because there are no documents with a text
field.
例如,您可能有一个 title_text
字段.您可以使用 q=title_text:test
直接查询该字段.
You may have, say, a title_text
field. You can query that field directly with q=title_text:test
.
您还可以模仿 Sunspot 的查询:使用 DisMax 查询解析器,在 qf
中明确指定要查询的字段.如果您想查询多个文本字段,这很有用,更不用说获得 DisMax 的其他好处了:q=test&defType=dismax&qf=title_text+body_text
You can also mimic Sunspot's queries: use the DisMax query parser, explicitly specifying the fields to query in qf
. This is helpful if you want to query against multiple text fields, not to mention receive the other benefits of DisMax: q=test&defType=dismax&qf=title_text+body_text
此外,Sunspot 将其查询记录到 development.log
,这是查找示例的好地方.
Also, Sunspot logs its queries to development.log
which is a good place to look for examples.
这篇关于使用 Susnpot Gem 进行 Solr 搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!