2009-12-04 31 views
5

Soy muy nuevo en lucene.net. Escribí esta aplicación de consola simple en C# que indexa algunos datos falsos. Luego quise poder buscar en el índice varios términos usando una consulta booleana.Lucene.Net ¿Qué estoy haciendo mal?

nunca consigo ningún resultado espalda. Aquí está el código. Cualquier ayuda sería muy apreciada. Gracias.

static void Main(string[] args) 
    { 
     StandardAnalyzer analyzer = new StandardAnalyzer(); 
     IndexWriter writer = new IndexWriter("Test", analyzer, true); 
     Console.WriteLine("Creating index"); 
     for (int i = 0; i < 1500; i++) 
     { 
      Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document(); 
      doc.Add(new Lucene.Net.Documents.Field("A", i.ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NO)); 
      doc.Add(new Lucene.Net.Documents.Field("B", "LALA" + i.ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NO)); 
      doc.Add(new Lucene.Net.Documents.Field("C", "DODO" + i.ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NO)); 
      doc.Add(new Lucene.Net.Documents.Field("D", i.ToString() + " MMMMM", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NO)); 
      writer.AddDocument(doc); 
     }    
     writer.Optimize(); 
     writer.Close(); 

     BooleanQuery query = new BooleanQuery(); 
     query.Add(new WildcardQuery(new Term("B", "lala*")), Lucene.Net.Search.BooleanClause.Occur.MUST); 
     query.Add(new WildcardQuery(new Term("C", "DoDo1*")), Lucene.Net.Search.BooleanClause.Occur.MUST); 

     IndexSearcher searcher = new IndexSearcher("Test"); 
     Hits hits = searcher.Search(query); 
     if (hits.Length() > 0) 
     { 
      for (int i = 0; i < hits.Length(); i++) 
      { 
       Console.WriteLine("{0} - {1} - {2} - {3}", 
        hits.Doc(i).GetField("A").StringValue(), 
        hits.Doc(i).GetField("B").StringValue(), 
        hits.Doc(i).GetField("C").StringValue(), 
        hits.Doc(i).GetField("D").StringValue()); 
      } 
     } 
     searcher.Close(); 

     Console.WriteLine("Done"); 

     Console.ReadLine(); 
    } 

entonces yo tengo que trabajar mediante el uso de MultiFieldQueryParser así:

static void Main(string[] args) 
    { 
     StandardAnalyzer analyzer = new StandardAnalyzer();    

     IndexWriter writer = new IndexWriter("Test", analyzer, true); 
     Console.WriteLine("Creating index"); 
     for (int i = 0; i < 1500; i++) 
     { 
      Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document(); 
      doc.Add(new Lucene.Net.Documents.Field("A", i.ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.TOKENIZED)); 
      doc.Add(new Lucene.Net.Documents.Field("B", "LALA" + i.ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.TOKENIZED)); 
      doc.Add(new Lucene.Net.Documents.Field("C", "DODO" + i.ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.TOKENIZED)); 
      doc.Add(new Lucene.Net.Documents.Field("D", i.ToString() + " MMMMM", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.TOKENIZED)); 
      writer.AddDocument(doc); 
     }    
     writer.Optimize(); 
     writer.Close();    

     BooleanQuery.SetMaxClauseCount(5000); 
     Query query = MultiFieldQueryParser.Parse(new string[] { "LALA*", "DODO*" }, new string[] { "B", "C" }, analyzer); 

     IndexSearcher searcher = new IndexSearcher("Test"); 
     Hits hits = searcher.Search(query); 
     if (hits.Length() > 0) 
     { 
      for (int i = 0; i < hits.Length(); i++) 
      { 
       Console.WriteLine("{0} - {1} - {2} - {3}", 
        hits.Doc(i).GetField("A").StringValue(), 
        hits.Doc(i).GetField("B").StringValue(), 
        hits.Doc(i).GetField("C").StringValue(), 
        hits.Doc(i).GetField("D").StringValue()); 
      } 
     } 
     searcher.Close(); 

     Console.WriteLine("Done"); 

     Console.ReadLine(); 
    } 

Este es posiblemente el mejor artículo que he encontrado para los nuevos desarrolladores de Lucene: http://www.ifdefined.com/blog/post/2009/02/Full-Text-Search-in-ASPNET-using-LuceneNET.aspx

Respuesta

5

Creo que hay es un problema al construir su índice. Agrega cuatro campos para cada documento, todos ellos se almacenan pero ninguno de ellos se indexa (=> Lucene.Net.Documents.Field.Index.NO). Deberías indexar al menos en el campo.

Mira que el StandardAnalyzer tokenize cada índice de campo de la siguiente manera: en minúscula y la división de palabras Inglés stop comunes. Por lo tanto, al crear su consulta, use el prefijo LOWERCASE para tener hits:

query.Add(new PrefixQuery(new Term("B", "lala")), BooleanClause.Occur.MUST); 
query.Add(new PrefixQuery(new Term("C", "dodo")), BooleanClause.Occur.MUST); 
+0

He marcado el campo A y he vuelto a ejecutar la aplicación. Todavía no devolvió ningún resultado. – dnoxs

+0

¿Alguna otra sugerencia? Gracias por tu pronta respuesta. – dnoxs

+0

Solo puede realizar una búsqueda en campos indexados. Entonces también debe indexar los campos "B" y "C". –

Cuestiones relacionadas