【问题标题】:Elasticsearch - sort based on cosine similarity of float arraysElasticsearch - 基于浮点数组的余弦相似度排序
【发布时间】:2019-07-26 07:48:17
【问题描述】:

是否可以根据两个不同浮点数组的余弦相似度进行排序?类似于如何通过传递坐标来按地理距离进行排序?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    如果一个数组是输入,我是可能的,但您必须将余弦相似度实现为脚本:

      "script": {
        "lang": "painless",
        "source": """
          def vector = params._source[params.vector_field];
          def dot_product = 0.0;
          def v_norm = 0.0;
          for (int i = 0; i < params.query_vector.length; ++i) { 
              def x = vector[i]; 
              dot_product += x * params.query_vector[i]; 
              v_norm += x * x;
          }
          return v_norm > 0 ? dot_product / (params.query_v_norm * Math.sqrt(v_norm)) : -1;
    """
      }
    

    但是,这使用了source 字段,这可能会很慢。 See this other question to make it faster

    【讨论】:

      猜你喜欢
      • 2011-02-21
      • 2020-08-12
      • 2017-12-01
      • 2019-09-08
      • 2011-01-01
      • 2017-12-12
      • 2020-01-23
      • 2013-05-24
      相关资源
      最近更新 更多