我想计算
doc['_num_matches']
的值以在script_score
中使用,它现在是一个假设字段,作为should子句匹配的计数,对于每个should子句匹配,将1添加到doc['_num_matches']
。
例如
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
}
如果您选择正确的映射,这很容易
映射
PUT /term_count_score
{
"mappings": {
"properties": {
"text": {
"type": "text",
"fields": {
"terms": {
"type": "text",
"analyzer": "whitespace_lowercase_trim_remove_duplicates_analyzer",
"fielddata": true
}
}
}
}
},
"settings": {
"analysis": {
"analyzer": {
"whitespace_lowercase_trim_remove_duplicates_analyzer": {
"tokenizer": "whitespace",
"filter": [
"lowercase",
"trim"
]
}
}
}
}
}
您的文件
PUT /term_count_score/_bulk
{"create":{"_id":1}}
{"text": "banana banana"}
{"create":{"_id":2}}
{"text": "apple apple apple apple apple apple banana"}
{"create":{"_id":3}}
{"text":"cherry banana apple"}
查询
GET /term_count_score/_search?filter_path=hits.hits.fields,hits.hits._score,hits.hits._id
{
"query": {
"function_score": {
"query": {
"match_all": {}
},
"functions": [
{
"script_score": {
"script": "doc['text.terms'].length"
}
}
]
}
},
"script_fields": {
"text_terms": {
"script": "doc['text.terms']"
}
}
}
我将
script_fields
添加到查询中以测试索引
回应
{
"hits" : {
"hits" : [
{
"_id" : "3",
"_score" : 3.0,
"fields" : {
"text_terms" : [
"apple",
"banana",
"cherry"
]
}
},
{
"_id" : "2",
"_score" : 2.0,
"fields" : {
"text_terms" : [
"apple",
"banana"
]
}
},
{
"_id" : "1",
"_score" : 1.0,
"fields" : {
"text_terms" : [
"banana"
]
}
}
]
}
}