一、测试数据准备:
1、基础语法:首先使用RESTER简单测一下ES的健康状况检查:GET: http://192.168.163.129:9200/_cat/health?v
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1573218278 21:04:38 elasticsearch green 1 1 0 0 0 0 0 0 - 100.0%
2、基础语法:查看节点数:GET : http://192.168.163.129:9200/_cat/nodes?v
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name 192.168.163.129 32 98 0 0.01 0.02 0.05 mdi * zId8-Ym
3、基础语法:查看索引: GET : http://192.168.163.129:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size 代表还没有建任何索引
4、基础语法:创建索引(相当于数据库database): PUT : http://192.168.163.129:9200/customer?pretty
{ "acknowledged": true, "shards_acknowledged": true, "index": "customer" } 代表:名为customer的索引创建成功
5、再次以查看索引的语法查看:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open customer _bYrCODMSfuddqFfXZdD7A 5 1 0 0 810b 810b
6、基础语法:向上面创建的索引中put一条数据: PUT : http://192.168.163.129:9200/customer/external/1?pretty
传参: { "name":"jiguiquan" }
返回结果:
{ "_index": "customer", //相当于数据库database "_type": "external", //相当于表table "_id": "1", //主键id "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true }
7、基础语法:查看刚刚放进去的数据: GET : http://192.168.163.129:9200/customer/external/1?pretty
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 1, "found": true, "_source": { "name": "jiguiquan" } }
二、SpringBoot2.x整合elasticSearch:
-
需要用到Spring Data Elasticsearch,文档地址:
https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/
-
版本说明:Springboot整合elasticsearch:
https://github.com/spring-projects/spring-data-elasticsearch/wiki
1、添加maven依赖:
<!-- 整合elasticsearch --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
2、创建一个Article实体类:
/** * 文章对象 * @author jigq * @create 2019-11-08 21:51 */ @Document(indexName = "blog", type = "article") @Data public class Article implements Serializable { private static final long serialVersionUID = -7319940137025326036L; private long id; private String title; private String summary; private String content; private int pv; }
3、接口继承ElasticSearchRepository,里面有很多默认实现类(注意:索引名称记得小写,类属性名称也要小写):
@Repository @Document(indexName = "blog", type = "article", shards = 1, replicas = 0) public interface ArticleRepository extends ElasticsearchRepository<Article, Long> { }
4、配置文件:官网建议:
https://docs.spring.io/spring-boot/docs/2.1.10.RELEASE/reference/htmlsingle/#boot-features-connecting-to-elasticsearch-spring-data
我们需要用到三个:
# ELASTICSEARCH (ElasticsearchProperties) spring.data.elasticsearch.cluster-name=elasticsearch spring.data.elasticsearch.cluster-nodes=192.168.163.129:9300 spring.data.elasticsearch.repositories.enabled=true
4、编写Controller测试接口:
@RestController @RequestMapping("/es") public class ArticleController { @Autowired private ArticleRepository repository; @RequestMapping("/save") @ResponseBody public void save(){ Article article = new Article(); article.setId(2L); article.setPv(888); article.setContent("this is 内容"); article.setTitle("i love xdclass 课程"); article.setSummary("概要搜索"); repository.save(article); } }
5、启动项目,访问接口,执行成果:
GET : localhost:8080/es/save
6、查看ES中的index索引:
GET : http://192.168.163.129:9200/_cat/indices?v
结果如下:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open customer _bYrCODMSfuddqFfXZdD7A 5 1 1 0 4.1kb 4.1kb yellow open blog ph8GNH2XTpSxRtbfYNFT9Q 5 1 1 0 5.9kb 5.9kb
7、查看具体索引index中的数据表结构:
http://192.168.163.129:9200/blog
结果如下(展示的是:索引为blog,type为article的表结构):
{ "blog": { "aliases": {}, "mappings": { "article": { "properties": { "content": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "id": { "type": "long" }, "pv": { "type": "long" }, "summary": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "title": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } }, "settings": { "index": { "refresh_interval": "1s", "number_of_shards": "5", "provided_name": "blog", "creation_date": "1573224373449", "store": { "type": "fs" }, "number_of_replicas": "1", "uuid": "ph8GNH2XTpSxRtbfYNFT9Q", "version": { "created": "5060899" } } } } }
8、查看具体的数据:
http://192.168.163.129:9200/blog/article/2
查看结果如下:
{ "_index": "blog", "_type": "article", "_id": "2", "_version": 1, "found": true, "_source": { "id": 2, "title": "i love xdclass 课程", "summary": "概要搜索", "content": "this is 内容", "pv": 888 } }
9、我们再增加一点数据:
public void save(){ Article article = new Article(); article.setId(1L); article.setPv(123); article.setContent("springboot整合es"); article.setTitle("子丹的springboot整合ElasticSearch"); article.setSummary("搜索框架整合"); repository.save(article); }
三、实现搜索功能(重要)
1、编写java接口:
//搜索功能实现 @GetMapping("/serach/{title}") public Object search(@PathVariable("title") String title){ // MatchAllQueryBuilder queryBuilder = QueryBuilders.matchAllQuery(); //搜索全部文档 MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("title", title); Iterable<Article> list = repository.search(queryBuilder); return list; }
2、测试接口:
localhost:9090/es/search/子丹
结果如下:
{ "content": [ { "id": 1, "title": "子丹的springboot整合ElasticSearch", "summary": "搜索框架整合", "content": "springboot整合es", "pv": 123 } ], "pageable": { "sort": { "sorted": false, "unsorted": true, "empty": true }, "offset": 0, "pageSize": 1, "pageNumber": 0, "paged": true, "unpaged": false }, "facets": [], "aggregations": null, "scrollId": null, "maxScore": 1.0132946, "totalElements": 1, "totalPages": 1, "number": 0, "size": 1, "sort": { "sorted": false, "unsorted": true, "empty": true }, "numberOfElements": 1, "first": true, "last": true, "empty": false }
3、如果我们再代码中选择的是全文搜索,那么就会把所有的数据都搜索出来:
{ "content": [ { "id": 2, "title": "i love xdclass 课程", "summary": "概要搜索", "content": "this is 内容", "pv": 888 }, { "id": 1, "title": "子丹的springboot整合ElasticSearch", "summary": "搜索框架整合", "content": "springboot整合es", "pv": 123 } ], "pageable": { "sort": { "sorted": false, "unsorted": true, "empty": true }, "offset": 0, "pageSize": 2, "pageNumber": 0, "paged": true, "unpaged": false }, "facets": [], "aggregations": null, "scrollId": null, "maxScore": 1, "totalElements": 2, "totalPages": 1, "number": 0, "size": 2, "sort": { "sorted": false, "unsorted": true, "empty": true }, "numberOfElements": 2, "first": true, "last": true, "empty": false }
至此,SpringBoot整合ElasticSearch的基础操作就完结啦!
如果要学习更多复杂的搜索,可以查看官方文档,比如多字段多条件查询的queryBuilder构建等!