zoukankan      html  css  js  c++  java
  • elasticsearch 例子

    依赖:

    	 <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            </dependency>
    

      

    配置文件:

    spring:
      data:
        elasticsearch: 
          cluster-nodes: 118.24.0.161:9300 
    

      

    例子代码:

    package com.sbl.pay.subaccount.controller;
    
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.ExecutionException;
    
    import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder;
    import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder;
    import org.elasticsearch.action.bulk.BulkRequestBuilder;
    import org.elasticsearch.action.delete.DeleteRequestBuilder;
    import org.elasticsearch.action.index.IndexRequestBuilder;
    import org.elasticsearch.action.index.IndexResponse;
    import org.elasticsearch.action.search.SearchRequestBuilder;
    import org.elasticsearch.action.update.UpdateRequestBuilder;
    import org.elasticsearch.common.xcontent.XContentBuilder;
    import org.elasticsearch.common.xcontent.XContentFactory;
    import org.elasticsearch.index.query.QueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.sbl.pay.subaccount.common.result.CommonPage;
    import com.sbl.pay.subaccount.entity.OfflineMerchant;
    import com.sbl.pay.subaccount.service.OfflineMerchantService;
    import com.sbl.pay.subaccount.util.BeanUtils;
    
    import io.swagger.annotations.ApiOperation;
    
    @RestController
    @RequestMapping(value = "/es")
    public class EsController {
    
        @Autowired
        ElasticsearchTemplate elasticsearchTemplate;
    
        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")
                                    .startObject()
                                        .field("id").
                                            startObject().field("type", "keyword").endObject()
                                    .endObject()
                           .endObject();
    
            elasticsearchTemplate.getClient().admin().indices().prepareCreate(INDEX).execute().actionGet();
            PutMappingRequestBuilder putMappingRequestBuilder = elasticsearchTemplate.getClient().admin().indices().preparePutMapping( INDEX );
            putMappingRequestBuilder.setType(TYPE).setSource(source);
    
            return putMappingRequestBuilder.execute().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 = elasticsearchTemplate.getClient().admin().indices().preparePutMapping( INDEX );
            putMappingRequestBuilder.setType(TYPE).setSource(source);
    
            return putMappingRequestBuilder.execute().actionGet();
        }
        
        
        
    
        @GetMapping("drop")
        @ApiOperation("删除索引")
        public Object drop() throws InterruptedException, ExecutionException, IOException {
            DeleteIndexRequestBuilder deleteIndexRequestBuilder = elasticsearchTemplate.getClient().admin().indices()
                    .prepareDelete(INDEX);
            return deleteIndexRequestBuilder.execute().actionGet();
        }
    
        /**
         * 通过商品查询运费规则
         * 
         * @param offlineMerchantId
         * @throws ExecutionException
         * @throws InterruptedException
         */
        @GetMapping("add")
        @ApiOperation("添加一条记录")
        public IndexResponse add() throws InterruptedException, ExecutionException {
            IndexRequestBuilder indexRequestBuilder = elasticsearchTemplate.getClient().prepareIndex();
            indexRequestBuilder.setIndex(INDEX).setType(TYPE);
    
            OfflineMerchant offlineMerchant = null;
            Map<String, Object> source = new HashMap<String, Object>();
            source.put("id", "3123123");
    
            indexRequestBuilder.setSource(source);
            return indexRequestBuilder.execute().get();
        }
    
        @Autowired
        OfflineMerchantService merchantService;
    
        @GetMapping("bulkAdd")
        @ApiOperation("批量添加")
        public Object bulkAdd() throws InterruptedException, ExecutionException {
            BulkRequestBuilder bulkRequestBuilder = elasticsearchTemplate.getClient().prepareBulk();
    
            CommonPage page = new CommonPage();
            page.setPageNum(1);
            page.setPageSize(200);
            List<OfflineMerchant> list = merchantService.getList(page);
    
            for (OfflineMerchant offlineMerchant : list) {
                IndexRequestBuilder indexRequest = elasticsearchTemplate.getClient().prepareIndex();
    
                indexRequest.setIndex(INDEX).setType(TYPE);
                indexRequest.setId(offlineMerchant.getId() + "");
    
                Map<String, Object> source = BeanUtils.objectToMap(offlineMerchant);
    
                indexRequest.setSource(source);
    
                bulkRequestBuilder.add(indexRequest);
            }
    
            return bulkRequestBuilder.execute().actionGet();
        }
    
        @GetMapping("delete")
        @ApiOperation("删除一条记录")
        public Object delete() throws InterruptedException, ExecutionException {
            DeleteRequestBuilder deleteRequestBuilder = elasticsearchTemplate.getClient().prepareDelete();
    
            long id = 211892173535453184l;
            deleteRequestBuilder.setIndex(INDEX).setType(TYPE).setId(id + "");
    
            return deleteRequestBuilder.execute().get();
        }
    
        @GetMapping("update")
        @ApiOperation("修改")
        public Object update() throws Exception {
            UpdateRequestBuilder updateRequestBuilder = elasticsearchTemplate.getClient().prepareUpdate();
            updateRequestBuilder.setIndex(INDEX).setType(TYPE);
    
            updateRequestBuilder.setId(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.setDoc( map );
            updateRequestBuilder.setDoc(source);
    
            return updateRequestBuilder.execute().get();
        }
    
        @GetMapping("search")
        @ApiOperation("查询")
        public Object search() throws InterruptedException, ExecutionException {
            SearchRequestBuilder indexRequestBuilder = elasticsearchTemplate.getClient().prepareSearch(INDEX);
            indexRequestBuilder.setTypes(TYPE);
    
            QueryBuilder queryBuilder = QueryBuilders.termQuery("name", "name");
    
            indexRequestBuilder.setQuery(queryBuilder);
            return indexRequestBuilder.execute().get().getHits().getHits().length;
        }
    
    
    }
  • 相关阅读:
    docker安装RabbitMQ
    通过Docker安装配置Mysql主从节点
    Docker基本使用命令
    flask接收post提交的json数据并保存至数据库
    前端面经
    js 仿朋友圈的时间显示 刚刚 几天前
    外部div宽度不是100%时,css设置图片宽高相等
    Vue项目图片剪切上传——vue-cropper的使用(二)
    Vue项目图片剪切上传——vue-cropper的使用
    vuex
  • 原文地址:https://www.cnblogs.com/cxygg/p/9723524.html
Copyright © 2011-2022 走看看