Sunil
Sunil

Reputation: 51

Search by field in Lucene

Although being a total newbie, may be this question is pretty naive. I want to search my index based on the index. So I tried created a document with just one index, Name, and then want to search for that particular field.

I am doing this in process of trying to find out if I can update the fields of a document without actually deleting a document in lucene.

Thanks.

Upvotes: 5

Views: 5624

Answers (2)

Adrian Smith
Adrian Smith

Reputation: 17593

You can search for words within a particular field with the colon syntax i.e. name:john.

But because a lot of indexes just have one field you are going to want to search on, there is a default field, in case you just search for john. You can set which field that is when you instanciate your QueryParser

QueryParser parser = new QueryParser(Version.LUCENE_30, "name", anAnalyzer);
Query q = parser.parse("john");

If you want to create your queries programmatically rather than parsing a user-entered query string, then you also have to specify the field explicitly, for example:

Query q = new TermQuery(new Term("name", "john"));

Links: Using fields in Lucene queries (Lucene Query Syntax) | QueryParser Javadoc | TermQuery Javadoc

Upvotes: 5

Thilo
Thilo

Reputation: 262814

I am doing this in process of trying to find out if I can update the fields of a document without actually deleting a document in lucene.

I do not understand the first question, but you cannot update a document in Lucene. You have to delete and re-insert.

Upvotes: 0

Related Questions