Over a million developers have joined DZone.

Wildcard queries and how Solr handles them

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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.



Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Rafał Kuć, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}