Elasticsearch简介与实战

本文介绍了Elasticsearch的基本操作,包括如何创建、查询和更新文档,详细讲解了各种查询方式,如指定条件、多条件、部分词汇匹配及聚合查询,并提供了Java代码示例来演示如何与ES交互。
摘要由CSDN通过智能技术生成

五、文档


1、创建文档

ES中的文档相当于MySQL中的表数据,数据格式为JSON格式。

由于文档生成时会自动创建一个唯一性标识,因为POST不是幂等性的,PUT是幂等性的,所以这里只能用POST。

可以指定id

2、查询文档

(1)根据id查询

(2)查询所有文档

127.0.0.1:9200/work/_search

![](https://img-blog.csdnimg.cn/20210925121749490.png?x-oss-process=ima 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 ge/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5ZOqIOWQkg==,size_20,color_FFFFFF,t_70,g_se,x_16)

3、更改文档内容

(1)修改文档id=1001的内容,恭喜哪吒大佬被评选为“2021博客之星TOP10”,锣鼓喧天,鞭炮旗鼓。

(2)局部更新

(3)局部更新成功,恭喜哪吒成功晋升TOP5。

六、复杂查询


1、指定条件查询

(1)查询name为哪吒的索引(通过请求路径:127.0.0.1:9200/work/_search?q=name:哪吒)

注意:满篇全是截图也不好看,以下就不截图了,望谅解。

(2)请求体查询

get请求:127.0.0.1:9200/work/_search

请求体:

{

“query”:{

“match”:{

“name”:“哪吒”

}

}

}

(3)分页查询

get请求:127.0.0.1:9200/work/_search

请求体:

{

“query”:{

“match_all”:{

}

} ,

“from”:0,

“size”:2

}

(4)只获取指定字段  and  根据id排序

{

“query”:{

“match_all”:{

}

} ,

“from”:0,

“size”:2,

“_source”:[“title”],

“sort”:{

“_id”:“desc”

}

}

2、多条件查询

must表示and匹配

{

“query”:{

“bool”:{

“must”:[

{

“match”:{

“name”:“哪吒”

}

},{

“match”:{

“title”:“博客专家”

}

}

]

}

}

}

should表示or匹配

{

“query”:{

“bool”:{

“should”:[

{

“match”:{

“name”:“哪吒”

}

},{

“match”:{

“name”:“CSDN”

}

}

]

}

}

}

范围匹配:工资大于10000

{

“query”:{

“bool”:{

“should”:[

{

“match”:{

“name”:“哪吒”

}

},{

“match”:{

“name”:“CSDN”

}

}

],

“filter”:{

“range”:{

“money”:10000

}

}

}

}

}

3、部分词汇匹配查询

将每一个值拆解,组成倒排索引,方便进行全文检索。

{

“query”:{

“match”:{

“name”:“哪”

}

}

}

完全匹配

{

“query”:{

“match_phrase”:{

“name”:“哪”

}

}

}

高亮显示

{

“query”:{

“match”:{

“name”:“哪”

}

},

“highlight”:{

“fields”:{

“name”:{}

}

}

}

4、聚合查询

(1)分组查询

{

“aggs”:{

“money_group”:{

“terms”:{

“field”:“money”

}

}

},

“size”:0

}

(2)平均值查询

{

“aggs”:{

“money_avg”:{

“avg”:{

“field”:“money”

}

}

},

“size”:0

}

七、代码实例


1、引入pom

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”>

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.2.1.RELEASE

com.guor

es

0.0.1-SNAPSHOT

es

Demo project for Spring Boot

<java.version>1.8</java.version>

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

test

org.elasticsearch

elasticsearch

7.8.0

org.elasticsearch.client

elasticsearch-rest-high-level-client

7.8.0

org.elasticsearch.client

elasticsearch-rest-client

org.elasticsearch.client

elasticsearch-rest-client

7.8.0

org.apache.logging.log4j

log4j-api

2.8.2

org.apache.logging.log4j

log4j-core

2.8.2

com.fasterxml.jackson.core

jackson-databind

2.9.9

org.projectlombok

lombok

true

junit

junit

4.12

org.springframework.boot

spring-boot-maven-plugin

2、添加索引

package com.guor.es.test;

import org.apache.http.HttpHost;

import org.elasticsearch.client.RequestOptions;

import org.elasticsearch.client.RestClient;

import org.elasticsearch.client.RestHighLevelClient;

import org.elasticsearch.client.indices.CreateIndexRequest;

import org.elasticsearch.client.indices.CreateIndexResponse;

public class ESTest {

public static void main(String[] args) throws Exception{

//创建es客户端

RestHighLevelClient esClient = new RestHighLevelClient(

RestClient.builder(new HttpHost(“localhost”,9200,“http”))

);

//创建索引

CreateIndexRequest request = new CreateIndexRequest(“student”);

CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);

boolean acknowledged = createIndexResponse.isAcknowledged();

System.out.println(“创建索引:”+acknowledged);

// 关闭es客户端

esClient.close();

}

}

3、运行出错

解决方法:

将pom中

org.elasticsearch.client

elasticsearch-rest-high-level-client

7.8.0

替换为:

org.elasticsearch.client

elasticsearch-rest-high-level-client

7.8.0

org.elasticsearch.client

elasticsearch-rest-client

org.elasticsearch.client

elasticsearch-rest-client

7.8.0

4、查询索引

package com.guor.es.test;

import org.apache.http.HttpHost;

import org.elasticsearch.client.RequestOptions;

import org.elasticsearch.client.RestClient;

import org.elasticsearch.client.RestHighLevelClient;

import org.elasticsearch.client.indices.CreateIndexRequest;

import org.elasticsearch.client.indices.CreateIndexResponse;

import org.elasticsearch.client.indices.GetIndexRequest;

import org.elasticsearch.client.indices.GetIndexResponse;

public class ESTest {

public static void main(String[] args) throws Exception{

//创建es客户端

RestHighLevelClient esClient = new RestHighLevelClient(

RestClient.builder(new HttpHost(“localhost”,9200,“http”))

);

//创建索引

GetIndexRequest request = new GetIndexRequest(“student”);

GetIndexResponse getIndexResponse = esClient.indices().get(request, RequestOptions.DEFAULT);

//响应状态

System.out.println(getIndexResponse.getAliases());

System.out.println(getIndexResponse.getMappings());

System.out.println(getIndexResponse.getSettings());

// 关闭es客户端

esClient.close();

}

}

5、插入文档

package com.guor.es.test;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.http.HttpHost;

import org.elasticsearch.action.index.IndexRequest;

import org.elasticsearch.client.RequestOptions;

import org.elasticsearch.client.RestClient;

import org.elasticsearch.client.RestHighLevelClient;

import org.elasticsearch.client.indices.GetIndexRequest;

import org.elasticsearch.client.indices.GetIndexResponse;

import org.elasticsearch.common.xcontent.XContentType;

public class ESTestDoc {

public static void main(String[] args) throws Exception{

addDoc();

}

private static void addDoc() throws Exception{

//创建es客户端

RestHighLevelClient esClient = new RestHighLevelClient(

RestClient.builder(new HttpHost(“localhost”,9200,“http”))

);

IndexRequest request = new IndexRequest();

request.index(“work”).id(“1001”);

Work work = new Work();

work.setName(“哪吒”);

work.setAge(28);

work.setSex(0);

ObjectMapper mapper = new ObjectMapper();

String workJson = mapper.writeValueAsString(work);

IndexRequest response = request.source(workJson, XContentType.JSON);

System.out.println(response.getShouldStoreResult());

esClient.index(request, RequestOptions.DEFAULT);

// 关闭es客户端

esClient.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值