假设我有以下模型,并且我已使其可使用 sunspot_rails
进行搜索。
class Case < ActiveRecord::Base
searchable do
end
end
Rails 中 Sunspot 的标准 schema.xml
将 id
声明为索引字段。当我使用 Web 界面访问 solr 并测试查询时,查询如下:
http://localhost:8982/solr/select/?q=id%3A%22Case+15%22&version=2.2&start=0&rows=10&indent=on
搜索 id
等于 Case 15
的 Cases
工作正常,并且返回结果。
问题是当我在 Rails 控制台中使用 Sunspot Rails 进行搜索时:
s = Case.search do
keywords('id:"Case 15"')
end
我得到:
=> <Sunspot::Search:{:fl=>"* score", :rows=>10, :start=>0, :q="id:\"Case 15\"", :defType=>"dismax", :fq=>["type:Case"]}>
这表明它正确地输入了 :q
正确的查询值,但命中率为 0:
s.hits
返回
=> []
如果我们假设 keywords
不等价,并且仅搜索 text
字段(全文搜索),而不搜索冒号 :
之前定义的字段,则我可以尝试以下操作:
s = Case.search do
with(:id, "Case 15")
end
但这失败了Sunspot 异常:
Sunspot::UnrecognizedFieldError: No field configured for Case with name 'id'
如何使用模型的索引标准 solr/sunspot id
字段进行搜索?
为了使问题更有用,我如何通过 id 进行排序。以下不起作用:
s = Case.search do
keywords("xxxx")
order_by :id, :desc
end
不起作用。 Sunspot::UnrecognizedFieldError: 没有为名为“id”的案例配置字段
Assume that I have the following model and I have made it searchable with sunspot_rails
.
class Case < ActiveRecord::Base
searchable do
end
end
Standard schema.xml
of Sunspot in Rails declare id
as an indexed field. When I use the web interface to access solr and test queries a query like:
http://localhost:8982/solr/select/?q=id%3A%22Case+15%22&version=2.2&start=0&rows=10&indent=on
which searches for Cases
with id
equal to Case 15
works fine and returns results.
The problem is when I carry out the search with Sunspot Rails in the rails console:
s = Case.search do
keywords('id:"Case 15"')
end
I get:
=> <Sunspot::Search:{:fl=>"* score", :rows=>10, :start=>0, :q="id:\"Case 15\"", :defType=>"dismax", :fq=>["type:Case"]}>
which show that it correctly puts in :q
the correct query value, but the hits are 0:
s.hits
returns
=> []
If we assume that keywords
is not equivalent and only searches the text
field (full-text search) and not the field defined before the colon :
, then I can try the following:
s = Case.search do
with(:id, "Case 15")
end
but this fails with a Sunspot exception:
Sunspot::UnrecognizedFieldError: No field configured for Case with name 'id'
How can I search using the indexed standard solr/sunspot id
field of my model?
And to make the question more useful, how can I order by the id. The following does not work:
s = Case.search do
keywords("xxxx")
order_by :id, :desc
end
does not work. Sunspot::UnrecognizedFieldError: No field configured for Case with name 'id'
发布评论
评论(1)
您所说的 id 是 Sunspot 内部字段,不应直接使用。
为什么不添加自己的 id 字段(更改变量名称,以避免名称冲突):
然后
其他(混乱)选项将直接破解 solr 参数:
The id that you are talking about is a Sunspot internal field and it should not be used directly.
Why not add your own id field (change variable name, to avoid name collision):
and then
Other (messy) option would be to hack directly solr params: