介绍:
Elasticsearch是一个开源的分布式搜索和分析引擎,最初由Elastic公司开发。它构建在Apache Lucene搜索引擎库之上,提供了一个强大的全文搜索和分析引擎, 它结合kibana、Logstash、Beats,是一整套技术栈,被叫做ELK,适用于各种用例,包括文本搜索、日志分析、实时数据分析、监控和报警等。

官网:
官网地址:Elastic — The Search AI Company | Elastic,目前最新的版本是8.x.x,国内大多使用6.x.x和7.x.x。
优势:
elasticsearch具备以下优势:
·支持分布式。可水平拓展
·提供Restful接口,可被任何语言调用
es在处理海量数据搜索时,速度非常的快,是因为它底层采用倒排索引。
★倒排索引:
首先介绍一下正向索引:

倒排索引:

总结:

IK分词器:


上述配置文件即表示添加扩展词典ext.dic,它就会在当前配置文件所在的目录中找这个文件。
总结:

基础概念:

与MySQL对比:

索引库操作
Mapping映射属性:

索引库操作:



总结:

文档处理:
CRUD:
新增:

查找、删除:

修改:
全量修改:

这种方式在文档id不存在时,就会相当于一个新增操作。
增量修改:

批量处理:

JavaRestClient:

客户端初始化:

商品表Mapping映射:
以商品表举例:

在kibana中写出即为:
PUT /items
{
"mappings": {
"properties": {
"id": {
"type":"keyword"
},
"name":{
"type": "text",
"analyzer": "ik_smart"
},
"price":{
"type": "integer"
},
"image":{
"type": "keyword",
"index": false
},
"category":{
"type": "keyword"
},
"brand":{
"type": "keyword"
},
"sold":{
"type": "integer"
},
"comment_count":{
"type": "integer",
"index": false
},
"isAD":{
"type": "boolean"
},
"update_time":{
"type": "date"
}
}
索引库操作:
创建索引库的JavaAPI与Restful接口API对比:


操作步骤:

代码演示:
class ItemTest {
private RestHighLevelClient client;
@BeforeEach
void setUp() {
// 初始化 RestHighLevelClient 对象
client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.178.130:9200")
));
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
@Test
void testCreateIndex() throws IOException {
//1.准备Request对象
CreateIndexRequest request = new CreateIndexRequest("items");
//2.准备请求参数
request.source(MAPPING_TEMPLATE, XContentType.JSON);
//3.发送请求
client.indices().create(request, RequestOptions.DEFAULT);
}
@Test
void testGetIndex() throws IOException {
//1.准备Request对象
GetIndexRequest request = new GetIndexRequest("items");
//2.发送请求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println("exists: " + exists);
}
@Test
void testDeleteIndex() throws IOException {
//1.准备Request对象
DeleteIndexRequest request = new DeleteIndexRequest("items");
//2.发送请求
client.indices().delete(request, RequestOptions.DEFAULT);
}
private static final String MAPPING_TEMPLATE = "{\n" +
" \"mappings\": {\n" +
" \"properties\": {\n" +
" \"id\": {\n" +
" \"type\":\"keyword\"\n" +
" },\n" +
" \"name\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_smart\"\n" +
" },\n" +
" \"price\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"image\":{\n" +
" \"type\": \"keyword\", \n" +
" \"index\": false\n" +
" },\n" +
" \"category\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"brand\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"sold\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"comment_count\":{\n" +
" \"type\": \"integer\", \n" +
" \"index\": false\n" +
" },\n" +
" \"isAD\":{\n" +
" \"type\": \"boolean\"\n" +
" },\n" +
" \"update_time\":{\n" +
" \"type\": \"date\"\n" +
" }\n" +
"}\n" +
"}\n" +
"}";
}
文档操作:
新增文档:

运行代码:
@SpringBootTest(properties = "spring.profiles.active=local")
class ESDocTest {
private RestHighLevelClient client;
@Autowired
private IItemService itemService;
@BeforeEach
void setUp() {
// 初始化 RestHighLevelClient 对象
client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.178.130:9200")
));
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
@Test
void testIndexDoc() throws IOException {
//获取数据
Item item = itemService.getById(317578L);
ItemDoc itemDoc = BeanUtil.copyProperties(item, ItemDoc.class);
//创建request对象
IndexRequest request = new IndexRequest("item").id(itemDoc.getId());
//准备JSON文档
request.source(JSONUtil.toJsonStr(itemDoc), XContentType.JSON);
//发送请求
client.index(request, RequestOptions.DEFAULT);
}
}
删除文档:

查询文档:

修改文档:

全量更新:可以使用新增文档的代码,在得到ItemDoc后修改它的属性值在新增即可。
局部更新:

文档操作基本步骤:

批处理:

代码示例:

DSL查询:


快速入门:

注意:单次查询默认最大数据数为10000,最多返回10条数据
叶子查询:

全文检索:
FIELD为要搜索的字段,TEXT为要搜索的内容

精确查询:

term查询一般用来搜不分词的字段,比如品牌等。如果搜分词的字段,VALUE只能写分好的词条,比如“脱脂”、“牛奶”等,才能搜到
range查询中gte和lte也可以写成gt和lt这样就是大于和小于。
ids查询:(批量查询id)

总结:

复合查询:

布尔查询:

示例:
搜索“智能手机”,但品牌必须是华为,价格必须是900~1599

排序和分页:
排序:

示例:
搜索商品,按照销量排序,销量一样则按照价格排序。

分页:

示例:
搜索商品,查询出销量排名前10的商品,销量一样时按照价格升序。

深度分页问题:

解决方案:

高亮显示:

(↑标签默认就为em)
搜索完整语法:

JavaRestClient查询:
快速入门:


构建查询条件:

全文检索查询:

精确查询:

布尔查询:

排序和分页:

高亮显示:


聚合:
聚合的分类:

DSL实现聚合:



Java客户端实现聚合:
构造请求参数:

解析结果:



















