阿里云 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(
                        "{\n" +
                                "    \"id\" : " + goods.getId() + ",\n" +
                                "    \"name\" : \"" + goods.getName()+ "\",\n" +
                                "    \"picture\" : \"" + goods.getPicture() + "\",\n" +
                                "    \"seo_title\" : \"" + goods.getSeoTitle() + "\",\n" +
                                "    \"seo_keywords\" : \"" + goods.getSeoKeywords() + "\"\n" +
                                "}", 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(
                    "{\n" +
                            "    \"id\" : " + goods.getId() + ",\n" +
                            "    \"name\" : \"" + goods.getName()+ "\",\n" +
                            "    \"picture\" : \"" + goods.getPicture() + "\",\n" +
                            "    \"seo_title\" : \"" + goods.getSeoTitle() + "\",\n" +
                            "    \"seo_keywords\" : \"" + goods.getSeoKeywords() + "\"\n" +
                            "}", 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("{\n" +
                "  \"query\": {\n" +
                "    \"match\": {\n" +
                "      \"id\":" + goodsId + "\n" +
                "    }\n" +
                "  }\n" +
                "}", 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

猜你喜欢

转载自www.cnblogs.com/lanqie/p/11165614.html