繁体   English   中英

Lucene.NET并在具有特定值的多个字段上进行搜索

[英]Lucene.NET and searching on multiple fields with specific values

我已经为我添加的每个文档创建了一个包含各种数据位的索引,每个文档的字段名称都不同。

稍后,当我来搜索索引时,我需要使用确切的字段/值来查询它 - 例如:

FieldName1 = X AND FieldName2 = Y AND FieldName3 = Z

使用Lucene .NET构建以下内容的最佳方法是什么:

  • 什么分析仪最适合这种完全匹配类型?
  • 在检索匹配时,我只需要返回一个特定字段(我将其添加到每个文档中) - 这应该是唯一存储的字段吗?
  • 稍后,我需要支持关键字搜索(因此字段可以包含值列表,我需要进行部分匹配)。

字段和值来自Dictionary<string, string> 它不是用户输入,而是由代码构造的。

谢谢,
基隆

好吧,我最终想出来了 - 这是我对它的看法(这可能是完全错误的,但它适用于):

public Guid? Find (Dictionary<string, string> searchTerms)
{
    if (searchTerms == null)
        throw new ArgumentNullException ("searchTerms");

    try
    {
            var directory = FSDirectory.Open (new DirectoryInfo (IndexRoot));
            if (!IndexReader.IndexExists (directory))
                return null;

            var mainQuery = new BooleanQuery ();
            foreach (var pair in searchTerms)
            {
                var parser = new QueryParser (
                    Lucene.Net.Util.Version.LUCENE_CURRENT, pair.Key, GetAnalyzer ());
                var query = parser.Parse (pair.Value);

                mainQuery.Add (query, BooleanClause.Occur.MUST);
            }

            var searcher = new IndexSearcher (directory, true);

            try
            {
                var results = searcher.Search (mainQuery, (Filter)null, 10);
                if (results.totalHits != 1)
                    return null;

                return Guid.Parse (searcher.Doc (results.scoreDocs[0].doc).Get (ContentIdKey));
            }
            catch
            {
                throw;
            }
            finally
            {
                if (searcher != null)
                    searcher.Close ();
            }
    }
    catch
    {
            throw;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM