如何计算should子句匹配数doc['_num_matches']并用于script_score
计算bool查询should子句匹配次数用于script_score
需要在script_score中使用虚拟字段doc['_num_matches'],该字段代表bool查询中should子句的匹配次数——每匹配一个独立的should子句,值加1,示例如下:
doc0: "banana" doc0['_num_matches'] == 1 doc1: "apple apple apple apple apple apple banana" doc1['_num_matches'] == 2 doc2: "apple banana cherry" doc2['_num_matches'] == 3
当前使用的查询脚本如下:
{ "query": { "function_score": { "query": { "bool": { "should": [ { "match": { "content": { "query": "apple", "analyzer": "kuromoji" } } }, { "match": { "content": { "query": "cherry", "analyzer": "kuromoji" } } }, { "match": { "content": { "query": "banana", "analyzer": "kuromoji" } } } ], "minimum_should_match": "2<80%" } }, "functions": [ { "script_score": { "script": { "source": "_score * Math.log(1 + doc['_num_matches'].value)" } } } ], "boost_mode": "replace" } }, "size": 200 }
内容的提问来源于stack exchange,提问作者djy
相关产品推荐
相关产品推荐

