马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
前言:
elasticsearch的整合流程可以参考:Elasticsearch7.15版本后新版本的接入-CSDN博客
索引
1.创建索引
- @Test
- public void contextLoads() throws IOException {
- ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
-
- boolean exists = elasticsearchClient.indices().exists(query -> query.index("new_ceshi")).value();
- System.out.println(exists);
- if (exists) {
- System.out.println("已存在");
- } else {
- final CreateIndexResponse products = elasticsearchClient.indices().create(builder -> builder.index("new_ceshi"));
- System.out.println(products.acknowledged());
- }
- }
复制代码 2.查询索引
- @Test
- public void contextLoads() throws IOException {
- ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
- GetIndexResponse products = elasticsearchClient.indices().get(query -> query.index("new_ceshi"));
- System.out.println(products.toString());
- }
复制代码 3.删除索引
- @Test
- public void contextLoads() throws IOException {
- ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
- boolean exists = elasticsearchClient.indices().exists(query -> query.index("new_ceshi")).value();
- System.out.println(exists);
- if (exists) {
- DeleteIndexResponse response = elasticsearchClient.indices().delete(query -> query.index("new_ceshi"));
- System.out.println(response.acknowledged());
- } else {
- System.out.println("索引不存在");
- }
- }
复制代码 索引映射
4.查询索引的映射
- @Test
- public void contextLoads() throws IOException {
- ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
- GetIndexResponse response = elasticsearchClient.indices().get(builder -> builder.index("new_bank"));
- System.out.println(response.result().get("new_bank").mappings().toString());
- }
复制代码 5.创建索引以及初始化索引映射
- @Test
- public void contextLoads() throws IOException {
- ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
- elasticsearchClient.indices()
- .create(builder -> builder.index("new_product")
- .mappings(map -> map.properties("name", p -> p.text(textProperty -> textProperty.analyzer("ik_max_word").searchAnalyzer("ik_max_word")))
- .properties("intro", p -> p.text(textProperty -> textProperty.analyzer("ik_max_word").searchAnalyzer("ik_max_word")))
- .properties("stock", p -> p.integer(integerProperty -> integerProperty)))
- );
- }
复制代码 文档
6.创建文档-自界说类数据存储容器
- @Test
- public void contextLoads() throws IOException {
- ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
- Produce produce = new Produce("饼干", "上好的饼干", 2000);
- IndexResponse response = elasticsearchClient.index(builder -> builder.index("new_product").id("1").document(produce));
- System.err.println(response.version());
- }
复制代码 效果:
7.创建文档-HashMap存储容器
- @Test
- public void contextLoads() throws IOException {
- ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
- HashMap<String, Object> doc = new HashMap<>();
- doc.put("name","油条");
- doc.put("intro","纯油炸的油条");
- doc.put("stock","999");
- final IndexResponse response = elasticsearchClient.index(builder -> builder.index("new_product").id("2").document(doc));
- }
复制代码 8.查询全部文档
- @Test
- public void contextLoads() throws IOException {
- ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
- SearchResponse<Object> response = elasticsearchClient.search(builder -> builder.index("new_product"), Object.class);
- List<Hit<Object>> hits = response.hits().hits();
- hits.forEach(
- x-> System.out.println(x.toString())
- );
- }
复制代码 9.查询某个id的文档
- @Test
- public void contextLoads() throws IOException {
- ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
- GetRequest new_product = new GetRequest.Builder()
- .index("new_product")
- .id("1")
- .build();
- GetResponse<Object> objectGetResponse = elasticsearchClient.get(new_product, Object.class);
- System.out.printf("objectGetResponse=========="+objectGetResponse.source());
- }
复制代码 10删除文档
- @Test
- public void contextLoads() throws IOException {
- ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
- //删除文档
- DeleteRequest new_product = new DeleteRequest.Builder()
- .index("new_product")
- .id("1")
- .build();
- DeleteResponse delete = elasticsearchClient.delete(new_product);
- System.out.printf("delete==========" + delete);
- }
复制代码 11.更新文档-自界说类
全更新
- @Test
- public void contextLoads() throws IOException {
- ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
- Produce produce = new Produce("铁锤", "全刚的大铁锤", 666);
- UpdateResponse<Produce> new_product = elasticsearchClient.update(builder -> builder.index("new_product").id("2").doc(produce), Produce.class);
- System.err.println(new_product.shards().successful());
- }
复制代码 指定字段修改.docAsUpsert(true)
- @Test
- public void contextLoads() throws IOException {
- ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
- Produce produce = new Produce();
- produce.setName("小铁锤");
- UpdateResponse<Produce> new_product = elasticsearchClient.update(builder -> builder.index("new_product").id("2").docAsUpsert(true).doc(produce), Produce.class);
- System.err.println(new_product.shards().successful());
- }
复制代码 12更新文档-Map更新
- @Test
- public void contextLoads() throws IOException {
- ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
- Map<String, Object> updateJson = new HashMap<>();
- updateJson.put("name","巨大铁锤");
- UpdateRequest<Object, Object> updateRequest = new UpdateRequest.Builder<>()
- .index("new_product")
- .id("2")
- .doc(updateJson)
- .build();
- UpdateResponse<Object> updateResponse = elasticsearchClient.update(updateRequest, Object.class);
- System.out.println("Document updated: " + updateResponse.result());
- }
复制代码 批量操作
13批量添加
- @Test
- public void contextLoads() throws IOException {
- ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
- List<SkuEsModel> skuEsModels = new ArrayList<>();
- BulkRequest.Builder br = new BulkRequest.Builder();
- for (SkuEsModel skuEsModel : skuEsModels) {
- br.operations(op->op.index(idx->idx.index("produces").id(String.valueOf(skuEsModel.getSkuId())).document(skuEsModel)));
- }
- BulkResponse response = elasticsearchClient.bulk(br.build());
- }
复制代码
复杂检索请查看:elasticsearch复杂检索,match、matchAll、matchPhrase、term、多条件查询、multiMatch多字段查询等
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |