Wildcard queries and how Solr handles them

One of our readers reported a very interesting problem, which can be summarized to the following question – “Why doesn’t ReversedWildcardFilterFactory doesn’t work with Polish letters ?”. This entry will attempt to answer this question.

Type and field definition

Our reader has the following type of data defined in the schema.xml file:

<fieldType name="c_string" class="solr.TextField">
 <analyzer type="index">
  <tokenizer class="solr.KeywordTokenizerFactory"/>
  <filter class="solr.ASCIIFoldingFilterFactory"/>
  <filter class="solr.LowerCaseFilterFactory" />
  <filter class="solr.ReversedWildcardFilterFactory" />
 </analyzer>
 <analyzer type="query">
  <tokenizer class="solr.KeywordTokenizerFactory"/>
  <filter class="solr.ASCIIFoldingFilterFactory"/>
  <filter class="solr.LowerCaseFilterFactory" />
  <filter class="solr.ReversedWildcardFilterFactory" />
 </analyzer>
</fieldType>

One of the fields in the schema.xml file looks like this:

<field name="word" type="c_string" indexed="true" stored="true" />

Data

For the purpose of this post I used the following data:

<add>
 <doc>
  <field name="id">1</field>
  <field name="word">Zażółć gęślą jaźń</field>
 </doc>
</add>

Problem definition

The problem has been defined as follows: “I have the Polish characters in the field named word. When I make a wildcard request with the Polish character, such as “word:*ą*” Solr doesn’t find any documents“.

What should we remember when using ReversedWildcardFilterFactory

When using ReversedWildcardFilterFactory be sure to define this filter only for the index analyzer, and not to define it for query analyzer. Thus, a revised c_string definition should look like this:

<fieldType name="c_string" class="solr.TextField">
 <analyzer type="index">
  <tokenizer class="solr.KeywordTokenizerFactory"/>
  <filter class="solr.ASCIIFoldingFilterFactory"/>
  <filter class="solr.LowerCaseFilterFactory" />
  <filter class="solr.ReversedWildcardFilterFactory" />
 </analyzer>
 <analyzer type="query">
  <tokenizer class="solr.KeywordTokenizerFactory"/>
  <filter class="solr.ASCIIFoldingFilterFactory"/>
  <filter class="solr.LowerCaseFilterFactory" />
 </analyzer>
</fieldType>

Is it working now ?

When we send the follwing query to Solr:

http://localhost:8983/solr/select?q=word:*ś*

It still does not return any results. Why ?

Solr and wildcard handling

The answer to the question posted above is simple enough – Solr does not analyze queries in which there are wildcards. Yes, this means that the filter ASCIIFoldingFilterFactory, during indexing, turns Polish characters onto their counterparts from the ASCII table characters, but when making queries this is not happening, despite the fact that the filters are defined correctly. And that is why we do not get any search results. However, in this was the case, we should get the query results after sending the following query:

http://localhost:8983/solr/select?q=word:*s*

And this is exactly the case, the above returns the search results we expected.

Possible solutions

If someone asked me what would I suggested to solve the problem, I would reply that at this time I would remove the Polish characters from the queries which have wildcards. Since version 1.5, Solr, ReversedWildcardFilterFactory will correctly handle characters outside the basic ASCII table, so there will be no need to use ASCIIFoldingFilterFactory, and hence, the problem will not exist.

This post is also available in: Polish

This entry was posted on Monday, December 20th, 2010 at 08:13 and is filed under About Solr. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.