91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot+Elasticsearch

發布時間:2020-08-09 04:24:29 來源:網絡 閱讀:2577 作者:小楊Java 欄目:大數據
  1. 前言
    1.1. 集成方式
    Spring Boot中集成Elasticsearch有4種方式:
    本文用后面兩種方式來分別連接并操作Elasticsearch
    1.2. 環境與配置
    服務端:elasticsearch-6.3.2 1臺
    客戶端:elasticsearch 6.4.1
    服務端配置文件:elasticsearch.yml
    /etc/security/limits.conf
    /etc/sysctl.conf
    1.3. 版本
    Spring Boot 2.0.5默認的elasticsearch版本很低,這里我們用最新版本6.4.1
    如果啟動過程中出現
    則說明,elasticsearch依賴的jar包版本不一致,統一改成6.4.1即可
    另外,Spring Boot 2.0.5依賴的spring-data-elasticsearch版本是3.0.1,需要升級到3.1.0
    SpringBoot+Elasticsearch
  2. 依賴
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cjs.example</groupId>
    <artifactId>cjs-elasticsearch-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>cjs-elasticsearch-example</name>
    <description></description>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <elasticsearch.version>6.4.1</elasticsearch.version>
    <spring.data.elasticsearch.version>3.1.0.RELEASE</spring.data.elasticsearch.version>
    </properties>
    <dependencies>
    <dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>${elasticsearch.version}</version>
    </dependency>
    <dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>${elasticsearch.version}</version>
    </dependency>
    <dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>${elasticsearch.version}</version>
    </dependency>
    <dependency>
    <groupId>org.elasticsearch.plugin</groupId>
    <artifactId>transport-netty4-client</artifactId>
    <version>${elasticsearch.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>${spring.data.elasticsearch.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    </dependencies>
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>
    </project>
  3. application.properties
    也許,大家會疑惑,配置文件中明明寫的端口是9200,為何這里配置文件中連接的時候寫的端口是9300呢?
    因為,配置9200是通過HTTP連接的端口,9300是TCP連接的端口
    SpringBoot+Elasticsearch
  4. 操作
    4.1. 使用Spring Data Elasticsearch Repositories操作Elasticsearch
    首先,定義一個實體類
    package com.cjs.example.entity;
    import lombok.Data;
    import org.springframework.data.annotation.Id;
    import org.springframework.data.elasticsearch.annotations.Document;
    import java.io.Serializable;@Data
    br/>@Data
    public class Commodity implements Serializable {@Id
    br/>@Id
    private String name;
    private String category;
    private Integer price;
    private String brand;
    private Integer stock;
    這里定義了Commodity實例,表示商品。在Elasticsearch 6.X 版本中,不建議使用type,而且在7.X版本中將會徹底廢棄type,所以此處我只指定了indexName,沒有指定type。這里,一個Commodity代表一個商品,同時代表一條索引記錄。
    類比關系型數據庫的話,Index相當于表,Document相當于記錄
    然后,需要自己定義一個接口,并繼承ElasticsearchRepository
    SpringBoot+Elasticsearch
    package com.cjs.example.dao;
    import com.cjs.example.entity.Commodity;
    import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
    import org.springframework.stereotype.Repository;@Repository
    br/>@Repository
    這里的Repository相當于DAO,操作mysql還是elasticsearch都是一樣的
    接下來,定義service接口
    package com.cjs.example.service;
    import com.cjs.example.entity.Commodity;
    import org.springframework.data.domain.Page;
    import java.util.List;
    public interface CommodityService {
    long count();
    Commodity save(Commodity commodity);
    void delete(Commodity commodity);
    Iterable<Commodity> getAll();
    List<Commodity> getByName(String name);
    Page<Commodity> pageQuery(Integer pageNo, Integer pageSize, String kw);
    實現類
    package com.cjs.example.service.impl;
    import com.cjs.example.entity.Commodity;
    import com.cjs.example.dao.CommodityRepository;
    import com.cjs.example.service.CommodityService;
    import org.elasticsearch.index.query.MatchQueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
    import org.springframework.data.elasticsearch.core.query.SearchQuery;
    import org.springframework.stereotype.Service;
    import java.util.ArrayList;
    import java.util.List;@Service
    br/>@Service
    br/>@Autowired
    br/>@Override
    return commodityRepository.count();@Override
    br/>@Override
    return commodityRepository.save(commodity);@Override
    br/>@Override
    commodityRepository.delete(commodity);
    // commodityRepository.deleteById(commodity.getSkuId());@Override
    br/>@Override
    return commodityRepository.findAll();@Override
    br/>@Override
    List<Commodity> list = new ArrayList<>();
    MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("name", name);
    Iterable<Commodity> iterable = commodityRepository.search(matchQueryBuilder);
    iterable.forEach(e->list.add(e));
    return list;@Override
    br/>@Override
    SearchQuery searchQuery = new NativeSearchQueryBuilder()
    .withQuery(QueryBuilders.matchPhraseQuery("name", kw))
    .withPageable(PageRequest.of(pageNo, pageSize))
    .build();
    return commodityRepository.search(searchQuery);
    在這個Service中演示了增刪查改操作,還有分頁查詢
    最后,寫一個測試類測試其中的方法
    package com.cjs.example;
    import com.cjs.example.entity.Commodity;
    import com.cjs.example.service.CommodityService;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.domain.Page;
    import org.springframework.test.context.junit4.SpringRunner;
    import java.util.List;@RunWith(SpringRunner.class)
    br/>@RunWith(SpringRunner.class)
    public class CjsElasticsearchExampleApplicationTests {@Autowired
    br/>@Autowired
    br/>@Test
    @Test
    br/>System.out.println(commodityService.count());
    @Test
    Commodity commodity = new Commodity();
    commodity.setSkuId("1501009001");
    commodity.setName("原味切片面包(10片裝)");
    commodity.setCategory("101");
    commodity.setPrice(880);
    commodity.setBrand("良品鋪子");
    commodityService.save(commodity);
    commodity = new Commodity();
    commodity.setSkuId("1501009002");
    commodity.setName("原味切片面包(6片裝)");
    commodity.setCategory("101");
    commodity.setPrice(680);
    commodity.setBrand("良品鋪子");
    commodityService.save(commodity);
    commodity = new Commodity();commodity.setSkuId("1501009004");
    commodity.setName("元氣吐司850g");
    commodity.setCategory("101");
    commodity.setPrice(120);
    commodity.setBrand("百草味");
    commodityService.save(commodity);
    @Test
    br/>commodity.setSkuId("1501009004");
    commodity.setName("元氣吐司850g");
    commodity.setCategory("101");
    commodity.setPrice(120);
    commodity.setBrand("百草味");
    commodityService.save(commodity);
    @Test
    Commodity commodity = new Commodity();commodity.setSkuId("1501009002");
    commodityService.delete(commodity);
    @Test
    br/>commodity.setSkuId("1501009002");
    commodityService.delete(commodity);
    @Test
    Iterable<Commodity> iterable = commodityService.getAll();iterable.forEach(e->System.out.println(e.toString()));
    @Test
    br/>iterable.forEach(e->System.out.println(e.toString()));
    @Test
    List<Commodity> list = commodityService.getByName("面包");System.out.println(list);
    @Test
    br/>System.out.println(list);
    @Test
    Page<Commodity> page = commodityService.pageQuery(0, 10, "切片");
    System.out.println(page.getTotalPages());
    System.out.println(page.getNumber());
    System.out.println(page.getContent());
    以上,便是使用Elasticsearch Repositories的方式
    4.2. 使用ElasticsearchTemplate方式操作Elasticsearch
    package com.cjs.example;
    import com.cjs.example.entity.Commodity;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
    import org.springframework.data.elasticsearch.core.query.*;
    import org.springframework.test.context.junit4.SpringRunner;
    import java.util.List;@RunWith(SpringRunner.class)
    br/>@RunWith(SpringRunner.class)
    public class ElasticsearchTemplateTest {@Autowired
    br/>@Autowired
    br/>@Test
    Commodity commodity = new Commodity();
    commodity.setSkuId("1501009005");
    commodity.setName("葡萄吐司面包(10片裝)");
    commodity.setCategory("101");
    commodity.setPrice(160);
    commodity.setBrand("良品鋪子");
    IndexQuery indexQuery = new IndexQueryBuilder().withObject(commodity).build();elasticsearchTemplate.index(indexQuery);
    @Test
    br/>elasticsearchTemplate.index(indexQuery);
    @Test
    SearchQuery searchQuery = new NativeSearchQueryBuilder()
    .withQuery(QueryBuilders.matchQuery("name", "吐司"))
    .build();
    List<Commodity> list = elasticsearchTemplate.queryForList(searchQuery, Commodity.class);
    System.out.println(list);
    ElasticsearchTemplate是自動配置的
    SpringBoot+Elasticsearch
  5. 演示
    SpringBoot+Elasticsearch
  6. 工程結構
    SpringBoot+Elasticsearch
  7. 參考
    https://docs.spring.io/spring-data/elasticsearch/docs/3.1.0.RELEASE/reference/html/#repositories.query-methods.details
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

辉南县| 双辽市| 年辖:市辖区| 铁岭县| 巩义市| 上高县| 兰溪市| 武安市| 湘乡市| 财经| 衢州市| 弥渡县| 盘锦市| 原阳县| 营山县| 甘德县| 赣榆县| 惠州市| 尼木县| 陆良县| 蒲城县| 东兴市| 秭归县| 安阳县| 龙海市| 枣庄市| 北安市| 泊头市| 隆子县| 恩施市| 涿鹿县| 谢通门县| 永春县| 天水市| 乐至县| 泰州市| 互助| 九龙坡区| 大洼县| 四会市| 石泉县|