简体   繁体   中英

Create a Simple highlight formatter for Lucene.net 3.0.3

I'm using Lucene.Net 3.0.3 and I have working code but I can't figure out how to add a simple formatter.

    Public Function Lucene_Index_Search_Complete(term As String) As String
    Dim sb As New StringBuilder()
    Dim sw As New StringWriter(sb)
    Dim writer As JsonWriter = New JsonTextWriter(sw)
    Try
        Dim d As Lucene.Net.Store.Directory = FSDirectory.Open(New DirectoryInfo(Server.MapPath("~") + "\\IndexedFiles_V33\\"))
        Dim indexReader As IndexReader = indexReader.Open(d, True)
        Dim indexSearch As Searcher = New IndexSearcher(indexReader)
        Dim a As Analyzer = New StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30)

        'IFormatter formatter = new SimpleHTMLFormatter("<b>", "</b>");


        Dim HighlightFormatter As SimpleHTMLFormatter = New SimpleHTMLFormatter("<span style='background:yellow;'>", "</span>")

        Dim FieldNames As String() = indexReader.GetFieldNames(indexReader.FieldOption.INDEXED_NO_TERMVECTOR).toArray
        Dim parser As MultiFieldQueryParser = New MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, FieldNames, a)

        Dim q As BooleanQuery = New BooleanQuery()

        term = term.Trim

        Dim phases As String() = Nothing
        If term.contains(",") Then
            phases = Split(term, ",")
            For Each phase As String In phases
                q.Add(parser.Parse(phase), Occur.SHOULD)
            Next
        Else
            phases = Split(term, " ")
            For Each phase As String In phases
                q.Add(parser.Parse(phase), Occur.MUST)
            Next
        End If



        Dim collector As TopScoreDocCollector = TopScoreDocCollector.create(1000, True)
        indexSearch.Search(q, collector)

        Dim hits As ScoreDoc() = collector.topDocs().scoreDocs


        Dim GrantID As String = ""
        Dim Title As String = ""
        Dim Posted_Date As String = ""
        Dim link As String = ""
        Dim jsonData As JObject = Nothing
        Dim purpose As String = ""
        Dim Short_Description As String = ""

        ' Loop through the matching hits, retrieving the document
        writer.WriteStartArray()
        For i As Integer = 0 To hits.Length - 1
            Try
                If i >= 50 Then
                    Exit For
                End If

                Dim docId As Integer = hits(i).doc
                Dim doc As Document = indexSearch.doc(docId)
                Dim Score As String = hits(i).Score.tostring
                GrantID = doc.get("GrantID")
                link = doc.get("link")
                jsonData = JObject.Parse(doc.get("JSON_Data"))
                Title = jsonData("Funding Opportunity Title").ToString
                Posted_Date = jsonData("Posted Date").ToString
                purpose = jsonData("Funding Opportunity Purpose").ToString

                writer.WriteStartObject()
                writer.WritePropertyName("id")
                writer.WriteValue((1 + i).ToString)
                writer.WritePropertyName("Grant_ID")
                writer.WriteValue(GrantID)
                writer.WritePropertyName("text")
                writer.WriteValue("<br/>" + Title + "<br/>" + purpose + "<br/><b>Score:</b> " + Score + "<br/>")
                writer.WritePropertyName("Score")
                writer.WriteValue(Score)
                writer.WriteEndObject()

            Catch ex As Exception
                WriteErrorLog(ex.Message, link)

            End Try
        Next

        writer.Close()


        Return sb.ToString




    Catch ex As Exception
        Return ex.Message

    End Try

End Function

All I want to do is to wrap the term with a yellow background. Any help is appreciated because I have searched for the answer and I think that I must be missing something. I need to highlight the search terms that are in the JSON fields of Funding Opportunity Purpose and Funding Opportunity Title

got it done

 Public Function GeneratePreviewSimpleText(q As Query, text As String, fieldName As String) As String
    Try
        Dim scorer As QueryScorer = New QueryScorer(q)
        Dim formatter As IFormatter = New SimpleHTMLFormatter("<span style='background:yellow;'>", "</span>")
        Dim fragmenter As SimpleFragmenter = New SimpleFragmenter(50)
        Dim highlighter As Highlighter = New Highlighter(formatter, scorer)
        highlighter.TextFragmenter = fragmenter
        Dim stream As TokenStream = New StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30).TokenStream(fieldName, New StringReader(text))


        Return highlighter.GetBestFragments(stream, text, 4, "<br/>")

    Catch ex As Exception

    End Try
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM