如何使用 Lucene.Net 索引和搜索业务实体?

发布于 2024-07-10 11:52:52 字数 212 浏览 10 评论 0原文

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

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

I would like to know how to use Lucene.NET for indexing and searching my business entities. I see NHibernate.Search has nice features for this issue, but it still needs DB. I don't need DB, I only want to store all data in my Lucene.NET's index. I also see java framework like Compass can do that stuff easily, but it's not .NET library.

Is there any ways for object designs or frameworks to solve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏雨凉 2024-07-17 11:52:52

尝试使用此代码使用 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

try this code to use Lucene.NET to index a snapshot of business entities..,
this has obvious limitations on type of properties and needs error checking but gives you a general idea of how to achieve this..

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

Update: Also look into this project http://www.codeplex.com/linqtolucene

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文