Rails Sunspot/Solr returning result for single character searches

I spent 8 hours trying to get this to work. I googled and found lots of people asking the question but no one with a solution, or rather the given solutions wasn’t working for me.

1) rake sunspot:solr:stop

2) Edit the solr/conf/schema.xml file;

<fieldType name="text" class="solr.TextField" omitNorms="false">
<analyzer>
<tokenizer class="solr.StandardTokenizerFactory"/>
<filter class="solr.StandardFilterFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
<filter class="solr.PorterStemFilterFactory"/>
<filter class="solr.NGramFilterFactory" minGramSize="1" maxGramSize="15"/> <!-- This is The New Line -->
</analyzer>
</fieldType>

3) rake sunspot:solr:start

4) rake sunspot:reindex

I think my problem was changing the schema but not restarting the solr server. Restarting the rails server isn’t enough.

6 thoughts on “Rails Sunspot/Solr returning result for single character searches”

  1. Hi thanks for helpe me,
    i used your snippet but solar not work in endge ngram mode …

    i have this code:

    schema.xml

    model

    searchable do
    text :title
    end

    controller

    def search
    if params[:q].nil?
    @products = []
    else
    @search = Sunspot.search( Product ) do
    fulltext params[:q]
    end

    @products = @search.results
    end
    end

    why ?? thanks.

    p.s. i restart solar and re index.

  2. I used u code in schema.xml…
    but it was not working
    Pls suggest me for single letter search in sunspot

    1. I’m not great with Solr, all I can say is that this code for me successfully matches single letters as well as longer strings.
      filter class="solr.NGramFilterFactory" minGramSize="1" maxGramSize="15"
      is the line that does this I think, with minGramSize setting the shortest searchable element to a single character.
      Therefore I can search for
      “a” (450,000 results),
      “b” (265,000 results) or
      “a b” (250,000 results with both the letters a and b within them).
      “b a” (same 250,000 results as ‘a b’ search).
      “a b c d e f g h i j k l m n o p q” gets 19 results.
      “a b c d e f g h i j k l m n o p q r s t u” gets 7 results
      “a b c d e f g h i j k l m n o p q r s t u v” gets zero results.

        1. 5 years ago so only what I wrote in the blog post, in the file solr/conf/schema.xml

          Before the last analyser tag
          Hth

Leave a Reply

Your email address will not be published. Required fields are marked *