1

Hi I'm a new rails developer using sunspot and solr.

Here's my searchable block:

searchable do
  text :name, :boost => 10
  text :description
  text :tags do
    tags.map(&:name)
  end
  text :ingredients do
    ingredients.map(&:ingredient)
  end
  string :ingredients, :multiple => true do 
    ingredients.map(&:ingredient)
  end
  string :user
end

and my controller:

@search = Recipe.search do |query|
  query.fulltext params[:search]
  query.paginate(:page => params[:page] || 1, :per_page => 20)
end
@recipes = @search.results

I'm building a recipe search engine. Basically, right now I have it so that you can search for the name of the recipe, ingredients, and tags associated with a recipe and have matching recipes show up.

But as you can see I'm also trying to create a faceted search on ingredients, which I have already made strings.

My goal is to be able to search for a recipe, like you can now, but have it so that if you add a plus symbol then an ingredient following, that it facets the search, and likewise for the minus symbol. So for example here's a query:

Chicken Piccata +lemon  => searches for chicken piccata with lemon
Chicken Piccata -lemon  => searches for chicken piccata without lemon

I've been trying on something like this:

with(:ingredients, params[:ingredients]) if params[:ingredients].present?

but I have no idea how to deal with the +/- side of it. How could I accomplish this?

Thanks for all help!

4

1 回答 1

1

不确定 Sunspot 查询的创建,但是 ....
Solr 支持布尔运算符,因此支持 NOT (-),它将返回没有单词的结果。

所以Chicken Piccata lemon-> 使用默认运算符 AND,应该返回所有结果

此外,Solr 支持 NOT 运算符,因此Chicken Piccata -lemon,将被解释为所有内容中没有柠檬的结果。

您也可以查询-ingredients:lemon以便排除结果。

于 2013-06-28T10:32:38.440 回答