07-30-2023, 01:40 PM
I'm implementing solr into a Rails app, specifically pertaining to ingredient searches. If I do a partial word match on a simple noun like "beef" or "chicken", I can type in any number of letters from 1 to the full string, and it finds ingredients containing those words. The problem comes into play when I have a word with a suffix, such as "eggs" (*-s*), "baked" (*-ed*), or "baking" (*-ing*).
Let's take "baking" as an example. I can search for "b", "ba", or "bak" to have any results with the word "baking" return. If I search for "baki", "bakin", or "baking", this leads to no results found.
I'm wondering if I'm doing something wrong with either my Rails code for the search, or if I need to edit something in the `schema.xml` file. My schema is the default provided by [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
Searching:
Ingredient.search_by_partial_name('baki') # => []
Ingredient.search_by_partial_name('bak') # => [<Ingredient "baking powder">,
<Ingredient "baking potato">,
...]
Thanks!
**Edit:** Here are the logs regarding the solr queries being performed for the above two examples.
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)
Let's take "baking" as an example. I can search for "b", "ba", or "bak" to have any results with the word "baking" return. If I search for "baki", "bakin", or "baking", this leads to no results found.
I'm wondering if I'm doing something wrong with either my Rails code for the search, or if I need to edit something in the `schema.xml` file. My schema is the default provided by [sunspot](
[To see links please register here]
). My model & search code looks like the following.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
Searching:
Ingredient.search_by_partial_name('baki') # => []
Ingredient.search_by_partial_name('bak') # => [<Ingredient "baking powder">,
<Ingredient "baking potato">,
...]
Thanks!
**Edit:** Here are the logs regarding the solr queries being performed for the above two examples.
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)