gpt4 book ai didi

Solr(太阳黑子)在包含后缀时找不到部分单词匹配

转载 作者:行者123 更新时间:2023-12-02 14:08:58 26 4
gpt4 key购买 nike

我正在将 solr 实现到 Rails 应用程序中,特别是与成分搜索相关的应用程序。如果我对“beef”或“chicken”等简单名词进行部分单词匹配,我可以输入从 1 到完整字符串的任意数量的字母,它会找到包含这些单词的成分。当我有一个带有后缀的单词时,例如“eggs”(-s)、“baked”(-ed) 或“baking”,问题就出现了(-ing)。

我们以“烘焙”为例。我可以搜索“b​​”、“ba”或“bak”以获取包含“baking”一词的任何结果。如果我搜索“baki”、“bakin”或“baking”,则会找不到任何结果。

我想知道我的 Rails 搜索代码是否有问题,或者我是否需要编辑 schema.xml 文件中的某些内容。我的模式是 sunspot 提供的默认模式。我的模型和搜索代码如下所示。

class Ingredient < ActiveRecord::Base
validates :name, presence: true, uniqueness: true

searchable do
text :name
end

def self.search_by_partial_name(name)
keywords = name.to_s.split(/\s+/).delete_if(&:blank?)

search = Sunspot.search(self) do
text_fields do
keywords.each do |keyword|
with(:name).starting_with(keyword)
end
end
end

search.results
end
end

搜索:

Ingredient.search_by_partial_name('baki')  # => []
Ingredient.search_by_partial_name('bak') # => [<Ingredient "baking powder">,
<Ingredient "baking potato">,
...]

谢谢!

编辑:以下是有关上述两个示例所执行的 solr 查询的日志。

Started GET "/admin/ingredients/search?term=bak" for 127.0.0.1 at 2014-11-23 09:21:01 -0700
Processing by Admin::IngredientsController#search as JSON
Parameters: {"term"=>"bak"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
SOLR Request (4.9ms) [ path=select parameters={fq: ["type:Ingredient", "name_text:bak*"], start: 0, rows: 30, q: "*:*"} ]
Ingredient Load (0.8ms) SELECT "ingredients".* FROM "ingredients" WHERE "ingredients"."id" IN (9853, 9858, 10099, 10281, 10289, 10295, 10350, 10498, 10507, 10583, 10733, 10787, 11048, 11148, 11395, 11603, 11634, 11676, 11734, 11863, 12031, 12189, 12268, 12399, 13128, 13577, 13830, 13886, 14272, 14366)
Completed 200 OK in 12ms (Views: 1.3ms | ActiveRecord: 1.1ms | Solr: 4.9ms)

Started GET "/admin/ingredients/search?term=baki" for 127.0.0.1 at 2014-11-23 09:21:22 -0700
Processing by Admin::IngredientsController#search as JSON
Parameters: {"term"=>"baki"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
SOLR Request (4.5ms) [ path=select parameters={fq: ["type:Ingredient", "name_text:baki*"], start: 0, rows: 30, q: "*:*"} ]
Completed 200 OK in 7ms (Views: 0.4ms | ActiveRecord: 0.4ms | Solr: 4.5ms)

最佳答案

在搜索查询末尾添加星号:

Ingredient.search_by_partial_name('baki*')

关于Solr(太阳黑子)在包含后缀时找不到部分单词匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26705292/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com