【发布时间】:2017-05-31 08:54:53
【问题描述】:
我一直在尝试使用 Lucene 来索引我们的代码数据库。不幸的是,索引中省略了一些术语。例如。在下面的字符串中,我可以搜索“版本号”以外的任何内容:
version-number "cAELimpts.spl SCOPE-PAY:10.1.10 25nov2013kw101730 Setup EMployee field if missing"
我已经尝试使用 Lucene.NET 3.1 和 pylucene 6.2.0 来实现它,结果相同。
以下是我在 Lucene.NET 中实现的一些细节:
using (var writer = new IndexWriter(FSDirectory.Open(INDEX_DIR), new CustomAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED))
{
Console.Out.WriteLine("Indexing to directory '" + INDEX_DIR + "'...");
IndexDirectory(writer, docDir);
Console.Out.WriteLine("Optimizing...");
writer.Optimize();
writer.Commit();
}
CustomAnalyzer 类:
public sealed class CustomAnalyzer : Analyzer
{
public override TokenStream TokenStream(System.String fieldName, System.IO.TextReader reader)
{
return new LowerCaseFilter(new CustomTokenizer(reader));
}
}
最后是 CustomTokenizer 类:
public class CustomTokenizer : CharTokenizer
{
public CustomTokenizer(TextReader input) : base(input)
{
}
public CustomTokenizer(AttributeFactory factory, TextReader input) : base(factory, input)
{
}
public CustomTokenizer(AttributeSource source, TextReader input) : base(source, input)
{
}
protected override bool IsTokenChar(char c)
{
return System.Char.IsLetterOrDigit(c) || c == '_' || c == '-' ;
}
}
看起来像“版本号”和其他一些术语没有被索引,因为它们存在于 99% 的文档中。这可能是问题的原因吗?
编辑:根据要求,FileDocument 类:
public static class FileDocument
{
public static Document Document(FileInfo f)
{
// make a new, empty document
Document doc = new Document();
doc.Add(new Field("path", f.FullName, Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("modified", DateTools.TimeToString(f.LastWriteTime.Millisecond, DateTools.Resolution.MINUTE), Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("contents", new StreamReader(f.FullName, System.Text.Encoding.Default)));
// return the document
return doc;
}
}
【问题讨论】:
-
所以您编写了一个自定义分析器并且无法按预期工作?您尝试使用 index 的
version-number的值是多少,即问题中显示的一个 long 值?您尚未展示您的Document结构,请提供该部分。 -
我已将 FileDocument 类添加到我的问题中。在创建自定义分析器之前,我已经尝试过 StandardAnalyzer。它非常简单,我希望它能索引所有包含术语“版本字符串”作为“内容”字段的一部分的文档。
-
这也很有趣。当我搜索“材料清单”时,Lucene 搜索不会产生任何结果(grepping 会找到几百个匹配项)。但是,当我搜索“删除物料清单”时,Lucene 和 grep 都找到相同数量的文件(大约 10 个)。这里发生了什么?如何使 Lucene 和 grep 搜索结果相同?
标签: lucene lucene.net pylucene