它的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);
搜索时,我使用以下代码。
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();
答案 0 :(得分:6)
如果您引用the Lucene Query Parser Syntax documentation,您会发现可以在查询末尾附加一个星号(*
),以匹配以特定字符串开头的所有单词。例如,假设您希望获得提及“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);
}
}
我不太清楚你可以在Lucene.NET中覆盖哪些方法,但我确信必须有类似的东西。查看its documentation,看来the QueryParser class有一个名为GetFieldQuery
的方法。也许这是你必须覆盖的方法。