Search code examples
javasolrsolr-query-syntax

SolrQuery does not returns results for less than 3 characters


My code is as below:

SolrQuery query = new SolrQuery();
    query.setQuery(q.trim());
try {
        QueryResponse res = getSolrServer().query(query);
        return res.getResults();
    } catch (SolrServerException sse) {
        log.error(sse);
    }

The problem is that when i have a query more then 3 characters this returns me the response, e.g. query string "che" would respond with results, however the query string "ch" would return me no response. Is there a way i can override the 3 character min length of Solr Query.

is the below xml causing the problem, if yes can i programmatically override it using java

<analyzer type="index">
    <tokenizer class="solr.NGramTokenizerFactory" minGramSize="3" maxGramSize="50" />
    <filter class="solr.LowerCaseFilterFactory"/>
</analyzer>

Appreciate the help in Advance.

Thanks and Regards,

Vaibhav


Solution

  • NGramTokenizerFactory :-
    Default behavior. Note that this tokenizer operates over the whole field. It does not break the field at whitespace. As a result, the space character is included in the encoding.

    <analyzer>
      <tokenizer class="solr.NGramTokenizerFactory"/>
    </analyzer>
    

    In: "hey man"

    Out: "h", "e", "y", " ", "m", "a", "n", "he", "ey", "y ", " m", "ma", "an"

    So with your configurations :- minGramSize="3" maxGramSize="50" the items less then 3 would be filtered

    For two alphabets words, as you don't have the terms in the index these would never be searchable. You would need to change the minGramSize to 2 to make them searchable.