目录
概述
1.安装Elasticsearch,从官网下载windwins版本的包,解压开箱即用
2.使用javaApi连接es
3.创建索引
4.获取索引记录
5.删除索引
6.向索引中添加doc
7.更新doc的某个值
8.查询doc文档
9.删除doc文档
10.批量添加doc
11.批量删除doc
12.批量更新doc
13.使用query查询索引下的所有doc
14.使用query带条件查询某个索引下的doc
15.使用query带分页查询某个索引下的doc
16.使用query带排序查询某个索引下的doc
17.使用query查询某个索引下的doc-查询某些字段
18.使用query查询某个索引下的doc-组合条件查询(and的关系)
19.使用query查询某个索引下的doc-组合条件查询(or的关系)
20.使用query查询某个索引下的doc-按范围查询
21.使用query查询某个索引下的doc-模糊查询
22.使用query查询某个索引下的doc-高亮显示某个字段
23.使用query查询某个索引下的doc-聚合查询(某个字段最大最小)
24.使用query查询某个索引下的doc-分组查询
25.集群部署elasticsearch简介
26.windows环境下elasticsearch集群部署
Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。 作为 Elastic Stack 的核心,它集中存储您的数据,帮助您发现意料之中以及意料之外的情况。Elasticsearch是java开发的,在7.8版本以上,需要使用jdk1.8以上版本。
官网访问地址:Download Elasticsearch | Elastic
Elasticsearch是面向文档型数据库,一条数据在这里就是一个文档。为了方便大家理解,我们将Elasticsearch里存储文档数据与关系型数据库mysql存储数据的概念进行类比
ES里的Index可以看做一个库,而type相当于表,Documents相当于行。
这里type的概念逐渐被弱化,Elasticsearch 6.x中,一个index下只能包含一个type,Elasticsearch 7.x中,type的概念已经被删除了。
目录说明
bin:程序启动的bat文件
config:配置信息
data:存放数据
jdk:自带的jdk
lib:需要的jar包
logs:日志文件
modules:功能模块
plugins:扩展功能包
点击bin目录下的elasticsearch.bat文件,启动elasticsearch项目,默认系统访问端口9200
使用http://127.0.0.1:9200/访问,看到以下页面,说明启动成功
(1)新建maven项目后,pom.xml导入需要的包
org.elasticsearch
elasticsearch
7.13.0
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.12.0
org.apache.logging.log4j
log4j-api
2.8.2
org.apache.logging.log4j
log4j-core
2.8.2
com.fasterxml.jackson.core
jackson-databind
2.12.3
junit
junit
4.12
(2)使用RestHighLevelClient创建客户端连接测试
public class EsClientTest {
@Test
public void testConneEs() throws IOException {
//1.创建es客户端
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.关闭连接
restHighLevelClient.close();
}
}
public class EsCreateIndex {
@Test
public void createIndex() throws IOException {
//1.创建客户端连接
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建索引请求
CreateIndexRequest createIndexRequest = new CreateIndexRequest("user");
//3.得到创建索引回调
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
//4.获取响应状态
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("创建索引的响应结果:"+acknowledged);
//5.关闭资源
restHighLevelClient.close();
}
}
通过网页访问索引
@Test
public void searchIndex() throws IOException {
//1.创建连接es客户端
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.封装请求索引的参数
GetIndexRequest getIndexRequest = new GetIndexRequest("user");
//3.根据参数获取索引返回值
GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);
//4.查看返回值情况
System.out.println(getIndexResponse.getAliases());
System.out.println(getIndexResponse.getMappings());
System.out.println(getIndexResponse.getSettings());
//5.关闭资源
restHighLevelClient.close();
}
控制台获取情况
@Test
public void deleteIndex() throws IOException {
//1.获取es客户端连接
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建需要删除的索引
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("user");
//3.执行删除索引
AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
//4.输出执行结果
System.out.println("执行删除结果:"+acknowledgedResponse.isAcknowledged());
//5,关闭客户端
restHighLevelClient.close();
}
@Test
public void docInsert() throws IOException {
//1.创建es客户端链接
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建操作的索引请求
IndexRequest indexRequest = new IndexRequest();
//3.设置向索引中添加的doc先关信息
//3.1指定向那个缩影中添加文档,可以指定id
indexRequest.index("user").id("1001");
//3.2创建实体并给字段赋值
User user = new User();
user.setName("张三");
user.setAge(30);
user.setPassword("123456");
//3.3把实体转成json格式
ObjectMapper objectMapper = new ObjectMapper();
String s = objectMapper.writeValueAsString(user);
//3.4设置请求的doc资源
indexRequest.source(s, XContentType.JSON);
//4.执行添加doc的操作
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println("执行结果:"+indexResponse.getResult());
//5.关闭资源
restHighLevelClient.close();
}
使用postman查看当前添加的doc文档情况
@Test
public void docUpdate() throws IOException {
//1.创建es客户端链接
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建更新操作的索引请求
UpdateRequest updateRequest = new UpdateRequest();
//3指定操作哪个索引,使用id指定是哪个doc
updateRequest.index("user").id("1001");
//4.设置需要修改的doc
updateRequest.doc(XContentType.JSON,"name","李四");
//5.执行修改doc的操作
UpdateResponse response = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println("执行结果:"+response.getResult());
//5.关闭资源
restHighLevelClient.close();
}
name字段值有张三更新为李四
@Test
public void docSelect() throws IOException {
//1.创建es客户端连接
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建查询doc数据
GetRequest getRequest = new GetRequest();
getRequest.index("user").id("1001");
//3.执行查询
GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
System.out.println(documentFields.getSourceAsString());
//4.关闭资源
restHighLevelClient.close();
}
控制台输出结果
@Test
public void docDelete() throws IOException {
//1.创建es客户端连接
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建删除的doc数据请求
DeleteRequest deleteRequest = new DeleteRequest();
deleteRequest.index("user").id("1001");
//3.执行删除
DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(response.toString());
//4.关闭资源
restHighLevelClient.close();
}
控制台执行结果
使用postman查询记录-已经删除成功
@Test
public void batchDocInster() throws IOException {
//1.创建es客户端链接
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建批量操作的请求
BulkRequest bulkRequest = new BulkRequest();
//3.往批量请求中添加记录
bulkRequest.add(new IndexRequest("user").id("1001").source(XContentType.JSON,"name","张三"));
bulkRequest.add(new IndexRequest("user").id("1002").source(XContentType.JSON,"name","李四"));
bulkRequest.add(new IndexRequest("user").id("1003").source(XContentType.JSON,"name","王五"));
//4.执行批量添加请求
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println("花费的时间:"+bulk.getTook());
System.out.println("执行的项:"+bulk.getItems());
//5.关闭资源
restHighLevelClient.close();
}
使用postman查看user索引下的所有文档
@Test
public void batchDocDelete() throws IOException {
//1.创建es客户端链接
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建批量操作的请求
BulkRequest bulkRequest = new BulkRequest();
//3.往批量请求中添加删除的记录
bulkRequest.add(new DeleteRequest("user").id("1001"));
bulkRequest.add(new DeleteRequest("user").id("1002"));
bulkRequest.add(new DeleteRequest("user").id("1003"));
//4.执行批量添加请求
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println("花费的时间:"+bulk.getTook());
System.out.println("执行的项:"+bulk.getItems());
//5.关闭资源
restHighLevelClient.close();
}
使用postman查看索引下的文档
@Test
public void batchDocUpdate() throws IOException {
//1.创建es客户端链接
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建批量操作的请求
BulkRequest bulkRequest = new BulkRequest();
//3.往批量请求中添加记录
bulkRequest.add(new UpdateRequest().index("user").id("1001").doc(XContentType.JSON,"name","张三1"));
bulkRequest.add(new UpdateRequest().index("user").id("1002").doc(XContentType.JSON,"name","李四1"));
bulkRequest.add(new UpdateRequest().index("user").id("1003").doc(XContentType.JSON,"name","王五1"));
//4.执行批量添加请求
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println("花费的时间:"+bulk.getTook());
System.out.println("执行的项:"+bulk.getItems());
//5.关闭资源
restHighLevelClient.close();
}
postman中查看索引下的文档
/**
* 查询指定索引下所有doc
*/
@Test
public void queryIndexOfAllDoc() throws IOException {
//1.创建es客户端
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建查询的请求
SearchRequest searchRequest = new SearchRequest();
//3.设置操作的索引
searchRequest.indices("user");
//4.设置请求的资源参数--查询所有的记录
searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
//5.执行查询
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//6.对查询结果进行输出
System.out.println("执行时间:"+response.getTook());
SearchHits hits = response.getHits();
System.out.println("查询到的记录条数:"+hits.getTotalHits());
//遍历输出所以的记录
for (SearchHit hit : hits){
System.out.println(hit.getSourceAsString());
}
//7.关闭资源
restHighLevelClient.close();
}
控制台打印消息
@Test
public void queryIndexOfAllDoc() throws IOException {
//1.创建es客户端
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建查询的请求
SearchRequest searchRequest = new SearchRequest();
//3.设置操作的索引
searchRequest.indices("user");
//4.设置请求的资源参数--带条件查询
searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",30)));
//5.执行查询
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//6.对查询结果进行输出
System.out.println("执行时间:"+response.getTook());
SearchHits hits = response.getHits();
System.out.println("查询到的记录条数:"+hits.getTotalHits());
//遍历输出所以的记录
for (SearchHit hit : hits){
System.out.println(hit.getSourceAsString());
}
//7.关闭资源
restHighLevelClient.close();
}
@Test
public void queryIndexOfAllDoc() throws IOException {
//1.创建es客户端
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建查询的请求
SearchRequest searchRequest = new SearchRequest();
//3.设置操作的索引
searchRequest.indices("user");
//4.设置请求的资源参数--分页查询
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//4.1设置起始行数
query.from(0);
//4.2设置每页的大小
query.size(2);
searchRequest.source(query);
//5.执行查询
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//6.对查询结果进行输出
System.out.println("执行时间:"+response.getTook());
SearchHits hits = response.getHits();
System.out.println("查询到的记录条数:"+hits.getTotalHits());
//遍历输出所以的记录
for (SearchHit hit : hits){
System.out.println(hit.getSourceAsString());
}
//7.关闭资源
restHighLevelClient.close();
}
@Test
public void queryIndexOfAllDoc() throws IOException {
//1.创建es客户端
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建查询的请求
SearchRequest searchRequest = new SearchRequest();
//3.设置操作的索引
searchRequest.indices("user");
//4.设置请求的资源参数--分页查询--排序
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//4.1设置排序方式:升序SortOrder.ASC、降序SortOrder.DESC
query.sort("age", SortOrder.DESC);
searchRequest.source(query);
//5.执行查询
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//6.对查询结果进行输出
System.out.println("执行时间:"+response.getTook());
SearchHits hits = response.getHits();
System.out.println("查询到的记录条数:"+hits.getTotalHits());
//遍历输出所以的记录
for (SearchHit hit : hits){
System.out.println(hit.getSourceAsString());
}
//7.关闭资源
restHighLevelClient.close();
}
@Test
public void queryIndexOfAllDoc() throws IOException {
//1.创建es客户端
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建查询的请求
SearchRequest searchRequest = new SearchRequest();
//3.设置操作的索引
searchRequest.indices("user");
//4.设置请求的资源参数--过滤查询字段
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//4.1设置包含哪些字段
String [] includes = {"name","age"};
//4.2设置去除哪些字段
String [] excludes = {};
query.fetchSource(includes,excludes);
searchRequest.source(query);
//5.执行查询
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//6.对查询结果进行输出
System.out.println("执行时间:"+response.getTook());
SearchHits hits = response.getHits();
System.out.println("查询到的记录条数:"+hits.getTotalHits());
//遍历输出所以的记录
for (SearchHit hit : hits){
System.out.println(hit.getSourceAsString());
}
//7.关闭资源
restHighLevelClient.close();
}
@Test
public void queryIndexOfAllDoc() throws IOException {
//1.创建es客户端
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建查询的请求
SearchRequest searchRequest = new SearchRequest();
//3.设置操作的索引
searchRequest.indices("user");
//4.设置请求的资源参数--组合条件查询
SearchSourceBuilder builder = new SearchSourceBuilder();
//4.1使用boolQuery方式
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//4.2设置查询条件
boolQueryBuilder.must(QueryBuilders.matchQuery("age",30)); //must必须满足
boolQueryBuilder.mustNot(QueryBuilders.matchQuery("name","lisi"));//mustNot必须不满足
//4.3给查询资源设置值
builder.query(boolQueryBuilder);
searchRequest.source(builder);
//5.执行查询
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//6.对查询结果进行输出
System.out.println("执行时间:"+response.getTook());
SearchHits hits = response.getHits();
System.out.println("查询到的记录条数:"+hits.getTotalHits());
//遍历输出所以的记录
for (SearchHit hit : hits){
System.out.println(hit.getSourceAsString());
}
//7.关闭资源
restHighLevelClient.close();
}
@Test
public void queryIndexOfAllDoc() throws IOException {
//1.创建es客户端
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建查询的请求
SearchRequest searchRequest = new SearchRequest();
//3.设置操作的索引
searchRequest.indices("user");
//4.设置请求的资源参数--组合条件查询
SearchSourceBuilder builder = new SearchSourceBuilder();
//4.1使用boolQuery方式
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//4.2设置查询条件
boolQueryBuilder.should(QueryBuilders.matchQuery("age",30)); //should应该满足
boolQueryBuilder.should(QueryBuilders.matchQuery("age",90));//should应该满足
//4.3给查询资源设置值
builder.query(boolQueryBuilder);
searchRequest.source(builder);
//5.执行查询
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//6.对查询结果进行输出
System.out.println("执行时间:"+response.getTook());
SearchHits hits = response.getHits();
System.out.println("查询到的记录条数:"+hits.getTotalHits());
//遍历输出所以的记录
for (SearchHit hit : hits){
System.out.println(hit.getSourceAsString());
}
//7.关闭资源
restHighLevelClient.close();
}
@Test
public void queryIndexOfAllDoc() throws IOException {
//1.创建es客户端
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建查询的请求
SearchRequest searchRequest = new SearchRequest();
//3.设置操作的索引
searchRequest.indices("user");
//4.设置请求的资源参数--范围查询
SearchSourceBuilder builder = new SearchSourceBuilder();
//4.1使用rangeQuery--指定按哪个字段范围查询
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
//4.2设置范围 gte:大于等于 gt:大于 lte:小于等于 lt:小于
rangeQuery.gte(30);
rangeQuery.lt(90);
//4.3给查询资源设置值
builder.query(rangeQuery);
searchRequest.source(builder);
//5.执行查询
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//6.对查询结果进行输出
System.out.println("执行时间:"+response.getTook());
SearchHits hits = response.getHits();
System.out.println("查询到的记录条数:"+hits.getTotalHits());
//遍历输出所以的记录
for (SearchHit hit : hits){
System.out.println(hit.getSourceAsString());
}
//7.关闭资源
restHighLevelClient.close();
}
@Test
public void queryIndexOfAllDoc() throws IOException {
//1.创建es客户端
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建查询的请求
SearchRequest searchRequest = new SearchRequest();
//3.设置操作的索引
searchRequest.indices("user");
//4.设置请求的资源参数--模糊查询
SearchSourceBuilder builder = new SearchSourceBuilder();
//4.1使用rangeQuery--指定按哪个字段模糊查询,Fuzziness.ONE模糊一位
FuzzyQueryBuilder fuzziness = QueryBuilders.fuzzyQuery("name", "张三").fuzziness(Fuzziness.ONE);
//4.3给查询资源设置值
builder.query(fuzziness);
searchRequest.source(builder);
//5.执行查询
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//6.对查询结果进行输出
System.out.println("执行时间:"+response.getTook());
SearchHits hits = response.getHits();
System.out.println("查询到的记录条数:"+hits.getTotalHits());
//遍历输出所以的记录
for (SearchHit hit : hits){
System.out.println(hit.getSourceAsString());
}
//7.关闭资源
restHighLevelClient.close();
}
@Test
public void queryIndexOfAllDoc() throws IOException {
//1.创建es客户端
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建查询的请求
SearchRequest searchRequest = new SearchRequest();
//3.设置操作的索引
searchRequest.indices("user");
//4.设置请求的资源参数--高亮显示
SearchSourceBuilder builder = new SearchSourceBuilder();
//4.1使用rangeQuery--指定按哪个字段查询
TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("age", "30");
//4.2设置高亮样式
HighlightBuilder highlightBuilder = new HighlightBuilder();
//4.3设置高亮的前缀
highlightBuilder.preTags("");
//4.4设置高亮的后缀
highlightBuilder.postTags("");
//4.4设置添加高亮的字段
highlightBuilder.field("name");
//4.5给查询添加高亮
builder.highlighter(highlightBuilder);
//4.6给查询资源设置值
builder.query(termsQueryBuilder);
searchRequest.source(builder);
//5.执行查询
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//6.对查询结果进行输出
System.out.println("执行时间:"+response.getTook());
SearchHits hits = response.getHits();
System.out.println("查询到的记录条数:"+hits.getTotalHits());
//遍历输出所以的记录
for (SearchHit hit : hits){
System.out.println(hit.getSourceAsString());
}
//7.关闭资源
restHighLevelClient.close();
}
@Test
public void queryIndexOfAllDoc() throws IOException {
//1.创建es客户端
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建查询的请求
SearchRequest searchRequest = new SearchRequest();
//3.设置操作的索引
searchRequest.indices("user");
//4.设置请求的资源参数--聚合查询
SearchSourceBuilder builder = new SearchSourceBuilder();
//4.1查询某个字段最大max、最小min
MaxAggregationBuilder maxAggregationBuilder = AggregationBuilders.max("maxAge").field("age");
builder.aggregation(maxAggregationBuilder);
searchRequest.source(builder);
//5.执行查询
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//6.对查询结果进行输出
System.out.println("执行时间:"+response.getTook());
SearchHits hits = response.getHits();
System.out.println("查询到的记录条数:"+hits.getTotalHits());
//遍历输出所以的记录
for (SearchHit hit : hits){
System.out.println(hit.getSourceAsString());
}
//7.关闭资源
restHighLevelClient.close();
}
@Test
public void queryIndexOfAllDoc() throws IOException {
//1.创建es客户端
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//2.创建查询的请求
SearchRequest searchRequest = new SearchRequest();
//3.设置操作的索引
searchRequest.indices("user");
//4.设置请求的资源参数--分组查询
SearchSourceBuilder builder = new SearchSourceBuilder();
//4.1查询按某个字段进行分组
TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");
builder.aggregation(aggregationBuilder);
searchRequest.source(builder);
//5.执行查询
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//6.对查询结果进行输出
System.out.println("执行时间:"+response.getTook());
SearchHits hits = response.getHits();
System.out.println("查询到的记录条数:"+hits.getTotalHits());
//遍历输出所以的记录
for (SearchHit hit : hits){
System.out.println(hit.getSourceAsString());
}
//7.关闭资源
restHighLevelClient.close();
}
背景:单台elasticsearch提供服务,往往都有最大的负载能力,超过这个阈值,服务器性能就会大大降低甚至不可用,所以生成环境中,一般都是运行在指定服务器集群中。
集群cluster:一个集群就是由一个或者多个服务器节点组织在一起,共同持有整个数据,并一起提供索引和搜索服务。一个Elasticsearch集群有一个唯一的名字标识,整个名字默认就是elasticsearch。整个名字很重要,因为一个节点只能通过指定某个集群的名字来加入集群。
节点Node:集群中包含很多的服务器,一个节点就是其中的一个服务器。作为集群的一部分,它存储数据,参与集群的索引和搜索功能。一个节点也是由一个表示来决定的,一个节点可以通过配置集群名称的方式来加入一个指定的集群
(1)创建一个elasticsearch-cluster的文件件,把elasticsearch服务文件拷贝三份进去,删除data和logs目录下的文件,分别命名为node-1001、node-1002、node-1003,作为三个节点服务
(2)修改集群文件目录中每个节点的config/elasticsearch.yml文件
node-1001节点配置
#集群名称,节点之间要保持一致
cluster.name: my-application
#节点名称,集群内要保持唯一
node.name: node-1001
#可以作为master节点
node.master: true
#存储数据
node.data: true
#ip地址
network.host: localhost
#http端口
http.port: 1001
#tcp监听端口,节点之间内部通信使用
transport.tcp.port: 9301
#节点之间相互通信的主机地址
#discovery.seed_hosts: ["host1", "host2"]
#discovery.zen.fd.ping_timeout:1m
#discovery.zen.fd.ping_retries:5
#跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"
此时从bin/elasticsearch.bat启动node-1001,控制服务台打印出节点名称、集群名称
使用postman查看节点的启动状态(启动)和节点数(1个)
node-1002节点配置
#集群名称,节点之间要保持一致
cluster.name: my-application
#节点名称,集群内要保持唯一
node.name: node-1002
#可以作为master节点
node.master: true
#存储数据
node.data: true
#ip地址
network.host: localhost
#http端口
http.port: 1002
#tcp监听端口,节点之间内部通信使用
transport.tcp.port: 9302
#节点之间相互通信的主机地址-查找主节点
discovery.seed_hosts: ["localhost:9301"]
#执行查找ping命令的超时时间
discovery.zen.fd.ping_timeout: 1m
discovery.zen.fd.ping_retries: 5
#跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"
此时从bin/elasticsearch.bat启动node-1002,控制服务台打印出节点名称、集群名称
使用postman查看节点的启动状态(启动)和节点数(2个)
node-1003节点配置
#集群名称,节点之间要保持一致
cluster.name: my-application
#节点名称,集群内要保持唯一
node.name: node-1003
#可以作为master节点
node.master: true
#存储数据
node.data: true
#ip地址
network.host: localhost
#http端口
http.port: 1003
#tcp监听端口,节点之间内部通信使用
transport.tcp.port: 9303
#节点之间相互通信的主机地址-查找主节点
discovery.seed_hosts: ["localhost:9301","localhost:9302"]
#执行查找ping命令的超时时间
discovery.zen.fd.ping_timeout: 1m
discovery.zen.fd.ping_retries: 5
#跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"
此时从bin/elasticsearch.bat启动node-1003,控制服务台打印出节点名称、集群名称
使用postman查看节点的启动状态(启动)和节点数(3个)