Read ElasticSearch Index from Java

2019-09-10 09:40发布

问题:

I'm trying to read an ElasticSearch (1.4) index from a Java program, but I don't really have an idea where to start.

I don't have a running ES instance so I cannot use the "normal" API.

What I understand is the acual index files are Lucene so there must be a way to read them. Performance is not an issue, so I don't mind if the program runs a little longer (or even all night).

回答1:

Yes, you're right Elasticsearch index (on one shard) just a normal Lucene index, so it's really easy to open it in Java program using Lucene.

Most simple way to do that is below:

IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(INDEX_PATH)));
IndexSearcher searcher = new IndexSearcher(reader);

It's Lucene 5.0, but Lucene 4.xx will be similar to this:

IndexReader reader = IndexReader.open(FSDirectory.open(new File(INDEX_PATH)), true);
IndexSearcher searcher = new IndexSearcher(reader);