Empty Query - More Like This (Lucene) -
i'm new @ java , lucene. i'm trying simple mlt test, i'm not getting results.
import java.io.file; import org.apache.lucene.analysis.standard.standardanalyzer; import org.apache.lucene.document.document; import org.apache.lucene.document.field; import org.apache.lucene.index.indexwriter; import org.apache.lucene.index.indexwriterconfig; import org.apache.lucene.search.indexsearcher; import org.apache.lucene.search.query; import org.apache.lucene.search.scoredoc; import org.apache.lucene.store.directory; import org.apache.lucene.util.version; import org.apache.lucene.index.directoryreader; import java.io.ioexception; import org.apache.lucene.document.stringfield; import org.apache.lucene.document.textfield; import org.apache.lucene.queries.mlt.morelikethis; import org.apache.lucene.queryparser.classic.parseexception; import org.apache.lucene.search.topdocs; import org.apache.lucene.store.simplefsdirectory; public class hellolucene { public static void main(string[] args) throws ioexception, parseexception { standardanalyzer analyzer = new standardanalyzer(version.lucene_40); string indexpath = "c:/users/name/desktop/lucene"; directory index = new simplefsdirectory(new file(indexpath)); indexwriterconfig config = new indexwriterconfig(version.lucene_40, analyzer); indexwriter w = new indexwriter(index, config); adddoc(w, "lucene in action", "193398817"); adddoc(w, "lucene dummies", "55320055z"); adddoc(w, "managing gigabytes", "55063554a"); adddoc(w, "the art of computer science", "9900333x"); w.close(); indexreader reader = directoryreader.open(index); system.out.println("reader has " + reader.numdocs() + " docs on it."); morelikethis mlt = new morelikethis(reader); mlt.setmintermfreq(1); mlt.setmindocfreq(1); query query = mlt.like(0); //doc id indexsearcher searcher = new indexsearcher(reader); topdocs topdocs = searcher.search(query,5); system.out.println("found " + topdocs.totalhits + " hits."); (scoredoc scoredoc : topdocs.scoredocs) { document doc = searcher.doc(scoredoc.doc); system.out.println(scoredoc.doc + ". " + doc.get("isbn") + "\t" + doc.get("title")); } reader.close(); } private static void adddoc(indexwriter w, string title, string isbn) throws ioexception { document doc = new document(); doc.add(new textfield("title", title, field.store.yes)); // use string field isbn because don't want tokenized doc.add(new stringfield("isbn", isbn, field.store.yes)); w.adddocument(doc); } }
and get:
reader has 4 docs on it.
found 0 hits.
i tried using luke check doc's id , there nothing wrong apparently.
did tests there, dont know whats wrong :(
did lot of searching on web, said settings mintermfreq , mindocfreq, tried 1 , 0, got nothing.
does have idea?
in advance!
[solved] edited:
its working now!
had add this:
mlt.setanalyzer(analyzer); mlt.setfieldnames(new string[] {"title"});
its working now! had add this:
mlt.setanalyzer(analyzer); mlt.setfieldnames(new string[] {"title"});
Comments
Post a Comment