elasticsearch - 全文和 knn_vector 混合搜索弹性

标签 elasticsearch full-text-search embedding hybrid

我目前正在开发一个搜索引擎,并且已经开始实现语义搜索。我使用弹性的开放发行版本,我的映射目前如下所示:

{
  "settings": {
    "index": {
      "knn": true,
      "knn.space_type": "cosinesimil"
    }
  },
  "mappings": {
    "properties": {
      "title": { 
        "type" : "text"
      },
      "data": { 
        "type" : "text"
      },
      "title_embeddings": {
        "type": "knn_vector", 
        "dimension": 600
      },
      "data_embeddings": {
        "type": "knn_vector", 
        "dimension": 600
      }
    }
  }
}

对于基本的 knn_vector 搜索,我使用这个:

{
  "size": size,
  "query": {
    "script_score": {
      "query": {
        "match_all": { }
      },
      "script": {
        "source": "cosineSimilarity(params.query_value, doc[params.field1]) + cosineSimilarity(params.query_value, doc[params.field2])",
        "params": {
          "field1": "title_embeddings",
          "field2": "data_embeddings",
          "query_value": query_vec
        }
      }
    }
  }
}

我已经成功地获得了一种混合搜索:

{
  "size": size,
  "query": {
    "function_score": {
      "query": {
        "multi_match": { 
          "query": query,
          "fields": ["data", "title"]
        }
      },
      "script_score": {
        "script": {
          "source": "cosineSimilarity(params.query_value, doc[params.field1]) + cosineSimilarity(params.query_value, doc[params.field2])",
          "params": {
            "field1": "title_embeddings",
            "field2": "data_embeddings",
            "query_value": query_vec
          }
        }
      }
    }
  }
}

问题是,如果我在文档中没有这个词,那么它就不会被返回。例如,通过第一个搜索查询,当我搜索特朗普(不在我的数据集中)时,我设法获取有关社交网络和政治的文档。我通过混合搜索没有得到这些结果。

我已经尝试过这个:

 {
  "size": size,
  "query": {
    "function_score": {
      "query": {
        "match_all": { }
      },
      "functions": [
      {
        "filter" : {
          "multi_match": { 
            "query": query,
            "fields": ["data", "title"]
          }
        },
        "weight": 1
      },
      {
        "script_score" : {
          "script" : {
            "source": "cosineSimilarity(params.query_value, doc[params.field1]) + cosineSimilarity(params.query_value, doc[params.field2])",
            "params": {
              "field1": "title_embeddings",
              "field2": "data_embeddings",
              "query_value": query_vec
            }
          }
        },
        "weight": 4
      }
      ],
      "score_mode": "sum",
      "boost_mode": "sum"
    }
  }
}

但是多重匹配部分为所有匹配的文档提供恒定的分数,我想使用过滤器对我的文档进行排名,就像在正常的全文查询中一样。有想法去做吗?或者我应该使用另一种策略?预先感谢您。

最佳答案

在 Archit Saxena 的帮助下,我的问题得到了解决:

{
  "size": size,
  "query": {
    "function_score": {
      "query": {
        "bool": { 
          "should" : [
            {
              "multi_match" : { 
                "query": query,
                "fields": ["data", "title"]
              }
            },
            {
              "match_all": { }
            }
          ],
          "minimum_should_match" : 0
        }
      },
      "functions": [
      {
        "script_score" : {
          "script" : {
            "source": "cosineSimilarity(params.query_value, doc[params.field1]) + cosineSimilarity(params.query_value, doc[params.field2])",
            "params": {
              "field1": "title_embeddings",
              "field2": "data_embeddings",
              "query_value": query_vec
            }
          }
        },
        "weight": 20
      }
      ],
      "score_mode": "sum",
      "boost_mode": "sum"
    }
  }
}

关于elasticsearch - 全文和 knn_vector 混合搜索弹性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67701323/

相关文章:

elasticsearch - 创建文档时的ElasticSearch Format字段

angularjs - Elasticsearch查询解析失败

elasticsearch - 将Google Cloud Datastore与ElasticSearch同步

sql-server - 创建 Azure SQL V12 全文索引非常慢

ios - 设置 YouTube 播放器的默认方向

Grails Elasticsearch插件在启动时创建索引?我该如何阻止它?

php - 全文搜索不返回总数

solr - 我错误地从 Solr Admin UI 卸载了默认的 Solr 集合

Keras:嵌入 LSTM

python - openai.error.APIConnectionError : Error communicating with OpenAI