Shabarish Poojary
Shabarish Poojary

Reputation: 13

How to match exact Search in Solr with space

How to search a part of the word when space is present between two words. My query this like below

/select?q=*:*&fq=pageType:program&fl=programLocation&rows=100&fq=programLocation:"Mohali"

The result I am getting is as below

"response":{"numFound":3,"start":0,"docs":[
      {
        "programLocation":["Mohali"]},
      {
        "programLocation":["Mohali"]},
      {
        "programLocation":["Mohali and Hyderabad"]}]

I want to retrieve only "Mohali", but now I am getting both "Mohali" and "Mohali and Hyderabad". How to form a query to only fetch Mohali?

Upvotes: 0

Views: 826

Answers (2)

amdev
amdev

Reputation: 3249

For an exact match with lucene search in java

?q="Your search"~0

Your can play with the integer to define the max distance between words

?q="Your search"~2

Exemple : "This is an awesome exemple of exact match"

  • ?q="awesome exemple"~0 will match
  • ?q="awesome exact match"~0 won't match
  • ?q="awesome exact match"~2 will match

Upvotes: 0

Abhijit Bashetti
Abhijit Bashetti

Reputation: 8658

You need to use the string as fieldtype for your field programLocation.

Apply the String as fieldtype and reindex the data.

<field name="programLocation" type="string" indexed="true" stored="true" required="true" multiValued="false" />

String stores the word/sentence as an exact string without performing any tokenization on it. Its useful in cases for storing exact matches, e.g, for faceting, sorting.

Opposite of string type is text. Text does the tokenization of data, performs processing such as lower-casing etc. It is helpful in case when we want to match part of a sentence.

If you want achieve the lowercase search then use the below fieldtype for you field.

<fieldType name="forExactMatch" class="solr.TextField" sortMissingLast="true" omitNorms="true">
      <analyzer>
        <!-- KeywordTokenizer does no actual tokenizing, so the entire
             input string is preserved as a single token
          -->
        <tokenizer class="solr.KeywordTokenizerFactory"/>
        <!-- The LowerCase TokenFilter does what you expect, which can be
             when you want your sorting to be case insensitive
          -->
        <filter class="solr.LowerCaseFilterFactory" />
      </analyzer>
    </fieldType>

If you have spaces the you can also use the below filter after the lowercase filter factory.

<filter class="solr.TrimFilterFactory" />

Then your field defination will look like below

<field name="programLocation" type="forExactMatch" indexed="true" stored="true"/>

Upvotes: 1

Related Questions