Lucene.Net多线正则表达式搜索

时间:2012-11-12 15:01:16

标签: c# regex lucene.net lucene

我们使用Lucene.Net 3.0.3空白分析器,我们使用相同的名称索引文件,将两个字段分开,其中包含Not_Analyzed和Analyzed选项,如下所示

        public static void WriteIndexes()
    {
        string indexPathRegex = ConfigurationManager.TfSettings.Application.CustomSettings["dbScritpsAddressRegex"];

        var analyzerRegex = new WhitespaceAnalyzer();
        var indexWriterRegex = new IndexWriter(indexPathRegex, analyzerRegex, IndexWriter.MaxFieldLength.UNLIMITED);

       foreach (LuceneIndex l in Indexes)
        {
            var doc = new Document();
            doc.Add(new Field("ServerName", l.ServerName.ToLowerInvariant(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));

            doc.Add(new Field("DatabaseName", l.DatabaseName.ToLowerInvariant(), Field.Store.YES, Field.Index.ANALYZED,Field.TermVector.NO));
            doc.Add(new Field("SchemaName", l.SchemaName.ToLowerInvariant(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
            doc.Add(new Field("ObjectType", l.ObjectType.ToLowerInvariant(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
            doc.Add(new Field("ObjectName", l.ObjectName.ToLowerInvariant(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
            doc.Add(new Field("Script", l.Script, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
            doc.Add(new Field("Script", l.Script, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO));

            indexWriterRegex.AddDocument(doc);
        }
        indexWriterRegex.Optimize();
        analyzerRegex.Close();
        indexWriterRegex.Close();




    }

当我们查找单行正则表达式时,它是正常的。但是当我们查找多行正则表达式时;如果搜索文件的大小小于16 KB,则可以。但是当它大于16 KB时, Lucene没有找到搜索关键字。这是一个错误吗?我们如何解决这个问题?

示例关键字:.*taxId.*\n.*customerNo.*

       public  List<item> SearchAllScriptInIndex()
    {
        string indexPathRegex = ConfigurationManager.TfSettings.Application.CustomSettings["dbScritpsAddressRegex"];
        var searcher = new Lucene.Net.Search.IndexSearcher(indexPathRegex, false);

        const int hitsLimit = 1000000;
        var analyzer = new WhitespaceAnalyzer();

        var parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, new[] { "Script", "DatabaseName", "ObjectType", "ServerName" }, analyzer);

        Term t = new Term("Script", Expression);
        RegexQuery scriptQuery = new RegexQuery(t);

        string s = string.Format("({0}) AND {1}", serverAndDatabasescript, objectTypeScript);
        var query = parser.Parse(s);

        BooleanQuery booleanQuery = new BooleanQuery();
        booleanQuery.Add(query, BooleanClause.Occur.MUST);
        booleanQuery.Add(scriptQuery, BooleanClause.Occur.MUST);

        var hits = searcher.Search(booleanQuery, null, hitsLimit, Sort.RELEVANCE).ScoreDocs;

        List<item> results = new List<item>();
        List<string> values = new List<string>();
        Dictionary<int, string> newLineIndices = new Dictionary<int, string>();
        foreach (var hit in hits)
        {
            var hitDocument = searcher.Doc(hit.Doc);
          string contentValue = hitDocument.Get("Script");
         LuceneIndex item = new LuceneIndex();
         item.ServerName = hitDocument.Get("ServerName");
          item.DatabaseName = hitDocument.Get("DatabaseName");
          item.ObjectName = hitDocument.Get("ObjectName");
          item.ObjectType = hitDocument.Get("ObjectType");
          item.SchemaName = hitDocument.Get("SchemaName");
          item.Script = hitDocument.Get("Script");
                    results.Add(item);

        }
        return results;

}

1 个答案:

答案 0 :(得分:0)

根据IndexWriter.AddDocument的文档和字段IndexWriter.MAX_TERM_LENGTH,支持的最大术语长度为16 383个字符。看起来比这更长的术语被忽略了,导致你描述的问题。

AddDocument的文档声称引发了异常,而该字段只是提到信息被写入infoStream [如果设置了]。

/// <p/>Note that each term in the document can be no longer
/// than 16383 characters, otherwise an
/// IllegalArgumentException will be thrown.<p/>

// [...]

/// <summary> Absolute hard maximum length for a term.  If a term
/// arrives from the analyzer longer than this length, it
/// is skipped and a message is printed to infoStream, if
/// set (see <see cref="SetInfoStream" />).
/// </summary>
public static readonly int MAX_TERM_LENGTH;

来源:IndexWriter.cs