文章目录
一、简介和下载
1、windows es下载安装
Elasticsearch(ES)是一个基于Lucene库的搜索引擎。它提供了一个分布式、支持多租户的全文搜索引擎,具有HTTP Web接口和无模式JSON文档。Elasticsearch是用Java开发的,并在Apache许可证下作为开源软件发布。Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
这里我选择了7.10.2,并使用迅雷下载,速度还可以
解压后目录结构
bin #启动文件
config:
log4j2 #日志配置文件
jvm.options #java虚拟机相关配置
elasticsearch.yml #elasticsearch的配置文件 默认9200端口
lib #相关jar包
logs #日志
modules #功能模块
plugins #插件
进入bin目录双击elasticsearch.bat
,然后访问http://127.0.0.1:9200/
,成功出现以下json信息
{
"name": "SHAWN",
"cluster_name": "elasticsearch",
"cluster_uuid": "jsUcSDzlQ_W7ikv3MOWrBw",
"version": {
"number": "7.10.2",
"build_flavor": "default",
"build_type": "zip",
"build_hash": "747e1cc71def077253878a59143c1f785afa92b9",
"build_date": "2021-01-13T00:42:12.435326Z",
"build_snapshot": false,
"lucene_version": "8.7.0",
"minimum_wire_compatibility_version": "6.8.0",
"minimum_index_compatibility_version": "6.0.0-beta1"
},
"tagline": "You Know, for Search"
}
2、可视化插件
- github地址:https://github.com/mobz/elasticsearch-head,按操作运行项目;若出现跨域问题进行配置
http.cors.enabled: true
http.cors.allow-origin: "*"
- google商店搜索ElasticSearch Head,安装插件
3、Kibana 下载与安装
ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是开源软件。新增了一个FileBeat,它是一个轻量级的日志收集处理工具(Agent),Filebeat占用资源少,适合于在各个服务器上搜集日志后传输给Logstash,官方也推荐此工具。
Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
Logstash 主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。
Kibana 也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好Web 界面,可以帮助汇总、分析和搜索重要数据日志。
注意要和ES版本一致
解压打开bin/kibana.bat
浏览器http://localhost:5601/
汉化,在config/kibana.yml
下
i18n.locale: "zh-CN"
二、核心概念
- 索引
- 字段类型(mapping)
- 文档(documents)(倒排索引)
三、IK分词器
分词:即把一段中文或者别的划分成一个个的关键字,我们在搜索时候会把自己的信息进行分词,会把数据库中或者索引库中的数据进行分词,然后进行一个匹配操作,默认的中文分词是将每个字看成一个词,比
如果要使用中文,建议使用ik分词器!
下载完成后放入es plugins中(注意版本匹配),之后重启es
ik_smart
最少切分,ik_max_word
最细粒度划分,穷尽词库可能
// 在Kibana 控制台进行访问
GET _analyze
{
"analyzer": "ik_smart",
"text": "中国女排"
}
GET _analyze
{
"analyzer": "ik_max_word",
"text": "中国女排"
}
ik分词器增加自己目录
在插件目录下config/IKAnalyzer.cfg.xml
进行配置
四、ES基础测试
1.创建一个索引
# put /索引名/~类型名~/文档id
PUT /test1/type1/1
{
"name": "shawn",
"age": "18"
}
- 字符串类型
text、keyword - 数值类型
long,integer,short,byte,double,float,half float,scaled float - 日期类型
date - te布尔值类型
boolean - 二进制类型
binary
2.映射,默认是_doc
PUT /test2
{
"mappings": {
"properties":{
"name":{
"type":"text"
},
"address":{
"type":"text"
},
"age":{
"type":"integer"
}
}
}
}
3.更新(有值就更新)
POST /test1/type1/1/_update
{
"name": "shawn",
"age": "20"
}
4、简单查询
GET /test1/_search?q=name:shawn
5、复杂操作搜索
select(排序,分页,高亮,模糊查询,精准查询!),里面可以有很多查询操作,和mysql类似
GET test1/_search
{
"query": {
"bool":{
"must": [
{
"match": {
"name": "shawn"
}
},
{
"match": {
"age": "18"
}
}
]
}
},
"_source": ["name","age"],
"from": 0,
"size": 1,
"highlight": {
"pre_tags":"<p class='key' style='color:red'>",
"post_tags":"</p>",
"fields": {
"name": {}
}
}
}
text
类型可以被分词 keyword
不能被分词
- 匹配
- 按照条件匹配
- 精确匹配
- 区间范围匹配
- 匹配字段过滤
- 多条件查询
- 高亮查询
五、springboot整合es
1、引入maven依赖
我的springboot版本是2.4.2
,es版本是7.10.2
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.10.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.10.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
2、创建配置文件
创建config/ElasticSearchConfig.java
配置文件
@Configuration
public class ElasticSearchConfig {
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
COMMON_OPTIONS = builder.build();
}
@Bean
public RestHighLevelClient client() {
return new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200)));
}
}
3、创建实体类
创建pojo/User.java
类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private Integer age;
}
4、测试文件
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RestHighLevelClient client;
// 测试索引的创建
@Test
void testCreateIndex() throws IOException {
// 创建索引请求
CreateIndexRequest request = new CreateIndexRequest("shawn_index");
// 客户端执行请求
CreateIndexResponse createIndexResponse =
client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
// 获取索引
@Test
void testExistIndex() throws IOException {
GetIndexRequest getIndexRequest = new GetIndexRequest("shawn_index");
boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
// 删除索引
@Test
void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("shawn_index");
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
// 添加文档
@Test
void testAddDocument() throws IOException {
// 创建对象
User user = new User("shawn",18);
// 创建请求
IndexRequest request = new IndexRequest("shawn_index");
// 规则
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1));
request.timeout("1s");
// 将微码数据放入请求 json
request.source(JSON.toJSONString(user), XContentType.JSON);
// 客户端发送请求
IndexResponse index = client.index(request, RequestOptions.DEFAULT);
System.out.println(index.toString());
}
// 获取文档
@Test
void testIsExists() throws IOException {
GetRequest getRequest = new GetRequest("shawn_index");
// 不获取返回的source上下文
getRequest.fetchSourceContext(new FetchSourceContext(false));
boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
// 获取文档信息
@Test
void testGetDocument() throws IOException {
GetRequest getRequest = new GetRequest("shawn_index","1");
GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
// 打印文档
System.out.println(documentFields.getSourceAsString());
}
// 更新文档信息
@Test
void testUpdateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("shawn_index", "1");
request.timeout("1s");
User user = new User("shawn22",22);
request.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse update = client.update(request, RequestOptions.DEFAULT);
// 打印文档
System.out.println(update.status());
}
// 删除文档信息
@Test
void testDeleteDocument() throws IOException {
DeleteRequest request = new DeleteRequest("shawn_index","1");
DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);
// 打印文档
System.out.println(delete.status());
}
//特殊的,真的项目一般都会批量插入数据!
@Test
void testBulkRequest() throws IOException{
BulkRequest bulkRequest=new BulkRequest();
bulkRequest.timeout("10s");
ArrayList<User> userlist=new ArrayList<>();
userlist.add(new User("shawn1",3));
//批处理请求
for(int i=0;i< userlist.size();i++){
bulkRequest.add(
new IndexRequest("shawn_index")
.id(""+(i+1))
.source(JSON.toJSONString(userlist.get(i)),XContentType.JSON));
}
BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.hasFailures());
}
// 查询
@Test
void testSearch() throws IOException {
// 构建搜索条件
SearchRequest searchRequest = new SearchRequest("shawn_index");
// 查询条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 匹配所有
// QueryBuilders.matchAllQuery();
// termquery精确匹配
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "shawn1");
searchSourceBuilder.query(termQueryBuilder);
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
// 分页
searchSourceBuilder.from(1);
searchSourceBuilder.size(4);
// // 高亮
// HighlightBuilder highlightBuilder = new HighlightBuilder();
// highlightBuilder.field("name");
// // 关闭多个高亮匹配
// highlightBuilder.requireFieldMatch(false);
// highlightBuilder.preTags("<span style='color:red'>");
// highlightBuilder.postTags("</span>");
// searchSourceBuilder.highlighter(highlightBuilder);
// 执行搜索
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析结果
System.out.println(JSON.toJSONString(searchResponse.getHits()));
System.out.println("=========================");
for (SearchHit hit : searchResponse.getHits()) {
System.out.println(hit.getSourceAsMap());
}
// // 如果高亮了需要解析高亮字段
// ArrayList<Map<String, Object>> list = new ArrayList<>();
// for (SearchHit hit : searchResponse.getHits()) {
// // 解析高亮
// Map<String, HighlightField> highlightFieldMap = hit.getHighlightFields();
// HighlightField highlightField = highlightFieldMap.get("name");
// Map<String, Object> sourceAsMap = hit.getSourceAsMap();
// // 替换原来的
// if(highlightField!=null){
// Text[] fragments = highlightField.fragments();
// String n_highlightField = "";
// for (Text text : fragments) {
// n_highlightField += text;
// }
// sourceAsMap.put("name",n_highlightField);
// }
// list.add(sourceAsMap);
// }
}