0
votes

I am trying to find a match using one word as a query but it can't seem to find it. I am using standard analyzer but can't find the exact result.

My indexed document is:

Document d = new Document();
d.add(new StringField("content","The quick brown fox jumps over the lazy dog"));

If I use a WildCard Query:

Term term = new Term("content","The*");
Query q = new WildcardQuery(term);

It will return the content: "The quick brown fox jumps over the lazy dog"

If I use a TermQuery :

Term term = new Term("content","The quick brown fox jumps over the lazy dog");
Query q = new TermQuery(term);

It will return the content: "The quick brown fox jumps over the lazy dog"

Now, I want to use "fox" as my new term

Term term= new Term("content","fox");

But I don't know which is the right query to use, or how to do it. I already tried QueryParser, TermQuery, and MultiPhrase but still no luck.

Any help will do or guide. Or just a simple query which I can use as a guide. I have read a lot of tutorials but can't find the right one. I am using lucene 5.0.

2

2 Answers

1
votes

Your problem is that you didn't read Javadoc for StringField which says:

<..> the entire String value is indexed as a single token <..>

Just use TextField and you will be fine.

0
votes

Based on your requirement, it seems that it would be better to tokenize the whole string:

Document doc = new Document(); 

doc.Add(new Field("Contents", "The quick brown fox jumps over the lazy dog", Field.Store.NO, Field.Index.TOKENIZED));

This should enable you to search for individual terms as well.