Elasticsearch
elasticserch 可以快速的储存,搜索分析海量数据,Elasticsearch 是一个分布式搜索服务,底层也是基于Lucene ,采用多shard(分片)的方式保证数据安全,提供RestFul API 并且提供自动resharding 功能, 除此之外我还用过solr也是基于lucene ,这个时候可定要对比下
http://i.zhcy.tk/blog/elasticsearchyu-solr/
solr 的确定是他牺牲了建立索引的事件,建立索引的时间长,但是搜索的速度非常的块
https://www.cnblogs.com/xiaoqi/p/solr-vs-elasticsearch.html
我们这里讲springboot 和 elasticsearch 整合
docker 安装 elasticsearch
docker search elasticsearch
docker pull registry.docker-cn.com/library/elasticsearch
docker images
run
默认初始会使用2G 的堆内存空间我们可以使用 -e 命令限制内存空间
elastic search 默认使用的端口是9200 在分布式的情况下,各个节点使用的端口是9300 我们都需要暴漏这两个接口
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES01 362c5cb1669b
-e 分配栈内存, -p 映射端口 , --name image 名字 id
docker ps -a
然后可以看到elasticsearch 已经启动
我们可以通过浏览器访问返回json 然后确定是否启动
我的内网ip 然后返回这样的数据
{
"name" : "Xyyh80B",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "e3RZPu0pS8uUlxVA6fJQFA",
"version" : {
"number" : "5.6.11",
"build_hash" : "bc3eef4",
"build_date" : "2018-08-16T15:25:17.293Z",
"build_snapshot" : false,
"lucene_version" : "6.6.1"
},
"tagline" : "You Know, for Search"
}
add
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_indexing_employee_documents.html
官网说的很清楚
GET
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_retrieving_a_document.html
如果未发现的话 就会返回这样的情况
{
"_index": "megacorp",
"_type": "employee",
"_id": "4",
"found": false
}
getAll
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_search_lite.html
使用查询表达式
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_search_with_query_dsl.html
Springboot整合Elasticsearch
springboot 默认使用springdata 操作elasticsearch ,
elasticsearch 自动配置
springboot提供了两种方式,默认使用springdata ,还整合了最流行的jest 客户端通过http方式操作elasticsearch
JestAutoConfiguration
默认jestAutoConfiguration 是不生效的, 如果想让生效需要导入jest 包
ElasticsearchDataAutoConfiguration
- Client节点信息clusterNodes clusterName
- elasticsearchTemplate 操作es
- 编写一个ElasticsearchRepository的子接口来操作ES
1.创建entity
package com.zzq.springboot03elasticsearch;
import io.searchbox.annotations.JestId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
public class Article {
@JestId
private Integer id ;
private String title ;
private String author ;
private String content ;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Article(Integer id, String title, String author, String content) {
this.id = id;
this.title = title;
this.author = author;
this.content = content;
}
}
这里注意一定要标识主键,如果不标识主键他就会自动生成主键,
然后编写逻辑代码创建索引
@Test
public void contextLoads() throws IOException {
//给es 中保存一个文档
Article article = new Article(1, "好消息", "zhnagsan", "Hello Word");
Index build = new Index.Builder(article).index("atguigu").type("news").build();
jestClient.execute(build);
}
注意一定要注入jestClient 客户端对象,然后执行execute方法
然后就ok了,
使用全文检索查询
@Test
public void search() throws IOException {
String json = "{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"content\" : \"hello\"\n" +
" }\n" +
" }\n" +
"}" ;
Search search = new Search.Builder(json).addIndex("atguigu").addType("news").build();
SearchResult res = jestClient.execute(search);
System.out.println(res.getJsonString());
}
更多的查询方式官网上都是有的 ,玩一个高亮搜索把
@Test
public void search() throws IOException {
String json = "{\n" +
" \"query\" : {\n" +
" \"match_phrase\" : {\n" +
" \"content\" : \"hello\"\n" +
" }\n" +
" },\n" +
" \"highlight\": {\n" +
" \"fields\" : {\n" +
" \"about\" : {}\n" +
" }\n" +
" }\n" +
"}" ;
Search search = new Search.Builder(json).addIndex("atguigu").addType("news").build();
SearchResult res = jestClient.execute(search);
System.out.println(res.getJsonString());
}
SpringdataElasticSearch 操作ElasticSearch
使用springdata 操作 Elastic Search 不是使用9200 通过http通讯的,springdata 操作elastic search 使用9300