2017-04-24 38 views
0

我正在使用Lucene.Net 3.0.3,我有工作代碼,但我無法弄清楚如何添加一個簡單的格式化程序。爲Lucene.net創建一個簡單的高亮格式化工具3.0.3

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 

我想要做的就是用黃色背景包裝這個詞。任何幫助表示讚賞,因爲我已經尋找答案,我認爲我必須失去一些東西。我需要強調的是在融資機會的目的和資金機會標題

+0

好像你的問題的標題應該改變。 'Lucene.net 3.0.3'對於嘗試查找有關向lucene添加格式化程序的信息沒有幫助。我不確定是否可以通過 – mdiehl13

+0

追溯更改標題/問題我同意 - 我更改了它 - 謝謝 – user1314159

+0

您應該添加解決方案作爲答案,以便人們知道這已得到解決。 – NightOwl888

回答

0

的JSON字段中的搜索條件得到了它做

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