2009-05-12 14 views
11

Mam problemy poszukiwaniu dokładnej frazy używając Lucene.NET 2.0.0.4Dokładna fraza wyszukiwania używając Lucene.net

Na przykład szukam „atrybut zakres ustawia zmienną” (w tym cytaty), ale nie otrzymują mecze, potwierdziłem w 100%, że fraza istnieje.

Czy ktoś może zasugerować, gdzie idę źle? Czy to jest nawet obsługiwane przez Lucene.NET? Jak zwykle dokumentacja API nie jest zbyt pomocna, a kilka artykułów CodeProject, które przeczytałem, nie porusza tego tematu.

Korzystanie poniższy kod, aby utworzyć indeks:

Directory dir = Lucene.Net.Store.FSDirectory.GetDirectory("Index", true); 

Analyzer analyzer = new Lucene.Net.Analysis.SimpleAnalyzer(); 

IndexWriter indexWriter = new Lucene.Net.Index.IndexWriter(dir, analyzer,true); 

//create a document, add in a single field 
Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document(); 

Lucene.Net.Documents.Field fldContent = new Lucene.Net.Documents.Field(
    "content", File.ReadAllText(@"Documents\100.txt"), 
    Lucene.Net.Documents.Field.Store.YES, 
    Lucene.Net.Documents.Field.Index.TOKENIZED); 

doc.Add(fldContent); 

//write the document to the index 
indexWriter.AddDocument(doc); 

Następnie wyszukać frazę używając:

//state the file location of the index 
Directory dir = Lucene.Net.Store.FSDirectory.GetDirectory("Index", false); 

//create an index searcher that will perform the search 
IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(dir); 

QueryParser qp = new QueryParser("content", new SimpleAnalyzer()); 

// txtSearch.Text Contains a phrase such as "this is a phrase" 
Query q=qp.Parse(txtSearch.Text); 


//execute the query 
Lucene.Net.Search.Hits hits = searcher.Search(q); 

Dokument docelowy wynosi około 7 MB zwykły tekst.

Widziałem to previous question, ale nie chcę wyszukiwania bliskości, tylko dokładne wyszukiwanie fraz.

Odpowiedz

13

Nie włączyłeś pozycji terminów. Tworzenie pola w następujący sposób powinno rozwiązać Twój problem.

Lucene.Net.Documents.Field fldContent = 
    new Lucene.Net.Documents.Field("content", 
     File.ReadAllText(@"Documents\100.txt"), 
    Lucene.Net.Documents.Field.Store.YES, 
    Lucene.Net.Documents.Field.Index.TOKENIZED, 
    Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS); 
14

Shashikant Kore is correct with his answer, musisz włączyć pozycje utrzymujące ...

jednak polecam nie przechowywanie tekst dokumentu w polu chyba że koniecznie musisz to wrócić z powrotem do Ciebie w poszukiwaniu wyniki ... Ustawienie sklepu na "NIE" może nieco zmniejszyć rozmiar indeksu.

Lucene.Net.Documents.Field fldContent = 
    new Lucene.Net.Documents.Field("content", 
     File.ReadAllText(@"Documents\100.txt"), 
    Lucene.Net.Documents.Field.Store.NO, 
    Lucene.Net.Documents.Field.Index.TOKENIZED, 
    Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS); 
Powiązane problemy