0
votes

I have a String address = "456 SOME STREET";

which I have to search in Lucene, I have created the index for this

StringField address = new StringField(Constants.ORGANIZATION_ADDRESS, address,Field.Store.YES);

And I am using Phrase Query to search this String using below Code

String[] tokens = address.split("\\s+");
PhraseQuery addressQuery = new PhraseQuery(Constants.ORGANIZATION_ADDRESS, tokens);
finalQuery.add(addressQuery, BooleanClause.Occur.MUST);

But its not giving me any result,I have tried TermQuery as well but that is also not working. Would really appreciate any help because I have tried many options now and I am unable to figure out whats wrong

I have also tried below For Indexing :

doc.add(new StringField(Constants.ORGANIZATION_ADDRESS, address,Field.Store.YES));

Search using Term Query :

fullAddressExact= fullAddressExact.toLowerCase();
TermQuery tq = new TermQuery(new Term(Constants.ORGANIZATION_ADDRESS,fullAddressExact));
finalQuery.add(tq, BooleanClause.Occur.MUST);

Even this doesnt give any result. My intention to get the exact match

1

1 Answers

0
votes

You should probably use TextField, not StringField when indexing the documents.

StringField stores the string as is, without breaking it into tokens, so in your example the index will contain "456 SOME STREET". Only a TermQuery with this term will retrieve it (or a PrefixQuery).

TextField is the standard field when indexing text, it splits the text into tokens (using a Tokenizer) and indexes the words separately, in your example, 456, SOME, STREET can all be used to find the document. Read more about it here (a bit old, but relevant).