Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add documents as block #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions luwak/src/main/java/uk/co/flax/luwak/DocumentBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.util.*;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.*;
import org.apache.lucene.index.memory.MemoryIndex;
import org.apache.lucene.search.IndexSearcher;
Expand Down Expand Up @@ -50,7 +51,8 @@ public abstract class DocumentBatch implements Closeable, Iterable<InputDocument
/** The {@link Similarity} to be used for scoring (if scoring is required) */
protected final Similarity similarity;

/** A list of {@link InputDocument} objects to match */
/** A list of {@link InputDocument} objects to match. These documents will be added as a block in the order
* provided. */
protected final List<InputDocument> documents = new ArrayList<>();

/**
Expand Down Expand Up @@ -182,9 +184,26 @@ public LeafReader getIndexReader() throws IOException {

private LeafReader build(IndexWriter writer) throws IOException {

for (InputDocument doc : documents) {
writer.addDocument(doc.getDocument());
}
final Iterator<InputDocument> it = documents.iterator();

Iterable<? extends Iterable<? extends IndexableField>> wrapped = new Iterable<Iterable<? extends IndexableField>>() {
@Override
public Iterator<Iterable<? extends IndexableField>> iterator() {
return new Iterator<Iterable<? extends IndexableField>>() {
@Override
public boolean hasNext() {
return it.hasNext();
}

@Override
public Iterable<? extends IndexableField> next() {
return it.next().getDocument();
}
};
}
};

writer.addDocuments(wrapped);

writer.commit();
LeafReader reader = SlowCompositeReaderWrapper.wrap(DirectoryReader.open(directory));
Expand Down