环境准备:VMware docker
创建一个linux虚拟机,使用docker pull elasticsearch
部署单体服务:
docker pull elasticsearch:6.8.13
#elasticsearch十分占用内存,用这种方式启动会导致Linux卡机
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:6.8.13
#所以需要限制内存(-e ES_JAVA_OPTS='-Xms64m -Xmx512m')
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS='-Xms64m -Xmx512m' elasticsearch:6.8.13
可采用其他文章的创建ealsticsearch
测试是否创建成功: 浏览器访问部署机ip地址:9300 跳转
tagline" : "You Know, for Search"显示成功
PostMan客户端工具
索引操作:
创建索引:PUT http://127.0.0.1:9200/shopping 具有幂等性,二次请求会产生错误
不允许用Post,不具有幂等性
查询&删除 GET http://127.0.0.1:9200/shopping
DELETE http://127.0.0.1:9200/shopping
文档操作:
添加文档: POST http://127.0.0.1:9200/shopping/_doc/1001 在请求体输入JSON格式的数据(必须传递数据不然会报错 request body is required)
elasticsearch7以后 _type默认是_doc,/_doc后面没有自己添加的id会自动添加id,Post是全量修改
查询文档 GET http://127.0.0.1:9200/shopping/_doc/1001
http://127.0.0.1:9200/_search
条件查询 GET http://127.0.0.1:9200/shopping/_serach?q=数据:值
or 请求体上查询
分页查询
在请求体上加上 form 和 size 给相应的值 还有sort排序 _source 哪个字段
多条件查询:
修改文档 POST OR PUT 全量修改 直接覆盖 http://127.0.0.1:9200/shopping/_doc/1001 Body内容修改
局部修改: POST http://127.0.0.1:9200/shopping/_update/1001 {"doc":{}}
JavaAPI 操作 ES
环境准备 引入Maven依赖 版本7以前type需要自己制定,7以后默认为_doc
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>6.5.4</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>6.5.4</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>6.5.4</version> </dependency> 创建ES 客户端 client = RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( HttpHost.create("http://localhost:9200") ) );
索引操作:
创建索引: client.indices()
.create(new CreateIndexRequest().index("shopping"),RequestOptions.DEFAULT);
查询or删除索引: 查询:client.indices() .get(new GetIndexRequest().indices("shopping"),RequestOptions.DEFAULT);
删除:client.indices() .delete(new DeleteIndexRequest().indices("shopping"),RequestOptions.DEFAULT);
文档操作:
不需要写mapping映射属性
添加文档:
ObjectMapper objectMapper = new ObjectMapper(); client.index(newIndexRequest("shopping").id("1002").type("_doc").source(objectMapper.writeValueAsString(//java对象),XContentType.JSON),RequestOptions.DEFAULT);
局部修改文档:
UpdateRequest request = new UpdateRequest(); request.index("shopping"); request.id("2001"); request.doc(XContentType.JSON,objectMapper.writeValueAsString(//java对象) client.update(new UpdateRequest(),RequestOptions.DEFAULT);
查询or删除文档:
查询:
GetRequest request = new GetRequest(); request.index("shopping"); request.id("2001"); client.get(request,RequestOptions.DEFAULT);
删除:
DeleteRequest request = new DeleteRequest(); request.index("shopping"); request.id("2001"); client.delete(request,RequestOptions.DEFAULT);
批量删除 or 增加:
BulkRequest request = new BulkRequest(); bulk.add();//有增加、删除的request client.bulk(request,RequestOptions.DEFAULT);
查询:
全量查询:
SearchRequest request = new SearchRequest(); request.indices("shopping"); request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())); client.search(request,RequestOptions.DEFAULT);
条件查询:
SearchRequest request = new SearchRequest(); request.indices("shopping"); request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",12))); client.search(request,RequestOptions.DEFAULT);
分页查询:
SearchRequest request = new SearchRequest(); request.indices("shopping"); SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()); query.from(); query.sort(); query.size(); request.source(query); client.search(request,RequestOptions.DEFAULT);
字段查询:
SearchRequest request = new SearchRequest(); request.indices("shopping"); SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()); String[] excludes = {}; String[] includes = {}; query.fetchSource(includes,excludes); request.source(query); client.search(request,RequestOptions.DEFAULT);
组合查询:
SearchRequest request = new SearchRequest(); request.indices("shopping"); SearchSourceBuilder builder = new SearchSourceBuilder(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must(); boolQueryBuilder.should(); boolQueryBuilder.mustNot(); builder.query(boolQueryBuilder); request.source(builder); client.search(request,RequestOptions.DEFAULT);
范围查询:
SearchRequest request = new SearchRequest(); request.indices("shopping"); SearchSourceBuilder builder = new SearchSourceBuilder(); RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age"); rangeQueryBuilder.gte(30); rangeQueryBuilder.lte(40); builder.query(rangeQueryBuilder; request.source(builder); client.search(request,RequestOptions.DEFAULT);
模糊查询:
SearchRequest request = new SearchRequest(); request.indices("shopping"); SearchSourceBuilder builder = new SearchSourceBuilder() ; FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name","wangwu").fuzziness(Fuzziness.ONE); builder.query(fuzzyQueryBuilder; request.source(builder); client.search(request,RequestOptions.DEFAULT);
标签:实战,shopping,入门,DEFAULT,request,client,ElasticSearch,query,new From: https://blog.csdn.net/m0_73834500/article/details/139826817