You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucenenet.apache.org by Muthusree <mu...@gmail.com> on 2007/06/29 11:15:12 UTC
New to Lucene.Net
Hi frnds,
I am trying to create a search engine using Lucene.Net in vb.net.
Indexing works well.. but i think there is some mistake with either the
parser or the searcher. I get the following error while running in the line
A i have mentioned below.
An unhandled exception of type 'System.NullReferenceException' occurred in
mysrch.exe
Additional information: Object reference not set to an instance of an
object.
Here is my code. Pls tell me wat's to be done ..
Imports Lucene.Net.Analysis
Imports Lucene.Net.Documents
Imports Lucene.Net.Documents.Field
Imports Lucene.Net.Analysis.Standard
Imports Lucene.Net.Index
Imports Lucene.Net.search
Imports Lucene.Net.QueryParsers
Imports Lucene.Net.Util.StringHelper
Imports System.IO
Imports System.IO.FileStream
Public Class indexer
Dim filedir As New DirectoryInfo("E:\lucene\test\Resumes\")
Dim indexdir As New String("C:\index\")
Dim anlr As New Lucene.Net.Analysis.Standard.StandardAnalyzer()
Dim writer As IndexWriter
Dim pattern As String
Dim docrootdirectory As String
Dim filepath, relativepath As String
Dim srchr As IndexSearcher
Dim q As Lucene.Net.Search.Query
Private Sub indexer_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim writer As New IndexWriter(indexdir, anlr, True)
writer.SetUseCompoundFile(True)
' AddDirectory(filedir, "*.doc")
newindexer()
End Sub
Public Sub newindexer()
Dim writer As New IndexWriter(indexdir, anlr, False)
Dim rawtext As String
Dim txtfiles() As FileInfo = filedir.GetFiles("*.*")
Dim i As Integer
For i = 0 To txtfiles.Length - 1
Dim txtfilepath As String = txtfiles(i).FullName
Dim txtfilename As String = txtfiles(i).ToString
If txtfiles(i).Extension = ".txt" Then
' Form1.Lblindex.Text = "Files " + txtfilepath + " has been
indexed"
Form1.Lblindex.Text = "Files has been indexed"
Dim textReader As New StreamReader(txtfilepath,
System.Text.Encoding.Default)
rawtext = textReader.ReadToEnd
Dim document As New Document()
document.Add(New Field("text", rawtext,
Store.YES, Index.TOKENIZED))
document.Add(New Field("path", txtfilepath, Store.YES,
Index.UN_TOKENIZED))
document.Add(New Field("filename", txtfilename, Store.YES,
Index.UN_TOKENIZED))
writer.AddDocument(document)
End If
Next i
writer.Close()
End Sub ' Index
Public Sub srch()
Try
Dim srchr As Searcher
srchr = New IndexSearcher("C:\index\")
Catch ex As IOException
MessageBox.Show(("The index doesn't exist or is damaged. Please
rebuild the index." + ControlChars.Cr + ControlChars.Lf + ControlChars.Cr +
ControlChars.Lf + "Details:" + ControlChars.Cr + ControlChars.Lf +
ex.Message))
Return
End Try
'Dim query As TermQuery
'query = QueryParser.Parse(q, "text", New StandardAnalyzer())
'' Dim querys As Query = QueryParser.Parse("index", "text", New
StandardAnalyzer())
Try
Dim parser As New QueryParser("text", anlr)
q = parser.Parse("the")
Catch ex As Lucene.Net.QueryParsers.ParseException
MessageBox.Show(ex.ToString)
End Try
' Dim query As Query = QueryParser.Parse(Text, "text", New
StandardAnalyzer())
If q Is Nothing Then
MsgBox("Query is null")
Else
Dim h As Hits = srchr.Search(q)
--------------------------------------------------------------------A
Dim i As Integer
For i = 0 To (h.Length()) - 1
Dim doc As Document = h.Doc(i)
Dim filename As String = doc.Get("filename")
Dim p As String = doc.Get("path")
Dim folder As String = Path.GetDirectoryName(p)
Dim di As New DirectoryInfo(folder)
Form1.rtb.AppendText(vbCrLf & filename & ",")
Next i
srchr.Close()
Form1.rtb.AppendText(vbCrLf & "NUMBER OF MATCHING CONTACTS: " +
h.Length())
For i = 0 To (h.Length()) - 1
Form1.rtb.AppendText("NAME: " + h.Doc(i).Get("filename"))
Next i
End If
End Sub 'search