【问题标题】:Lucene.NET and searching on multiple fields with specific valuesLucene.NET 并搜索具有特定值的多个字段
【发布时间】:2010-03-29 16:28:42
【问题描述】:

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

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

FieldName1 = X AND FieldName2 = Y AND FieldName3 = Z

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

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

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

谢谢,
基隆

【问题讨论】:

    标签: c# lucene.net


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多