开发者

Alphabetical Search WebSolr Ruby on Rails

I`m trying to perform alphabetical search in solr, but i cannot do it until this moment

I want to search by letter, and my search needs to retrieve only the words that begin with the specified letter at the beggining, not if the words contains the letter anywhere else

example: if i click on letter "a" it must retrive "ant", "animal" etc

not "banana", "camelo"

my controller nowadays look like this:

@articles = Sunspot.search(Article) do keywords(params[:q]) paginate(:page => params[:page], :per_page => 20) order_by :relevance, :desc order_by :article_order,开发者_运维技巧 :asc end

Can anyone help? thanks in advance


This depends a little bit on how your model is configured to be indexed.

EdgeNGramFilterFactory

Add the EdgeNGramFilterFactory to your text field type in your schema.xml, or create a new type for a text field processed with an NGram filter.

The EdgeNGramFilterFactory turns each term into a set of NGrams. "Dog" is expanded into "d", "do", and "dog".

This approach will let you match any term present in your text fields using the start of a letter. It's useful for expanding the matches for your queries, however if you're using this for a kind of first-letter filtering, then it may not be what you want.

'Starts with' QueryFilter

If the value you're testing is a simple scalar string value (an article title, category name, author name, so on) then you can index that value as a string and search for matches with a query filter.

In Sunspot:

class Article
  searchable do
    # ...
    string :title
  end
end

@search = Sunspot.search(Article) do
  keywords(params[:q])
  paginate(:page => params[:page], :per_page => 20)
  where(:title).starting_with(params[:letter])
  order_by :relevance, :desc
  order_by :article_order, :asc
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜