如何在Lucene中执行通配符搜索 [英] How to perform a wildcard search in Lucene

查看:168
本文介绍了如何在Lucene中执行通配符搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Lucene对通配符搜索有广泛的支持,并且我知道您可以搜索以下内容:

I know that Lucene has extensive support for wildcard searches and I know you can search for things like:

Stackover * (将返回 Stackoverflow )

也就是说,我的用户对学习查询语法不感兴趣. Lucene可以使用现成的分析器执行这种类型的通配符搜索吗?还是应该在每个搜索查询后附加"*"?

That said, my users aren't interested in learning a query syntax. Can Lucene perform this type of wildcard search using an out-of-box Analyzer? Or should I append "*" to every search query?

推荐答案

使用字符串操作来做到这一点非常棘手,特别是因为QueryParser支持增强,短语等.

Doing this with string manipulations is tricky to get right, especially since the QueryParser supports boosting, phrases, etc.

您可以使用QueryVisitor将TermQuery重写为PrefixQuery.

You could use a QueryVisitor that rewrites TermQuery into PrefixQuery.

public class PrefixRewriter : QueryVisitor {
    protected override Query VisitTermQuery(TermQuery query) {
        var term = query.GetTerm();
        var newQuery = new PrefixQuery(term);
        return CopyBoost(query, newQuery);
    }
}

QueryVisitor基类当前可以在 gitlab <中找到/a>.

The QueryVisitor base class can currently be found at gitlab.

该代码最初发布在博客帖子,该帖子现已失效.该博客文章仍为

The code was initially posted on a blog post which is now defunct. The blog post is still available at archive.org.

这篇关于如何在Lucene中执行通配符搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆