Lucene search result -
i have below data index in lucene 4.8 , code.
finance expense admin expenses transaction expense salary expenses
indexing:
try { writer = createwriter(ramdirectory); for(string line : readfile(file_path)) { string[] split = line.split(","); document doc = new document(); doc.add(new textfield("id", split[0].trim(), field.store.yes)); doc.add(new textfield("name", split[1].trim(), field.store.yes)); writer.adddocument(doc); } writer.commit(); } { if(writer != null) { writer.close(); } }
search
indexsearcher searcher = new indexsearcher(directoryreader.open(ramdirectory)); queryparser nameqparser = new queryparser(version.lucene_48, "name", new standardanalyzer(version.lucene_48)); query query = nameqparser.parse("expense"); topdocs queryresults = searcher.search(query, 10);
above code return below 2 result. not return result has 's' in @ end.
current outcome:
finance expense transaction expense
expected outcome:
finance expense admin expenses transaction expense salary expenses
please suggest wrong in code.
you use wildcardquery, because word expense
contains in 4 document can pass search string *expense*
return documents having expense in name
field.
don't forget set queryparser
allow leading wildcards following:
queryparser.setallowleadingwildcard(true)
make following changes in code:
nameqparser.setallowleadingwildcard(true); query query = nameqparser.parse("*expense*");
Comments
Post a Comment