全局使用 Java 模式 PatternCaptureGroupTokenFilter

发布于 2025-01-19 06:01:36 字数 1435 浏览 3 评论 0原文

我目前正在 Elasticsearch 插件中使用模式捕获组令牌过滤器。目前,该插件工作得非常好,并且还发出令牌。但是,问题是我在模式捕获组令牌过滤器中使用的模式,它仅返回第一个匹配项,而不会继续其后的匹配项。

我的 TokenFilterFactory 文件中的模式捕获令牌过滤器定义如下所示:

PatternCaptureGroupTokenFilter(tokenStream, true, Pattern.compile("(?<![\\p{Alnum}])(\\p{Alnum}+\\p{Punct}\\p{Alnum}+)"))

我知道在 JAVA 的情况下,我可以通过使用 find() 和 group() 方法解决此问题,但不知道是否可以解决简单地用图案本身。

我想测试的模式是“(?

我在 java 文件中的代码如下所示:

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.pattern.PatternCaptureGroupTokenFilter;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.analysis.AbstractTokenFilterFactory;

import java.util.regex.Pattern;

public class AlPuAlTokenFilterFactory extends AbstractTokenFilterFactory {

    public AlPuAlTokenFilterFactory(IndexSettings indexSettings, Environment environment, String name, Settings settings) {
        super(indexSettings, name, settings);

    }

    @Override
    public TokenStream create(TokenStream tokenStream) {
        return new PatternCaptureGroupTokenFilter(tokenStream, true, Pattern.compile("(\\p{Alnum}+\\p{Punct}\\p{Alnum}+)(?=(\\p{Punct}))"));
    }
}

I am currently using the Pattern Capture Group Token Filter inside an Elasticsearch plugin. Currently, the plugin is working absolutely fine and is emitting the tokens also. But, the issue is that the pattern which I am using inside the Pattern Capture Group Token Filter, it is returning only the first match and not continuing the matching after it.

My Pattern Capture Token Filter definition inside the TokenFilterFactory file looks like the below one:

PatternCaptureGroupTokenFilter(tokenStream, true, Pattern.compile("(?<![\\p{Alnum}])(\\p{Alnum}+\\p{Punct}\\p{Alnum}+)"))

I know in the case of JAVA, I can solve this issue by using find() and group() methods, but don't know if it can be resolved simply with the pattern itself.

The pattern that I want to test is "(?<![\p{Alnum}])(\p{Alnum}+\p{Punct}\p{Alnum}+)".I want to test it on the string: ":port&hid$query-input" And, I am expecting the following tokens as output. port&hid, hid$query, query-input

My code in the java file looks like:

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.pattern.PatternCaptureGroupTokenFilter;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.analysis.AbstractTokenFilterFactory;

import java.util.regex.Pattern;

public class AlPuAlTokenFilterFactory extends AbstractTokenFilterFactory {

    public AlPuAlTokenFilterFactory(IndexSettings indexSettings, Environment environment, String name, Settings settings) {
        super(indexSettings, name, settings);

    }

    @Override
    public TokenStream create(TokenStream tokenStream) {
        return new PatternCaptureGroupTokenFilter(tokenStream, true, Pattern.compile("(\\p{Alnum}+\\p{Punct}\\p{Alnum}+)(?=(\\p{Punct}))"));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2025-01-26 06:01:36

你可以试试这个:

(\w+[&$-])(?=(\w+))

解释:

  1. 这里我得到一个单词 \w+ 后跟 &或 $ 或 -。我将其打包为第 1 组
  2. 然后我积极地期待另一个直接的单词。我将其打包为第 2 组。

因此,您可以从 3 场比赛中从第 1 组和第 2 组获得所需的输出:

Match1: port&
group 1:port&
group 2:hid

Match 2: hid$
group 1:hid$
group 2:query

Match 3:query-
group 1:query-
group 2:input

因此,您可以从第 1 组和第 2 组获得所需的输出

演示

You may try this:

(\w+[&$-])(?=(\w+))

Explanation:

  1. Here I get a word \w+ followed by & or $ or -. I package this as Group 1
  2. Then I positively look ahead for another immediate word. I package this as group 2.

So you get your desired ouput from group 1 and group 2 from 3 matches:

Match1: port&
group 1:port&
group 2:hid

Match 2: hid$
group 1:hid$
group 2:query

Match 3:query-
group 1:query-
group 2:input

So you get your desired ouput from group 1 and group 2

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文