如何在 Elasticsearch 中将某些词组组合成 token?
How to combine certain set of words into token in Elasticsearch?
对于像"This is a beautiful day"这样的字符串,我想将字符串标记为标记:
"This, is, a, beautiful, day, beautiful day" 我可以在其中指定一组要组合的单词。在这种情况下,只有 "beautiful" 和 "day".
到目前为止,我已经使用 Shingle 过滤器生成如下标记列表:
"This, This is, is, is a, a, a beautiful, beautiful, beautiful day, day"
如何进一步过滤上面的标记列表以产生我想要的结果?
这是我当前的代码:
shingle_filter = {
"type": "shingle",
"min_shingle_size": 2,
"max_shingle_size": 3,
"token_separator": " "
}
body = {'tokenizer':'standard','filter':['lowercase', shingle_filter], 'text':sample_text['content'], 'explain':False}
standard_tokens = analyze_client.analyze(body= body, format='text')
纠结了一下,看来predicate_token_filter就是我需要的。
shingle_filter = {
"type": "shingle",
"token_separator": " "}
predicate_token_filter_temp = {
"type" : "predicate_token_filter",
"script" : {
"source" : "String term = \"beautiful day\"; token.getTerm().toString().equals(term)"
}}
body = {'tokenizer':'standard','filter':['lowercase', shingle_filter, predicate_token_filter_temp], 'text':sample_text['content'], 'explain':False}
standard_tokens = analyze_client.analyze(body= body, format='text')
我不确定这是最好的方法,但它完成了工作。
对于像"This is a beautiful day"这样的字符串,我想将字符串标记为标记: "This, is, a, beautiful, day, beautiful day" 我可以在其中指定一组要组合的单词。在这种情况下,只有 "beautiful" 和 "day".
到目前为止,我已经使用 Shingle 过滤器生成如下标记列表: "This, This is, is, is a, a, a beautiful, beautiful, beautiful day, day"
如何进一步过滤上面的标记列表以产生我想要的结果?
这是我当前的代码:
shingle_filter = {
"type": "shingle",
"min_shingle_size": 2,
"max_shingle_size": 3,
"token_separator": " "
}
body = {'tokenizer':'standard','filter':['lowercase', shingle_filter], 'text':sample_text['content'], 'explain':False}
standard_tokens = analyze_client.analyze(body= body, format='text')
纠结了一下,看来predicate_token_filter就是我需要的。
shingle_filter = {
"type": "shingle",
"token_separator": " "}
predicate_token_filter_temp = {
"type" : "predicate_token_filter",
"script" : {
"source" : "String term = \"beautiful day\"; token.getTerm().toString().equals(term)"
}}
body = {'tokenizer':'standard','filter':['lowercase', shingle_filter, predicate_token_filter_temp], 'text':sample_text['content'], 'explain':False}
standard_tokens = analyze_client.analyze(body= body, format='text')
我不确定这是最好的方法,但它完成了工作。