tags:

views:

39

answers:

2

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.

A: 

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.

Thilo
Hi Thilo, I just want to search my index by field. For example, I want to get the value of a particular field[say Name] in my index. Usually you do that by Document.getField("Name") if you know the document but I am not able to get it working while searching the index.
Sunil
Document.getField will only work if the field is STORED (and not just INDEXED).
Thilo
A: 

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

Adrian Smith