1. Overview

Solr support wild card search using two special operators * and ?. We can specify more than one wild card operators within a single term.

Solr default query parser does not support wildcard characters in a phrase search.

2. How it works

Wild card query matches documents that have fields matching a wildcard expression. user’s query will be rewrite into lucene’s wild card query or prefix query.In case of solr core contails, millions of documents wild card searches can be run slow compared to normal term query as it needs to iterate over many terms.

3. Wild Card query characters

As we discussed, * and ? characters are used for wild card searches.

3.1 Single character

? character is used to match any single characters

3.1.1 Syntax

FIELD_NAME:?[TERM_FIRST_HALF]?[TERM_SECOND_HALF]?[TERM_THIRD_HALF]?

3.1.2 Example

In below example, we have use single ? character in search term and multiple ? characters as well.

http://localhost:8983/solr/WildCardExample/select?
  fl=id,manu,name,features&
  indent=on&
  q=manu:samsu?g OR name:char?ct?rs OR features:tec??olo??
  &wt=json
  &rows=2

3.1.3 Output

{
  "responseHeader":{
    "status":0,
    "QTime":6,
    "params":{
      "q":"manu:samsu?g OR name:char?ct?rs OR features:tec??olo??",
      "indent":"on",
      "fl":"id,manu,name,features",
      "rows":"2",
      "wt":"json"}},
  "response":{"numFound":3,"start":0,"docs":[
      {
        "id":"SP2514N",
        "name":"Samsung SpinPoint P120 SP2514N - hard drive - 250 GB - ATA-133",
        "manu":"Samsung Electronics Co. Ltd.",
        "features":["7200RPM, 8MB cache, IDE Ultra ATA-133",
          "NoiseGuard, SilentSeek technology, Fluid Dynamic Bearing (FDB) motor"]},
      {
        "id":"GB18030TEST",
        "name":"Test with some GB18030 encoded characters",
        "features":["No accents here",
          "这是一个功能",
          "This is a feature (translated)",
          "这份文件是很有光泽",
          "This document is very shiny (translated)"]}]
  }}

3.2 Multiple characters

*  character used to search for zero or more characters.For example wild card search *zone will matches jdzone,javazone and javadeveloperzone.

3.2.1 Syntax

FIELD_NAME:*[TERM_FIRST_HALF]*[TERM_SECOND_HALF]*[TERM_THIRD_HALF]*

3.2.2 Example

http://localhost:8983/solr/WildCardExample/select?
  indent=on&
  q=manu:samsun* OR name:*me OR features:te*gy&
  wt=json&
  fl=id,name,manu

3.2.3 Output

{
  "responseHeader":{
    "status":0,
    "QTime":8,
    "params":{
      "q":"manu:samsun* OR name:*me OR features:te*gy",
      "indent":"on",
      "fl":"id,name,manu",
      "wt":"json"}},
  "response":{"numFound":4,"start":0,"docs":[
      {
        "id":"SP2514N",
        "name":"Samsung SpinPoint P120 SP2514N - hard drive - 250 GB - ATA-133",
        "manu":"Samsung Electronics Co. Ltd."},
      {
        "id":"GB18030TEST",
        "name":"Test with some GB18030 encoded characters"},
      {
        "id":"UTF8TEST",
        "name":"Test with some UTF-8 encoded characters",
        "manu":"Apache Software Foundation"},
      {
        "id":"EN7800GTX/2DHTV/256M",
        "name":"ASUS Extreme N7800GTX/2DHTV (256 MB)",
        "manu":"ASUS Computer Inc."}]
  }}

3.3 Combine Single and Multiple characters

We can also combine single characters(?) and multiple characters (*) in a single search term.

3.3.1 Example

http://localhost:8983/solr/WildCardExample/select?
  indent=on&
  q=features:?oi?eg*&
  wt=json

3.3.2 Output

{
  "responseHeader":{
    "status":0,
    "QTime":2,
    "params":{
      "q":"features:?oi?eg*",
      "indent":"on",
      "fl":"id,manu,name,features",
      "wt":"json",
      "_":"1523180431354"}},
  "response":{"numFound":1,"start":0,"docs":[
      {
        "id":"SP2514N",
        "name":"Samsung SpinPoint P120 SP2514N - hard drive - 250 GB - ATA-133",
        "manu":"Samsung Electronics Co. Ltd.",
        "features":["7200RPM, 8MB cache, IDE Ultra ATA-133",
          "NoiseGuard, SilentSeek technology, Fluid Dynamic Bearing (FDB) motor"]}]
  }}

4. Conclusion

In this article, we have discussed some basic of solr wild card query. Special characters that are used in wild card query with example.

5. References

Refer Solr Reference GuideSolr Multiple Filter QueriesMain Query vs Filter Query for more details.

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *