elasticsearch 7.4 常用API操作之 在 spring boot 中的使用(二)

1、ES 7.4 整合spring boot,并进行配置

第一步: pom依赖:

<!--ES 微服务插件-->
<properties>
    <java.version>1.8</java.version>
    <elasticsearch.version>7.6.2</elasticsearch.version>
    <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>

 <!--ES高级API-->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.6.2</version>
</dependency>

第二步,编写配置注入bean:

package com.xunqi.gulimall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description:
 * @Created: with IntelliJ IDEA.
 * @author: YL
 * @createTime: 2020-06-04 16:46
 **/

@Configuration
public class GulimallElasticSearchConfig {
   
	
    public static final RequestOptions COMMON_OPTIONS;
    static {
   
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        COMMON_OPTIONS = builder.build();
    }

    @Bean
    public RestHighLevelClient esRestClient(){
   
        RestHighLevelClient client = new RestHighLevelClient(
        //注意,这里的IP和端口写你的
                RestClient.builder(new HttpHost("192.168.56.10", 9200, "http")));
        return  client;
    }
}

第三步,编写配置,自定义阻塞返回方法,设置交互类型为 json:

package com.xunqi.gulimall.search.config;

import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.xunqi.common.exception.BizCodeEnum;
import com.xunqi.common.utils.R;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description: 自定义阻塞返回方法,设置交互类型为 json
 * @Created: with IntelliJ IDEA.
 * @author: YL
 * @createTime: 2020-07-13 11:30
 **/

@Configuration
public class GulimallSearchSentinelConfig {
   

    public GulimallSearchSentinelConfig() {
   

        WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() {
   
            @Override
            public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws IOException {
   
                R error = R.error(BizCodeEnum.TO_MANY_REQUEST.getCode(), BizCodeEnum.TO_MANY_REQUEST.getMessage());
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json");
                response.getWriter().write(JSON.toJSONString(error));
            }
        });
    }
}

2、使用

我们先创建一个mapping

类似于我们想操作数据库,就需要先创建表结构一样

#这是ES中的 mapper 结构
PUT gulimall_product
{
   
  "mappings": {
   
  #创建的字段
    "properties": {
   
      "skuId": {
   
        "type": "long"
      },
      "spuId": {
   
        "type": "long"
      },
      "skuTitle": {
   
        "type": "text",
        #分词类型
        "analyzer": "ik_smart"
      },
      "skuPrice": {
   
        "type": "keyword"
      },
      "skuImg": {
   
        "type": "keyword"
      },
      "saleCount": {
   
        "type": "long"
      },
      "hosStock": {
   
        "type": "boolean"
      },
      "hotScore": {
   
        "type": "long"
      },
      "brandId": {
   
        "type": "long"
      },
      "catelogId": {
   
        "type": "long"
      },
      "brandName": {
   
        "type": "keyword"
      },
      "brandImg": {
   
        "type": "keyword"
      },
      "catalogName": {
   
        "type": "keyword"
      },
      #对象索引
      "attrs": {
   
      #对象索引的类型
        "type": "nested",
        "properties": {
   
          "attrId": {
   
            "type": "long"
          },
          "attrName": {
   
            "type": "keyword"
          },
          "attrValue": {
   
            "type": "keyword"
          }
        }
      }
    }
  }
}

注:可以把JSON 放到 kibana 里去执行

创建好mapping后,就可以使用Java操作数据了。

ES中写入数据

数据写入的实体类操作类封装:

package com.xunqi.common.es;

import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description:
 * @Created: with IntelliJ IDEA.
 * @author: 夏沫止水
 * @createTime: 2020-06-06 14:07
 **/

@Data
public class SkuEsModel {
   

    private Long skuId;

    private Long spuId;

    private String skuTitle;

    private BigDecimal skuPrice;

    private String skuImg;

    private Long saleCount;

    private Boolean hasStock;

    private Long hotScore;

    private Long brandId;

    private Long catalogId;

    private String brandName;

    private String brandImg;

    private String catalogName;

    private List<Attrs> attrs;

    @Data
    public static class Attrs {
   

        private Long attrId;

        private String attrName;

        private String attrValue;
    }
}

写入操作:

package com.xunqi.gulimall.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.xunqi.common.es.SkuEsModel;
import com.xunqi.gulimall.search.config.GulimallElasticSearchConfig;
import com.xunqi.gulimall.search.constant.EsConstant;
import com.xunqi.gulimall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的小蜗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值