today i doing integration mongodb with Apache lucene,using the library
https://github.com/rstiller/mongo-lucene
presented an example throw exception NullPointerException
MongoClient mongoClient = new MongoClient("localhost" , 27017 );
DB db = mongoClient.getDB("test");
DBCollection dbCollection = db.getCollection("users");
// serializers + map-store
DBObjectSerializer<String> keySerializer = new SimpleFieldDBObjectSerializer<String>("key");
DBObjectSerializer<MapDirectoryEntry> valueSerializer = new MapDirectoryEntrySerializer("value");
ConcurrentMap<String, MapDirectoryEntry> store = new MongoConcurrentMap<String, MapDirectoryEntry>(dbCollection, keySerializer, valueSerializer);
// lucene directory
Directory dir = new MapDirectory(store);
// index files
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
IndexWriter writer = new IndexWriter(dir,config);
Document doc = new Document();
doc.add(new Field("title", "My file's content ...", Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
writer.close();
// search index
Query q = new QueryParser(Version.LUCENE_36, "first", analyzer).parse("Jo*");
IndexReader reader = IndexReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
System.out.println(hits.length);
depencies
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>com.github.mongoutils</groupId>
<artifactId>mongo-lucene</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>3.6.1</version>
</dependency>
Exception arise in string IndexWriter writer = new IndexWriter(dir,config);
Prompt in what a problem or an alternative, but not a standard search mongodb because it is not suitable.with examples
stacktrace
Exception in thread "main" java.lang.NullPointerException
at org.apache.lucene.index.SegmentInfos.getLastCommitGeneration(SegmentInfos.java:160)
at org.apache.lucene.index.SegmentInfos$FindSegmentsFile.run(SegmentInfos.java:605)
at org.apache.lucene.index.SegmentInfos$FindSegmentsFile.run(SegmentInfos.java:554)
at org.apache.lucene.index.SegmentInfos.read(SegmentInfos.java:359)
at org.apache.lucene.index.IndexReader.indexExists(IndexReader.java:1099)
at org.apache.lucene.index.IndexWriter.<init>(IndexWriter.java:1112)
at com.officeteam.Lucene.App.main(App.java:53)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…