docker pull elasticsearch:7.7.0
docker run --name elasticsearch -d -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 elasticsearch:7.7.0
docker pull mobz/elasticsearch-head:5
docker run --name elasticsearch-head -d -p 9100:9100 mobz/elasticsearch-head:5
在连接ElasticSearch会发现无法连接,由于时前后端分离开发,所以会存在跨域问题,需要在服务端做跨域处理。执行命令
docker exec -it elasticsearch /bin/bash
进入到第一步创建的ElasticSearch容器中,修改配置文件vi config/elasticsearch.yml即可。
http.cors.enabled: true
http.cors.allow-origin: "*"
重启容器 docker restart elasticsearch
访问即可
问题汇总 elasticsearch 出现远程主机强迫关闭了一个现有的连接。
Caused by: java.io.IOException: 远程主机强迫关闭了一个现有的连接。
at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:828) ~[elasticsearch-rest-client-7.6.2.jar:7.6.2]
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:248) ~[elasticsearch-rest-client-7.6.2.jar:7.6.2]
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:235) ~[elasticsearch-rest-client-7.6.2.jar:7.6.2]
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1514) ~[elasticsearch-rest-high-level-client-7.6.2.jar:7.6.2]
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1484) ~[elasticsearch-rest-high-level-client-7.6.2.jar:7.6.2]
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1454) ~[elasticsearch-rest-high-level-client-7.6.2.jar:7.6.2]
at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:970) ~[elasticsearch-rest-high-level-client-7.6.2.jar:7.6.2]
at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.lambda$search$10(ElasticsearchRestTemplate.java:269) ~[spring-data-elasticsearch-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.execute(ElasticsearchRestTemplate.java:359) ~[spring-data-elasticsearch-4.0.6.RELEASE.jar:4.0.6.RELEASE]
... 64 common frames omitted
Caused by: java.io.IOException: 远程主机强迫关闭了一个现有的连接。
2.1实体类注解
首先先了解几个实体类常用的注解
@Document注解作用在类上,标记实体类为文档对象,常用属性如下:
(1)indexName:对应索引库名称;
(2)type:对应在索引库中的类型;
(3)shards:分片数
(4)replicas:副本数;
@Field作用在成员变量,标记为文档的字段,并制定映射属性;
(1)@Id:作用在成员变量,标记一个字段为id主键;一般id字段或是域不需要存储也不需要分词;
(2)type:字段的类型,取值是枚举,FieldType;
(3)index:是否索引,布尔值类型,默认是true;
(4)store:是否存储,布尔值类型,默认值是false;
(5)analyzer:分词器名称
@Field(type = FieldType.Keyword)和 @Field(type = FieldType.Text)区别
FieldType.Keyword存储字符串数据时,不会建立索引。不进行分词,直接索引
FieldType.Text在存储字符串数据的时候,会自动建立索引,也会占用部分空间资源。支持分词,全文检索
@Field(analyzer=“ik_max_word”,searchAnalyzer=“ik_max_word”)
表示是否分词,如果是分词就会按照分词的单词搜索,如果不是分词就按照整体搜索。
总结:一般我们会在常用搜索的字段加 @Field(analyzer = “ik_max_word”,type = FieldType.Text) 建立索引。
3.1构建布尔查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
布尔查询是最常用的组合查询,根据子查询的规则,只有当文档满足所有子查询条件时,elasticsearch引擎才将结果返回。布尔查询支持的子查询条件共4中:
must_not表示不符合该条件
演示代码
//设置分页
Pageable pageable = PageRequest.of(pageNum, pageSize);
//全局搜索构造器
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
//分页
nativeSearchQueryBuilder.withPageable(pageable);
//布尔构造器
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// boolQueryBuilder.must(QueryBuilders.termQuery("brandId", keyword));
boolQueryBuilder.must(QueryBuilders.termQuery("brandName", keyword));
// boolQueryBuilder.must(QueryBuilders.termQuery("productName", keyword));
nativeSearchQueryBuilder.withFilter(boolQueryBuilder);
NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
Page<EsProduct> search = productRepository.search(searchQuery);
3.2匹配查询(常用)
@Override
public Page<EsProduct> MatchQueryBuilder(String keyword, Integer pageNum, Integer pageSize) {
//设置分页
Pageable pageable = PageRequest.of(pageNum, pageSize);
//全局搜索构造器
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
//分页
nativeSearchQueryBuilder.withPageable(pageable);
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("productName", keyword);
nativeSearchQueryBuilder.withQuery(matchQueryBuilder);
NativeSearchQuery build = nativeSearchQueryBuilder.build();
Page<EsProduct> search = productRepository.search(build);
return search;
}
3.3精确查询
@Override
public Page<EsProduct> TermQueryBuilder(String keyword, Integer pageNum, Integer pageSize) {
//设置分页
Pageable pageable = PageRequest.of(pageNum, pageSize);
//全局搜索构造器
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
//分页
nativeSearchQueryBuilder.withPageable(pageable);
//精确查询
TermQueryBuilder productName = QueryBuilders.termQuery("productName", keyword);
nativeSearchQueryBuilder.withQuery(productName);
NativeSearchQuery build = nativeSearchQueryBuilder.build();
Page<EsProduct> search = productRepository.search(build);
return search;
}
个人搭建项目代码地址:
https://github.com/hongjiatao/spring-boot-anyDemo
欢迎收藏点赞三连。谢谢!有问题可以留言博主会24小时内无偿回复。