Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solr exact word search

Tags:

I want to configure my Solr search engine so I get an exact match for the search term I enter.

eg. 'taxes' should return documents with 'taxes' and not 'tax', 'taxation' etc.

Any help or tips would be appreciated.

like image 489
Ruth Avatar asked Apr 13 '10 15:04

Ruth


People also ask

How do I search exact match in SOLR?

Just do a phrase search by surrounding the search text with double quotes (field_name:"search text"). You can set the field-type of the field as "string"(class="solr. StrField") instead of "text"(class="solr. TextField") if you are going to do exact match search.

What is phrase search in Solr?

Phrase match: A simple way by which we can achieve exact matching in Solr is by using the default string type. It is exact phrase matching. the string is a useful type for facet where we search the index by using the text pulled from the index itself.

How do I query in SOLR collection?

You can search for "solr" by loading the Admin UI Query tab, enter "solr" in the q param (replacing *:* , which matches all documents), and "Execute Query". See the Searching section below for more information. To index your own data, re-run the directory indexing command pointed to your own directory of documents.


2 Answers

I presume your field is a TextField, by default solr does a fuzzy search on this field. What you want is to set up your field as a string field and add no tokenizer then you'll get an exact match.

You can even combine the exact search with a fuzzy search and use DisMax to boost the relative weights.

Example (schema.xml) :

<field name="name"             type="string" indexed="true" stored="false" required="true" /> <field name="nameString"       type="string" indexed="true" stored="false" required="true" /> <copyField source="name" dest="nameString"/> 

Example (solrconfig.xml) :

<requestHandler name="accounts" class="solr.SearchHandler">     <lst name="defaults">       <str name="defType">dismax</str>       <str name="qf">         nameString^10.0 name^5.0 description^1.0       </str>       <str name="tie">0.1</str>     </lst>   </requestHandler> 
like image 110
developresource Avatar answered Sep 18 '22 17:09

developresource


To turn off stemming in your schema.xml, you can define text field like this:

<types>     <!-- other fields definition -->     <fieldType name="text_no_stem" class="solr.TextField" omitNorms="false">       <analyzer>           <tokenizer class="solr.StandardTokenizerFactory"/>           <filter class="solr.StandardFilterFactory"/>           <filter class="solr.LowerCaseFilterFactory"/>       </analyzer>    </fieldType>     <!-- other fields definition -->  </types>  <fields>     <!-- other fields definition -->     <dynamicField name="*_nostem" type="text_no_stem" indexed="true" stored="true"/>     <!-- other fields definition -->  </fields> 

I'm using sunspot to integrate solr with Ruby on Rails. With this in the schema.xml I define my searchable block like this:

searchable do     text(:wants, as: :wants_nostem) end 
like image 32
wawka Avatar answered Sep 21 '22 17:09

wawka