elasticsearch学习(三)

transportclient和restclient区别

使用springboot构建es项目主体方法有两种:transportclientrestclient

  • 方法一:transportclient

transportclient是通过监听es端口的tcp链接进行数据传输,springboot项目可以通过引入spring-boot-starter-data-elasticsearch来实现,是由spring官方所出。但是由于es版本更新较快,而官方内置的es版本更新速度慢,适配的都是较低版本,容易出现版本对应步上的问题。

  • 方法二:restclient

restclient是通过http来进行数据传输,相对于transportclient,它的版本兼容性更好,可以通过整合elasticsearch-rest-high-level-client来实现。且在官方说明中transportclient会在es高版本中被弃用,故推荐使用restclient;

elasticsearch项目构建

  • spring-boot-starter-data-elasticsearch

导入maven工程,es版本与导入的springboot版本相关

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

添加配置文件

spring.data.elasticsearch.client.reactive.endpoints=127.0.0.1:9200
#是否遵守SSL(Secure Sockets Layer 安全套接字协议)协议
spring.data.elasticsearch.client.reactive.use-ssl=false
#连接超时时间
spring.data.elasticsearch.client.reactive.connection-timeout=100000
spring.data.elasticsearch.client.reactive.socket-timeout=500000
#是否开启本地存储
spring.data.elasticsearch.repositories.enabled=true

创建一个实体类

@ApiModel(description = "Goods实体")
@Data
//type已被注释为过失,不传情况下,es会默认创建一个type为:_doc
@Document(indexName = "big_data_center_goods", type = "goods", shards = 2, replicas = 0)
public class Goods implements Serializable {
    @ApiModelProperty(value = "主键ID")
    //指定es中的id
    @Id
    private String id;
    @ApiModelProperty(value = "店铺编码")
    private String shopCode;
    @ApiModelProperty(value = "商品名称")
    private String goodsName;
    @ApiModelProperty(value = "商品价格")
    private BigDecimal price;
    @ApiModelProperty(value = "商品描述")
    private String desc;
    @ApiModelProperty(value = "创建时间")
    private String createTime;
}

创建repository

import cn.pdl.pelasticsearch.entity.Goods;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

@Repository
//ElasticsearchRepository已继承ElasticsearchCrudRepository,其内置了很多方法实现,类似JPA,通过规范方法命名就能实现查询操作;
public interface GoodsRepository extends ElasticsearchRepository<Goods, String>{

    Page<Goods> findAllByGoodsName(String shopName, Pageable pageable);

    Page<Goods> findAllByShopCodeContains(String shopCode, Pageable pageable);

    Page<Goods> findAllById(String id,Pageable pageable);
}

创建service层即其实现

public interface IGoodsService {

    Optional<Goods> findById(String id);

    Page<Goods> getGoods(String id, Pageable pageable);

    Goods save(Goods blog);

    Iterable<Goods> findAll();

    Page<Goods> findByShopCode(String shopCode, Pageable pageable);
}

@Slf4j
@Service
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    @Qualifier("goodsRepository")
    private GoodsRepository goodsRepository;

    @Override
    public Optional<Goods> findById(String id) {
        Optional<Goods> optionalShop = null;
        try {
            optionalShop = goodsRepository.findById(id);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return optionalShop;
    }

    @Override
    public Page<Goods> getGoods(String id, Pageable pageable) {
        return goodsRepository.findAllById(id,pageable);
    }

    @Override
    public Goods save(Goods blog) {
        Goods shop = null;
        try {
            shop = goodsRepository.save(blog);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return shop;
    }

    @Override
    public Iterable<Goods> findAll() {
        Iterable<Goods> shops = null;
        try {
            shops = goodsRepository.findAll();
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return shops;
    }


    @Override
    public Page<Goods> findByShopCode(String shopCode, Pageable pageable) {
        Page<Goods> shopPage = null;
        try {
            shopPage = goodsRepository.findAllByShopCodeContains(shopCode, pageable);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return shopPage;
    }
}

创建controller层

@RestController
@RequestMapping("/es/appApi")
@Api(value = "es相关接口",tags = {"es相关"})
public class ESController {
    @Autowired
    IGoodsService goodsService;

    @ApiOperation(value = "查询所有", notes="查询所有", httpMethod = "GET")
    @GetMapping(value = "/findByAll")
    public ResponseEntity<Iterable<Goods>> findByAll() {
        Iterable<Goods> data = goodsService.findAll();
        return new ResponseEntity<>(data, HttpStatus.OK);
    }

    @ApiOperation(value = "新增商品信息", notes="新增商品信息", httpMethod = "POST")
    @PostMapping(value = "/createGoods")
    public ResponseEntity<Goods> createGoods(@RequestBody Goods goods) {
        String id = UUID.randomUUID().toString().replace("-", "");
        String createTime = LocalDateTime.now().toString();
        goods.setId(id);
        goods.setCreateTime(createTime);
        Goods data = goodsService.save(goods);
        return new ResponseEntity<>(data, HttpStatus.OK);
    }
}
  • elasticsearch-rest-high-level-client

导入maven工程,es版本可以自己根据需要来改变

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.3.1</version>
        </dependency>

配置文件与上面一致
创建实体类

@Data
public class Goods {
    private String goodsName;
    private String goodsCode;
    private String desc;
    private String id;
//索引名称
    private String indexName;
}

封装es方法,实现对数据的增删改查操作,对他入参可根据需要替换成扩展性更高的数据类型

import cn.pdl.pelasticsearch2.entity.Goods;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.ml.PostDataRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.awt.print.Pageable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
@Slf4j
public class ESUtil {
    @Autowired
    private RestHighLevelClient restHighLevelClient;

  //创建索引
    public  String createIndex(String indexName) throws IOException {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest();
        createIndexRequest.index(indexName).settings(Settings.builder()
                .put("index.number_of_shards",3)//设置分片数量,默认为5个
                .put("index.number_of_replicas",2).build());//设置主分片数量
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        return createIndexRequest.index();
    }
  //判断索引是否存在
    public boolean exists(String indexName) throws IOException {
        GetIndexRequest getIndexRequest = new GetIndexRequest();
        getIndexRequest.indices(indexName);
        return restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
    }
  //插入数据
    public RestStatus insert(String indexName, Object object) throws IOException {
        IndexRequest indexRequest = new IndexRequest();
        // source方法里面需要填写对应数据的类型,默认数据类型为json
        indexRequest.index(indexName).source(JSON.toJSONString(object), XContentType.JSON);
        IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        return response.status();
    }
  //查询索引下的所有数据
    public List<Goods>  search(Goods goods) throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder source = new SearchSourceBuilder();
        source.from(0);
        source.size(10);
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        boolQueryBuilder.should(matchAllQueryBuilder);
        searchRequest.indices(goods.getIndexName()).source(source);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        List<Goods> list=new ArrayList<>();
        response.getHits().iterator().forEachRemaining(hit-> {
            list.add(JSON.parseObject(hit.getSourceAsString(), Goods.class));
        });
        return list;
    }
  //根据条件查询
    public List<Goods>  searchByParams(Goods goods) throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder source = new SearchSourceBuilder();
        source.from(0);
        source.size(10);
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        MatchQueryBuilder goodsName = QueryBuilders.matchQuery("goodsName", goods.getGoodsName());
        MatchQueryBuilder desc = QueryBuilders.matchQuery("desc", goods.getDesc());
        boolQueryBuilder.must(desc);
        source.query(boolQueryBuilder);
        searchRequest.indices(goods.getIndexName()).source(source);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        List<Goods> list=new ArrayList<>();
        response.getHits().iterator().forEachRemaining(hit-> {
            list.add(JSON.parseObject(hit.getSourceAsString(), Goods.class));
        });
        return list;
    }
  //根据条件删除数据
    public Boolean deleteData(Goods goods) throws IOException{
        DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest();
        SearchSourceBuilder source = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        MatchQueryBuilder goodsName = QueryBuilders.matchQuery("goodsName", goods.getGoodsName());
        boolQueryBuilder.must(goodsName);
        source.query(boolQueryBuilder);
        deleteByQueryRequest.setQuery(boolQueryBuilder).indices(goods.getIndexName());
        BulkByScrollResponse bulkByScrollResponse = restHighLevelClient.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT);
        return true;
    }
  //根据主键删除数据
    public Boolean deleteById(String indexName,String id) throws IOException{
        DeleteRequest deleteRequest = new DeleteRequest();
        deleteRequest.index(indexName).id(id);
        DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        return true;
    }
}

创建controller层

@Slf4j
@RestController
@RequestMapping("/es/2.0")
@Api(value = "es学习",tags = {"es学习"})
public class ESController {
    @Autowired
    private ESUtil esUtil;

    @PostMapping("/createIndex")
    @ApiOperation(value = "创建索引",notes = "创建索引")
    public String createIndex(@RequestBody Goods goods){
        try {
            esUtil.createIndex(goods.getIndexName());
        } catch (IOException e) {
            return e.getMessage();
        }
        return "success";
    }

    @PostMapping("/insertData")
    @ApiOperation(value = "插入数据",notes = "创建索引")
    public String insertData(@RequestBody Goods goods){
        try {
            esUtil.insert("pdl",goods);
        } catch (IOException e) {
            return e.getMessage();
        }
        return "success";
    }

    @PostMapping("/findAll")
    @ApiOperation(value = "查询所有",notes = "查询所有")
    public List<Goods> findAll(@RequestBody Goods goods){
        List<Goods> list = null;
        try {
            list = esUtil.search(goods);
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return list;
    }

    @PostMapping("/findAllByParams")
    @ApiOperation(value = "根据条件查询所有",notes = "根据条件查询所有")
    public List<Goods> findAllByParams(@RequestBody Goods goods){
        List<Goods> list = null;
        try {
            list = esUtil.searchByParams(goods);
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return list;
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除",notes = "删除")
    public String delete(@RequestBody Goods goods){
        List<Goods> list = null;
        try {
            esUtil.deleteData(goods);
        } catch (IOException e) {
            log.error(e.getMessage());
            return e.getMessage();
        }
        return "success";
    }
}

上一篇:elasticsearch学习(二)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,492评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,048评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,927评论 0 358
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,293评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,309评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,024评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,638评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,546评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,073评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,188评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,321评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,998评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,678评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,186评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,303评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,663评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,330评论 2 358