另一种在查询时不匹配所有不良词的方法是在索引时使用synonym
token filter来匹配这些词并标记包含不良词的文档。
所有你需要做的是存储在文件系统中的文件你的坏字(Elasticsearch主目录):
analysis/badwords.txt
:
word1 => BADWORD <--- pick whatever you want the badword to be replaced with
word2 => BADWORD
...
word1000 => BADWORD
那么你的索引设置需要使用synonym
令牌过滤
curl -XPUT localhost:9200/my_index -d '{
"settings" : {
"analysis" : {
"analyzer" : {
"badwords" : {
"tokenizer" : "whitespace",
"filter" : ["synonym"]
}
},
"filter" : {
"synonym" : {
"type" : "synonym",
"synonyms_path" : "analysis/badwords.txt"
}
}
}
},
"mappings": {
"my_type": {
"properties": {
"content": {
"type": "string",
"index_analyzer": "badwords"
}
}
}
}
}'
然后,当你的索引文档用content
场包含一些BA d字符与您的badwords.txt
文件中的字符相匹配,它会被您在同义词文件中选择的替换字正确替换。
curl -XPOST 'localhost:9200/my_index/_analyze?analyzer=badwords&pretty' -d 'you are a word2'
{
"tokens" : [ {
"token" : "you",
"start_offset" : 0,
"end_offset" : 3,
"type" : "word",
"position" : 1
}, {
"token" : "are",
"start_offset" : 4,
"end_offset" : 7,
"type" : "word",
"position" : 2
}, {
"token" : "a",
"start_offset" : 8,
"end_offset" : 9,
"type" : "word",
"position" : 3
}, {
"token" : "BADWORD",
"start_offset" : 10,
"end_offset" : 14,
"type" : "SYNONYM",
"position" : 4
} ]
}
来源
2015-09-27 05:24:03
Val
我想你必须为此写一个自定义匹配器。无论如何,1000个元素的香草布尔查询不会有效。 – Ashalynd
最初的请求会很慢,但是如果您可以使用过滤器而不是查询禁止的单词列表,那么该过滤器将被缓存(使后续执行非常便宜!) –