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是可以直接支持跨集群的哈!

原文地址:https://www.cnblogs.com/bonelee/p/7813488.html