我创建了Solr过滤器来从特定文本中检索电子邮件并仅返回电子邮件!Solr中的自定义电子邮件过滤器不起作用
这是我的代码:
public final class NormalizeAffliationFilter extends TokenFilter {
private CharTermAttribute charTermAttr;
protected NormalizeAffliationFilter(TokenStream ts) {
super(ts);
this.charTermAttr = addAttribute(CharTermAttribute.class);
}
@Override
public boolean incrementToken() throws IOException {
if (!input.incrementToken()) {
return false;
}
String token =charTermAttr.toString();
Pattern pattern = Pattern.compile("([a-z0-9_.-]+)@([a-z0-9_.-]+[a-z])");
Matcher matcher = pattern.matcher(token);
StringBuilder sb = new StringBuilder();
while(matcher.find()){
sb.append(matcher.group());
}
sb.append(" ");
String email = sb.toString();
charTermAttr.setEmpty();
charTermAttr.copyBuffer(email.toCharArray(), 0, email.length());
return true;
}
我添加
<fieldType name="emailnormalized" class="solr.TextField">
<analyzer type="query">
<tokenizer class="solr.ClassicTokenizerFactory"/>
<filter class="ir.pandapp.NormalizeAffliationFilterFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="index">
<tokenizer class="solr.ClassicTokenizerFactory"/>
<filter class="ir.pandapp.NormalizeAffliationFilterFactory"/>
<filter class="ir.pandapp.NormalizeAffliationFilterFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
<field name="mods.affiliation" type="emailnormalized" indexed="true" stored="true" multiValued="true"/>
我在我的代码来获取日志,并增加了一些系统输出schema.xml中的字段类型和领域有用!它获取令牌并且只有电子邮件令牌会返回!
我也Solr中测试了分析:
这一切后,当我在Solr的搜索,这是行不通的!
喜欢如果字段值是:“aaaaemail:[email protected]”,我搜索:“aaaa”它返回此文档!
但它应该只会返回这个时,我搜索:“[email protected]”。我检查了模式浏览器,它只索引了电子邮件(正确的形式)。 我不知道接下来要检查什么!有谁知道我错过了什么?
嗨,你能否澄清这一点:“在索引我的数据之后,它显示了整个文本”。什么显示全文?这是查询的结果吗? –
@CliffWillsher是的,我编辑它!感谢您的注意! – saeedeh
你为什么不能用'solr.PatternReplaceCharFilterFactory'实现这个任何理由? – MatsLindh