es的基本操作

环境搭建参考: https://blog.csdn.net/weixin_45647685/article/details/123242577  ,启动dos,然后导入

 

 

 

Elasticsearch
Elasticsearch 基于java,是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引
副本机制,restful风格接口,多数据源,自动搜索负载等。
Logstash
Logstash 基于java,是一个开源的用于收集,分析和存储日志的工具。
Kibana
Kibana 基于nodejs,也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的
Web 界面,可以汇总、分析和搜索重要数据日志。
Beats
Beats是elastic公司开源的一款采集系统监控数据的代理agent,是在被监控服务器上以客户端形式运行的数据收集
器的统称,可以直接把数据发送给Elasticsearch或者通过Logstash发送给Elasticsearch,然后进行后续的数据分析活
动。
Beats由如下组成:
Packetbeat:是一个网络数据包分析器,用于监控、收集网络流量信息,Packetbeat嗅探服务器之间的流量,
解析应用层协议,并关联到消息的处理,其支 持ICMP (v4 and v6)、DNS、HTTP、Mysql、PostgreSQL、
Redis、MongoDB、Memcache等协议;
Filebeat:用于监控、收集服务器日志文件,其已取代 logstash forwarder;
Metricbeat:可定期获取外部系统的监控指标信息,其可以监控、收集 Apache、HAProxy、MongoDB
MySQL、Nginx、PostgreSQL、Redis、System、Zookeeper等服务;

 

创建非结构化索引
在Lucene中,创建索引是需要定义字段名称以及字段的类型的,在Elasticsearch中提供了非结构化的索引,就是不
需要创建索引结构,即可写入数据到索引中,实际上在Elasticsearch底层会进行结构化操作,此操作对用户是透明
的。
创建空索引:

 

RESTful API
在Elasticsearch中,提供了功能丰富的RESTful API的操作,包括基本的CRUD、创建索引、删除索引等操作。
 
PUT /haoke
{
"settings": {
"index": {
"number_of_shards": "2", #分片数
"number_of_replicas": "0" #副本数
}
}
}
#删除索引
DELETE /haoke
{
"acknowledged": true
}
 
插入数据
URL规则:
POST /{索引}/{类型}/{id}

 

 

POST /haoke/user/1001
#数据
{
"id":1001,
"name":"张三",
"age":20,
"sex":"男"
}
#响应
{
"_index": "haoke",
"_type": "user",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
 
不指定具体的id : 
 
POST /haoke/user/
{
"id":1002,
"name":"张三",
"age":20,
"sex":"男"
}

 

更新数据后: 

PUT /haoke/user/1001
{
"id":1001,
"name":"张三",
"age":21,
"sex":"女"

 

 

 

 

可以看到数据已经被覆盖了。
问题来了,可以局部更新吗?
-- 可以的。
前面不是说,文档数据不能更新吗?
其实是这样的:
在内部,依然会查询到这个文档数据,然后进行覆盖操作,步骤如下:
1. 从旧文档中检索JSON
2. 修改它
3. 删除旧文档
4. 索引新文档
#注意:这里多了_update标识
POST /haoke/user/1001/_update
{
"doc":{
"age":23
}
 
示例: 
#注意:这里多了_update标识
POST /haoke/user/1001/_update
{
"doc":{
"age":23
}
}
 

 

 

 

 

删除数据
在Elasticsearch中,删除文档数据,只需要发起DELETE请求即可。
DELETE /haoke/user/1001 
 
搜索数据
 
GET /haoke/user/BbPe_WcB9cFOnF3uebvr
#返回的数据如下
{
"_index": "haoke",
"_type": "user",
"_id": "BbPe_WcB9cFOnF3uebvr",
"_version": 8,
"found": true,
"_source": { #原始数据在这里
"id": 1002, 
"name": "李四",
"age": 40,
"sex": "男"
}
 
GET /haoke/user/_search
 
{
"took": 26,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "haoke",
"_type": "user",
"_id": "BbPe_WcB9cFOnF3uebvr",
"_score": 1,
"_source": {
"id": 1002,
"name": "李四",
"age": 40,
"sex": "男"
}
},
{
"_index": "haoke",
"_type": "user",
"_id": "1001",
"_score": 1,
"_source": {
"id": 1001,
"name": "张三",
"age": 20,
"sex": "男"
}
},
{
"_index": "haoke",
"_type": "user",
"_id": "1003",
"_score": 1,
"_source": {
"id": 1003,
"name": "王五",
"age": 30,
"sex": "男"
}
},
{
"_index": "haoke",
"_type": "user",
"_id": "1004",
"_score": 1,
"_source": {
"id": 1004,
"name": "赵六",
"age": 30,
"sex": "男"
}
}
]
}
}
 
#查询年龄等于20的用户
GET /haoke/user/_search?q=age:20
 
 
 

具体代码实现:
package com.tm.service.impl;

import com.alibaba.fastjson.JSONObject;

import com.tm.config.config;

import com.tm.mapper.EsSyncGoodsSpuMapper;

import com.tm.model.entity.EsSyncGoodsSpuEntity;

import org.elasticsearch.action.bulk.BulkRequest;

import org.elasticsearch.action.index.IndexRequest;

import org.elasticsearch.client.RestHighLevelClient;

import org.elasticsearch.common.xcontent.XContentType;

import org.springframework.context.annotation.Configuration;

import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.List;

/**
* @author q请问请问q
* @createTime 2021年11月25日 18:20:00
*/

//EnableScheduling开启定时器
//Component让spring能够扫描到当前类

@EnableScheduling

@Component

public class EsSyncGoodsSpuServiceImpl {
//注入一下

@Resource

private EsSyncGoodsSpuMapper esSyncGoodsSpuMapper;

//这个定时器注解就是每过去5秒执行一下这个aaa的方法

@Scheduled(cron = "0/5 * * * * ?")

public void aaa() {
//这边是查询mysql的数据

List<EsSyncGoodsSpuEntity> list = esSyncGoodsSpuMapper.aaa();

//调用高层对象

RestHighLevelClient restHighLevelClient = config.esRestClient();

//然后我这边使用forEach循环将数据添加到es中

list.forEach(a -> {
//创建一个索引请求(这里面写的是我们想要添加的索引)

IndexRequest index = new IndexRequest("goods_spu");

//这边是获取到我们查询得到的数据将这个查询的id当成我们es中_id(不要es自带的)

index.id(a.getSpuId().toString());

//创建批量操作对象

BulkRequest request = new BulkRequest();

//这里我将查询到的数据循环转换成json

index.source(JSONObject.toJSONString(a), XContentType.JSON);

//将转换成json的数据添加到我们创建的对象中去

request.add(index);

try {
//将数据通过bulk操作进入es。。

restHighLevelClient.bulk(request, config.COMMON_OPTIONS);

} catch (Exception e) {
e.printStackTrace();

}

//打印......

System.out.println(list);

});

}

}

添加索引:  put的方式 ,加上索引名称: 

http://192.168.119.128:9200/goods_search  method : put  

响应: 

{
"acknowledged": true,
"shards_acknowledged": true,
"index": "goods_search"
}

查询: http://192.168.119.128:9200/goods_search,goods_search1  method: get 

响应: 

       

 

 _all查询所有的key 

_close 关闭一个主键  

_open 打开一个主键

删除一个主键 : 

 

 

 

 

映射操作 : 

 

 

 

添加映射: 

put person/_mapping
{
"properties": {
"name":{
"type" :"keyword"
},
"age":{
"type":"integer"
 }
 } 
}

 

 

 添加文档: 

 文档需要跟id

 

 

 post提交可以不指定id,put必须指定id

 

 

springboot 整合 ES : 

 

java封装的配置类:

package com.elasticsearch.hm.elasticsearch.config;


import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticsearchConfig {

private String host ;
private String port ;

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public String getPort() {
return port;
}

public void setPort(String port) {
this.port = port;
}

@Bean
public RestHighLevelClient clientss(){
return new RestHighLevelClient(RestClient.builder(
new HttpHost(
getHost() ,
Integer.parseInt(getPort()),
"http"
))) ;
}

}

测试类:
@Autowired
private RestHighLevelClient clientss ;

@Test
void contextLoads() {

System.out.printf(""+clientss);
}

@Test
public void addIndex() throws IOException {

   //创建索引,映射 

IndicesClient indices = clientss.indices();

CreateIndexRequest indexRequest = new CreateIndexRequest("itmage");

CreateIndexResponse createIndexResponse = indices.create(indexRequest, RequestOptions.DEFAULT);
System.out.printf(String.valueOf(createIndexResponse.isShardsAcknowledged()));

}


批量操作


 

 






 

posted @ 2022-06-23 09:46  迈克尔-唐-僧  阅读(105)  评论(0)    收藏  举报