Elasticsearch基础(三)索引和文档操作
1、api种类
1.1 TransportClient
是Elasticsearch官方的api
TransportClient可以支持2.x,5.x版本,TransportClient将会在Elasticsearch 7.0弃用并在8.0中完成删除。
1.2 RestClient
也是Elasticsearch官方的api。
官方地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html
替代TransportClient,它使用HTTP请求而不是Java序列化请求。
1.3 Jest
Jest是Java社区开发的,是Elasticsearch的Java Http Rest客户端
1.4 Spring Data Elasticsearch
spring集成的Elasticsearch开发包
2、 RestClient-索引和文档操作
pom:
<dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.6.2</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.6.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13</version></dependency>
java代码:
import static org.junit.Assert.assertTrue;import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CloseIndexRequest;
import org.elasticsearch.client.indices.CloseIndexResponse;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class RestHighLevelClientDemo {private RestHighLevelClient client;/*** 创建客户端* * @return*/@Beforepublic void buildClient() {client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.1.1.5", 9200, "http")));}/*** 创建索引* @throws IOException*/@Testpublic void test001createIndex() throws IOException {deleteIndex();CreateIndexRequest request = new CreateIndexRequest("person_index");//索引名不能有大写字母,必须全部小写,否则es无法创建索引// 分片配置request.settings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0));// mapping配置Map<String, Object> name = new HashMap<String, Object>();name.put("type", "text");Map<String, Object> age = new HashMap<String, Object>();age.put("type", "text");Map<String, Object> properties = new HashMap<String, Object>();properties.put("name", name);properties.put("age", age);Map<String, Object> mapping = new HashMap<String, Object>();mapping.put("properties", properties);request.mapping(mapping);// 为索引设置一个别名request.alias(new Alias("person_alias"));CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);System.out.println(request.index()+"创建索引"+createIndexResponse.isAcknowledged());}/*** 判断索引是否存在* @throws IOException*/@Testpublic void test002existsIndex() throws IOException {GetIndexRequest request = new GetIndexRequest("person_index");request.includeDefaults(true);request.indicesOptions(IndicesOptions.lenientExpandOpen());boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);if (exists) {System.out.println("索引存在");} else {System.out.println("索引不存在");}assertTrue(exists);}/*** 打开索引*/@Testpublic void test003openIndex() {OpenIndexRequest request = new OpenIndexRequest("person_index");try {OpenIndexResponse openIndexResponse = client.indices().open(request, RequestOptions.DEFAULT);boolean acknowledged = openIndexResponse.isAcknowledged();System.out.println("openIndex:"+acknowledged);} catch (IOException e) {e.printStackTrace();}}/**** 关闭索引*/@Testpublic void test004closeIndex() {CloseIndexRequest request = new CloseIndexRequest("person_index"); try {CloseIndexResponse closeIndexResponse = client.indices().close(request, RequestOptions.DEFAULT);boolean acknowledged = closeIndexResponse.isAcknowledged(); System.out.println("closeIndex:"+acknowledged);test003openIndex();} catch (IOException e) {e.printStackTrace();}}/*** 删除索引*/public void deleteIndex() {DeleteIndexRequest request = new DeleteIndexRequest("person_index"); AcknowledgedResponse deleteIndexResponse;try {deleteIndexResponse = client.indices().delete(request, RequestOptions.DEFAULT);boolean acknowledged = deleteIndexResponse.isAcknowledged(); System.out.println("deleteIndex:"+acknowledged);} catch (IOException e) {e.printStackTrace();}}// doc 操作#########################################################################/*** 新增文档* @throws IOException*/@Testpublic void test006insertDoc() throws IOException {Map<String, Object> jsonMap = new HashMap<>();jsonMap.put("user", "kimchy");jsonMap.put("id", new Date().getTime());jsonMap.put("postDate", new Date());jsonMap.put("message", "trying out Elasticsearch22");IndexRequest indexRequest = new IndexRequest("person_index").id("2").source(jsonMap);IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);System.out.println("insertDoc:"+indexResponse.getId());}/*** 查询文档* @throws IOException*/@Testpublic void test007queryDoc() throws IOException {GetRequest getRequest = new GetRequest("person_index", "2");GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);System.out.println("queryDoc:"+getResponse.getSourceAsString());}/*** 更新文档*/@Testpublic void test008updateDoc() {Map<String, Object> jsonMap = new HashMap<>();jsonMap.put("updated", new Date());jsonMap.put("car", "BMW");jsonMap.put("message", "daily update");UpdateRequest request = new UpdateRequest("person_index", "2").doc(jsonMap);try {UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);System.out.println("updateDoc:");} catch (Exception e) {e.printStackTrace();}}/*** 删除文档*/@Testpublic void test009deleteDoc() {DeleteRequest request = new DeleteRequest("person_index", "a06KsXEBRH_WDXoYCFbn");try {DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {System.out.println("文档删除成功");}} catch (IOException e) {e.printStackTrace();}}@Testpublic void test010deleteIndex2() {//deleteIndex();}
}
Elasticsearch基础(三)索引和文档操作相关推荐
- ES的创建索引和文档操作
索引操作 1)创建索引 对比关系型数据库,创建索引就等同于创建数据库 点击postman ,创建一个new collections 再改个名 在Postmnan中,向ES服务器发PUT请求: http ...
- 第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查...
第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲-elasticsearch(搜索引擎)基本的索引和文档CRUD操作.增.删.改.查 elasticsearch(搜索引擎)基本的索引 ...
- Jquery的事件操作和文档操作
对于熟悉前端开发的小伙伴,相信对于Jquery一定不陌生,相对于JavaScript的繁琐,Jquery更加的简洁,当然简洁不意味着简单,我们可以使用Jquery完成我们想要实现全部功能,这里为小白们 ...
- ElasticSearch之HTTP索引操作和文档操作
文章目录 1. 核心概念及数据格式 1.1 索引( Index) 1.2 类型( Type) 1.3 文档( Document) 1.4 字段( Field) 1.5 映射( Mapping) 1.6 ...
- elasticsearch基础1——索引、文档
用于复习快速回顾. 目录 1.初识弹性搜索elasticsearch 1.1.了解ES 1.1.1.elasticsearch的作用 1.1.2.ELK弹性栈 1.1.3.elasticsearch和 ...
- Elasticsearch中索引和文档的管理
索引的管理 创建索引 输入: put manage-test 输出: {"acknowledged" : true,"shards_acknowledged" ...
- 在 Java 应用程序中使用 Elasticsearch: 高性能 RESTful 搜索引擎和文档存储快速入门指南
如果您使用过 Apache Lucene 或 Apache Solr,就会知道它们的使用体验非常有趣.尤其在您需要扩展基于 Lucene 或 Solr 的解决方案时,您就会了解 Elasticsear ...
- elasticsearch 第五篇(文档操作接口)
INDEX API 示例: 1 2 3 4 5 PUT /test/user/1 { "name": "silence", "age": 2 ...
- elasticsearch5.0.0中索引和文档接口的变化
2019独角兽企业重金招聘Python工程师标准>>> 索引接口变化 当运行索引映射的时候禁止关闭或删除索引操作. 在5.0之前的版本,当索引正在进行映射操作的时候,关闭索引或者删除 ...
最新文章
- 设计模式(六)命令模式
- 解决MongoDB 日志文件过大,清理后还占用很大磁盘空间的问题
- UNIX进程的创建,进程链和进程扇
- 自带flash的浏览器_解决Flash插件已被屏蔽的问题(谷歌、火狐、IE、Edge)
- 初步理解NServiceBus
- linux添加隧道,linux配置多级服务器登录和隧道映射
- 多元线性回归--machine learning
- jquery初级视频教程
- 顺丰全栈资源下的自动化运维灵魂
- CodeForces 366C Dima and Salad
- 22478计算机代码,数字2247代表啥意思 2247数字意思
- 大起底神盾七号重疾险:赔两次价格还不贵,到底值不值?
- VS2008鼠标右键不灵敏,TFS的Local Path无法打开对应文件夹
- PXE系列之一:PXE环境搭建
- 阿里云2022年双十一活动各云产品新购和续费优惠政策汇总
- nas 微型计算机,NETGEAR无线路由器和NAS试用
- vlookup反向查询_VLOOKUP的反向查找功能
- RSD处理高分5号高光谱(GF5 AHSI)数据(二)——波段编辑复制和删除
- 几种MySQL高可用方案整理
- C. Anu Has a Function---------------------------思维
热门文章
- matlab循环输出图像,运用matlab实现循环语句中的多幅图像显示
- html新闻上下自动滚动代码,jQuery网站公告上下滚动自动轮播代码
- Python学习:day20正则表达式
- HDFS与MapReduce
- CSDN联合腾讯云重磅发布“腾讯云数据库TDSQL工程师路线图”
- 为了学习数据库索引,我们花了5000美元
- 进程全家桶,看这一篇就够了 | 原力计划
- 美团外卖回应佣金争议;苹果回应戴口罩解锁 iPhone;新 Edge 浏览器明年将不再支持 Win7 | 极客头条...
- 异构计算崛起,GPU加速计算服务器FP5468G2应运而生
- 如何实现自动化前端开发?