ElasticSearch入门-测试用例

1.软件的下载

访问 Elasticsearch 官网 https://www.elastic.co/cn/downloads/elasticsearch 下载安装包:
ElasticSearch入门-测试用例_第1张图片

2.依赖的添加和使用测试

项目的结构-自己新建一个maven项目
ElasticSearch入门-测试用例_第2张图片

pom.xml


<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.0modelVersion>

    <parent>
        <artifactId>spring-boot-starter-parentartifactId>
        <groupId>org.springframework.bootgroupId>
        <version>2.3.6.RELEASEversion>
        <relativePath/>
    parent>

    <groupId>org.examplegroupId>
    <artifactId>es-springartifactId>
    <version>1.0version>
    <dependencies>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-elasticsearchartifactId>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
        dependency>
    dependencies>
project>

application.properties

elasticsearch.host=127.0.0.1
elasticsearch.port=9200
logging.level.com.junda.es=warn

ES_Config

@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
@Data
@EqualsAndHashCode(callSuper = false)
public class ES_Config extends AbstractElasticsearchConfiguration {
    private String host;
    private Integer port;
    @Override
    public RestHighLevelClient elasticsearchClient() {
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
        return restHighLevelClient;
    }
}

Product

@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Document(indexName = "product",shards = 3,replicas = 1)
public class Product {
    @Id
    private Long id;
    @Field(type = FieldType.Text)
    private String title;
    @Field(type = FieldType.Keyword)
    private String category;
    @Field(type = FieldType.Double)
    private Double price;
    @Field(type = FieldType.Keyword,index = false)
    private String images;
}

ProductDao

@Repository
public interface ProductDao extends ElasticsearchRepository<Product,Long> {
}

SpringDataESDaoTest

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataESDaoTest {
    @Autowired
    private ProductDao productDao;


    //保存
    @Test
    public void save(){
        Product product = new Product();
        product.setId(3L)
                .setTitle("oppo手机")
                .setCategory("手机")
                .setPrice(299.0)
                .setImages("http://www.ashyy.kiikl/111");
        productDao.save(product);
    }
    //更新
    @Test
    public void update(){
        Product product = new Product();
        product.setId(2L)
                .setTitle("华为手机")
                .setCategory("手机")
                .setPrice(2999.0)
                .setImages("http://www.ashyy.kiikl/111");
        productDao.save(product);
    }
    //通过id查询
    @Test
    public void findById(){
        Product product = productDao.findById(2L).get();
        System.out.println("product = " + product);
    }
    //查询所有
    @Test
    public void findAll(){
        Iterable<Product> all = productDao.findAll();
        for (Product product : all){
            System.out.println("product = " + product);
        }
    }
    //删除数据
    @Test
    public void delete(){
        Product product = new Product();
        product.setId(1L);
        productDao.delete(product);
    }
    //保持多条数据
    @Test
    public void saveAll(){
        List<Product> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Product product = new Product();
            product.setId(Long.valueOf(i))
                    .setTitle("华为手机")
                    .setCategory("手机")
                    .setPrice(2999.0)
                    .setImages("http://www.ashyy.kiikl/111");
            list.add(product);
        }
        productDao.saveAll(list);
    }
    //分页查询
    @Test
    public void findByPageable(){
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        int currentPage=0;
        int pageSize=5;
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize, sort);
        Page<Product> all = productDao.findAll(pageRequest);
        for (Product product : all){
            System.out.println("product = " + product);
        }
    }
    //文档查询
    @Test
    public void termQuery(){
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");
        Iterable<Product> all = productDao.search(termQueryBuilder);
        for (Product product : all){
            System.out.println("product = " + product);
        }
    }
    //文档分页查询
    @Test
    public void findQueryByPageable(){
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");
        int currentPage=0;
        int pageSize=5;
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
        Iterable<Product> all = productDao.search(termQueryBuilder);
        for (Product product : all){
            System.out.println("product = " + product);
        }
    }
}

SpringDatESIndexTest

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataESIndexTest {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Test
    public void createIndex(){
        System.out.println("创建索引");
    }

    @Test
    public void deleteIndex(){
        boolean b = elasticsearchRestTemplate.deleteIndex(Product.class);
        System.out.println("删除成功与否 = " + b);
    }
}

SpringDataESMainApplication

@SpringBootApplication
public class SpringDataESMainApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringDataESMainApplication.class,args);
    }
}

你可能感兴趣的:(ELasticSearch,elasticsearch,java)