引入依赖
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> </dependency>
初始化对象
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder( HttpHost.create("http://192.168.150.101:9200") ));
索引CRUD操作
public class ElasticIndexTest { private RestHighLevelClient client; @BeforeEach void setUp(){ client=new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.88.95",9200,"http"))); } @AfterEach void tearDown() throws IOException { if(client!=null) client.close(); } @Test void create() throws IOException { CreateIndexRequest test = new CreateIndexRequest("hmall"); test.source(MAPPING_TEMPLATE, XContentType.JSON); client.indices().create(test, RequestOptions.DEFAULT); } @Test void get() throws IOException { GetIndexRequest test = new GetIndexRequest("hmall"); client.indices().get(test, RequestOptions.DEFAULT); } @Test void delete() throws IOException { DeleteIndexRequest test = new DeleteIndexRequest("hmall"); client.indices().delete(test, RequestOptions.DEFAULT); } private final static String MAPPING_TEMPLATE="{\n" + " \"mappings\": {\n" + " \"properties\": {\n" + " \"id\":{\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"name\":{\n" + " \"type\": \"text\",\n" + " \"analyzer\": \"ik_smart\"\n" + " },\n" + " \"price\":{\n" + " \"type\": \"integer\"\n" + " },\n" + " \"image\":{\n" + " \"type\": \"keyword\",\n" + " \"index\": false\n" + " },\n" + " \"category\":{\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"brand\":{\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"sold\":{\n" + " \"type\": \"integer\"\n" + " },\n" + " \"comment_count\":{\n" + " \"type\": \"integer\",\n" + " \"index\": false\n" + " },\n" + " \"isAD\":{\n" + " \"type\": \"boolean\" \n" + " },\n" + " \"update_time\":{\n" + " \"type\": \"date\"\n" + " }\n" + " }\n" + " }\n" + "}"; }
文档CRUD操作
public class ElasticDocumentTest { @Autowired private IItemService service; private RestHighLevelClient client; @BeforeEach void setUp(){ client=new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.88.95",9200,"http"))); } @AfterEach void tearDown() throws IOException { if(client!=null) client.close(); } /* 新增 * 全量修改:写入重复ID即视为全量修改 * */ @Test void addAndUpdateDoc() throws IOException { Item byId = service.getById("317578"); ItemDoc itemDoc = BeanUtil.copyProperties(byId, ItemDoc.class); IndexRequest request=new IndexRequest("hmall").id(itemDoc.getId()); request.source(JSONUtil.toJsonStr(itemDoc),XContentType.JSON); client.index(request,RequestOptions.DEFAULT); } /*局部修改*/ @Test void updateDoc() throws IOException { UpdateRequest request=new UpdateRequest("hmall","317578"); request.doc( "price","25600" ); client.update(request,RequestOptions.DEFAULT); } /*查询*/ @Test void getDoc() throws IOException { GetRequest getRequest=new GetRequest("hmall","317578"); GetResponse response = client.get(getRequest, RequestOptions.DEFAULT); String json = response.getSourceAsString(); ItemDoc itemDoc = JSONUtil.toBean(json, ItemDoc.class); } /*删除*/ @Test void deleteDoc() throws IOException { DeleteRequest deleteRequest=new DeleteRequest("hmall","317578"); client.delete(deleteRequest, RequestOptions.DEFAULT); } }
文档批处理操作:
/*批处理*/ @Test void bulkDoc() throws IOException { int pageSize=500; int pageNum=1; while(true){ Page<Item> page = service.lambdaQuery() .eq(Item::getStatus, 1) .page(Page.of(pageNum, pageSize)); List<Item>items=page.getRecords(); if(items==null||items.isEmpty()){ return; } BulkRequest request=new BulkRequest(); for (Item item : items) { request.add(new IndexRequest("hmall") .id(item.getId().toString()) .source(JSONUtil.toJsonStr(BeanUtil.copyProperties(item, ItemDoc.class)),XContentType.JSON) ); } client.bulk(request,RequestOptions.DEFAULT); pageNum++; } }
标签:java,void,client,IOException,引入,new,type,throws,es From: https://www.cnblogs.com/kun1790051360/p/18184010