前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于ElasticSearch7.5.1基本操作(HTTP方式)

基于ElasticSearch7.5.1基本操作(HTTP方式)

作者头像
用户1215919
发布2021-12-28 12:46:32
1430
发布2021-12-28 12:46:32
举报
文章被收录于专栏:大大的微笑大大的微笑

依赖

代码语言:javascript
复制
<dependency>
	<groupId>commons-httpclient</groupId>
	<artifactId>commons-httpclient</artifactId>
	<version>3.1</version>
</dependency>
代码语言:javascript
复制
    /**
     * 创建索引
     * @throws IOException
     */
    private static void createIdx() throws IOException {
        String mapping = 
			{
	"mappings": {
		"properties": {
			"user": {
				"type": "text",
				"analyzer": "ik_max_word",
				"search_analyzer": "ik_max_word"
			},
			"title": {
				"type": "text",
				"analyzer": "ik_max_word",
				"search_analyzer": "ik_max_word"
			},
			"desc": {
				"type": "text",
				"analyzer": "ik_max_word",
				"search_analyzer": "ik_max_word"
			}
		}
	}
}

        System.out.println(service.doPut("http://localhost:9200/users", mapping, headers,null));
    }

    /**
     * 创建或者更新用户信息
     * @throws IOException
     */
    private static void createOrUpdateUser() throws IOException {
        User user = new User(1L,"Lisa","HR", "人力资源管理");
        System.out.println(user);
        System.out.println(service.doPost("http://localhost:9200/users/_doc/" + user.getId(), new Gson().toJson(user), headers,null));
    }

    /**
     * 删除用户信息
     * @throws IOException
     */
    private static void deleteUser() throws IOException {
        System.out.println(service.doDelete("http://localhost:9200/users/_doc/1"));
    }

    /**
     * 根据用户ID获取用户信息
     * @throws IOException
     */
    private static void getUserById() throws IOException {
        System.out.println(service.doGet("http://localhost:9200/users/_doc/1", null,null));
    }

    /**
     * 获取所有的用户信息
     * @throws IOException
     */
    private static void getAllUser() throws IOException {
        System.out.println(service.doGet("http://localhost:9200/users/_search", null,null));
    }

    /**
     * 条件获取用户信息
     * @throws IOException
     */
    private static void  getConditionUser() throws IOException {
        String params = "{\"query\" : { \"match\" : { \"desc\" : \"搬砖\" }}, \"from\": 0, \"size\":100}";
        System.out.println(service.doPost("http://localhost:9200/users/_search", params, headers,null));
    }

HTTP请求具体封装省略,自行封装

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档