概述
ElasticSearch(简称为es)是一个开源的高扩展的分布式全文检索引擎,它可以近乎 实时存储、检索数据;
本身扩展性很好,可以扩展到上百台服务器,处理PB级别(大数据时代)的数据。es是使用java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的REST-ful的API来隐藏Lucene的复杂性,从而让全文搜索变得简单。
下载地址
ElasticSearch Kibana 安装 https://www.elastic.co/cn
es可视化界面:head的插件 https://github.com/mobz/elasticsearch-head/
启动head插件,访问9100端口
# 安装cnpm的命令
npm install -g cnpm -registry= https://registry.npm.taobao.org
# cnpm下载资源更快
cnpm install
npm run start
IK分词器插件 https://github.com/medcl/elasticsearch-analysis-ik/releases
字段类型
官网类型文档地址: https://www.elastic.co/guide/en/elasticsearch/reference/current/keyword.html
- 字符串类型
text、keyword
- 数值类型
long,integer,short,byte,double,float,half float,scaled float
- 日期类型
date
- te布尔值类型
boolean·
- 二进制类型
binary
- 等等……
集成Springboot
<!--整合es--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <!--es--> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.6.1</version> </dependency> <!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.80</version> </dependency>
配置类
@Configuration public class ElasticSearchConfig { @Bean public RestHighLevelClient restHighLevelClient(){ RestHighLevelClient client=new RestHighLevelClient( RestClient.builder(new HttpHost("127.0.0.1",9200,"http")) ); return client; } }
创建索引
CreateIndexRequest index = new CreateIndexRequest("index"); CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(index, RequestOptions.DEFAULT);
添加文档
User user = new User("学习", 25); IndexRequest index = new IndexRequest("index"); index.id("1"); index.timeout(TimeValue.timeValueSeconds(1)); index.source(JSON.toJSONString(user), XContentType.JSON); IndexResponse indexResponse = restHighLevelClient.index(index, RequestOptions.DEFAULT); System.out.println(indexResponse.toString());
获取文档的信息
GetRequest getRequest = new GetRequest("index","1"); GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT); System.out.println(getResponse.getSourceAsString());
批量插入数据
BulkRequest bulkRequest = new BulkRequest(); bulkRequest.timeout("1s"); ArrayList<User> users = new ArrayList<>(); users.add(new User("yu",3)); users.add(new User("hello",3)); for (int i = 0; i < users.size(); i++) { bulkRequest.add(new IndexRequest("index") .id(""+(i+1))// 不设置id,则会生成默认id .source(JSON.toJSONString(users.get(i)),XContentType.JSON)); } BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulkResponse.hasFailures());// 是否失败,返回false代表成功
查询
SearchRequest searchRequest=new SearchRequest("index"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "狂神"); searchSourceBuilder.query(matchQueryBuilder); searchRequest.source(searchSourceBuilder); SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); for(SearchHit searchHit:search.getHits().getHits()){ System.out.println(searchHit.getSourceAsMap()); }
注:本文为学习狂神ElasticSearch视频所做的笔记,详细链接https://www.kuangstudy.com/bbs/1501584046755880962