Left leading wild card does not work

Hi all,

I am trying to build lucene query, now I want to search like *hello*, but it does not work.

Instead, it works fine with hello*, so hibernate does not support left leading wild card, right?

Is there a api to setAllowLeadingWildcard(true)?

What I am gonna do if I want search phone *581* if there is a record of ‘0405812222’.

By the way, I don’t want to use ngrams, just simple query, just wild card.

so hibernate does not support left leading wild card, right?

It’s generally not a good idea, but it does work. You just have to use queryBuilder.keyword().wildcard().onField("myField).matching("*581*").createQuery(). Don’t forget the .wildcard() call. See the documentation.

If it doesn’t work for you, please provide more details: your mapping, your analyzers, the exact data that gets indexed, how you build the query, what terms you pass to the query.

Is there a api to setAllowLeadingWildcard(true)?

No, since it works by default, without any warning, on contrary to Lucene’s QueryParser.

By the way, I don’t want to use ngrams, just simple query, just wild card.

As you wish. There are limitations to wildcard queries, though. You probably already know that performance won’t be great with a leading wildcard, but more importantly wildcard queries are not analyzed, so any cleanup you want to be performed on the search terms (removing dots, removing spaces, …), you will have to perform yourself. That’s a Lucene limitation, and it affects both the Lucene and Elasticsearch integrations.

{  
   "query":{  
      "bool":{  
         "must":{  
            "bool":{  
               "must":[  
                  {  
                     "range":{  
                        "groupid_numeric":{  
                           "gte":25019,
                           "lte":25019
                        }
                     }
                  },
                  {  
                     "wildcard":{  
                        "contactPhoneNumbers.phoneNumber":{  
                           "value":"*61410270916*"
                        }
                     }
                  }
               ]
            }
         },
         "filter":{  
            "type":{  
               "value":"au.com.mydesktop.contacts.data.entity.Contact"
            }
         }
      }
   },
   "_source":[  
      "id"
   ],
   "sort":[  
      {  
         "_score":{  
            "order":"desc"
         }
      },
      {  
         "id":{  
            "order":"asc",
            "missing":"_first"
         }
      }
   ]
}

Hi yoann, this the ES query generated by hibernate search. As you can see I am just searching by the telephone number but using left leading wild card. I know that is not good because it will scan the whole document. Now ahead of wildcarding(phone search), there the a group number search, the range field,

Now my question is does this range field(group number search) would narrow down the scanning documents process?

Does it only scan all records with that group number or still scan the whole documents?

Or if the records group number is not 25019, does it fail fast and go to next record instead of still checking its phone number?

Or this group_number field would be beneficial for left-leading wild card searching?

Thanks

Honestly I don’t know, I generally try to keep away from wildcard queries, especially with leading wildcards. You may have better luck asking this in the Elasticsearch forums.

But if don’t have much data, and the query executes in reasonable time, I’d say don’t bother.

Thanks so much, right now I have 41 million records in that document.

Ok, that’s already quite a lot, a leading wildcard probably will be a problem. But you can always reconsider your choice to avoid ngrams.