Click here to Skip to main content
15,887,436 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
I am using below code for the searching and I want to do a exact search in my application for example in the data there is a title = dos the complete reference and user can search in that only from keyword dos so there is zero result in the grid so what can I do for the exact search in lucene.net



C#
Private Function search(textsearch As String) As DataTable

    Dim pks = New List(Of String)()
    Dim searchCriteria As String
    searchCriteria = cmbSearch.Text
    Dim directory As Directory = FSDirectory.GetDirectory(MainWindow.folderName)
    Dim analyzer = New StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)
    Dim reader = IndexReader.Open(directory, True)
    Dim searcher = New IndexSearcher(reader)
    If SearchCriteria = "Title" Then
        Dim queryparser = New QueryParser(Lucene.Net.Util.Version.LUCENE_29, "title", analyzer)
        Dim query = queryparser.Parse(textsearch)
        Dim collector = TopScoreDocCollector.create(1000, True)
        searcher.Search(query, collector)
        Dim hits As ScoreDoc() = collector.TopDocs().scoreDocs

        Dim dt As New DataTable()
        Dim SrNo As Integer = 1
        dt.Columns.Add("SrNo", GetType(Int32))
        dt.Columns.Add("Title", GetType(String))
        dt.Columns.Add("Author", GetType(String))
        dt.Columns.Add("AccessionNo", GetType(String))
        dt.Columns.Add("Location", GetType(String))
        dt.Columns.Add("Status", GetType(String))
        For i As Integer = 0 To hits.Length - 1
            Dim result As New SearchResultsTitle()
            ' get the document from index
            Dim docId As Integer = hits(i).doc
            Dim doc As Document = searcher.Doc(docId)
            Dim row As DataRow = dt.NewRow()
            row("SrNo") = SrNo
            row("Title") = doc.[Get]("title").ToString
            row("Author") = doc.[Get]("Author").ToString
            row("AccessionNo") = doc.[Get]("AccessionNo")
            row("Location") = doc.[Get]("location").ToString
            row("Status") = doc.[Get]("status").ToString
            dt.Rows.Add(row)
            SrNo = SrNo + 1
        Next
        dGridResults.ItemsSource = dt.DefaultView
    End If





C#
And for the indexing data I am using below code for indexing






C#
Public Sub createindex_Click(sender As Object, e As RoutedEventArgs) Handles createindex.Click
    Try
        Dim conString As String
        conString = "Data Source='" + servername.Text + "';Initial Catalog='" + selectdatabase.Text + "'; User ID='" + username.Text + "';Password='" + password.Text + "'"
        Dim conn As SqlConnection = New SqlConnection(conString)

        Dim BibliDS As New DataSet
        Dim DataDS As New DataSet
        Dim dt As DataTable

        Dim strTagSbFld As String
        strTagSbFld = GetTagSbFldSQL()

        conn.Open()
        Dim strSQL As String = (strTagSbFld)
        adap.SelectCommand = New SqlCommand(strSQL, conn)
        adap.Fill(DataDS)
        conn.Close()

        Dim dialog = New FolderBrowserDialog()
        Dim result = dialog.ShowDialog()
        folderName = dialog.SelectedPath


        'lucene index creating code

        Dim directory As Directory = FSDirectory.GetDirectory(folderName)

        Dim analyzer As Lucene.Net.Analysis.Analyzer = New StandardAnalyzer()
        Dim indexWriter As New IndexWriter(directory, analyzer)
        indexWriter.SetRAMBufferSizeMB(10.0)
        indexWriter.SetUseCompoundFile(False)
        indexWriter.SetMaxMergeDocs(10000)
        indexWriter.SetMergeFactor(100)

        dt = DataDS.Tables(0)
        If dt IsNot Nothing Then
            If dt.Rows.Count > 0 Then
                For Each dr As DataRow In dt.Rows
                    'Create the Document object
                    Dim doc As New Document()
                    For Each dc As DataColumn In dt.Columns
                        'Populate the document with the column name and value from our query
                        doc.Add(New Field(dc.ColumnName, dr(dc.ColumnName).ToString(), Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS))
                    Next
                    ' Write the Document to the catalog
                    indexWriter.AddDocument(doc)
                Next
            End If
        Else
            MsgBox("No Data")
        End If

        'indexWriter.Optimize()
        'Close the writer
        indexWriter.Flush()
        indexWriter.Close()
    Catch ex As Exception
        MsgBox("Incorrect Username or Password")

    End Try
End Sub
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900