全局使用 Java 模式 PatternCaptureGroupTokenFilter
我目前正在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以试试这个:
解释:
\w+
后跟 &或 $ 或 -。我将其打包为第 1 组因此,您可以从 3 场比赛中从第 1 组和第 2 组获得所需的输出:
因此,您可以从第 1 组和第 2 组获得所需的输出
演示
You may try this:
Explanation:
\w+
followed by & or $ or -. I package this as Group 1So you get your desired ouput from group 1 and group 2 from 3 matches:
So you get your desired ouput from group 1 and group 2
Demo