前言

  • 本文对 Elasticsearch 8.19 有效
  • 对要用 author_id 对第一作者加分

正文

  • 使用 doc['author_id'] , 得到的数组内容不能保持原始顺序
GET my_index/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "id_combine": [
              "2031435113240",
              "2031592776783"
            ]
          }
        }
      ]
    }
  },
  "rescore": [
    {
      "window_size": 5000,
      "query": {
        "score_mode": "multiply",
        "rescore_query": {
          "function_score": {
            "max_boost": 1000,
            "score_mode": "multiply",
            "boost_mode": "multiply",
            "functions": [
              {
                "filter": {
                  "script": {
                    "script": {
                      "lang": "painless",
                      "source": """
                                  def ids = doc['author_id'];
                                  Debug.explain(ids);
                                """,
                      "params": {
                        "auid": "4465029247"
                      }
                    }
                  }
                },
                "weight": 10
              }
            ]
          }
        },
        "query_weight": 1,
        "rescore_query_weight": 1
      }
    }
  ],
  "track_total_hits": true,
  "from": 0,
  "size": 10,
  "_source": [
    "id",
    "title",
    "pub_year",
    "author_id"
  ]
}
  • 在 rescore 的脚本中用 params._source 取到的值为 null
GET my_index/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "id_combine": [
              "2031435113240",
              "2031592776783"
            ]
          }
        }
      ]
    }
  },
  "rescore": [
    {
      "window_size": 5000,
      "query": {
        "score_mode": "multiply",
        "rescore_query": {
          "function_score": {
            "max_boost": 1000,
            "score_mode": "multiply",
            "boost_mode": "multiply",
            "functions": [
              {
                "filter": {
                  "script": {
                    "script": {
                      "lang": "painless",
                      "source": """
                                  def ids = params._source;
                                  Debug.explain(ids);
                                """,
                      "params": {
                        "auid": "4465029247"
                      }
                    }
                  }
                },
                "weight": 10
              }
            ]
          }
        },
        "query_weight": 1,
        "rescore_query_weight": 1
      }
    }
  ],
  "track_total_hits": true,
  "from": 0,
  "size": 10,
  "_source": [
    "id",
    "title",
    "pub_year",
    "author_id"
  ]
}
  • 使用 runtime_mappings 生成第一作者动态字段,达到预期效果
GET my_index/_search
{
  "runtime_mappings": {
    "auid_1st": {
      "type": "keyword",
      "script": {
        "source": """
          def auid = params._source.author_id;
          if (auid == null) return;
          if (auid instanceof List && auid.size() > 0) emit(auid.get(0).toString());
          else if (!(auid instanceof List)) emit(auid.toString());
        """
      }
    }
  },
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "id_combine": [
              "2031435113240",
              "2031592776783"
            ]
          }
        }
      ]
    }
  },
  "rescore": [
    {
      "window_size": 5000,
      "query": {
        "score_mode": "multiply",
        "rescore_query": {
          "function_score": {
            "max_boost": 1000,
            "score_mode": "multiply",
            "boost_mode": "multiply",
            "functions": [
              {
                "filter": {
                  "term": {
                    "auid_1st": {
                      "value": "4465029247"
                    }
                  }
                },
                "weight": 10
              }
            ]
          }
        },
        "query_weight": 1,
        "rescore_query_weight": 1
      }
    }
  ],
  "track_total_hits": true,
  "from": 0,
  "size": 10,
  "_source": [
    "id",
    "title",
    "pub_year",
    "author_id"
  ]
}
本文出自 qbit snap

标签: none

添加新评论