Highlighter in lucene.net not working for wildchard and fuzzy search -
Highlighter in lucene.net not working for wildchard and fuzzy search -
highlighter using lucene.net ( 3.0.3) not working below code. if searching word "dealing" highlighter showing if searching word wildchar "deal*" there no highlighting
protected void btnindex_click(object sender, eventargs e) { string indexpath = @"d:\temp\luceneindex1"; lucene.net.store.directory directory = fsdirectory.open(indexpath); analyzer analyzer = new standardanalyzer(lucene.net.util.version.lucene_30); indexwriter author = new indexwriter(directory, analyzer, indexwriter.maxfieldlength.unlimited); indexreader reddish = indexreader.open(directory, true); int totdocs = red.maxdoc; red.close(); //add documents index string text = string.empty; text = "one thing may of interest, if dealing vast quantites of info may want create static field fields , reuse them rather creating new 1 each time rebuild index. demo lucene index created 1 time per application run, in production application may build index every 5 mins or that, in case recommend reusing field objects making static fields re-used."; int txts = totdocs; addtexttoindex(txts++, text, writer); writer.optimize(); writer.dispose(); //setup searcher indexsearcher searcher = new indexsearcher(directory); queryparser parser = new queryparser(lucene.net.util.version.lucene_30, "postbody", analyzer); text = txtsearchdata.text; label1.text = search(text, searcher, parser, analyzer); //clean searcher.close(); directory.close(); } private static void addtexttoindex(int txts, string text, indexwriter writer) { document doc = new document(); doc.add(new field("id", txts.tostring(), field.store.yes, field.index.analyzed, field.termvector.with_positions_offsets)); doc.add(new field("postbody", text, field.store.yes, field.index.analyzed, field.termvector.with_positions_offsets)); writer.adddocument(doc); } private string search(string text, indexsearcher searcher, queryparser parser, analyzer analyzer) { string indexpath = @"d:\temp\luceneindex1"; lucene.net.store.directory directory = fsdirectory.open(indexpath); string result = ""; string snip = ""; var booleanquery = new booleanquery(); var fuzzyquery = new fuzzyquery(new term("postbody", text), 0.7f, 3); booleanquery.add(new booleanclause(fuzzyquery, occur.should)); //supply conditions query query = parser.parse(text); fastvectorhighlighter highlighter = gethighlighter(); parser.allowleadingwildcard = true; query = parser.parse(text); booleanquery.maxclausecount = 10; query = query.rewrite(indexreader.open(directory, true)); query.rewrite(indexreader.open(directory, true)); fieldquery fieldquery = highlighter.getfieldquery(booleanquery); topscoredoccollector collector = topscoredoccollector.create(100, true); searcher.search(query, collector); scoredoc[] hits = collector.topdocs().scoredocs; int results = hits.length; console.writeline("found {0} results", results); (int = 0; < hits.length; i++) { int docid = hits[i].doc; float score = hits[i].score; lucene.net.documents.document doc = searcher.doc(docid); result = "score: " + score.tostring() + " field: " + doc.get("id") + " field2: " + doc.get("postbody"); string text1 = doc.get("postbody"); string[] hight = getfragmentswithhighlightedterms(analyzer, query, "postbody", text1, 5, 100, directory); } homecoming result + " :::: " + snip; } private fastvectorhighlighter gethighlighter() { fraglistbuilder fraglistbuilder = new simplefraglistbuilder(); fragmentsbuilder fragmentsbuilder = new scoreorderfragmentsbuilder( basefragmentsbuilder.colored_pre_tags, basefragmentsbuilder.colored_post_tags); homecoming new fastvectorhighlighter(true, true, fraglistbuilder, fragmentsbuilder); } private static string[] getfragmentswithhighlightedterms(analyzer analyzer, query query, string fieldname, string fieldcontents, int fragmentsize, int maxsize, lucene.net.store.directory directory) { tokenstream stream = tokensources.gettokenstream(fieldname, fieldcontents, analyzer); // spanscorer scorer = new spanscorer();//(query, fieldname, new cachingtokenfilter(stream)); query = query.rewrite(indexreader.open(directory, true)); queryscorer scorer = new queryscorer(query, fieldname); scorer.isexpandmultitermquery = true;// (true); simplespanfragmenter fragmenter = new simplespanfragmenter(scorer, fragmentsize); highlighter highlighter = new highlighter(scorer); highlighter.textfragmenter = fragmenter; highlighter.maxdoccharstoanalyze = maxsize; string[] fragments = highlighter.getbestfragments(stream, fieldcontents, 10); homecoming fragments; }
lucene
Comments
Post a Comment