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;
}
}