elasticarch例子

更新时间:2023-07-05 09:45:03 阅读: 评论:0

elasticarch例⼦依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticarch</artifactId>
</dependency>
配置⽂件:
spring:
data:
elasticarch:
cluster-nodes: 118.24.0.161:9300
例⼦代码:
package com.sbl.ller;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import urrent.ExecutionException;
import org.elasticarch.action.admin.indices.delete.DeleteIndexRequestBuilder;
import org.elasticarch.action.admin.indices.mapping.put.PutMappingRequestBuilder;
import org.elasticarch.action.bulk.BulkRequestBuilder;
import org.elasticarch.action.delete.DeleteRequestBuilder;
import org.elasticarch.action.index.IndexRequestBuilder;
import org.elasticarch.action.index.IndexRespon;
import org.elasticarch.action.arch.SearchRequestBuilder;
import org.elasticarch.action.update.UpdateRequestBuilder;
import nt.XContentBuilder;
import nt.XContentFactory;
import org.elasticarch.index.query.QueryBuilder;
import org.elasticarch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ElasticarchTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sbl.sult.CommonPage;
import com.sbl.ity.OfflineMerchant;
import com.sbl.pay.subaccount.rvice.OfflineMerchantService;
import com.sbl.pay.subaccount.util.BeanUtils;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping(value = "/es")
public class EsController {
@Autowired
ElasticarchTemplate elasticarchTemplate;
public static final String INDEX = "merchant";
public static final String TYPE = "doc";
@GetMapping("create")
@ApiOperation("创建")
public Object create() throws InterruptedException, ExecutionException, IOException {
XContentBuilder source = XContentFactory.jsonBuilder()
.startObject()
.field("properties")
updated
.startObject()
.field("id").
startObject().field("type", "keyword").endObject()
最简单的家长会发言稿
.
endObject()
.endObject();
PutMappingRequestBuilder putMappingRequestBuilder = Client().admin().indices().preparePutMapping( INDEX );        putMappingRequestBuilder.tType(TYPE).tSource(source);
ute().actionGet();
}
@GetMapping("createMapping")
@ApiOperation("创建映射")
public Object createMapping() throws InterruptedException, ExecutionException, IOException {
XContentBuilder source = XContentFactory.jsonBuilder()
.startObject()
.field("properties")
.startObject()
.field("id").
startObject().field("type", "keyword").endObject()
.endObject()
丁香花长什么样子.endObject();
PutMappingRequestBuilder putMappingRequestBuilder = Client().admin().indices().preparePutMapping( INDEX );        putMappingRequestBuilder.tType(TYPE).tSource(source);
ute().actionGet();
}
@GetMapping("drop")
@ApiOperation("删除索引")
public Object drop() throws InterruptedException, ExecutionException, IOException {
DeleteIndexRequestBuilder deleteIndexRequestBuilder = Client().admin().indices()
.prepareDelete(INDEX);
ute().actionGet();
}
/**
* 通过商品查询运费规则
张家界十大名吃
*
* @param offlineMerchantId
* @throws ExecutionException
* @throws InterruptedException
*/
@GetMapping("add")
@ApiOperation("添加⼀条记录")
public IndexRespon add() throws InterruptedException, ExecutionException {
IndexRequestBuilder indexRequestBuilder = Client().prepareIndex();
indexRequestBuilder.tIndex(INDEX).tType(TYPE);
OfflineMerchant offlineMerchant = null;
Map<String, Object> source = new HashMap<String, Object>();
source.put("id", "3123123");
indexRequestBuilder.tSource(source);
ute().get();
}
@Autowired
OfflineMerchantService merchantService;
@GetMapping("bulkAdd")
@ApiOperation("批量添加")
public Object bulkAdd() throws InterruptedException, ExecutionException {
BulkRequestBuilder bulkRequestBuilder = Client().prepareBulk();
CommonPage page = new CommonPage();
page.tPageNum(1);
page.tPageSize(200);
List<OfflineMerchant> list = List(page);
for (OfflineMerchant offlineMerchant : list) {
IndexRequestBuilder indexRequest = Client().prepareIndex();
indexRequest.tIndex(INDEX).tType(TYPE);
indexRequest.Id() + "");
Map<String, Object> source = BeanUtils.objectToMap(offlineMerchant);
indexRequest.tSource(source);
bulkRequestBuilder.add(indexRequest);九州岛
大腿根部内侧疼}
ute().actionGet();
}
@GetMapping("delete")
@ApiOperation("删除⼀条记录")
public Object delete() throws InterruptedException, ExecutionException {
DeleteRequestBuilder deleteRequestBuilder = Client().prepareDelete();
long id = 211892173535453184l;
deleteRequestBuilder.tIndex(INDEX).tType(TYPE).tId(id + "");
ute().get();
}
@GetMapping("update")
@ApiOperation("修改")
public Object update() throws Exception {
刚直不阿UpdateRequestBuilder updateRequestBuilder = Client().prepareUpdate();
updateRequestBuilder.tIndex(INDEX).tType(TYPE);
updateRequestBuilder.tId(241924518082519040L + "");
XContentBuilder source = XContentFactory.jsonBuilder().startObject().field("name", "name").endObject();        System.out.println(source.string());
// Map<String, String> map = new HashMap<>();觉得英语
// map.put("name", "name");
// updateRequestBuilder.tDoc( map );
updateRequestBuilder.tDoc(source);
ute().get();
}
@GetMapping("arch")
@ApiOperation("查询")
public Object arch() throws InterruptedException, ExecutionException {
SearchRequestBuilder indexRequestBuilder = Client().prepareSearch(INDEX);        indexRequestBuilder.tTypes(TYPE);
QueryBuilder queryBuilder = Query("name", "name");
indexRequestBuilder.tQuery(queryBuilder);
ute().get().getHits().getHits().length;
}
}

本文发布于:2023-07-05 09:45:03,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1079621.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:记录   添加   运费   映射   根部   规则
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图