5

I just have done setting up sunspot_rails and it seems working well except one thing. After I made 3 records like below

  1. name=John
  2. name=John2
  3. name=John3

when I search with the keyword "John", only 1st record shows up. it looks like complete matching. I'd like to have all of them to be appeared as search result.

Is this supposed to be happened as default? or did I setup something wrong??

MKK
  • 2,713
  • 5
  • 31
  • 51
  • possible duplicate of http://stackoverflow.com/questions/10601867/sunspot-solr-search-like-rails-active-record-like-search – x1a4 Jun 23 '12 at 05:55
  • Could you post the `searchable` block from your model and your `Model.search` block in your controller? – aNoble Jun 23 '12 at 06:47
  • x1a4, thanks! I tried that but after adding those line they asked to do, no result shows up. really weird :( – MKK Jun 23 '12 at 06:55

2 Answers2

8

If you want return substrings in fulltext search, you can take a look in

https://github.com/sunspot/sunspot/wiki/Matching-substrings-in-fulltext-search

Also you can add a file sunspot_solr.rb for pagination of results in myapp/config/initializers/ with:

Sunspot.config.pagination.default_per_page = 100

return 100 results for this case.

Added:

Your schema.xml file is founded in yourappfolder/solr/conf

Also you can add <filter class="solr.NGramFilterFactory"/> to match arbitrary substrings.

This is my particular config for schema.xml:

<fieldType name="text" class="solr.TextField" omitNorms="false">
  <analyzer>
    <tokenizer class="solr.StandardTokenizerFactory"/>
    <filter class="solr.StandardFilterFactory"/>
    <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
</fieldType>
<fieldtype class="solr.TextField" name="text_pre" positionIncrementGap="100">
  <analyzer type="index">
    <tokenizer class="solr.WhitespaceTokenizerFactory"/>
    <filter class="solr.LowerCaseFilterFactory"/>
    <filter class="solr.NGramFilterFactory" minGramSize="2" maxGramSize="10"/>
    <filter class="solr.ISOLatin1AccentFilterFactory"/>
    <filter class="solr.TrimFilterFactory" />
    <filter class="solr.EdgeNGramFilterFactory" minGramSize="2" maxGramSize="10"/>
  </analyzer>
  <analyzer type="query">
    <tokenizer class="solr.WhitespaceTokenizerFactory"/>
    <filter class="solr.LowerCaseFilterFactory"/>
    <filter class="solr.ISOLatin1AccentFilterFactory"/>
    <filter class="solr.TrimFilterFactory" />
  </analyzer>
</fieldtype>

For me it does works fine with full strings and substrings for all keywords. Please do not forget to restart the server and reindex your models for the changes to take effect.

Regards!

index
  • 3,697
  • 7
  • 36
  • 55
hyperrjas
  • 10,666
  • 25
  • 99
  • 198
  • ahhh thanks. After I added "Sunspot.config.pagination.default_per_page = 100 ", it seemed it went sucessfully, but another problem came out :( now I succeeded at substring search for the keyword which is english language but not for multi-byte like Japanese. It was perfectly working until adding the line. ummmmm.... – MKK Jun 23 '12 at 08:37
  • If you follow that instruction, the search result only can be the records that its starting chacaracters matches with the keyword. Not the records that contains keyword in the middle of the strings. So complicated. – MKK Jun 23 '12 at 08:53
  • Thank you. if this response its valid you must accept this response please. For special characters you take a look https://github.com/sunspot/sunspot/wiki/Searching-for-special-characters-with-sunspot%2C-solr-and-tomcat – hyperrjas Jun 23 '12 at 08:54
  • I have added the fix for arbitrary substrings. Thank you! – hyperrjas Jun 23 '12 at 09:00
  • if the record's field start with the search keyword, it shows as search result. But if the search keyword is contain in the middle or in the end of the string, no record hits. – MKK Jun 23 '12 at 09:10
  • I have added my schema.xml config in response. For me it does works fine with this config with string, substrings...etc. – hyperrjas Jun 23 '12 at 10:47
  • Thanks!!! your schema worked perfectly as I wanted!!! now what can I do to enale it search for multi-byte language??? – MKK Jun 23 '12 at 16:30
  • Please accept this response and you must make other question with multi-byte problem. Thank you! – hyperrjas Jun 23 '12 at 16:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12947/discussion-between-mkk-and-hyperrjas) – MKK Jun 23 '12 at 17:01
  • Wow, would be nice if the gem built something in for this... ugh. – Abram Jan 26 '13 at 22:04
0

Thanks!!!

block from girls controller(girls_controller.rb)

def index

  @search = Girl.search do  
    fulltext params[:search]  
  end  
  @girls = @search.results 

#    @girls = Girl.all
#
#    respond_to do |format|
#      format.html # index.html.erb
#      format.json { render json: @girls }
#    end
end

block from Girl model(girl.rb)

searchable do 
  text :name_en, :name_es, name_ja
end
MKK
  • 2,713
  • 5
  • 31
  • 51