track_total_hits: true
获取超过1w条数据 需要加上 "track_total_hits":true ,不然只能显示出9999条
Boosting加权查询
GET indexName/_search?pretty{"query": {"boosting": {"positive": {"term": {"text": "apple"}},"negative": {"term": {"text": "pie tart fruit crumble tree"}},"negative_boost": 0.5}}}
Constant 固定分值
GET /_search{"query": {"constant_score": {"filter": {"term": {"user.id": "kimchy"}},"boost": 1.2}}}
disjuction最大分值查询
GET /_search{"query": {"dis_max": {"queries": [{"term": {"title": "Quick pets"}},{"term": {"body": "Quick pets"}}],"tie_breaker": 0.7 #相关性打分}}}
function_score 函数式打分查询
GET /_search{"query": {"function_score": {"query": { "match_all": {} },"boost": "5","random_score": {},"boost_mode": "multiply" # 乘法运算}}}
GET /_search?pretty{"query": {"function_score": {"query": {"match_all": {}},"boost": "5", # 整个查询的权重"functions": [{"filter": {"match": {"test": "bar"}},"random_score": {},"weight": 23},{"filter": {"match": {"test": "cat"}},"weight": 42}],"max_boost": 42,"score_mode": "max", # multiply、sum、avg、first、max、min"boost_mode": "multiply", # multiply、replace、sum、avg、max、min"min_score": 42}}}
要使 min_score 起作用,需要对查询返回的所有文档进行评分,然后一一过滤掉。
The function_score query provides several types of score functions.
- script_score
- weight
- random_score
- field_value_factor
- decay functions: gauss, linear, exp
高斯衰减函数比较重要
script score
GET /_search?pretty{"query": {"function_score": {"query": {"match": {"message": "elasticsearch"}},"script_score": {"script": {"source": "Math.log(2 + doc['my-int'].value)"}}}}}
script_score 函数允许包装另一个查询并自定义它的评分,可以选择使用脚本表达式从文档中的其他数字字段值派生的计算。
