Muthu
Muthu

Reputation: 1550

Regexp Query in Lucene

I am working with lucene search, I am having list of names like

1) CITY OF CAIRO

2) CALICUT OFFICIAL BRANCH

3) CAIRO BRANCH OFFICE

If the user types "CO", It have to bring the names that start with "C" and second word should start with "O", It should return 1st name(CITY OF CAIRO) and 2nd name (CALICUT OFFICIAL BRANCH). The 3rd name(CAIRO BRANCH OFFICE) name should not be returned, As it is not having the second word started with "O". I am using the RegexpQuery query. It is listing all the above 3 names which is wrong.

RegexpQuery dynamicRegEx3 = new RegexpQuery(new Term("name", "C.* O.*"));

What is the way to achieve this ?

Printed query is :::: name:/C.* O.*/

Upvotes: 1

Views: 996

Answers (1)

Toto
Toto

Reputation: 91385

Your regex is greedy, use this instead:

RegexpQuery dynamicRegEx3 = new RegexpQuery(new Term("name", "C[^ ]* O.*"));

[^ ] stands for any character that is not a space.

Upvotes: 1

Related Questions