**1. 查询一个索引的所有文档**
```java
/**
* 查询一个索引的所有文档
*/
@Test
public void matchAllQuery() throws IOException {
//获取客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
SearchRequest request = new SearchRequest();
//设置要查询的索引
request.indices("student");
//构建SearchSourceBuilder
SearchSourceBuilder builder = new SearchSourceBuilder();
//查询所有数据
builder.query(QueryBuilders.matchAllQuery());
request.source(builder);
//发送请求并获取响应
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
log.info(hit.getSourceAsString());
}
//{"name":"zhangsan","nickname":"zhangsan","sex":"男","age":30}
//{"name":"lisi","nickname":"lisi","sex":"男","age":20}
//{"name":"wangwu","nickname":"wangwu","sex":"女","age":40}
//{"name":"zhangsan01","nickname":"zhangsan01","sex":"女","age":50}
//{"name":"zhangsan02","nickname":"zhangsan02","sex":"女","age":30}
client.close(); //关闭连接
}
```
**2. 关键字精准查询**
```java
/**
* 关键字精准查询。
* term 查询,精确的关键词匹配查询,不对查询条件进行分词。
*/
@Test
public void termQuery() throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
SearchRequest request = new SearchRequest();
//设置要查询的索引
request.indices("student");
//SearchSourceBuilder
SearchSourceBuilder builder = new SearchSourceBuilder();
//查询条件:age=30
builder.query(QueryBuilders.termQuery("age", "30"));
request.source(builder);
//发送请求并获取响应
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
log.info(hit.getSourceAsString());
}
//{"name":"zhangsan","nickname":"zhangsan","sex":"男","age":30}
//{"name":"zhangsan02","nickname":"zhangsan02","sex":"女","age":30}
client.close(); //关闭连接
}
```
**3. 分页查询**
```java
/**
* 分页查询
*/
@Test
public void pageQuery() throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
SearchRequest request = new SearchRequest();
//设置要查询的索引
request.indices("student");
//构建SearchSourceBuilder
SearchSourceBuilder builder = new SearchSourceBuilder();
//查询所有文档
builder.query(QueryBuilders.matchAllQuery());
//构建分页查询
//从第1页开始(第一页默认从0开始), from=(pageNum−1)∗size, pageNum为当前第几页
builder.from(0);
//每页显示2条数据
builder.size(2);
request.source(builder);
//发送请求并获取响应
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
log.info(hit.getSourceAsString());
}
//{"name":"zhangsan","nickname":"zhangsan","sex":"男","age":30}
//{"name":"lisi","nickname":"lisi","sex":"男","age":20}
client.close();
}
```
**4. 排序**
```java
/**
* 排序
*/
@Test
public void orderQuery() throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//构建查询request
SearchRequest request = new SearchRequest();
//设置要查询的索引
request.indices("student");
//构建查询体
SearchSourceBuilder builder = new SearchSourceBuilder();
//查询所有
builder.query(QueryBuilders.matchAllQuery());
//排序,升序排序
builder.sort("age", SortOrder.ASC);
request.source(builder);
//发送请求并获取响应
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
log.info(hit.getSourceAsString());
}
//{"name":"lisi","nickname":"lisi","sex":"男","age":20}
//{"name":"zhangsan","nickname":"zhangsan","sex":"男","age":30}
//{"name":"zhangsan02","nickname":"zhangsan02","sex":"女","age":30}
//{"name":"wangwu","nickname":"wangwu","sex":"女","age":40}
//{"name":"zhangsan01","nickname":"zhangsan01","sex":"女","age":50}
client.close();
}
```
**5. 过滤字段**
```java
/**
* 过滤字段
*/
@Test
public void filterQuery() throws IOException {
//获取客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//构建查询request
SearchRequest request = new SearchRequest();
//设置要查询的索引
request.indices("student");
//构建查询体
SearchSourceBuilder builder = new SearchSourceBuilder();
//查询条件:查询所有
builder.query(QueryBuilders.matchAllQuery());
//查询过滤字段
String[] excludes = {}; //不查询的字段
String[] includes = {"name", "age"}; //只查询的字段
builder.fetchSource(includes, excludes);
request.source(builder);
//发送请求并获取响应
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
log.info(hit.getSourceAsString());
}
//{"name":"zhangsan","age":30}
//{"name":"lisi","age":20}
//{"name":"wangwu","age":40}
//{"name":"zhangsan01","age":50}
//{"name":"zhangsan02","age":30}
client.close();
}
```
**6. 组合查询**
```java
/**
* 组合查询
* bool把各种其它查询通过must(必须 )、must_not(必须不)、should(应该)的方式进行组合。
*/
@Test
public void boolQuery() throws IOException {
//获取客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//构建搜索请求对象
SearchRequest request = new SearchRequest();
//设置要查询的索引
request.indices("student");
//构建查询体
SearchSourceBuilder builder = new SearchSourceBuilder();
//构建bool查询体
BoolQueryBuilder boolBuilder = new BoolQueryBuilder();
//条件1:必须age=30
boolBuilder.must(QueryBuilders.matchQuery("age", "30"));
//条件2:必须name!=zhangsan
boolBuilder.mustNot(QueryBuilders.matchQuery("name", "zhangsan"));
//条件3:应该sex=男
boolBuilder.should(QueryBuilders.matchQuery("sex", "男"));
builder.query(boolBuilder);
request.source(builder);
//发送请求并获取响应
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
log.info(hit.getSourceAsString());
}
//{"name":"zhangsan02","nickname":"zhangsan02","sex":"女","age":30}
}
```
**7. 范围查询**
```java
/**
* 范围查询
* gt 大于(>)
* gte 大于等于(>=)
* lt 小于(<)
* lte 小于等于(<=)
*/
@Test
public void rangeQuery() throws IOException {
//获取客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//构建搜索请求对象
SearchRequest request = new SearchRequest();
//设置要查询的索引
request.indices("student");
//构建查询体
SearchSourceBuilder builder = new SearchSourceBuilder();
//构建范围查询体
RangeQueryBuilder rangeBuilder = QueryBuilders.rangeQuery("age");
rangeBuilder.gte("30"); //age大于等于30
rangeBuilder.lte("40"); //age小于等于40
builder.query(rangeBuilder);
request.source(builder);
//发送请求并获取响应
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
log.info(hit.getSourceAsString());
}
//{"name":"zhangsan","nickname":"zhangsan","sex":"男","age":30}
//{"name":"wangwu","nickname":"wangwu","sex":"女","age":40}
//{"name":"zhangsan02","nickname":"zhangsan02","sex":"女","age":30}
}
```
**8. 模糊查询**
```java
/**
* 模糊查询:返回包含与搜索字词相似的字词的文档。
* 1)编辑距离是将一个术语转换为另一个术语所需的一个字符更改的次数。这些更改可以包括:
* * 1、更改字符(box → fox)
* * 2、删除字符(black → lack)
* * 3、插入字符(sic → sick)
* * 4、转置两个相邻字符(act → cat)
* 为了找到相似的术语,`fuzzy` 查询会在指定的编辑距离内创建一组搜索词的所有可能的变体或扩展。然后查询返回每个扩展的完全匹配。
* 通过 `fuzziness` 修改编辑距离。一般使用默认值 AUTO,根据术语的长度生成编辑距离。
*/
@Test
public void fuzzyQuery() throws IOException {
//获取客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//构建搜索请求对象
SearchRequest request = new SearchRequest();
//设置要查询的索引
request.indices("student");
//构建查询体
SearchSourceBuilder builder = new SearchSourceBuilder();
//模糊查询
builder.query(QueryBuilders
.fuzzyQuery("name", "zhangsan")
.fuzziness(Fuzziness.AUTO));
request.source(builder);
//发送请求并获取响应
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
log.info(hit.getSourceAsString());
}
//{"name":"zhangsan","nickname":"zhangsan","sex":"男","age":30}
//{"name":"zhangsan01","nickname":"zhangsan01","sex":"女","age":50}
//{"name":"zhangsan02","nickname":"zhangsan02","sex":"女","age":30}
}
```
**9. 高亮查询**
```java
/**
* 高亮查询
*/
@Test
public void highlightQuery() throws IOException {
//获取客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
SearchRequest request = new SearchRequest();
//设置要查询的索引
request.indices("student");
//构建SearchSourceBuilder
SearchSourceBuilder builder = new SearchSourceBuilder();
//查询name=zhangsan
TermsQueryBuilder termsBuilder = QueryBuilders.termsQuery("name", "zhangsan");
builder.query(termsBuilder);
//构建高亮字段
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags("<font color='red'>"); //前置标签
highlightBuilder.postTags("</font>"); //后置标签
highlightBuilder.field("name"); //要高亮的字段
builder.highlighter(highlightBuilder);
request.source(builder);
//发送请求并获取响应
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
log.info(hit.getSourceAsString());
Map<String, HighlightField> highlightFieldMap = hit.getHighlightFields();
log.info("高亮结果:{}", highlightFieldMap);
}
//{"name":"zhangsan","nickname":"zhangsan","sex":"男","age":30}
//高亮结果:{name=[name], fragments[[<font color='red'>zhangsan</font>]]}
}
```
**10. 聚合查询**
```java
/**
* 聚合查询
*/
@Test
public void aggsQuery() throws IOException {
//获取客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
SearchRequest request = new SearchRequest();
//设置要查询的索引
request.indices("student");
SearchSourceBuilder builder = new SearchSourceBuilder();
//求最大值
//searchSourceBuilder.aggregation(AggregationBuilders.max("max_age").field("age"));
//最小值
//searchSourceBuilder.aggregation(AggregationBuilders.min("min_age").field("age"));
//stats可以一次性将max/min/avg/count/sum求出
builder.aggregation(AggregationBuilders.stats("stats_age").field("age"));
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//...,"aggregations":{"stats#stats_age":{"count":5,"min":20.0,"max":50.0,"avg":34.0,"sum":170.0}}}
log.info("{}", response);
client.close();
}
```
**11. 分组统计**
```java
/**
* 分组统计
*/
@Test
public void groupbyQuery() throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
SearchRequest request = new SearchRequest();
request.indices("student"); //设置要搜索的索引
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.aggregation(AggregationBuilders.terms("age_groupby").field("age"));
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//...
// {"key":30,"doc_count":2},
// {"key":20,"doc_count":1},
// {"key":40,"doc_count":1},
// {"key":50,"doc_count":1}]}}}
log.info("{}", response);
}
```
- Elasticsearch是什么
- 全文搜索引擎
- Elasticsearch与Solr
- 数据结构
- 安装Elasticsearch
- Linux单机安装
- Windows单机安装
- 安装Kibana
- Linux安装
- Windows安装
- es基本语句
- 索引操作
- 文档操作
- 映射操作
- 高级查询
- es-JavaAPI
- maven依赖
- 索引操作
- 文档操作
- 高级查询
- es集群搭建
- Linux集群搭建
- Windows集群搭建
- 核心概念
- 索引(Index)
- 类型(Type)
- 文档(Document)
- 字段(Field)
- 映射(Mapping)
- 分片(Shards)
- 副本(Replicas)
- 分配(Allocation)
- 系统架构
- 分布式集群
- 单节点集群
- 故障转移
- 水平扩容
- 应对故障
- 路由计算
- 分片控制
- 写流程
- 读流程
- 更新流程
- 多文档操作流程
- 分片原理
- 倒排索引
- 文档搜索
- 动态更新索引
- 近实时搜索
- 持久化变更
- 段合并
- 文档分析
- 内置分析器
- 分析器使用场景
- 测试分析器
- 指定分析器
- 自定义分析器
- 文档处理
- 文档冲突
- 乐观并发控制
- 外部系统版本控制
- es优化
- 硬件选择
- 分片策略
- 合理设置分片数
- 推迟分片分配
- 路由选择
- 写入速度优化
- 批量数据提交
- 优化存储设备
- 合理使用合并
- 减少Refresh的次数
- 加大Flush设置
- 减少副本的数量
- 内存设置
- 重要配置
- es常见问题
- 为什么要使用Elasticsearch
- master选举流程
- 集群脑裂问题
- 索引文档流程
- 更新和删除文档流程
- 搜索流程
- ES部署在Linux时的优化方法
- GC方面ES需要注意的点
- ES对大数据量的聚合实现
- 并发时保证读写一致性
- 字典树
- ES的倒排索引
- Spring Data Elasticsearch
- 环境搭建
- 索引操作
- 文档操作