0
votes

I am storing data in lucene.Net I add a document with multiple fields :

var doc = new Document();

doc.Add(new Field("CreationDate", dt, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("FileName", path, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("Directory", dtpath, Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("Text", text.ToString(), Field.Store.YES, Field.Index.ANALYZED));
...
writer.AddDocument(doc);

I want to move through all items and return fields "CreationDate" and "Directory" for each document. But a Term can only except 1 field :

var termEnum = reader.Terms(new Term("CreationDate"));

How do i make it return the 2 fields ???

Thanks Martin

2

2 Answers

1
votes

When you iterate over search results, read the document and load the values from it:

int docId = hits[i].doc;  
Document doc = searcher.Doc(docId); 
String creationDate = doc.Get("CreationDate");
String directory = doc.get("Directory");
// ...and so on
0
votes

You can ontain a enumeration of all documents containing a given term with:

var termDocEnum = reader.TermDocs(new Term("CreationDate"));

You can use that enumeration to fetch documents using the docId:

Document doc = searcher.doc(termDocEnum.doc);

Which should make it easy enough to use the Document API to get the information you are looking for.

Note, as hinted at earlier, this will only get documents for which the term given has a value specified! If that is a problem, you can either call TermDocs once for each pertinent argument and merge the sets as needed (done easily enough with a hash table indexed on docId, or some such), or call TermDocs with no argument, and use seek to find the appropriate Terms (merging will again need to be done manually if necessary).

A TermEnum, as passed from the 'terms' method does not give you a docId (you would have to use the termDocs method to get them with each term in the enum, I believe.)