2009-01-06 7 views
0

Je voudrais savoir comment utiliser Lucene.NET pour indexer et rechercher mes entités commerciales. Je vois NHibernate.Search a de bonnes fonctionnalités pour ce problème, mais il a toujours besoin de DB. Je n'ai pas besoin de DB, je veux seulement stocker toutes les données dans mon index Lucene.NET. Je vois aussi un framework java comme Compass qui peut facilement faire ça, mais ce n'est pas une bibliothèque .NET.Comment indexer et rechercher des entités commerciales en utilisant Lucene.Net?

Existe-t-il des moyens pour que les conceptions d'objet ou les frameworks résolvent ce problème?

Répondre

1

essayer ce code à utiliser Lucene.NET pour indexer un instantanédes entités commerciales .., cela a des limites évidentes sur le type de propriétés et les besoins de contrôle d'erreur, mais vous donne une idée générale de la façon d'y parvenir ..

public class IndexHelper 
{ 
    static Analyzer analyzer = new StandardAnalyzer(); 
    // Store the index in memory: 
    static Directory directory = new RAMDirectory(); 
    static IndexWriter iwriter; 

    static Dictionary<string, List<WeakReference>> indexedObjects = new Dictionary<string, List<WeakReference>>(); 

    static IndexHelper() 
    { 
     iwriter = new IndexWriter(directory, analyzer, true); 
     iwriter.SetMaxFieldLength(25000); 
    } 

    public static void IndexObject(object entity) 
    { 
     Document doc = new Document(); 
     PropertyInfo[] entityProperties = entity.GetType().GetProperties(); 
     string entityKey = entity.GetHashCode().ToString(); 

     List<WeakReference> entityList; 

     if (indexedObjects.TryGetValue(entityKey, out entityList) == false) 
     { 
      entityList = new List<WeakReference>(); 
      indexedObjects.Add(entityKey, entityList); 
     } 

     entityList.Add(new WeakReference(entity)); 

     doc.Add(new Field("@HASH", entityKey, Field.Store.YES, Field.Index.UN_TOKENIZED)); 

     foreach (PropertyInfo pInfo in entityProperties) 
     { 
      String propertyName = pInfo.Name; 
      object propertyValue = pInfo.GetValue(entity, null); //Assuming all properties are of non index type 
      String text = "null"; 
      if (propertyValue != null) text = propertyValue.ToString(); 

      doc.Add(new Field(propertyName, text, Field.Store.YES, 
       Field.Index.TOKENIZED)); 
     } 

     iwriter.AddDocument(doc); 
     iwriter.Close(); 

    } 

    public static List<WeakReference> Search(string queryString, string fieldName) 
    { 
     // Now search the index: 
     IndexSearcher isearcher = new IndexSearcher(directory); 

     Lucene.Net.QueryParsers.QueryParser qp = new Lucene.Net.QueryParsers.QueryParser(fieldName, analyzer); 
     qp.SetDefaultOperator(Lucene.Net.QueryParsers.QueryParser.OR_OPERATOR); 
     qp.SetLowercaseExpandedTerms(true); 


     Query query = qp.Parse(queryString); 

     List<WeakReference> results = new List<WeakReference>(); 
     Hits hits = isearcher.Search(query); 
     // Iterate through the results: 
     for (int i = 0; i < hits.Length(); i++) 
     { 
      Document hitDoc = hits.Doc(i); 

      List<WeakReference> matchedObjects; 

      if (indexedObjects.TryGetValue(hitDoc.GetField("@HASH").StringValue(), out matchedObjects)) 
      { 
       results.AddRange(matchedObjects); 
      } 

     } 

     isearcher.Close(); 

     return results; 
    } 
} 

Mise à jour: regardez aussi dans ce projet http://www.codeplex.com/linqtolucene

Questions connexes