Lucene.Net小实例 VB.NET

Imports Lucene.Net.Analysis.Standard
Imports Lucene.Net.Analysis
Imports Lucene.Net.Index
Imports Lucene.Net.Documents
Imports System.IO
Imports Lucene.Net.Search
Imports Lucene.Net.QueryParsers


Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Dim str As String = "我们是中国人; 我们 是 人;we are chiness; 172.16.34.172;youpeizun@126.com;#$*;85*34;58 69"
        'Response.Write(Me.TestStandardTokenizer(str))
    End Sub

    Private Function TestStandardTokenizer(ByVal text As String) As String
        Dim result As String = ""
        Dim tr As TextReader = New StringReader(text)
        Dim st As StandardTokenizer = New StandardTokenizer(tr)
        While (Not st.Next() Is Nothing)
            result = result & st.token.ToString() & "/ "
        End While
        Return result
    End Function
   
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim INDEX_STORE_PATH As String = Server.MapPath("Index")
        Dim INDEX_PATH As String = TextBox1.Text

        Dim writer As IndexWriter
        writer = New IndexWriter(INDEX_STORE_PATH, New StandardAnalyzer(), True)

        IndexDirectory(writer, New FileInfo(INDEX_PATH))
        writer.Optimize()
        writer.Close()

        TextBox2.Text = "提示:索引完成\n"


    End Sub

    Private Sub IndexDirectory(ByVal writer As IndexWriter, ByVal file As FileInfo)
        If Directory.Exists(file.FullName) Then
            Dim files As String() = Directory.GetFileSystemEntries(file.FullName)
            If Not files Is Nothing Then
                Dim i As Integer
                For i = 0 To files.Length - 1
                    IndexDirectory(writer, New FileInfo(files(i)))
                Next
            End If
        ElseIf file.Extension = ".txt" Then
            IndexFile(file, writer)
        End If
    End Sub

    Private Sub IndexFile(ByVal file As FileInfo, ByVal writer As IndexWriter)
        Dim doc As Document = New Document()
        doc.Add(New Field("filename", file.FullName, Field.Store.YES, Field.Index.UN_TOKENIZED))
        doc.Add(New Field("contents", New StreamReader(file.FullName, System.Text.Encoding.Default)))
        writer.AddDocument(doc)
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim INDEX_STORE_PATH As String = Server.MapPath("index")   'INDEX_STORE_PATH 为索引存储目录
        Dim KEYWORD As String = TextBox2.Text '搜索关键字

        Dim searcher As IndexSearcher
        searcher = New IndexSearcher(INDEX_STORE_PATH)

        Dim q As QueryParser = New QueryParser("contents", New StandardAnalyzer())
        Dim query As Query = q.Parse(KEYWORD)

        Dim hits As Hits = searcher.Search(query)
        TextBox3.Text = "Results number is " & hits.Length()

        If Not hits Is Nothing Then
            Dim i As Integer
            For i = 0 To hits.Length - 1
                Dim doc As Document = hits.Doc(i)
                TextBox3.Text = TextBox3.Text & " 第" & i & "个结果地址" & doc.Get("filename") & "\n"
            Next
        End If
        searcher.Close()

    End Sub
End Class

原文地址:https://www.cnblogs.com/twilight/p/1522207.html