2010-12-07 8 views
2

ich die folgende Methode (Beispiel aus this link genommen) habenWarum ist mein Lucene.Net „Hits“ -Sammlung Länge „0“ Rückkehr

Public Function ReadIndex(ByVal q As String, ByVal page As Integer?) As List(Of Domain.[Event]) Implements ILuceneService.ReadIndex 
     ''# This starts us at the first record if the user doesn't have a page specified 
     If page Is Nothing Then page = 0 
     Dim i As Integer = page 

     ''# Variables used by Lucene 
     Dim reader As IndexReader = IndexReader.Open(luceneDirectory) 
     Dim searcher As IndexSearcher = New IndexSearcher(reader) 
     Dim query As Query = New TermQuery(New Term("fullText", q.ToLower)) 
     Dim hits As Hits = searcher.Search(query) 

     Dim ResultIDs As List(Of Integer) = New List(Of Integer) 
     Dim HC = hits.Length ''# FOR DEBUGGING PURPOSES 
     While (i <= (page * 10) AndAlso i < hits.Length) 
      Dim document As Document = hits.Doc(i) 
      Dim score As Single = hits.Score(i) 
      ResultIDs.Add(document.[Get]("id")) 
      i += 1 
     End While 

     ''# Self explanitory 
     searcher.Close() 
     Return EventService.QueryEvents().Where(Function(e) (ResultIDs.Contains(e.ID))).ToList() 
    End Function 

Aber wenn ich einen Haltepunkt an

 Dim HC = hits.Length ''# FOR DEBUGGING PURPOSES 

und es im Debugger analysieren, sie sagt immer es eine Länge von 0 hat und sagt

Kinder nicht AUSWERT sein könnten

ted

Erster Screenshot
alt text


Zweiter Screenshot
alt text

Ich bin nicht sicher, was das bedeutet, aber die Wieder Ergebnis der Abfrage ist immer ein einzelner Datensatz, der zurückgegeben wird. Selbst wenn ich genau weiß, dass mehr als eins zurückgegeben werden sollte.


Wenn Sie den gesamten Service lesen möchten, finden Sie ihn unten.

Imports System.Web 
Imports System.Text 
Imports Lucene.Net.Index 
Imports Lucene.Net.Search 
Imports Lucene.Net.Documents 
Imports Lucene.Net.Analysis.Standard 
Imports Lucene.Net.Store 

Namespace Domain 
    Public Class LuceneService : Implements ILuceneService 
     Private luceneDirectory As Directory = FSDirectory.GetDirectory(HttpContext.Current.Server.MapPath("~/App_Data/"), False) 
     Private ExceptionService As Domain.IExceptionService 
     Private EventService As Domain.EventService 
     Sub New() 
      ExceptionService = New Domain.ExceptionService(New Domain.ExceptionRepository) 
      EventService = New Domain.EventService(New Domain.EventRepository) 
     End Sub 

     Public Function AddIndex(ByVal searchableEvent As [Event]) As Boolean Implements ILuceneService.AddIndex 

      Dim builder As New StringBuilder 
      builder.Append(Trim(searchableEvent.Description)) 
      builder.Append(" ") 
      builder.Append(Trim(searchableEvent.Title)) 
      builder.Append(" ") 
      builder.Append(Trim(searchableEvent.Location.Name)) 
      builder.Append(" ") 
      builder.Append(Trim(searchableEvent.Region.Region)) 
      builder.Append(" ") 
      builder.Append(Trim(searchableEvent.StartDateTime.ToString("yyyy/MM/dd"))) 
      builder.Append(" ") 
      builder.Append(Trim(searchableEvent.TicketPriceHigh.ToString)) 
      builder.Append(" ") 
      builder.Append(Trim(searchableEvent.TicketPriceLow.ToString)) 
      builder.Append(" ") 
      builder.Append(Trim(searchableEvent.URL)) 
      builder.Append(" ") 
      builder.Append(Trim(searchableEvent.User.UserName)) 

      CreateIndex() 
      Dim writer As New IndexWriter(luceneDirectory, New StandardAnalyzer(), False) 

      Dim doc As Document = New Document 

      doc.Add(New Field("id", searchableEvent.ID, Field.Store.YES, Field.Index.UN_TOKENIZED)) 
      doc.Add(New Field("fullText", builder.ToString, Field.Store.YES, Field.Index.TOKENIZED)) 
      doc.Add(New Field("user", searchableEvent.User.UserName, Field.Store.YES, Field.Index.UN_TOKENIZED)) 
      doc.Add(New Field("location", searchableEvent.Location.Name, Field.Store.YES, Field.Index.UN_TOKENIZED)) 
      doc.Add(New Field("date", searchableEvent.StartDateTime, Field.Store.YES, Field.Index.UN_TOKENIZED)) 

      writer.AddDocument(doc) 

      writer.Optimize() 
      writer.Close() 
      Return True 

     End Function 

     Public Function DeleteIndex(ByVal searchableEvent As [Event]) As Boolean Implements ILuceneService.DeleteIndex 
      Throw New NotImplementedException 
     End Function 

     Public Function ReadIndex(ByVal q As String, ByVal page As Integer?) As List(Of Domain.[Event]) Implements ILuceneService.ReadIndex 

      Dim IDList As List(Of Integer) = New List(Of Integer) 
      If page Is Nothing Then page = 0 
      Dim i As Integer = page 

      ''# Variables used by Lucene 
      Dim reader As IndexReader = IndexReader.Open(luceneDirectory) 
      Dim searcher As IndexSearcher = New IndexSearcher(reader) 
      Dim query As Query = New TermQuery(New Term("fullText", q.ToLower)) 
      Dim hits As Hits = searcher.Search(query) 

      Dim HC = hits.Length ''# For Debugging Purposes 

      While (i <= (page * 10) AndAlso i < hits.Length()) 
       Dim document As Document = hits.Doc(i) 
       Dim score As Single = hits.Score(i) 
       IDList.Add(document.[Get]("id")) 
       i += 1 
      End While 

      ''# Self explanitory 
      searcher.Close() 
      Return EventService.QueryEvents().Where(Function(e) (IDList.Contains(e.ID))).ToList() 
     End Function 

     Public Function UpdateIndex(ByVal searchableEvent As [Event]) As Boolean Implements ILuceneService.UpdateIndex 
      Throw New NotImplementedException 
     End Function 

     Private Sub CreateIndex() Implements ILuceneService.CreateIndex 
      If Not IndexReader.IndexExists(HttpContext.Current.Server.MapPath("~/App_Data/")) Then 
       Dim writer As New IndexWriter(HttpContext.Current.Server.MapPath("~/App_Data/"), New StandardAnalyzer(), True) 
       writer.Close() 
      End If 
     End Sub 
    End Class 
End Namespace 
+0

Was ist ein Beispielwert von 'SearchQuery' an dem Punkt, an dem Sie suchen? – Justin

+0

Ich habe die Frage bearbeitet, um diese Zeile zu entfernen. Ich bekomme die gleichen Ergebnisse mit einer vereinfachten Version der Abfrage. –

+0

Außerdem verwende ich Lucene.Net Version 2.0.0.4 (es war die einzige vorkompilierte Version, die ich finden konnte). Ich bin gerade dabei, 2.4 herunterzuladen. Ich werde es kompilieren und sehen, ob es einen Unterschied macht. –

Antwort

0

Nun, es scheint, als ob die Aktualisierung auf v2.4 lesen und bearbeiten mein Code, um das Problem gelöst ist.

Public Function ReadIndex(ByVal q As String, ByVal page As Integer?) As Domain.Pocos.LuceneResults Implements ILuceneService.ReadIndex 
     ''# A timer variable to determine now long the method executes for 
     Dim tStart As DateTime = DateTime.Now 

     ''# Creates a container that we use to store all of the result ID's 
     Dim IDList As List(Of Integer) = New List(Of Integer) 

     ''# First we set the initial page number. 
     ''# If it'S null, it means it's zero 
     If (page Is Nothing) Or (page <= 0) Then page = 1 

     ''# [i] is the variable we use to extract the appropriate (needed) 
     ''# documents from the results. Its initial value is the page number 
     ''# multiplied by the number of results we want to return (in our 
     ''# case 10). The [last] variable is used to stop the while loop at 
     ''# the 10th record by simply adding 9 to the [i] variable. 
     Dim i = (page - 1) * 10 
     Dim last As Integer = i + 9 

     ''# Variables used by Lucene 
     Dim reader As IndexReader = IndexReader.Open(luceneDirectory) 
     Dim searcher As IndexSearcher = New IndexSearcher(reader) 
     Dim parser As QueryParser = New QueryParser("fullText", New StandardAnalyzer()) 
     Dim query As Query = parser.Parse(q.ToLower) 

     ''# We're using 10,000 as the maximum number of results to return 
     ''# because I have a feeling that we'll never reach that full amount 
     ''# anyways. And if we do, who in their right mind is going to page 
     ''# through all of the results? 
     Dim topDocs As TopDocs = searcher.Search(query, Nothing, 10000) 
     Dim doc As Document = Nothing 

     ''# loop through the topDocs and grab the appropriate 10 results based 
     ''# on the submitted page number 
     While i <= last AndAlso i < topDocs.totalHits 
      doc = searcher.Doc(topDocs.scoreDocs(i).doc) 
      IDList.Add(doc.[Get]("id")) 
      i += 1 
     End While 

     ''# Self explanitoryB 
     searcher.Close() 
     reader.Close() 
     Dim EventList As List(Of Domain.Event) = EventService.QueryEvents().Where(Function(e) (IDList.Contains(e.ID))).ToList() 

     Dim tStop As DateTime = DateTime.Now 

     ''# Instead of just returning a list of results 
     ''# I've decided to create a POCO that also contains 
     ''# the number of results and how long the method took to execute. 
     Dim LucienResults As New Domain.Pocos.LuceneResults With {.EventList = EventList, 
                    .ExecuteTime = (tStop - tStart), 
                    .TotalResults = topDocs.totalHits} 

     Return LucienResults 
    End Function 
0
Dim HC = hits.Length 

Länge ist eine Methode, nicht eine Eigenschaft, sollte es

Dim HC = hits.Length() 
+0

Ja, das ist es leider nicht. –