ES cross cluster search跨集群查询
ES 5.3以后出的新功能。测试demo如下:
下载ES 5.5版本,然后分别本机创建2个实例,配置如下:
cluster.name: xx1
network.host: 127.0.0.1
http.port: 9200
transport.tcp.port: 9300
cluster.name: xx2
network.host: 127.0.0.1
http.port: 9201
transport.tcp.port: 9301
再创建一个实例用于跨集群搜索,配置如下:
http.port: 9202
transport.tcp.port: 9302
search:
remote:
cluster_one:
seeds: 127.0.0.1:9300
cluster_two:
seeds: 127.0.0.1:9301
然后写入测试数据 es_data.json:
{ "index" : { "_index" : "test2", "_type" : "xx"}}
{ "age" : 100, "name":"bone" }
插入一条数据到9200机器:
curl -XPOST localhost:9200/_bulk --data-binary @es_data.json
然后写入测试数据 es_data2.json:
{ "index" : { "_index" : "test2", "_type" : "xx"}}
{ "age" : 99, "name":"jack" }
同理再插入一条数据到9201机器:
curl -XPOST localhost:9201/_bulk --data-binary @es_data2.json
执行搜索:
curl -XPOST localhost:9202/cluster_*:test2/xx/_search?q=* {"took":23,"timed_out":false,"_shards":{"total":10,"successful":10,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"cluster_two:test2","_type":"xx","_id":"AV-jy_6M9ed_QHEOL8Zd","_score":1.0,"_source":{ "age" : 99, "name":"jack" }},{"_index":"cluster_one:test2","_type":"xx","_id":"AV-jy8ivwbfD6QGw1gPg","_score":1.0,"_source":{ "age" : 100, "name":"bone" }}]}}
可以看到获得了两个集群的搜索数据。
执行聚合:
curl -XPOST localhost:9202/cluster_*:test2/xx/_search? -d ' { "aggs": { "all_age": { "terms": { "field": "age" } } } } '
返回:
{"took":25,"timed_out":false,"_shards":{"total":10,"successful":10,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"cluster_two:test2","_type":"xx","_id":"AV-jy_6M9ed_QHEOL8Zd","_score":1.0,"_source":{ "age" : 99, "name":"jack" }},{"_index":"cluster_one:test2","_type":"xx","_id":"AV-jy8ivwbfD6QGw1gPg","_score":1.0,"_source":{ "age" : 100, "name":"bone" }}]},"aggregations":{"all_age":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,
"buckets":[{"key":99,"doc_count":1},{"key":100,"doc_count":1}]}}}
可以看到聚合的返回包含了两个集群的合并结果。
参考:
https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-cross-cluster-search.html
https://www.elastic.co/guide/en/kibana/current/management-cross-cluster-search.html kibana是可以直接支持跨集群的哈!
标签:
elasticsearch
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」