ElasticSearch基础操作

发布时间:2026/6/26 19:22:02
ElasticSearch基础操作 1. 安装postman略2. HTTP操作2.1 文档操作2.1.1 创建文档post{_index: shopping, # 索引名字_type: _doc, # 类型文档_id: LK6O_J4BxidwAQ78tNpn, # 唯一标识类似mysql主键id这里是随机生成。_version: 1,result: created, # 创建成功_shards: { # 分片信息total: 2, # 分片数量successful: 1,failed: 0},_seq_no: 0,_primary_term: 1}{ _index: shopping, # 索引名字 _type: _doc, # 类型文档 _id: LK6O_J4BxidwAQ78tNpn, # 唯一标识类似mysql主键id这里是随机生成。 _version: 1, result: created, # 创建成功 _shards: { # 分片信息 total: 2, # 分片数量 successful: 1, failed: 0 }, _seq_no: 0, _primary_term: 1 }如果要自己指定id而不是让es随机生成可以在URL中指定http://hadoop101:9200/shopping/_doc/12.1.2 查看文档get错误http://hadoop101:9200/shopping/_doc/正确http://hadoop101:9200/shopping/_doc/1要带上主键2.1.3 修改文档post覆盖模式http://hadoop101:9200/shopping/_doc/1更新模式修改单个字段http://hadoop101:9200/shopping/_update/1覆盖会以当前的字段名、数量覆盖。更新则会更新当前字段的内容如果这个字段以前不存在则会新增。2.1.4 删除文档delete、post根据主键删除deletehttp://hadoop101:9200/shopping/_doc/1根据条件删除posthttp://hadoop101:9200/shopping/_delete_by_query正则匹配{ query : { regexp : { title: _doc_.* } } }完全匹配{ query : { match : { title: _doc_ } } }2.2 高级查询2.2.1 查询所有文档gethttp://hadoop101:9200/shopping/_search{ query : { match_all : {} } }took: 5, # 查询耗时ms2.2.2 匹配查询http://hadoop101:9200/shopping/_search{ query : { match : { title : 手机名字 } } }查看要查找的内容会怎样进行切分。post(http://hadoop101:9200/shopping/_analyze) { text : 手机1, field : name }2.2.3 字段匹配查询gethttp://hadoop101:9200/shopping/_search { query : { multi_match : { query : 手, fields : [name, title] } } }2.2.4 关键字精确查询{ query : { term : { title.keyword : 手机1 } } } --------------------------- # title是text类型在es中会对这个字段进行分词也就是底层存储的是[手机1] { query : { term : { title : { value : 手机1 } } } }2.2.5 多关键字精确查询{ query : { terms : { title.keyword : [手机2, 手机1] } } }2.2.6 指定展示查询的字段{ _source : [name, title], query : { terms : { title.keyword : [手机2, 手机1] } } }2.2.7 过滤字段includes展示的字段excludes不展示的字段。同时存在的字段则不会展示{ _source : { includes : [price], excludes : [price] }, query : { terms : { title.keyword : [手机2, 手机1] } } }2.2.8 组合查询{ query : { bool : { must : [{term:{name.keyword:手}}] , must_not : [{term : {title.keyword:手机2}}] } } }2.2.9 范围查询{ query : { range : { price : { gte : 10, lte : 10000 } } } }2.2.10 模糊查询{ query : { fuzzy : { name : { value : Samsung } } } }2.2.11 多字段排序_score不是一个实际字段而是es查询时计算出来的匹配度。比如在淘宝搜索运动鞋按价格排序。如果价格相同则根据匹配度来排序。这个字段暂时还不知道用处。{query: { match_all : {} } , sort : [ {price: { order: desc }}, { _score : { order : desc } } ] }2.2.12 聚合查询size0则只返回聚合结果size3则返回3条查询结果并返回聚合结果聚合的还是所有price的累加。{aggs:{ max_age : { sum : {field:price} } } ,size : 0 }2.2.13 桶聚合查询意思是可以对多个字段进行聚合类似group by A, B{ aggs:{ max_age : { terms : {field:name.keyword}, aggs: { sum-price: { sum : {field : price} } } } } ,size : 0 }2.3 映射操作索引库 database索引 表文档 一行数据# 查询有多少个索引表 [get] http://hadoop101:9200/_cat/indices # 查看索引建表语句 [get] http://hadoop101:9200/shopping/_mappingtype类型stringtext可分词keyword不可分词numericallong,integer,short,byte,double,float,half_floatscaled_float浮点、高精度date日期array数组Object对象indexTrue字段会被索引默认false不会被索引不能用来搜索store数据将独立存储默认false2.3.1 创建索引[put] http://hadoop101:9200/test02/在URL中写_mapping是索引已经存在的前提下。如果没有存在则用下面的方式{ mappings:{ properties:{ name:{ type:text, index:true }, sex:{ type:text, index:false }, age:{ type:long, index:false } } } }使用下面这种写法要注意。如果这里定义的index和以前的不一样会报错。{ properties:{ name:{ type:text, index:true }, price:{ type:long, index:true } } }2.3.2 查看索引【GET】 http://hadoop101:9200/test03/_mapping2.4 索引操作了解【GET】 http://hadoop101:9200/_cat/indices?v 查看所有索引【GET】 http://hadoop101:9200/test03 查看单个索引【DELETE】 http://hadoop101:9200/test03 删除索引3. Java API操作3.1 创建Mavendependencies dependency groupIdorg.elasticsearch/groupId artifactIdelasticsearch/artifactId version7.8.0/version /dependency !-- elasticsearch 的客户端 -- dependency groupIdorg.elasticsearch.client/groupId artifactIdelasticsearch-rest-high-level-client/artifactId version7.8.0/version /dependency !-- elasticsearch 依赖 2.x 的 log4j -- dependency groupIdorg.apache.logging.log4j/groupId artifactIdlog4j-api/artifactId version2.8.2/version /dependency dependency groupIdorg.apache.logging.log4j/groupId artifactIdlog4j-core/artifactId version2.8.2/version /dependency dependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-databind/artifactId version2.9.9/version /dependency !-- junit 单元测试 -- dependency groupIdjunit/groupId artifactIdjunit/artifactId version4.12/version /dependency /dependencies3.2 索引操作3.2.1创建索引RestHighLevelClient esClient new RestHighLevelClient( RestClient.builder(new HttpHost(hadoop101, 9200, http)) ); // 创建索引 - 请求对象 CreateIndexRequest request new CreateIndexRequest(users); // 发送请求获取响应 CreateIndexResponse response esClient.indices().create( request, RequestOptions.DEFAULT ); boolean acknowledged response.isAcknowledged(); // 响应状态 System.out.println(操作状态 acknowledged); esClient.close();3.2.2查看索引// 查询索引 GetIndexRequest request new GetIndexRequest(test); // 发送请求获取响应 GetIndexResponse response client.indices().get( request, RequestOptions.DEFAULT ); System.out.println(表索引别称 response.getAliases()); System.out.println(映射mappings response.getMappings()); System.out.println(设置settings response.getSettings());3.2.3删除索引// 删除索引 DeleteIndexRequest request new DeleteIndexRequest(test); // 发送请求获取响应 AcknowledgedResponse response client.indices().delete( request, RequestOptions.DEFAULT ); // 操作结果 System.out.println(操作结果 response.isAcknowledged());3.3 文档操作3.3.1新增文档// 新增文档 IndexRequest request new IndexRequest(); // 设置索引及唯一性标识 request.index(user).id(1001); // 创建数据 User user new User(); user.setName(zhangsan); user.setAge(18); user.setSex(man....); ObjectMapper objectMapper new ObjectMapper(); String productionJson objectMapper.writeValueAsString(user); // 添加文档数据数据格式为Json格式 request.source(productionJson, XContentType.JSON); // 客户端发送请求获取响应对象 IndexResponse response client.index(request, RequestOptions.DEFAULT);3.3.2修改文档要修改多个字段则如下语法// 修改文档 - 请求对象 UpdateRequest request new UpdateRequest(); // 配置修改参数 request.index(user).id(1001); // 修改数据 request.doc(XContentType.JSON, name, 张三, sex, nv); // 客户端发送 UpdateResponse response client.update(request, RequestOptions.DEFAULT);3.3.3查询文档3.3.4删除文档3.3.5批量操作3.4 高级查询3.4.1请求体查询3.4.2高亮查询3.4.3聚合查询