Solr exact word search
Asked Answered
C

6

24

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.

Chintz answered 13/4, 2010 at 15:31 Comment(0)
L
26

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>
Lett answered 1/3, 2011 at 1:15 Comment(4)
Is this possible at query time, with a special operator? Like =taxesEsdraelon
@mlissner, The above XML is simply default parameters which are added to the query. U could have done so by adding to your query defType=dismax&qa=nameString^10.0 name^5.0 description^1.0 etc etcLordinwaiting
@ItayMoav, the goal is to have the users be able to place the exact match query.Esdraelon
Yes, I understood you. If u know u can add those params to query, you can put a UI on top that will generate the query...Lordinwaiting
L
7

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
Lidstone answered 14/3, 2014 at 20:12 Comment(0)
J
4

Turn off stemming.

Jasso answered 13/4, 2010 at 15:33 Comment(3)
Thanks Hank, maybe I should have phrased my question more appropriately, How do I disable stemming? :)Chintz
see lucene.472066.n3.nabble.com/…Backslide
sometimes re-index could be painful, especially if it took a long time on big data. Is there another way around instead of turning off stemming?Rorqual
C
4

Use the quotes for exact match result :

Example :

core Name : core1 Key : namestring

http://localhost:8983/solr/core1/select?q=namestring:"taxes"&wt=json&indent=true

Chromoplast answered 6/10, 2015 at 10:42 Comment(0)
L
2

Use solr string field whcih will do an exact value search e.g

<fieldType class="solr.StrField" name="string" omitNorms="true" sortMissingLast="true" />
Lifesize answered 19/1, 2015 at 10:54 Comment(0)
R
0

For using double quotes (") like Priya suggested, you also need to escape every single backslash with another backslash.

Revels answered 3/5 at 12:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.