J'ai la méthode suivante (exemple tiré de this link)Pourquoi ma Lucene.Net longueur de collection "hits" retour "0"
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
Mais quand je mets un point d'arrêt à
Dim HC = hits.Length ''# FOR DEBUGGING PURPOSES
et analyser dans le débogueur, il dit toujours qu'il a une longueur de 0
et dit
les enfants ne pouvaient pas être évalua ted
Première Screenshot
Deuxième Screenshot
Je ne sais pas ce que cela signifie, cependant, la re sult de la requête est toujours un enregistrement unique retourné. Même si je sais pertinemment que plus d'un devrait être retourné.
Si vous souhaitez lire l'ensemble du service, il est posté ci-dessous.
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
Qu'est-ce qu'un exemple de valeur de 'SearchQuery' au point où vous effectuez une recherche? – Justin
J'ai modifié la question pour supprimer cette ligne. J'obtiens les mêmes résultats avec une version simplifiée de la requête. –
En outre, j'utilise Lucene.Net version 2.0.0.4 (c'était la seule version précompilée que j'ai pu trouver). Je suis en train de télécharger 2.4 maintenant. Je vais le compiler et voir si cela fait une différence. –