SpringBoot整合SpringDataElasticsearch的過程詳解

Spring Data Elasticsearch提供了ElasticsearchTemplate工具類,實(shí)現(xiàn)了POJO與elasticsearch文檔之間的映射

站在用戶的角度思考問題,與客戶深入溝通,找到潮南網(wǎng)站設(shè)計(jì)與潮南網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋潮南地區(qū)。

elasticsearch本質(zhì)也是存儲(chǔ)數(shù)據(jù),它不支持事物,但是它的速度遠(yuǎn)比數(shù)據(jù)庫(kù)快得多,

可以這樣來(lái)對(duì)比elasticsearch和數(shù)據(jù)庫(kù)

  • 索引(indices)--------數(shù)據(jù)庫(kù)(databases)
  • 類型(type)------------數(shù)據(jù)表(table)
  • 文檔(Document)---------------- 行(row)
  • 字段(Field)-------------------列(Columns )

整合:

1,在SprinBoot工程中引入jar包

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

2,配置文件

spring.data.elasticsearch.cluster-name=elasticsearch //名字必須和elasticsearch.yml里面的cluster.name相同
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300 
spring.data.elasticsearch.repositories.enabled=true

3,創(chuàng)建實(shí)體,并對(duì)類和屬性進(jìn)行標(biāo)注

@Document(indexName = "item",type = "docs", shards = 1, replicas = 0)//標(biāo)記為文檔類型,ndexName:對(duì)應(yīng)索引庫(kù)名稱type:對(duì)應(yīng)在索引庫(kù)中的類型,shards:分片數(shù)量,默認(rèn)5,replicas:副本數(shù)量,默認(rèn)1
public class Item {
  @Id //主鍵
  private Long id;
  @Field(type = FieldType.Text, analyzer = "ik_max_word") //標(biāo)記為成員變量  FieldType,可以是text、long、short、date、integer等  text:存儲(chǔ)數(shù)據(jù)時(shí)候,會(huì)自動(dòng)分詞,并生成索引  keyword:存儲(chǔ)數(shù)據(jù)時(shí)候,不會(huì)分詞建立索引  analyzer:分詞器名稱
  private String title; //標(biāo)題
  @Field(type = FieldType.Keyword)
  private String category;// 分類
  @Field(type = FieldType.Keyword)
  private String brand; // 品牌
  @Field(type = FieldType.Double)
  private Double price; // 價(jià)格
  @Field(index = false, type = FieldType.Keyword)//index:是否索引
  private String images; // 圖片地址

4.引入模板ElasticsearchTemplate

   @Autowired
  private ElasticsearchTemplate elasticsearchTemplate;

5.創(chuàng)建一個(gè)索引

   //添加索引
  @Test
  public void addIndex() {
    elasticsearchTemplate.createIndex(Item.class);
  }

6.刪除索引

  //刪除索引
  @Test
  public void delete(){
    elasticsearchTemplate.deleteIndex("item");
  }

7.新增對(duì)象

繼承Repository提供的一些子接口,就能具備各種基本的CRUD功能,這里繼承ElasticsearchCrudRepository

首先定義一個(gè)對(duì)象的接口

public interface ItemRepository extends ElasticsearchCrudRepository<Item,Long> {
}

然后注入ItemRepository

  @Autowired
  private ItemRepository itemRepository;

新增對(duì)象

//新增一個(gè)對(duì)象
  @Test
  public void insert(){
    Item item = new Item(2L,"堅(jiān)果R1","手機(jī)","錘子",2500.00,"http://image.baidu.com/13123.jpg");
    //Order order = new Order(20180020,"菜單");
    itemRepository.save(item);
  }

批量新增

//批量新增
  @Test
  public void insertList(){
    List<Item> list = new LinkedList<>();
    list.add(new Item(9L,"華為p20","手機(jī)","華為",3500.00,"http://image.baidu.com/13123.jpg"));
    list.add(new Item(10L,"華為p30","手機(jī)","華為",5450.00,"http://image.baidu.com/13123.jpg"));
    list.add(new Item(11L,"華為p30 pro","手機(jī)","華為",6980.00,"http://image.baidu.com/13123.jpg"));
    itemRepository.saveAll(list);
  }

8.查詢

//根據(jù)字段查詢所有
  @Test
  public void queryAll(){
    //升序,相應(yīng)降序?yàn)閐scending
    Iterable<Item> items = this.itemRepository.findAll(Sort.by("price").ascending());
    for (Item item : items){
      System.out.println(item);
    }
  }

9.自定義查詢方法

Spring Data 的另一個(gè)強(qiáng)大功能,是根據(jù)方法名稱自動(dòng)實(shí)現(xiàn)功能,你的方法名叫做:findByTitle,那么它就知道你是根據(jù)title查詢,然后自動(dòng)幫你完成,無(wú)需寫實(shí)現(xiàn)類。當(dāng)然,方法名稱要符合一定的約定:

SpringBoot整合Spring Data Elasticsearch的過程詳解

上圖是截取csdn上博主我要取一個(gè)響亮的昵稱的圖

根據(jù)手機(jī)名查找手機(jī)

//自定義方法,根據(jù)Title查詢
  @Test
  public void findByTitle(){
    Item item = this.itemRepository.findByTitle("堅(jiān)果pro");
    System.out.println(item);
  }

區(qū)間查詢

//根據(jù)區(qū)間查詢
  @Test
  public void queryByPriceBetween(){
    List<Item> list = this.itemRepository.findByPriceBetween(2000.00, 3500.00);
    for (Item item : list) {
      System.out.println("item = " + item);
    }
  }

模糊查詢

//模糊查詢
  @Test
  public void queryLikeTitle(){
    List<Item> list = this.itemRepository.findByTitleLike("R2");
    for (Item item : list){
      System.out.println(item);
    }
  }

使用自定義方法需要在接口里面申明方法

public interface ItemRepository extends ElasticsearchCrudRepository<Item,Long> {
  Item findByTitle(String title);
  List<Item> findByPriceBetween(double price1, double price2);
  List<Item> findByTitleLike(String title);
}

10.自定義查詢

//自定義查詢,查詢數(shù)目等
  @Test
  public void matchQuery(){
    // 構(gòu)建查詢條件
    NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
    // 添加基本分詞查詢
    queryBuilder.withQuery(QueryBuilders.matchQuery("title","堅(jiān)果"));
    //獲取結(jié)果
    Page<Item> items = (Page<Item>) this.itemRepository.findAll();
    //條數(shù)
    long total = items.getTotalElements();
    System.out.println("total = "+total);
    for (Item item : items){
      System.out.println(item);
    }
  }關(guān)鍵的是NativeSearchQueryBuilder這個(gè)類

分頁(yè)查詢

//分頁(yè)查詢
  @Test
  public void queryByPage(){
    NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
    nativeSearchQueryBuilder.withQuery(QueryBuilders.termQuery("category","手機(jī)"));
    int page = 0;
    int size = 2;
    nativeSearchQueryBuilder.withPageable(PageRequest.of(page,size));
    Page<Item> items = (Page<Item>) this.itemRepository.findAll();
    long total = items.getTotalElements();
    int totalPage = items.getTotalPages();
    int nowPage = items.getNumber();
    int pageSize = items.getSize();
    System.out.println("總條數(shù) = "+total);
    System.out.println("總頁(yè)數(shù) = "+totalPage);
    System.out.println("當(dāng)前頁(yè) = "+nowPage);
    System.out.println("每頁(yè)大小 = "+pageSize);
    for (Item item : items){
      System.out.println(item);
    }
  }

還有很多,就不意義列舉

在elasticsearch-head上查看數(shù)據(jù)

SpringBoot整合Spring Data Elasticsearch的過程詳解

關(guān)于安裝elasticsearch-head,參考地址

Spring Data Elasticsearch文檔:https://docs.spring.io/spring-data/elasticsearch/docs/3.1.10.RELEASE/reference/html/

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)站名稱:SpringBoot整合SpringDataElasticsearch的過程詳解
新聞來(lái)源:http://bm7419.com/article16/giggdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、品牌網(wǎng)站設(shè)計(jì)、域名注冊(cè)、全網(wǎng)營(yíng)銷推廣、網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)