zoukankan      html  css  js  c++  java
  • 阿里云 elasticsearch 增删改查

    kibana 控制台

    # 查询所有数据
    GET /yixiurds_dev/_search
    {
      "query": {
        "match_all": {
        }
      }
    }
    
    # 查询数据
    GET /yixiurds_dev/elasticsearch/_search
    {
      "query": {
        "match": {
          "id":126
        }
      }
    }
    
    # 添加数据
    POST /yixiurds_dev/elasticsearch/
    {
      "id":126,
      "name":"测试",
      "picture":"//yixiulian.oss-cn-shenzhen.aliyuncs.com/youpin/storage/376b74064cfeba5eff83b13ec24a269a.jpg",
      "seo_title": "",
      "seo_keywords": ""
    }
    
    # 修改数据
    PUT /yixiurds_dev/elasticsearch/MQmk2msBS7i08yOenxyX
    {
      "id":126,
      "name":"测试2",
      "picture":"//yixiulian.oss-cn-shenzhen.aliyuncs.com/youpin/storage/376b74064cfeba5eff83b13ec24a269a.jpg",
      "seo_title": "",
      "seo_keywords": ""
    
    }
    
    # 不能删除数据
    DELETE /yixiurds_dev/elasticsearch/_delete
    {
      "id":126
    }
    
    # 删除数据
    DELETE /yixiurds_dev/elasticsearch/MQmk2msBS7i08yOenxyX
    {}
    
    # 删除索引
    DELETE /yixiurds_dev
    
    # 创建索引
    PUT /yixiurds_dev
    
    

    代码示例

    @Component
    public class ElasticsearchUtils {
    
        private static final Logger logger = LoggerFactory.getLogger(ElasticsearchUtils.class);
        private static String elasticHostName;
        private static String elasticUserName;
        private static String elasticPassword;
        private static String elasticIndex;
        private static RestClient restClient;
    
        private static RestClient getRestClient() {
            if (restClient == null) {
                synchronized (ElasticsearchUtils.class) {
                    if (restClient == null) {
                        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(elasticUserName, elasticPassword));
                        restClient = RestClient.builder(new HttpHost(elasticHostName, 9200))
                                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)).build();
                    }
                    return restClient;
                }
            } else {
                return restClient;
            }
        }
    
        /**
         * 更新搜索的商品
         */
        public static void updateGoodsList(List<Goods> goodsList) {
            RestClient restClient = getRestClient();
            goodsList.forEach(goods -> {
                GoodsEsResp goodsEsResp = getGoods(goods.getId());
                List<String> esIds = goodsEsResp.getEsIds();
                if (CollectionUtils.isEmpty(esIds)) {
                    return;
                }
    
                esIds.forEach(esId -> {
                    String method = "PUT";
                    String endpoint = "/" + elasticIndex + "/elasticsearch/" + esId;
                    HttpEntity entity = new NStringEntity(
                            "{
    " +
                                    "    "id" : " + goods.getId() + ",
    " +
                                    "    "name" : "" + goods.getName()+ "",
    " +
                                    "    "picture" : "" + goods.getPicture() + "",
    " +
                                    "    "seo_title" : "" + goods.getSeoTitle() + "",
    " +
                                    "    "seo_keywords" : "" + goods.getSeoKeywords() + ""
    " +
                                    "}", ContentType.APPLICATION_JSON);
    
                    Response response = null;
                    try {
                        response = restClient.performRequest(method, endpoint, Collections.<String, String>emptyMap(), entity);
                        logger.info(JSON.toJSONString(response));
                    } catch (IOException e) {
                        logger.error(e.toString());
                    }
    
                });
    
            });
        }
    
    
        /**
         * 添加搜索的商品
         */
        public static void addGoodsList(List<Goods> goodsList) {
            RestClient restClient = getRestClient();
            goodsList.forEach(goods -> {
                String method = "POST";
                String endpoint = "/" + elasticIndex + "/elasticsearch/";
                HttpEntity entity = new NStringEntity(
                        "{
    " +
                                "    "id" : " + goods.getId() + ",
    " +
                                "    "name" : "" + goods.getName()+ "",
    " +
                                "    "picture" : "" + goods.getPicture() + "",
    " +
                                "    "seo_title" : "" + goods.getSeoTitle() + "",
    " +
                                "    "seo_keywords" : "" + goods.getSeoKeywords() + ""
    " +
                                "}", ContentType.APPLICATION_JSON);
    
                Response response = null;
                try {
                    response = restClient.performRequest(method, endpoint, Collections.<String, String>emptyMap(), entity);
                    logger.info(JSON.toJSONString(response));
                } catch (IOException e) {
                    logger.error(e.toString());
                }
    
            });
        }
    
        /**
         * 删除搜索的商品
         */
        public static void delGoodsList(List<Goods> goodsList) {
            RestClient restClient = getRestClient();
            goodsList.forEach(goods -> {
                GoodsEsResp goodsEsResp = getGoods(goods.getId());
                List<String> esIds = goodsEsResp.getEsIds();
                if (CollectionUtils.isEmpty(esIds)) {
                    return;
                }
    
                esIds.forEach(esId -> {
                    String method = "DELETE";
                    String endpoint = "/" + elasticIndex + "/elasticsearch/" + esId;
                    HttpEntity entity = new NStringEntity(
                            "{}", ContentType.APPLICATION_JSON);
                    Response response = null;
                    try {
                        response = restClient.performRequest(method, endpoint, Collections.<String, String>emptyMap(), entity);
                        logger.info(JSON.toJSONString(response));
                    } catch (IOException e) {
                        logger.error(e.toString());
                    }
    
                });
    
            });
        }
    
        /**
         * 获取搜索的商品
         */
        public static GoodsEsResp getGoods(Long goodsId) {
            GoodsEsResp goodsEsResp = new GoodsEsResp();
            RestClient restClient = getRestClient();
            String method = "GET";
            String endpoint = "/" + elasticIndex + "/elasticsearch/_search";
            HttpEntity entity = new NStringEntity("{
    " +
                    "  "query": {
    " +
                    "    "match": {
    " +
                    "      "id":" + goodsId + "
    " +
                    "    }
    " +
                    "  }
    " +
                    "}", ContentType.APPLICATION_JSON);
    
            Response response = null;
            try {
                response = restClient.performRequest(method, endpoint, Collections.<String, String>emptyMap(), entity);
                JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(response.getEntity()));
                Object hits = jsonObject.get("hits");
                JSONArray jsonArray = (JSONArray) ((JSONObject) hits).get("hits");
                List<String> esIds = new ArrayList<>();
                jsonArray.forEach(o -> {
                    String esId = (String) ((JSONObject) o).get("_id");
                    if (StringUtils.isNotBlank(esId)) {
                        esIds.add(esId);
                    }
                });
                goodsEsResp.setEsIds(esIds);
                logger.info(JSON.toJSONString(response));
            } catch (IOException e) {
                logger.error(e.toString());
            }
    
            return goodsEsResp;
        }
    
    
        public String getElasticHostName() {
            return elasticHostName;
        }
    
        @Value("${aliyun_elasticsearch_host}")
        public void setElasticHostName(String elasticHostName) {
            ElasticsearchUtils.elasticHostName = elasticHostName;
        }
    
        public String getElasticUserName() {
            return elasticUserName;
        }
    
        @Value("${aliyun_elasticsearch_username}")
        public void setElasticUserName(String elasticUserName) {
            ElasticsearchUtils.elasticUserName = elasticUserName;
        }
    
        public String getElasticPassword() {
            return elasticPassword;
        }
    
        @Value("${aliyun_elasticsearch_password}")
        public void setElasticPassword(String elasticPassword) {
            ElasticsearchUtils.elasticPassword = elasticPassword;
        }
    
        public String getElasticIndex() {
            return elasticIndex;
        }
    
        @Value("${aliyun_elasticsearch_index}")
        public void setElasticIndex(String elasticIndex) {
            ElasticsearchUtils.elasticIndex = elasticIndex;
    
        }
    }
    

    指定ID代替自动生成的_id可以节省一次查询

    # 添加数据
    POST /index_test/type_test/127
    {
      "id":127,
      "name":"测试",
      "picture":"//yixiulian.oss-cn-shenzhen.aliyuncs.com/youpin/storage/376b74064cfeba5eff83b13ec24a269a.jpg",
      "seo_title": "",
      "seo_keywords": ""
    }
    

    查询所有:

    # id查询数据
    GET /index_test/type_test/127
    

    批量同步

    DataWorks定时同步数据库的数据到ES

    一个index 对应多个type:

    PUT /index_test4
    {
      "mappings":{
                "type_1":{
                            "_all" : {"enabled" : true},
                            "properties":{
                                        "field_1":{ "type":"string"},
                                        "field_2":{ "type":"long"}
                            }
                },
                "type_2":{
                            "properties":{
                                        "field_3":{ "type":"string"},
                                        "field_4":{ "type":"date"}
                            }
                }
      }
    }
    
  • 相关阅读:
    C# winform 数据库链接
    Second easyui框架学习
    First,映射数据库到项目
    Mybatis随笔
    spring注解简单了解
    SSH Mybatis 框架
    Maven pom.xml
    Spring
    LayaBox 摄像机Unit8Array数据获取、截图
    lvs和keepalived搭建高可用性系统
  • 原文地址:https://www.cnblogs.com/lanqie/p/11165614.html
Copyright © 2011-2022 走看看