JDK1.8 以上
ElasticSearch下载地址:ElasticSearch下载地址
elasticsearch-head下载地址:elasticsearch-head
kibana下载地址:kibana下载地址
IK分词器插件:IK分词器插件
以上版本要一致
解压即可
注意事项
默认启动大小为 IG运行内存
根据自级的电脑可以配置
如果是虚拟机请配置小一点
如果阿里云服务器配置低的话也需要配置小一点
bin 下的 elasticsearch.bat 双击即可
127.0.0.1:9200
下载地址:elasticsearch-head
安装依赖
cnpm install
npm run start
访问:127.0.0.1:9100
访问不了…
elasticsearch.yml
// 添加
http.cors.enabled: true
http.cors.allow-origin: "*"
ELK是Elasticsearch. Logstash. Kibana三大开源框架首字母大写简称。市面上也被成为Elastic Stack。其中Elasticsearch是- 一个基于Lucene、分布式、通过Restful方式进行交互的近实时搜索平台框架。像类似百度、谷歌这种大数据全文搜索引擎的场景都可以使用Elasticsearch作为底层支持框架,可见Elasticsearch提供的搜索能力确实强大,市面上很多时候我们简称Elasticsearch为es.
Logstash是ELK的中央数据流引擎,用于从不同日标(文件/数据存储/MQ )收集的不同格式数据,经过过滤后支持输出到不同目的
地(文件/MQ/redis/elasticsearch/kafka等)。Kibana可以将elasticsearch的数据通过友好的页面展示出来 ,提供实时分析的功
能。
市面上很多开发只要提到ELK能够一致说出它是一 个日志分析架构技术栈总称 ,但实际上ELK不仅仅适用于日志分析,它还可以支持其它任何数据分析和收集的场景,日志分析和收集只是更具有代表性。并非唯一性。
kibana下载地址:kibana下载地址
启动
bin 下的 kibana.bat 双击使用
访问:
127.0.0.1:5601
i18n.locale: "zh-CN"
IK分词器插件:IK分词器插件
method | url地址 | 描述 |
---|---|---|
PUT | localhost:9200/索引名称/类型名称/文档id | 创建文档(指定文档id ) |
POST | localhost:9200/索引名称/类型名称 | 创建文档(随机文档id ) |
POST | localhost:9200/索引名称/类型名称/文档id/_ update | 修改文档 |
DELETE | localhost:9200/索引名称/类型名称/文档id | 删除文档 |
GET | localhost:9200/索引名称/类型名称/文档id | 查询文档通过文档id |
POST | localhost:9200/索引名称/类型名称/ search | 查询所有文档 |
// put /索引名/类型名/文档ID {请求体}
PUT /test1/type1/1
{
"name": "古力娜扎",
"age":26
}
// 索引字段类型设置
PUT /test2
{
"mappings":{
"properties":{
"name":{
"type":"text"
},
"age":{
"type":"long"
}
}
}
}
GET test2
//1. 直接put 相同的ID 版本号增加
//2. update
UPDATE /test1/type1/1/_update
{
"doc"{
"name": "古力娜扎",
"age":30
}
}
DELETE test1
但是springboot默认的ES版本依赖和自己的ES不一致
修改
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artificatId>elasticsearch-rest-high-level-client</artificatId>
<version>7.6.2</version>
</dependency>
ElasticSearchConfig.java
public class ElasticSearchConfig{
@Bean
public RestHighLevelClient restHighLevelClient (){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost",9200,"http")));
return client;
}
}
增删改查-----测试类
@Autowired
private RestHighLevelClient client;
// 1.创建索引
void testCreateIndex(){
CreateIndexRequest request = new CreateIndexRequest("test_index");
CreateIndexResponse response = client.indices().creatre(request,RequestOptions.DEFAULT);
System.out.println(response);
// 2.获取索引信息
void testGetIndex(){
GetIndexRequest request = new GetIndexRequest("test_index");
boolean b = client.indicies.exists(request,RequestOptions.DEFAULT);
System.out.println(b);
}
// 3. 删除索引
void testDeleteIndex(){
DeleteIndexRequest request = new DeleteIndexRequest("test_index");
AcknowledgeResponse delete = client.indicies.delete(request,RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledge());
}
}
创建实体类
@Component
@AllArgsConstrator
@NoArgsConstrator
public class User{
private String name;
private int age;
}
测试类
// 4.添加文档
void testAddDoc(){
User user = new User("古力娜扎",26);
IndexRequest request = new IndexRequest("test_index");
// 规则 put /test_index/_doc/1
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1));
request.tomeout("1s");
request.source(JSON.toJSONString(user),XContentType.JSON);
// 发送
IndexResponse response = client.index(request,RequestOptions.DEFAULT);
System.out.println(response.toString());
System.out.println(response.status());
}
// 5.获取文档
void testGetDoc(){
GetRequest request = new GetRequest("test_index","1");
request.fetchSourceContext(new FetchSourceContext(false));
request.storeFields("_none_");
// 是否存在???
boolean b = client.exists(request,RequestOptions.DEFAULT);
System.out.println(b);
// 获取
GetResponse response = client.get(request,RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
System.out.println(response);
}
// 6. 更新文档
void testUpdateDoc(){
UpdateRequest updateRequest = new UpdateRequest("test_index",1);
updateRequest.timeout("1s");
User user = new User("迪丽热巴",28);
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse response = client.update(request,RequestOptions.DEFAULT);
System.out.println(response);
System.out.println(response.status());
}
// 7.删除文档
void testDeleteDoc(){
DeleteRequest request = new DeleteRequest("test_index",1);
updateRequest.timeout("1s");
DeleteResponse response = client.delete(request,RequestOptions.DEFAULT);
System.out.println(response);
System.out.println(response.status());
}
// 8.批量插入数据
void testBulkRequest(){
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("15s");
ArrayList<User> userList = new ArrayList<>();
userList.add(new User("古力娜扎1号",26));
userList.add(new User("古力娜扎2号",27));
userList.add(new User("古力娜扎3号",28));
userList.add(new User("古力娜扎4号",29));
userList.add(new User("古力娜扎5号",30));
userList.add(new User("古力娜扎6号",31));
userList.add(new User("古力娜扎7号",32));
for(int i = 0;i<=userList.size();i++){
bulkRequest.add(
new IndexRequest("test.index").
id(""+(i+1)).
source(JSON.toJSONString(userList.get(i)),XContentType.JSON));
}
// 批量插入
BulkResponse response = client.bulk(bulkRequest,RequestOptions.DEFAULT);
System.out.println(bulkRequest.hasFailures());
}
// 9.条件搜索
void testSearch(){
SearchRequest searchRequest = new SearchRequest("test_index");
// 构造条件
SearchSourceBuilder builder = new SearchSourceBuilder();
// 精确匹配
//TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name","古力娜扎");
// 匹配全部 matchAllQuery();
builder.query(termQueryBuilder);
// 分页
builder.from();
builder.size();
builder.timeout("60s");
searchRequest.source(builder);
SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(response.getHits()));
//遍历出来
for(SearchHit docFields : searchRequest.getHits().getHits()){
System.out.println(docFields.getSourceAsMap());
}
}