Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Yuval Lombard 14 posts 44 karma points
    Aug 07, 2014 @ 13:59
    Yuval Lombard
    0

    Umbraco Examine Search Results Highlighting Problem

    Hi all,

    I'm experiencing some troubles when trying to highlight search results of terms that are partial words

    for example if my website contains the word "basis", and i will search for basis, it will find it and highlight it, but if i search for the word "basi" it will find the right results but the code below will not work this time, thus i will get no results:

    Lucene.Net.Search.Query query = this.GetQueryParser(highlightField).Parse(luceneRawQuery);

            QueryScorer fragmentScorer = new QueryScorer(query.Rewrite(searcher.GetIndexReader()));
    
            Highlighter highlighter = new Highlighter(this.HighlightFormatter, fragmentScorer);
    
            Lucene.Net.Analysis.TokenStream tokenStream = this.HighlightAnalyzer.TokenStream(highlightField, new System.IO.StringReader(value));
    
            return highlighter.GetBestFragments(tokenStream, value, this.MaxNumHighlights, this.Separator); 
    
            return highlightField;
    

    did anyone experienced it before?

    Thanks a lot :)

    Yuval

  • MK 429 posts 905 karma points
    Aug 14, 2014 @ 10:11
    MK
    100

    Hi,

    You can try the following:

    public string GetHighlight(string value, string highlightField, Lucene.Net.Search.IndexSearcher searcher, string luceneRawQuery) {

    var finalQuery = new BooleanQuery(); string[] terms = luceneRawQuery.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); foreach (string term in terms)
    {
      var fuzzyQuery = new FuzzyQuery(new Lucene.Net.Index.Term(highlightField, term), MIN_SIMILARITY, PREFIX_LENGTH);
      finalQuery.Add(new BooleanClause(fuzzyQuery, BooleanClause.Occur.MUST));
    }
    QueryScorer fragmentScorer = new QueryScorer(finalQuery.Rewrite(searcher.GetIndexReader()));
    Lucene.Net.Highlight.Highlighter highlighter = new Highlighter(this.HighlightFormatter, fragmentScorer); Lucene.Net.Analysis.TokenStream tokenStream = this.HighlightAnalyzer.TokenStream(highlightField, new System.IO.StringReader(value)); string result = highlighter.GetBestFragments(tokenStream, value, this.MaxNumHighlights, this.Separator);
    return result;

    }

    Regards,

    Moshe

Please Sign in or register to post replies

Write your reply to:

Draft