Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query lucene with "like" operator? [duplicate]

The wildcard * can only be used at the end of a word, like user*.

I want to query with a like %user%, how to do that?

like image 415
Freewind Avatar asked Jul 22 '10 10:07

Freewind


People also ask

How do you use the wildcard in Lucene?

Lucene supports single and multiple character wildcard searches within single terms (not within phrase queries). To perform a single character wildcard search use the "?" symbol. To perform a multiple character wildcard search use the "*" symbol. You can also use the wildcard searches in the middle of a term.

How do you find special characters in Lucene?

You can't search for special characters in Lucene Search. These are + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ / @. You can search for special characters, with the exception of the @ character, in a field-level search as long as you escape them using \ before the special character.

How do you query in Lucene?

A query written in Lucene can be broken down into three parts: Field The ID or name of a specific container of information in a database. If a field is referenced in a query string, a colon ( : ) must follow the field name. Terms Items you would like to search for in a database.

Why is Lucene so fast?

Why is Lucene faster? Lucene is very fast at searching for data because of its inverted index technique. Normally, datasources structure the data as an object or record, which in turn have fields and values.


2 Answers

The trouble with LIKE queries is that they are expensive in terms of time taken to execute. You can set up QueryParser to allow leading wildcards with the following:

QueryParser.setAllowLeadingWildcard(true)

And this will allow you to do searches like:

*user*

But this will take a long time to execute. Sometimes when people say they want a LIKE query, what they actually want is a fuzzy query. This would allow you to do the following search:

user~

Which would match the terms users and fuser. You can specify an edit distance between the term in your query and the terms you want matched using a float value between 0 and 1. For example user~0.8 would match more terms than user~0.5.

I suggest you also take a look at regex query, which supports regular expression syntax for Lucene searches. It may be closer to what you really need. Perhaps something like:

.*user.*

like image 134
Jon Avatar answered Sep 19 '22 15:09

Jon


Lucene provides the ReverseStringFilter that allows to do leading wildcard search like *user. It works by indexing all terms in reverse order.

But I think there is no way to do something similar to 'LIKE %user%'.

like image 26
Pascal Dimassimo Avatar answered Sep 20 '22 15:09

Pascal Dimassimo