So I'm using sunspot in my rails project, and I'm indexing song names. What I want to do is when I do a search for a specific term (for example "lose yourself"), in the top of my results list to appear the records that contain the exact match for the query (with spaces and everything).
To do that I decided to define a new fieldType
in the schema.xml
using KeywordTokenizerFactory
like this (I was planning to use it alongside the normal text field with the StandardTokenizer
, use both of these and boost the results that came out using the KeywordTokenized
field):
<fieldType name="text_exact" class="solr.TextField" omitNorms="false">
<analyzer>
<tokenizer class="solr.KeywordTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
<filter class="solr.TrimFilterFactory"/>
</analyzer>
</fieldType>
and adding a dynamic field like this (not sure it should be multivalued for this purpose, maybe someone can enlighten me on this too):
<dynamicField name="*_exact" stored="false" type="text_exact" multiValued="true" indexed="true"/>
Now, in my song.rb
file i have this in the searchable
configuration:
text :song_name do
self.name
end
text :song_name_exact, as: :song_name_exact do
self.name
end
The problem is that when I try to search by using the song_name_exact
field I get no results if my query contains spaces (so if i have a song called foo
and I search for foo
it will find it, but if i have one called foo bar
and search for foo bar
, the search query returns no results.
So first of all, I would ask if my approach to this is correct, and why isn't the search over the field with keyword tokenizer working properly?