ElasticSearch使用和集成
下载慢的小伙伴们可以到 华为云的镜像去下载
速度很快,自己找对应版本就可以
ElasticSearch: https://mirrors.huaweicloud.com/elasticsearch/?C=N&O=D
logstash: https://mirrors.huaweicloud.com/logstash/?C=N&O=D
kibana: https://mirrors.huaweicloud.com/kibana/?C=N&O=D
ik: https://github.com/medcl/elasticsearch-analysis-ik/releases;https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.6.1
[]
ELK三剑客,解压即用
http.cors.enabled: true
http.cors.allow-origin: "*"
bug
- 索引创建失败?修改jvm内存值
https://blog.csdn.net/qq_31573519/article/details/77542506
es基本操作指令
- 创建文档 索引
PUT /test1/type/1
{
"name":"lg",
"age": 12
}
- 查询
GET /test1/type1/1 - 修改
# 第一种方式: 版本会增加;缺点:容易会漏字段
PUT /test1/type1/1
{
"name": "xiaoli",
"age": 100
}
#第二种方式: 这样不会漏字段,修改指定的字段
POST /test1/_doc/1/_update
{
"doc":{
"name": "法外狂徒"
}
}
- 删除
通过DELETE命令实现删除,根据你的请求来判断是删除索引还是删除文档记录
DELETE test1
DELETE lg
关于文档的基本操作(重点)
- 基本操作
# 添加索引 对应的数据
PUT /kuangshen/user/1
{
"name": "狂神说",
"age": 23,
"desc":"一顿操作稳如虎,一看战绩250",
"tags": ["技术", "温暖", "直男"]
}
PUT /kuangshen/user/2
{
"name": "张三",
"age": 3,
"desc":"法外狂徒",
"tags": ["渣男", "搞笑", "交友"]
}
PUT /kuangshen/user/3
{
"name": "李四",
"age": 30,
"desc":"不知道形容",
"tags": ["靓女", "搞笑", "唱歌"]
}
# 获取数据
GET kuangshen/user/1
# 更新数据PUT(不推荐):如果不穿值,就会被覆盖
PUT /kuangshen/user/3
{
"name": "李四233",
"age": 30,
"desc":"不知道形容",
"tags": ["靓女", "搞笑", "唱歌"]
}
# Post _update,推荐使用这种更新方式(推荐): 灵活,想修改那个字段值就改那个。
POST kuangshen/user/1/_update
{
"doc":{
"name": "狂神说java312321"
}
}
# 简单搜索
GET kuangshen/user/1
# 默认查询
GET kuangshen/user/_search?q=name:三
- 复杂操作 select(排序,分页,高亮,模糊,精准查询)
分数:score: 匹配度越高,分数越高
GET kuangshen/user/_search
{
"query": { # 查询的条件
"match": {
"name": "狂神"
}
},
"_source": ["name", "source"] # 过滤字段
}
# hit:
#我们之后使用java对象操作es,所有的方法和对象的这里里面的key
# 排序 通过 sort字段
GET kuangshen/user/_search
{
"query": { # 查询的条件
"match": {
"name": "狂神"
}
},
"sort": [ # 排序条件
{
"age": {
"order": "asc"
}
}
]
}
# 分页 kuangshen/user/_search/{current}/{pageSize}
GET kuangshen/user/_search
{
"query": { # 查询的条件
"match": {
"name": "狂神"
}
},
"sort": [ # 排序条件
{
"age": {
"order": "asc"
}
}
],
"from": 0, # limit 2 个参数
"size": 1
}
# 多条件查询 模糊; must多个条件 同时满足(相当于and)
GET kuangshen/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "狂神"
}
},
{
"match": {
"age": 23
}
}
]
}
}
}
# 多条件查询 模糊; should 多个条件 满足其一即可(相当于or)
GET kuangshen/user/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "狂神"
}
},
{
"match": {
"age": 23
}
}
]
}
}
}
# 多条件查询 模糊; must_not 多个条件 满足其一即可(相当于or)
GET kuangshen/user/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"age": 23
}
}
]
}
}
}
# 范围条件查询 模糊; range
GET kuangshen/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "狂神"
}
}
],
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
}
}
}
# 匹配多个标签查询 模糊; match 多个条件使用空格隔开
GET kuangshen/user/_search
{
"query": {
"match": {
"tags": "男 搞笑"
}
}
}
# jin
集成springBoot
- POM
<!-- es-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.2.13.RELEASE</version>
</dependency>
- 配置
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")
)
);
return client;
}
}
- 使用 CRUD
@RestController
@RequestMapping("/api")
public class esTest {
@Autowired
private RestHighLevelClient restHighLevelClient;
//1. 创建索引
@PostMapping("/createIndex")
public String createIndex() throws IOException {
//创建 一个新建索引请求
CreateIndexRequest request = new CreateIndexRequest("coupon_index");
//执行创建索引的请求
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(JSON.toJSON(createIndexResponse).toString());
//{"fragment":false,"acknowledged":true,"shardsAcknowledged":true}
/**
* 使用命令
* PUT /test2
* {
* "acknowledged" : true,
* "shards_acknowledged" : true,
* "index" : "test2"
* }
* */
return JSON.toJSON(createIndexResponse).toString();
}
//2. 查询索引是否存在
@PostMapping("/existIndex")
public String testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("coupon_index");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(JSON.toJSON(exists).toString());
//true
System.out.println(exists);
return JSON.toJSON(exists).toString();
}
//3. 查询索引信息
@PostMapping("/getIndex")
public String testGetIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("coupon_index");
GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(request, RequestOptions.DEFAULT);
String s = getIndexResponse.toString();
Object o = JSON.toJSON(getIndexResponse);
System.out.println(o.toString());
System.out.println(s);
return s;
}
//4. 删除索引
@PostMapping("/deleteIndex")
public String testDeleteIndex() throws IOException {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("coupon_index");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSON(delete));
//{"fragment":false,"acknowledged":true}
return JSON.toJSON(delete).toString();
}
/**
* 1. 给对应的索引, 创建文档
* @throws IOException
*/
@PostMapping("/createDoc")
public String testCreateDocument() throws IOException {
IndexRequest indexRequest = new IndexRequest("coupon_index");
UserInfo user = new UserInfo("张飞","12123123");
IndexRequest source = indexRequest.source(JSONObject.toJSONString(user), XContentType.JSON);
IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index.toString());
//
return index.toString();
}
/**
* 2. 文档是否存在
* @throws IOException
*/
@PostMapping("/existDoc")
void testExistDocument() throws IOException {
//testapi 索引中 是否存在 1 的文档
GetRequest getRequest = new GetRequest("coupon_index", "1");
boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
/**
* 3. 获取文档信息
* @throws IOException
*/
@PostMapping("/getDoc")
void testGetDocument() throws IOException {
GetRequest getRequest = new GetRequest("coupon_index", "gBd0W3MBYL0QvcF5Z9tv");
GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
System.out.println(documentFields.getSource());
}
/**
* 4. 更新文档信息
* @throws IOException
*/
@PostMapping("/updateDoc")
void testUpdatDocument() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("coupon_index", "jxeBW3MBYL0QvcF5idvD");
UserInfo user = new UserInfo("张飞","坦克");
updateRequest.doc(JSONObject.toJSONString(user),XContentType.JSON);
UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.status());
}
/**
* 5. 删除文档信息
* @throws IOException
*/
@PostMapping("/deleteDoc")
void testDeleteDocument() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("coupon_index", "jxeBW3MBYL0QvcF5idvD");
DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(delete.status());
}
/**
* 6. 查询文档 条件
*/
@PostMapping("/getDocByContion")
void testSearchDocument() throws IOException {
SearchRequest searchRequest = new SearchRequest("coupon_index");
//匹配字段
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("username", "李白");
//构建查询器
searchRequest.source(new SearchSourceBuilder().query(matchQueryBuilder));
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse.getHits().getTotalHits());
}
}
网友评论