-->

How to read data from solr/data/index

2020-07-23 08:57发布

问题:

How to read data from solr/data/index by some simple console Java application? I found some solution.

But maybe there is more simple way. Help please with that, I really don't know what to do.

回答1:

It's my own solution. I get index files from solr 4.4 and I also use lucene-core-4.4.0.jar library. Maybe it can help someone.

    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;

    import org.apache.lucene.document.Document;
    import org.apache.lucene.index.IndexReader;
    import org.apache.lucene.queryparser.classic.ParseException;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.apache.solr.client.solrj.SolrServerException;

    public class SomeClass {

        public static void main(String[] args) throws IOException {


            Directory dirIndex = FSDirectory.open(new File("solr/home/data/index"));
            IndexReader indexReader = IndexReader.open(dirIndex);
            Document doc = null;   

            for(int i = 0; i < indexReader.numDocs(); i++) {
                doc = indexReader.document(i);
            }

            System.out.println(doc.toString());

            indexReader.close();
            dirIndex.close();
        }
   }


回答2:

There is a project called Luke which is a GUI for users to inspect Lucene indices.

Here is a blog post with more detail.