【问题标题】:How to Indexing and searching business entities using Lucene.Net?如何使用 Lucene.Net 索引和搜索业务实体?
【发布时间】:2009-01-06 04:37:42
【问题描述】:

我想知道如何使用 Lucene.NET 来索引和搜索我的业务实体。我看到 NHibernate.Search 有很好的功能来解决这个问题,但它仍然需要数据库。我不需要数据库,我只想将所有数据存储在我的 Lucene.NET 索引中。我还看到像 Compass 这样的 java 框架可以轻松完成这些工作,但它不是 .NET 库。

对象设计或框架有什么方法可以解决这个问题吗?

【问题讨论】:

    标签: .net nhibernate frameworks lucene.net compass-lucene


    【解决方案1】:

    尝试使用此代码使用 Lucene.NET 来索引业务实体的快照.., 这对属性类型有明显的限制,需要进行错误检查,但可以让您大致了解如何实现这一点..

    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;
        }
    }
    

    更新:也看看这个项目http://www.codeplex.com/linqtolucene

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      相关资源
      最近更新 更多