它的Ershad在这里.我正在研究lucene.现在我能够搜索这个单词.但是如果我输入单词的一部分,我就无法得到结果.你能不能建议做些什么.
对于索引,我使用以下代码
writer = new IndexWriter(directory, new StandardAnalyzer(), true);
writer.SetUseCompoundFile(true);
doc.Add(Field.UnStored("text", parseHtml(html)));
doc.Add(Field.Keyword("path", relativePath));
writer.AddDocument(doc);
Run Code Online (Sandbox Code Playgroud)
对于搜索,我使用以下代码.
Query query = QueryParser.Parse(this.Query,"text",new StandardAnalyzer());
// create the result DataTable
this.Results.Columns.Add("title", typeof(string));
this.Results.Columns.Add("sample", typeof(string));
this.Results.Columns.Add("path", typeof(string));
// search
Hits hits = searcher.Search(query);
this.total = hits.Length();
Run Code Online (Sandbox Code Playgroud)
如果您参考Lucene查询分析器语法文档,您会发现可以*在查询末尾添加星号()以匹配以特定字符串开头的所有单词.例如,假设您希望获得提及"caterpillar"和"catamaran"的结果.您的搜索查询将是"cat*".
但是,如果您不直接控制搜索查询(例如,如果用户正在输入他们自己的搜索查询),那么您可能需要一些小技巧QueryParser.我的经验完全在于Lucene的Java版本.希望Lucene.NET的原理与之相同.
在Java中,您可以扩展QueryParser类并覆盖其newTermQuery(Term)方法.传统上,此方法将返回一个TermQuery对象.但是,子类将返回一个PrefixQuery.例如:
public class PrefixedTermsQueryParser extends QueryParser {
// Some constructors...
protected Query newTermQuery(Term term) {
return new PrefixQuery(term);
}
}
Run Code Online (Sandbox Code Playgroud)
我不太确定你可以在Lucene.NET中覆盖哪些方法,但我确信必须有类似的东西.查看其文档,看起来QueryParser类有一个名为的方法GetFieldQuery.也许这是你必须覆盖的方法.