Rafal Kuc is a team leader and software developer. Right now he is a software architect and Solr and Lucene specialist. Mainly focused on Java, but open on every tool and programming language that will make the achievement of his goal easier and faster. Rafal is also one of the founders of solr.pl site where he tries to share his knowledge and help people with their problems. Rafał is a DZone MVB and is not an employee of DZone and has posted 75 posts at DZone. You can read more from them at their website. View Full User Profile

Wildcard queries and how Solr handles them

08.19.2011
| 7472 views |
  • submit to reddit

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.



References
Published at DZone with permission of Rafał Kuć, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: