I want to read every single index. I want to read and print to console the single term in my index. (I don't want to view the contents with Luke). Must I use the class IndexReader
?
Can someone help me?
I tried to do:
iReader = IndexReader.open(directory);
int num = iReader.numDocs();
for ( int i = 0; i < num; i++)
{
if ( ! iReader.isDeleted( i))
{
org.apache.lucene.document.Document d = iReader.document(i);
System.out.println( "d=" +d.getField("title").tokenStreamValue());
}
}
org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();
//aggiungo tutti i documenti
Field title = new Field(
"title",
testDoc.title,
Field.Store.YES,
Field.Index.ANALYZED,
Field.TermVector.WITH_POSITIONS_OFFSETS);
doc.add(title);
Field content = new Field(
"content",
testDoc.content,
Field.Store.YES,
Field.Index.ANALYZED,
Field.TermVector.WITH_POSITIONS_OFFSETS)开发者_Python百科;
doc.add(content);
iWriter.addDocument(doc);
but d = null;
Where did I go wrong?
I want to retrieve the term to the Field title that I indexed...
Thanks very much.
Again, I use Java, but the principle will be the same.
What you want to do is similar to enumerating term frequencies, but you just care about distinct fields.
This example and this example on how to count term frequencies in a Lucene index should get you going.
I'm using Lucene.Net, but I presume the logic is identical.
There must be exactly one of StringValue(), ReaderValue() and BinaryValue() set. Those not in use will either return null, or throw an exception. In your case, try reading StringValue() instead.
To examine the index, use IndexReader
. The class has a method document(int)
which you can use to find the individual documents that the index contains. The document then offers you all the fields that were created for that document.
With the field, you can either get it's value or the stream of tokens (i.e. the strings that end up in the index).
[EDIT] If you delete documents, the index will have holes. So you must add a check:
org.apache.lucene.document.Document d = iReader.document(i);
if( d == null ) continue; // <<-- You need this check
System.out.println( "d=" +d.getField("title").tokenStreamValue());
精彩评论