c++ elasticsearch: 查询表中的所有向量

本文介绍了如何通过Kibana、curl、Python和C++的方式向Elasticsearch的image-index索引发送GET请求,执行全匹配查询,并解析返回的结果,包括计数和图像向量数据。
摘要由CSDN通过智能技术生成

目录

kibana方式

curl方式

python方式

方式-1

方式-2

c++方式

结果解析


kibana方式

curl方式

python方式

方式-1
import json
import requests

header = { "Content-Type": "application/json"}
params = { "query": {"match_all": {} } }

res = requests.post('http://xx:9200/image-index/_search', data=json.dumps(params), headers=header)
num = len(res.json()['hits']['hits'])
print(num)

for i in range(num):
    id = res.json()['hits']['hits'][i]['_id']
    vector = res.json()['hits']['hits'][i]['_source']['image-vector']
    print(id,vector)
4
2 [42, 8, -15]
3 [15, 11, 23]
1 [1, 5, -20]
4 [1, 5, -20]

注意:上面例子里image-vector表不是dense_vector类型

方式-2
import requests

header = { "Content-Type": "application/json"}
strP = '{ "query": {"match_all": {} } }'
res = requests.post('http://xx:9200/image-index/_search', data=strP, headers=header)

num = len(res.json()['hits']['hits'])
print(num)

for i in range(num):
    id = res.json()['hits']['hits'][i]['_id']
    vector = res.json()['hits']['hits'][i]['_source']['image-vector']
    print(id,vector)
4
2 [42, 8, -15]
3 [15, 11, 23]
1 [1, 5, -20]
4 [1, 5, -20]

c++方式

#include <iostream>
#include "third/httplib/httplib.h"
#include "third/nlohmann/json.hpp"

using namespace std;
using namespace httplib;
using namespace nlohmann;

int main()
{
    Client client("http://xx:9200");

    string pStr = R"({ "query": {"match_all": {} } })";

    auto res = client.Post("/image-index/_search", pStr, "application/json");
    auto status_code = res->status;
    auto body = json::parse(res->body);
    cout << status_code << endl;
    cout << body << endl;
}
200
{"_shards":{"failed":0,"skipped":0,"successful":1,"total":1},"hits":{"hits":[{"_id":"2","_index":"image-index","_score":1.0,"_source":{"file-type":"png","image-vector":[42,8,-15],"title":"alpine lake"}},{"_id":"3","_index":"image-index","_score":1.0,"_source":{"file-type":"jpg","image-vector":[15,11,23],"title":"full moon"}},{"_id":"1","_index":"image-index","_score":1.0,"_source":{"file-type":"jpg","image-vector":[1,5,-20],"title":"moose family"}},{"_id":"4","_index":"image-index","_score":1.0,"_source":{"file-type":"jpg","image-vector":[1,5,-20],"title":"moose family"}}],"max_score":1.0,"total":{"relation":"eq","value":4}},"timed_out":false,"took":0}
结果解析
#include <iostream>
#include "third/httplib/httplib.h"
#include "third/nlohmann/json.hpp"
#include <tuple>

using namespace std;
using namespace httplib;
using namespace nlohmann;

int main()
{
    Client client("http://xx:9200");

    string pStr = R"({ "query": {"match_all": {} } })";

    auto res = client.Post("/image-index/_search", pStr, "application/json");
    auto status_code = res->status;
    auto body = json::parse(res->body);
    cout << status_code << endl;
    
    int vNum = body["hits"]["total"]["value"];
    string vId;
    tuple<int,int,int> vector;
    for (int i=0;i<vNum;i++)
    {
        vId = body["hits"]["hits"][i]["_id"];
        vector = body["hits"]["hits"][i]["_source"]["image-vector"];
        cout << vId << ", [" << get<0>(vector) << ", "<<get<1>(vector)<<", "<<get<2>(vector)<<"]\n";
    }
}
200
2, [42, 8, -15]
3, [15, 11, 23]
1, [1, 5, -20]
4, [1, 5, -20]

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值