以下向量检索的实例均在elasticsearch7.13.2 kibana7.13.2上执行且通过
1.创建含有向量字段的索引,向量的数据类型为dense_vector
PUT pic_vector_test1
{
"mappings": {
"properties": {
"title": {
"type": "text",
"index": true
},
"p": {
"type": "text",
"index": true
},
"title_vector": {
"type": "dense_vector",
"dims": 2,
"index": true
}
}
}
}
2.像索引中添加数据
POST pic_vector_test1/_doc
{
"title" : "专利1",
"pn" : "cn290102112s",
"title_vector" : [1, 1]
}
POST pic_vector_test1/_doc
{
"title" : "专利2",
"pn" : "cn290102112s",
"title_vector" : [1, 2]
}
POST pic_vector_test1/_doc
{
"title" : "专利3",
"pn" : "cn29033312s",
"title_vector" : [1, 2]
}
POST pic_vector_test1/_doc
{
"title" : "专利5",
"pn" : "cn29033312s",
"title_vector" : [1, 2]
}
POST pic_vector_test1/_doc
{
"title" : "专利6",
"pn" : "cn290102112s",
"title_vector" : [1, 1]
}
POST pic_vector_test1/_doc
{
"title" : "专利55",
"pn" : "cn290102112s",
"title_vector" : [1, 2]
}
POST pic_vector_test1/_doc
{
"title" : "专利31",
"pn" : "cn290102112s",
"title_vector" : [6.0, 2.999]
}
3.使用向量进行查询
GET pic_vector_test1/_search
{
"query": {
"script_score": {
"query": {
"match_all": {
}
},
"script": {
"source": "l2norm(params.query_vector,'title_vector')",
"params": {
"query_vector": [1,1]
}
}
}
}
}
4.带分组的检索
GET pic_vector_test1/_search
{
"size": 0,
"query": {
"script_score": {
"query": {
"match_all": {
}
},
"script": {
"source": "1 / (1 + l2norm(params.query_vector, doc['title_vector']))",
"params": {
"query_vector": [1,1]
}
}
}
},
"aggs": {
"id_distint": {
"terms": {
"field": "pn",
"size": 5
},
"aggs": {
"my_hits": {
"top_hits": {
"_source": {},
"size": 2
}
}
}
}
}
}