简体   繁体   English

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

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

I've created an index with various bits of data for each document I've added, each document can differ in it field name. 我已经为我添加的每个文档创建了一个包含各种数据位的索引,每个文档的字段名称都不同。

Later on, when I come to search the index I need to query it with exact field/ values - for example: 稍后,当我来搜索索引时,我需要使用确切的字段/值来查询它 - 例如:

FieldName1 = X AND FieldName2 = Y AND FieldName3 = Z

What's the best way of constructing the following using Lucene .NET: 使用Lucene .NET构建以下内容的最佳方法是什么:

  • What analyser is best to use for this exact match type? 什么分析仪最适合这种完全匹配类型?
  • Upon retrieving a match, I only need one specific field to be returned (which I add to each document) - should this be the only one stored? 在检索匹配时,我只需要返回一个特定字段(我将其添加到每个文档中) - 这应该是唯一存储的字段吗?
  • Later on I'll need to support keyword searching (so a field can have a list of values and I'll need to do a partial match). 稍后,我需要支持关键字搜索(因此字段可以包含值列表,我需要进行部分匹配)。

The fields and values come from a Dictionary<string, string> . 字段和值来自Dictionary<string, string> It's not user input, it's constructed from code. 它不是用户输入,而是由代码构造的。

Thanks, 谢谢,
Kieron 基隆

Well, I figured it out eventually - here's my take on it (this could be completely wrong, but it works for): 好吧,我最终想出来了 - 这是我对它的看法(这可能是完全错误的,但它适用于):

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