简体   繁体   English

如何使用lucene搜索单词的一部分?

[英]How to do search of part of a word using lucene?

Its Ershad here.I am working on lucene. 这里是Ershad。我正在研究Lucene。 Now i am able to search the word.But if i type part of word, i am unable to get the results. 现在我可以搜索单词了。但是如果输入单词的一部分,我将无法获得结果。 Can you pls suggest what to be done. 您能提出建议做什么吗?

For indexing, i am using the below code 对于索引,我正在使用以下代码

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);

For searching, i am using the below code. 为了搜索,我正在使用以下代码。

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();

If you refer to the Lucene Query Parser Syntax documentation , you will find that you can append an asterisk ( * ) to the end of your query to match all those words that begin with a particular string. 如果您参考Lucene查询解析器语法文档 ,则会发现您可以在查询末尾附加星号( * ),以匹配所有以特定字符串开头的单词。 For example, suppose you want to get results mentioning both "caterpillar" and "catamaran". 例如,假设您想获得同时提及“毛毛虫”和“双体船”的结果。 Your search query would be "cat*". 您的搜索查询将是“ cat *”。

However, if you are not in direct control of the search query (for example, if the user is entering their own search queries), then you may need a little trickery on the part of the QueryParser . 但是,如果您不能直接控制搜索查询(例如,如果用户正在输入自己的搜索查询),则QueryParser可能需要一些QueryParser My experience is solely with the Java version of Lucene. 我的经验完全是Java的Lucene版本。 Hopefully the principles are the same with Lucene.NET. 希望原理与Lucene.NET相同。

In Java, you could extend the QueryParser class and override its newTermQuery(Term) method. 在Java中,您可以扩展QueryParser类并覆盖其newTermQuery(Term)方法。 Traditionally, this method would return a TermQuery object. 传统上,此方法将返回TermQuery对象。 However, the child class would instead return a PrefixQuery . 但是,子类将返回PrefixQuery For example: 例如:

public class PrefixedTermsQueryParser extends QueryParser {

    // Some constructors...

    protected Query newTermQuery(Term term) {
        return new PrefixQuery(term);
    }

}

I am not terribly sure what methods you could override in Lucene.NET, but I am sure there must be something similar. 我不确定要在Lucene.NET中重写哪些方法,但是我肯定必须有类似的方法。 Looking at its documentation , it appears the QueryParser class has a method called GetFieldQuery . 查看其文档 ,似乎QueryParser类具有一个称为GetFieldQuery的方法。 Perhaps this is the method you would have to override. 也许这是您必须重写的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM