之前两节课,我觉得已经很了解整个es的相关度评分的算法了,算法思想,TF/IDF
,vector model
,boolean model
; 实际的公式,query norm
,query coordination
,boost
对相关度评分进行调节和优化的常见的4种方法
1、query-time boost
GET /forum/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": {
"query": "java spark",
"boost": 2
}
}
},
{
"match": {
"content": "java spark"
}
}
]
}
}
}
2、重构查询结构
重构查询结果,在es新版本中,影响越来越小了。一般情况下,没什么必要的话,大家不用也行。
GET /forum/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"content": "java"
}
},
{
"match": {
"content": "spark"
}
},
{
"bool": {
"should": [
{
"match": {
"content": "solution"
}
},
{
"match": {
"content": "beginner"
}
}
]
}
}
]
}
}
}
3、negative boost
搜索包含java
,不包含spark
的doc
,但是这样子很死板
搜索包含java
,尽量不包含spark
的doc
,如果包含了spark
,不会说排除掉这个doc
,而是说将这个doc
的分数降低
包含了negative term
的doc
,分数乘以negative boost
,分数降低
GET /forum/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"content": "java"
}
}
],
"must_not": [
{
"match": {
"content": "spark"
}
}
]
}
}
}
GET /forum/_search
{
"query": {
"boosting": {
"positive": {
"match": {
"content": "java"
}
},
"negative": {
"match": {
"content": "spark"
}
},
"negative_boost": 0.2
}
}
}
negative
的doc
,会乘以negative_boost
,降低分数
4、constant_score
如果你压根儿不需要相关度评分,直接走constant_score
加filter
,所有的doc
分数都是1,没有评分的概念了
GET /forum/_search
{
"query": {
"bool": {
"should": [
{
"constant_score": {
"query": {
"match": {
"title": "java"
}
}
}
},
{
"constant_score": {
"query": {
"match": {
"title": "spark"
}
}
}
}
]
}
}
}
报错:
{
"error" : {
"root_cause" : [
{
"type" : "parsing_exception",
"reason" : "[constant_score] query does not support [query]",
"line" : 7,
"col" : 22
}
],
"type" : "x_content_parse_exception",
"reason" : "[7:22] [bool] failed to parse field [should]",
"caused_by" : {
"type" : "parsing_exception",
"reason" : "[constant_score] query does not support [query]",
"line" : 7,
"col" : 22
}
},
"status" : 400
}
GET /forum/_search
{
"query": {
"constant_score": {
"filter": {
"terms": {
"title": [
"java",
"spark"
]
}
}
}
}
}
GET /forum/_search
{
"query": {
"bool": {
"should": [
{
"constant_score": {
"filter": {
"term": {
"title": "java"
}
}
}
},
{
"constant_score": {
"filter": {
"term": {
"title": "spark"
}
}
}
}
]
}
}
}