java - My Lucene search doesn't return results -
i'm studying lucene , first test class. i'm trying implement in memory search , borrowed codes examples. search cannot return hits. can me please? thanks.
package my.test; import java.io.ioexception; import org.apache.lucene.analysis.standard.standardanalyzer; import org.apache.lucene.analysis.util.chararrayset; import org.apache.lucene.document.document; import org.apache.lucene.document.field; import org.apache.lucene.document.stringfield; import org.apache.lucene.index.indexwriter; import org.apache.lucene.index.indexwriterconfig; import org.apache.lucene.index.indexwriterconfig.openmode; import org.apache.lucene.index.term; import org.apache.lucene.search.booleanclause; import org.apache.lucene.search.booleanquery; import org.apache.lucene.search.indexsearcher; import org.apache.lucene.search.prefixquery; import org.apache.lucene.search.scoredoc; import org.apache.lucene.search.searchermanager; import org.apache.lucene.store.ramdirectory; import org.apache.lucene.util.version; public class testinmemorysearch { public static void main(string[] args) { // construct ramdirectory hold in-memory representation of index. ramdirectory idx = new ramdirectory(); try { // make writer create index indexwriterconfig iwc = new indexwriterconfig(version.lucene_42, new standardanalyzer(version.lucene_42, chararrayset.empty_set)); iwc.setopenmode(openmode.create_or_append); indexwriter writer = new indexwriter(idx, iwc); // add document objects containing quotes writer.adddocument(createdocument("theodore roosevelt man", "it behooves every man remember work of " + "critic, of altogether secondary importance, , that, " + "in end, progress accomplished man " + "things.")); writer.adddocument(createdocument("friedrich hayek", "the case individual freedom rests largely on " + "recognition of inevitable , universal ignorance " + "of of concerning great many of factors on " + "which achievements of our ends , welfare depend.")); writer.adddocument(createdocument("ayn rand", "there nothing take man's freedom away " + "him, save other men. free, man must free " + "of brothers.")); writer.adddocument(createdocument("mohandas gandhi", "freedom not worth having if not connote " + "freedom err.")); // optimize , close writer finish building index writer.close(); // build indexsearcher using in-memory index searchermanager mgr = new searchermanager(idx, null); try { document[] hits = search(mgr, "man", 100); (document doc : hits) { string title = doc.get("title"); string content = doc.get("content"); system.out.println("found match:[title]" + title + ", [content]" + content); } } catch (ioexception e) { e.printstacktrace(); } } catch (ioexception ioe) { // in example aren't doing i/o, // exception should never thrown. ioe.printstacktrace(); } } /** * make document object un-indexed title field , indexed * content field. */ private static document createdocument(string title, string content) { document doc = new document(); doc.add(new stringfield("title", title, field.store.yes)); doc.add(new stringfield("content", content, field.store.yes)); return doc; } private static document[] search(searchermanager searchmanager, string searchstring, int maxresults) throws ioexception { indexsearcher searcher = null; try { // build query. string[] tokens = searchstring.split("\\s+"); booleanquery query = new booleanquery(); (string token : tokens) { query.add(new prefixquery(new term("title", token)), booleanclause.occur.must); query.add(new prefixquery(new term("content", token)), booleanclause.occur.must); } searcher = searchmanager.acquire(); scoredoc[] scoredocs = searcher.search(query, maxresults).scoredocs; document[] documents = new document[scoredocs.length]; (int = 0; < scoredocs.length; i++) { documents[i] = searcher.doc(scoredocs[i].doc); } return documents; } { if (searcher != null) { searchmanager.release(searcher); } } } }
stringfield seems obvious choice, isn't want use here. want textfield. stringfield
represents field single token, keyword or identifier. textfield
analyzes , tokenizes field full-text searching.
fixing simple changing, in search
method:
doc.add(new stringfield("title", title, field.store.yes)); doc.add(new stringfield("content", content, field.store.yes));
to
doc.add(new textfield("title", title, field.store.yes)); doc.add(new textfield("content", content, field.store.yes));