开发者

Anyways of making a Lucene field stored as well as streamed through a reader

Is there a way of creating a field in Lucene which can accept a reader or InputStream and also store the content of it? I want to store the data so that it can be used at the time of Highlighting and I want to stream the data because th开发者_运维问答e content of the documents might be really large.

I do not see a constructor for a field that allows me to use a reader as well as lets me store the value. Thanks


If your documents are not too large, just read them into memory first, and then specify the resulting value as a String when adding a new Field. If the documents are large, split them into manageable chunks and perform the above operation on each chunk. Make sure to use the same field name for each chunk, so that Lucene will search over all values.

Example:

IndexWriter writer = ...
String id = ...
String[] lines = ...
Document doc = new Document();
doc.add(new Field("id", id, Store.YES, Index.NOT_ANALYZED, TermVector.NO );
for (String line: lines) {
    doc.add(new Field("text", line, Store.YES, Index.ANALYZED, TermVector.WITH_POSITIONS);
}
writer.addDocument(doc);

Lucene will automatically merge the values specified at each add with the same field name, so that it can search over the pooled set.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜