elasticsearch-搜索-评分(四)

Dis Max Query

打分测试

PUT /blogs/_doc/1
{
    "title": "Quick brown rabbits",
    "body":  "Brown rabbits are commonly seen."
}

PUT /blogs/_doc/2
{
    "title": "Keeping pets healthy",
    "body":  "My quick brown fox eats rabbits on a regular basis."
}

搜索

POST /blogs/_search
{
    "query": {
        "bool": {
            "should": [
                { "match": { "title": "Brown fox" }},
                { "match": { "body":  "Brown fox" }}
            ]
        }
    }
}

文档1会排在前面 

原因?shoud打分原理

1.查询should2个语句

2.加和2个评分

3.乘以匹配语句的总数

4.除以所有语句总数

纠正

dis_max会将语句中 匹配度最高的返回 而不会相加

POST blogs/_search
{
    "query": {
        "dis_max": {
            "queries": [
                { "match": { "title": "Quick pets" }},
                { "match": { "body":  "Quick pets" }}
            ]
        }
    }
}

Multi Match

打分测试

1.添加索引

PUT /titles
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "english"
      }
    }
  }
}

2.录入数据

POST titles/_bulk
{ "index": { "_id": 1 }}
{ "title": "My dog barks" }
{ "index": { "_id": 2 }}
{ "title": "I see a lot of barking dogs on the road " }

3.搜索

GET titles/_search
{
  "query": {
    "match": {
      "title": "barking dogs"
    }
  }
}

文档1会排在前面,barking dogs 分词 分成了barking dog   但是因为1更短所以排在前面

纠正

DELETE /titles
PUT /titles
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "english",
        "fields": {"std": {"type": "text","analyzer": "standard"}}//增加一个分词字段 采用standard分词
      }
    }
  }
}
GET /titles/_search
{
   "query": {
        "multi_match": {
            "query":  "barking dogs",
            "type":   "most_fields",
            "fields": [ "title", "title.std" ]//搜索  2个搜索的评分相加 可以让文档2排在前面
        }
    }
}
GET /titles/_search
{
   "query": {
        "multi_match": {
            "query":  "barking dogs",
            "type":   "most_fields",
            "fields": [ "title^10", "title.std" ] //可以设置字段的权重
        }
    }
}

设置and关系

GET /titles/_search
{
   "query": {
        "multi_match": {
            "query":  "barking dogs",
            "type":   "cross_fields",
            "operator":"and",
            "fields": [ "title^10", "title.std" ] //可以设置字段的权重
        }
    }
}
原文地址:https://www.cnblogs.com/LQBlog/p/13688040.html