es - elasticsearch search - rescore

402 阅读2分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :rescore有什么特点?
答 :
在这里插入图片描述
问 :rescore如何使用?
答 :

# rescore
PUT /rescore_test
{
  "mappings" : {
    "properties" : {
      "name" : {"type" : "text"}
    }
  }
}

# 索引
POST /rescore_test/_doc/1
{
  "name"  : "hello good",
  "count" : 10
}

# 索引
POST /rescore_test/_doc/2
{
  "name"  : "hello hello good",
  "count" : 15
}

# 索引
POST /rescore_test/_doc/3
{
  "name"  : "hello hello hello good me",
  "count" : 20
}

# rescore 搜索
# 在query搜索的基础上进行rescore的match_phrase的搜索
GET /rescore_test/_search
{
  "query" : {
    "match" : {
      "name" : "hello good"
    }
  },
  "rescore" : {
    "query"       : {
      "rescore_query" : {
        "match_phrase" : {
          "name" : {
            "query" : "hello hello good",
            "slop"  : 2
          }
        }
      }
    },
    "window_size" : 50
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.7458272,
    "hits" : [
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.7458272,
        "_source" : {
          "name" : "hello hello good",
          "count" : 15
        }
      },
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.72005475,
        "_source" : {
          "name" : "hello hello hello good me",
          "count" : 20
        }
      },
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.31931415,
        "_source" : {
          "name" : "hello good",
          "count" : 10
        }
      }
    ]
  }
}

# rescore 搜索
# resocre 搜索时对query和rescore搜索分别加权
GET /rescore_test/_search
{
  "query" : {
    "match" : {
      "name" : "hello good"
    }
  },
  "rescore" : {
    "query" : {
      "rescore_query" : {
        "match_phrase" : {
          "name" : {
            "query" : "hello hello good",
            "slop"  : 2
          }
        }
      },
      "query_weight"         : 1,
      "rescore_query_weight" : 2
    },
    "window_size" : 50
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.1635083,
    "hits" : [
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.1635083,
        "_source" : {
          "name" : "hello hello good",
          "count" : 15
        }
      },
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.1397249,
        "_source" : {
          "name" : "hello hello hello good me",
          "count" : 20
        }
      },
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.31931415,
        "_source" : {
          "name" : "hello good",
          "count" : 10
        }
      }
    ]
  }
}

# rescore 搜索
# resocre 搜索时对query和rescore搜索分别加权
# 结果评分求积
GET /rescore_test/_search
{
  "query" : {
    "match" : {
      "name" : "hello good"
    }
  },
  "rescore" : {
    "query" : {
      "rescore_query" : {
        "match_phrase" : {
          "name" : {
            "query" : "hello hello good",
            "slop"  : 2
          }
        }
      },
      "query_weight"         : 1,
      "rescore_query_weight" : 2,
      "score_mode"           : "multiply"
    },
    "window_size" : 50
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.31931415,
    "hits" : [
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.31931415,
        "_source" : {
          "name" : "hello good",
          "count" : 10
        }
      },
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.2741208,
        "_source" : {
          "name" : "hello hello good",
          "count" : 15
        }
      },
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.2521249,
        "_source" : {
          "name" : "hello hello hello good me",
          "count" : 20
        }
      }
    ]
  }
}

# rescore 搜索
# resocre 搜索时对query和rescore搜索分别加权
# 结果评分求平均值
GET /rescore_test/_search
{
  "query" : {
    "match" : {
      "name" : "hello good"
    }
  },
  "rescore" : {
    "query" : {
      "rescore_query" : {
        "match_phrase" : {
          "name" : {
            "query" : "hello hello good",
            "slop"  : 2
          }
        }
      },
      "query_weight"         : 1,
      "rescore_query_weight" : 2,
      "score_mode"           : "avg"
    },
    "window_size" : 50
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.58175415,
    "hits" : [
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.58175415,
        "_source" : {
          "name" : "hello hello good",
          "count" : 15
        }
      },
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.5698624,
        "_source" : {
          "name" : "hello hello hello good me",
          "count" : 20
        }
      },
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.31931415,
        "_source" : {
          "name" : "hello good",
          "count" : 10
        }
      }
    ]
  }
}

# rescore 搜索
# resocre 搜索时对query和rescore搜索分别加权
# 结果评分取最大值
GET /rescore_test/_search
{
  "query" : {
    "match" : {
      "name" : "hello good"
    }
  },
  "rescore" : {
    "query" : {
      "rescore_query" : {
        "match_phrase" : {
          "name" : {
            "query" : "hello hello good",
            "slop"  : 2
          }
        }
      },
      "query_weight"         : 1,
      "rescore_query_weight" : 2,
      "score_mode"           : "max"
    },
    "window_size" : 50
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.83934015,
    "hits" : [
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.83934015,
        "_source" : {
          "name" : "hello hello hello good me",
          "count" : 20
        }
      },
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.83536226,
        "_source" : {
          "name" : "hello hello good",
          "count" : 15
        }
      },
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.31931415,
        "_source" : {
          "name" : "hello good",
          "count" : 10
        }
      }
    ]
  }
}

# rescore 搜索
# resocre 搜索时对query和rescore搜索分别加权
# 结果评分取最小值
GET /rescore_test/_search
{
  "query" : {
    "match" : {
      "name" : "hello good"
    }
  },
  "rescore" : {
    "query" : {
      "rescore_query" : {
        "match_phrase" : {
          "name" : {
            "query" : "hello hello good",
            "slop"  : 2
          }
        }
      },
      "query_weight"         : 1,
      "rescore_query_weight" : 2,
      "score_mode"           : "min"
    },
    "window_size" : 50
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.32814604,
    "hits" : [
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.32814604,
        "_source" : {
          "name" : "hello hello good",
          "count" : 15
        }
      },
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.31931415,
        "_source" : {
          "name" : "hello good",
          "count" : 10
        }
      },
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.30038467,
        "_source" : {
          "name" : "hello hello hello good me",
          "count" : 20
        }
      }
    ]
  }
}

# multi rescores
GET /rescore_test/_search
{
  "query" : {
    "match" : {
      "name" : "hello"
    }
  },
   "rescore" : [
     {
      "query" : {
        "rescore_query" : {
          "match_phrase" : {
            "name" : {
              "query" : "hello hello good",
              "slop"  : 2
            }
          }
        },
        "query_weight"         : 1,
        "rescore_query_weight" : 2
      },
      "window_size" : 50
    },
    {
      "query" : {
        "rescore_query" : {
          "function_score" : {
            "script_score" : {
              "script" : {
                "source" : "Math.log10(doc.count.value + 2)"
              }
            }
          }
        },
        "score_mode" : "multiply"
      },
      "window_size" : 10
    }
  ]
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.3811765,
    "hits" : [
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.3811765,
        "_source" : {
          "name" : "hello hello hello good me",
          "count" : 20
        }
      },
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.2603258,
        "_source" : {
          "name" : "hello hello good",
          "count" : 15
        }
      },
      {
        "_index" : "rescore_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.17229891,
        "_source" : {
          "name" : "hello good",
          "count" : 10
        }
      }
    ]
  }
}