Skip to content

Instantly share code, notes, and snippets.

@martijnvg
Created March 12, 2018 15:03
Show Gist options
  • Save martijnvg/2c33a12d98faf948aa0094066acd5be1 to your computer and use it in GitHub Desktop.
Save martijnvg/2c33a12d98faf948aa0094066acd5be1 to your computer and use it in GitHub Desktop.
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.MultiFields;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Bits;
import java.nio.file.Paths;
public class Application {
public static void main(String[] args) throws Exception {
try (Directory directory = FSDirectory.open(Paths.get(args[0]))) {
try (IndexReader indexReader = DirectoryReader.open(directory)) {
Bits bits = MultiFields.getLiveDocs(indexReader);
for (int docId = 0; docId < indexReader.maxDoc(); docId++) {
if (bits.get(docId)) {
Document document = indexReader.document(docId);
StringBuilder jsonDoc = new StringBuilder("{");
for (IndexableField field : document) {
jsonDoc.append('"').append(field.name()).append('"').append(' ');
jsonDoc.append('"').append(field.stringValue()).append('"').append('\n');
}
jsonDoc.append("}");
// send jsonDoc to ES index api over http:
// ...
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment