1. cluster
1)代表一个集群,集群中有多个节点,主节点通过选举产生,这是对于集群内部来讲。而ES是去中心化的,从集群外部来看整个ES集群逻辑上是一个整体,与任何一个节点通信和与整个ES集群通信是等价的
2)主节点:负责管理集群状态,包括管理分片的状态和副本的状态,以及节点的发现和删除。
curl http://192.168.10.129:9200/_cluster/health?pretty
2. Shards
1)代表索引分片,ES可以把一个完整的索引水平分成多个分片,分布到不同的节点上。构成分布式搜索,提高性能和吞吐量。
2)分片的数量只能在创建索引库时指定,索引库创建后不能更改。(默认一个索引库5个分片,每个分片最多存储2147483519条数据)
curl -H "Content-Type:application/json" -XPUT http://192.168.10.129:9200/test -d '{"settings":{"number_of_shards":3}}'
3. Replicas
ES可以给索引分片设置副本,作用:一是提高容错性,当某个节点损坏或丢失时可以从副本中恢复。二是提高ES查询效率,ES会自动对搜索请求进行负载均衡。(默认一个分片有一个副本,主分片和副本不会存在一个节点中)
4. recovery
代表数据恢复或叫数据重新分布,ES在有节点加入或退出时会根据机器的负载对索引分片进行重新分配,挂掉的节点重新启动时也会进行数据恢复
5. Geteway
ES索引的持久化存储方式,ES默认将索引存放到内存中,当内存满了时再持久化到硬盘。当ES集群关闭再重新启动时就会冲Gateway中读取到索引数据。ES支持多种类型,如:本地文件系统,分布式系统,Hadoop的HDFS和Amazon的s3云存储。
6. Discovery.zen
代表ES自动发现节点机制,ES是一个基于P2P的系统,先通过广播寻找存在的节点,再通过多播协议来进行节点之间的通信,同时也支持点对点的交互。
7. Transport
ES内部节点或集群与客户端的交互方式,默认内部是使用tcp协议进行交互,同时支持http协议(json)、thrift、servlet、memcached、zeroMQ等传输协议(通过插件方式集成)。
8. setting
9. Mapping
10. ES的端口
#9200:Http协议端口,主要用于外部通讯,外部使用RESTful接口进行访问。
#9300:TCP协议端口,ES集群通过9300进行通讯,Java早期客户端(TransportClient)也通过TCP协议与集群进行通信。
11. 搭建一个JAVA项目
1)要求:
编译器(IDE)eclipse、JDK1.8、Tomcat7.0+、Maven3.0+
2)创建一个maven项目,添加ES客户端依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.6.1</version>
</dependency>
3)客户端连接ES集群并测试
@Test
public void test() throws Exception{
// 初始化客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("192.168.10.129", 9200, "http")));
// 构造请求
GetRequest getRequest = new GetRequest(
"test",
"user",
"1");
boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
if(exists){
System.out.println("存在");
}else{
System.out.println("不存在");
}
client.close();
}
高级客户端将会在内部创建低级客户端,用来执行基于提供的构建器请求,并管理其生命周期。
当不再需要时,需要关闭高级客户端实例,以便它所使用的所有资源以及底层Http客户端实例及其线程得到正确释放。
12. Document API
1)索引index四种
/**
* 插入数据,JSON格式
* @throws IOException
*/
@Test
public void test() throws IOException{
IndexRequest request = new IndexRequest(
"test",
"user",
"1");
String jsonString = "{"+
"\"user\":\"kimchy\","+
"\"postDate\":\"2013-01-30\","+
"\"message\":\"trying out ElasticSearch\""+
"}";
request.source(jsonString, XContentType.JSON);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.getId());
}
/**
* 插入数据,Map格式
* @throws IOException
*/
@Test
public void test2() throws IOException{
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("user", "kimchy");
jsonMap.put("postData", "2013-01-30");
jsonMap.put("message", "trying out ElasticSearch");
IndexRequest indexRequest = new IndexRequest("test3", "user", "1").source(jsonMap);
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(indexResponse.getId());
}
/**
* XContentBulider格式
* @throws IOException
*/
@Test
public void test3() throws IOException{
XContentBuilder bulider = XContentFactory.jsonBuilder();
bulider.startObject();
{
bulider.field("user", "kimchy");
bulider.timeField("postDate", new Date());
bulider.field("message", "trying out ElasticSearch");
}
bulider.endObject();
IndexRequest indexRequest = new IndexRequest("test3", "user", "1").source(bulider);
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(indexResponse.getId());
}
/**
* Object格式
* @throws IOException
*/
@Test
public void test4() throws IOException{
IndexRequest request = new IndexRequest("test2", "user2", "1")
.source("user", "kimchy",
"postDate", new Date(),
"message", "trying out ElasticSearch");
IndexResponse indexResponse =client.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.getId());
}
2)查询get以及存在exists
构造请求
GetRequest getRequest = new GetRequest(
"test",
"user",
"1");
// getRequest.fetchSourceContext(new FetchSourceContext(false));
// getRequest.storedFields("_none_");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println(getResponse.getSource());
boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
3)更新update
UpdateRequest updateRequest = new UpdateRequest(
"test",
"user",
"2");
String jsonString = "{"+
"\"user\":\"kimchy\","+
"\"postDate\":\"2013-01-30\","+
"\"message\":\"trying out ElasticSearch\""+
"}";
updateRequest.doc(jsonString, XContentType.JSON);
UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
4)删除delete
DeleteRequest deleteRequest = new DeleteRequest(
"test",
"user",
"1");
DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
5)批量操作
// 初始化客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("192.168.10.129", 9200, "http")));
BulkRequest request = new BulkRequest();
request.add(new IndexRequest("post", "doc", "1").source(XContentType.JSON, "field", "foo"));
request.add(new IndexRequest("post", "doc", "2").source(XContentType.JSON, "field", "bar"));
request.add(new IndexRequest("post", "doc", "3").source(XContentType.JSON, "field", "abc"));
BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);
client.close();
6)mget批量查询
MultiGetRequest request = new MultiGetRequest();
request.add(new MultiGetRequest.Item("test", "user", "1"));
request.add(new MultiGetRequest.Item("post", "doc", "1"));
MultiGetResponse mget = client.mget(request, RequestOptions.DEFAULT);
for (MultiGetItemResponse itemResponse : mget) {
GetResponse response = itemResponse.getResponse();
if(response.isExists()){
String json = response.getSourceAsString();
System.out.println(json);
}
}