java - Lucene changing from RAMDirectory to FSDIrectory - Content-Field missing -



java - Lucene changing from RAMDirectory to FSDIrectory - Content-Field missing -

i'm lucene starter , and got stuck on problem during alter ramdirectory fsdirectory:

first code:

private static indexwriterconfig iwc = new indexwriterconfig(version.lucene_43, new standardanalyzer(version.lucene_43)); directory dir = fsdirectory.open(new file(indexloc)); //indexloc = "path/to/dir/" // ramdirectory dir = new ramdirectory(); // index made content indexwriter author = new indexwriter(dir, iwc); // store both position , offset info fieldtype type = new fieldtype(); type.setstored(true); type.setstoretermvectors(true); type.setstoretermvectoroffsets(true); type.setstoretermvectorpositions(true); type.setindexed(true); type.settokenized(true); idocumentparser p = documentparserfactory.getparser(f); arraylist<parserdocument> docs = p.getparseddocuments(); (int = 0; < docs.size(); i++) { document doc = new document(); field id = new stringfield("id", "doc_" + i, field.store.yes); doc.add(id); field text = new field("content", docs.get(i).getcontent(), type); doc.add(text); writer.adddocument(doc); } writer.close(); // searcher indexsearcher searcher = new indexsearcher(directoryreader.open(dir)); // search using spanquery spantermquery fleeceq = new spantermquery(new term("content", "zahl")); topdocs results = searcher.search(fleeceq, 10); (int = 0; < results.scoredocs.length; i++) { scoredoc scoredoc = results.scoredocs[i]; system.out.println("score doc: " + scoredoc); } indexreader reader = searcher.getindexreader(); atomicreader wrapper = slowcompositereaderwrapper.wrap(reader); map<term, termcontext> termcontexts = new hashmap<term, termcontext>(); spans spans = fleeceq.getspans(wrapper.getcontext(), new bits.matchallbits(reader.numdocs()), termcontexts); int window = 2;// words within 2 of match while (spans.next() == true) { map<integer, string> entries = new treemap<integer, string>(); system.out.println("doc: " + spans.doc() + " start: " + spans.start() + " end: " + spans.end()); int start = spans.start() - window; int end = spans.end() + window; terms content = reader.gettermvector(spans.doc(), "content"); termsenum termsenum = content.iterator(null); bytesref term; while ((term = termsenum.next()) != null) { // store bytesref here, string easier // illustration string s = new string(term.bytes, term.offset, term.length); docsandpositionsenum positionsenum = termsenum.docsandpositions(null, null); if (positionsenum.nextdoc() != docidsetiterator.no_more_docs) { int = 0; int position = -1; while (i < positionsenum.freq() && (position = positionsenum.nextposition()) != -1) { if (position >= start && position <= end) { entries.put(position, s); } i++; } } } system.out.println("entries:" + entries); }

it's code found on great website , wanted seek .... everything works great using ramdirectory. if alter fsdirectory it's giving me nullpointerexception :

exception in thread "main" java.lang.nullpointerexception @ com.org.test.textdb.mymethod(textdb.java:184) @ com.org.test.main.main(main.java:31)

the statement terms content = reader.gettermvector(spans.doc(), "content"); seems no result , returns null. exception. why? in ramdir works fine.

it seems indexwriter or reader (really don't know) didn't write or didn't read field "content" index. don't know why 'written' in ramdirectory , not written in fsdirectory?!

anybody thought that?

gave test quick test run, , can't reproduce issue.

i think issue here old documents in index. way written, every time run, more documents added index. old documents previous runs won't deleted, or overwritten, they'll stick around. so, if have run before on same directory, perhaps, before added line type.setstoretermvectors(true);, of results may these old documents term vectors, , reader.gettermvector(...) homecoming null, if document not store term vectors.

of course, indexed in ramdirectory dropped execution finishes, issue not occur in case.

simple solution seek deleting index directory , run again.

if want start fresh index when run this, can set through indexwriterconfig:

private static indexwriterconfig iwc = new indexwriterconfig(version.lucene_43, new standardanalyzer(version.lucene_43)); iwc.setopenmode(indexwriterconfig.openmode.create);

that's guess, of course, seems consistent behavior you've described.

java lucene ramdirectory

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -