1

我们使用 Lucene.Net 3.0.3 Whitespace Analyzer 并索引具有相同名称的文件,并使用 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;

}

4

1 回答 1

0

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

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

于 2012-11-16T08:47:53.653 回答