Sunspot Rails 按型号 id 排序搜索结果?

发布于 2024-11-25 14:11:18 字数 1498 浏览 5 评论 0 原文

假设我有以下模型,并且我已使其可使用 sunspot_rails 进行搜索。

class Case < ActiveRecord::Base
  searchable do
  end
end

Rails 中 Sunspot 的标准 schema.xmlid 声明为索引字段。当我使用 Web 界面访问 solr 并测试查询时,查询如下:

http://localhost:8982/solr/select/?q=id%3A%22Case+15%22&version=2.2&start=0&rows=10&indent=on

搜索 id 等于 Case 15Cases 工作正常,并且返回结果。

问题是当我在 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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2024-12-02 14:11:18

您所说的 id 是 Sunspot 内部字段,不应直接使用。

为什么不添加自己的 id 字段(更改变量名称,以避免名称冲突):

class Case < ActiveRecord::Base
  searchable do
    integer :model_id {|content| content.id }
  end
end

然后

s = Case.search do
  keywords("xxxx")
  order_by :model_id, :desc
end

其他(混乱)选项将直接破解 solr 参数

s = Case.search do
  keywords("xxxx")
  adjust_solr_params(:sort, 'id desc')
end

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):

class Case < ActiveRecord::Base
  searchable do
    integer :model_id {|content| content.id }
  end
end

and then

s = Case.search do
  keywords("xxxx")
  order_by :model_id, :desc
end

Other (messy) option would be to hack directly solr params:

s = Case.search do
  keywords("xxxx")
  adjust_solr_params(:sort, 'id desc')
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文