java - Exception in thread "main" org.apache.lucene.index.IndexNotFoundException: no segments* file found in org.apache.lucene.store.SimpleFSDirectory -
/* * change template, choose tools | templates * , open template in editor. */
import java.io.file; import java.io.filereader; import java.io.ioexception; import java.io.reader; import java.util.date; import org.apache.lucene.analysis.standard.standardanalyzer; import org.apache.lucene.document.document; import org.apache.lucene.document.textfield; import org.apache.lucene.index.directoryreader; import org.apache.lucene.index.indexreader; import org.apache.lucene.index.indexwriter; import org.apache.lucene.store.simplefsdirectory; import org.apache.lucene.index.indexwriterconfig; import org.apache.lucene.queryparser.classic.parseexception; import org.apache.lucene.queryparser.classic.queryparser; import org.apache.lucene.search.indexsearcher; import org.apache.lucene.search.query; import org.apache.lucene.search.topdocs; import org.apache.lucene.util.version; import org.apache.lucene.store.directory; public class openbookcrackindexer { public static final version luceneversion = version.lucene_43; /** * @param args command line arguments */ public static int index(file indexdir, directory datadir) throws ioexception { indexwriterconfig luceneconfig = new indexwriterconfig( luceneversion, new standardanalyzer(luceneversion)); indexwriter writer = new indexwriter(datadir, luceneconfig); file[] files = indexdir.listfiles(); (file file : files) { if (file.getname().endswith(".pdf")) { document document = new document(); string path = file.getcanonicalpath(); document.getfield(path); reader reader = new filereader(file); document.add(new textfield(path.tostring(), reader)); writer.adddocument(document); } } return writer.numdocs(); } public static void search(string str) throws ioexception, parseexception { if (str.isempty()) { system.out.println("error :"+str.tostring()); } file datadir = new file("c:\\users\\xxxxx\\desktop\\print_imp\\lucene"); if (!datadir.exists()) { throw new ioexception(datadir + "does not exist or not directory"); } // string[] files = datadir.list(); // for(int = 0;i< files.length;i++){ // system.out.println(files[i].tostring()); // } directory tosearch = new simplefsdirectory(datadir); indexreader indexreader = directoryreader.open(tosearch); indexsearcher searcher = new indexsearcher(indexreader); standardanalyzer analyzer = new standardanalyzer(luceneversion); queryparser queryparser = new queryparser(luceneversion, "contents", analyzer); query query = queryparser.parse(str); topdocs td = searcher.search(query, 20); system.out.println("number of hits: " + td.totalhits); (int = 0; < td.totalhits; i++) { system.out.println("doc number " + td.scoredocs[i].doc + "score :" + td.scoredocs[i].score); } } public static void main(string[] args) throws exception { file indexdir = new file("c:\\users\\xxxxx\\desktop\\print_imp"); file datadir = new file("c:\\users\\xxxxx\\desktop\\print_imp\\lucene"); if (!indexdir.exists() || !datadir.exists()) { throw new ioexception(datadir + "does not exist or not directory"); } //simplefsdirectory sdindexdir = new simplefsdirectory(indexdir); simplefsdirectory sddatadir = new simplefsdirectory(datadir); //directory dirindex = sdindexdir; directory dirdata = sddatadir; //long start = new date().gettime(); //int numindexed = index(indexdir, dirdata); //long end = new date().gettime(); //system.out.println("indexed :" + numindexed + " time took index: " + (end - start) + " milliseconds"); search("algorithms"); } } here trying basic file directory indexing lucene 4.3.. book lucene in action. , due version changes not use code ... can 1 me out error getting ...
error
exception in thread "main" org.apache.lucene.index.indexnotfoundexception: no segments* file found in org.apache.lucene.store.simplefsdirectory@c:\<path>lockfactory=org.apache.lucene.store.nativefslockfactory@52fe85: files: [write.lock, _0.fdt, _0.fdx, _0.fnm, _0.nvd, _0.nvm, _0.si, _0_lucene41_0.doc, _0_lucene41_0.pos, _0_lucene41_0.tim, _0_lucene41_0.tip, _1.fdt, _1.fdx, _1.fnm, _1.nvd, _1.nvm, _1.si, _1_lucene41_0.doc, _1_lucene41_0.pos, _1_lucene41_0.tim, _1_lucene41_0.tip, _2.fdt, _2.fdx, _2.fnm, _2.nvd, _2.nvm, _2.si, _2_lucene41_0.doc, _2_lucene41_0.pos, _2_lucene41_0.tim, _2_lucene41_0.tip, _3.fdt, _3.fdx, _3.fnm, _3.nvd, _3.nvm, _3.si, _3_lucene41_0.doc, _3_lucene41_0.pos, _3_lucene41_0.tim, _3_lucene41_0.tip, _4.cfe, _4.cfs, _4.si, _5.cfe, _5.cfs, _5.si, _6.cfe, _6.cfs, _6.si, _7.cfe, _7.cfs, _7.si, _8.fdt, _8.fdx] @ org.apache.lucene.index.segmentinfos$findsegmentsfile.run(segmentinfos.java:741) @ org.apache.lucene.index.standarddirectoryreader.open(standarddirectoryreader.java:52) @ org.apache.lucene.index.directoryreader.open(directoryreader.java:66)
you need close writer in order commit changes!
Comments
Post a Comment