23-ElasticsearchOptions客户端操作





对象

    package com.study.entity;

    import org.springframework.data.annotation.Id;
    import org.springframework.data.elasticsearch.annotations.Document;
    import org.springframework.data.elasticsearch.annotations.Field;
    import org.springframework.data.elasticsearch.annotations.FieldType;

    /**
     * @Document:将这个类的对象转化问es中的一条文档进行注入
     *      indexName:用来指定文档的索引名称
     *      createIndex:用来指定是否创建索引(索引不存在的时候)
     */

    @Document(indexName = "products", createIndex = true)
    public class Product {

        @Id //用来将放入对象id值 作为文档_id  进行映射
        private Integer id;
        @Field(type = FieldType.Keyword)
        private String title;
        @Field(type = FieldType.Double)
        private Double price;
        @Field(type = FieldType.Text)
        private String description;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public Double getPrice() {
            return price;
        }

        public void setPrice(Double price) {
            this.price = price;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }

操作测试

    package com.study;

    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.study.entity.Product;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
    import org.springframework.data.elasticsearch.core.SearchHit;
    import org.springframework.data.elasticsearch.core.SearchHits;
    import org.springframework.data.elasticsearch.core.query.Query;

    @SpringBootTest
    public class ElasticSearchOptionsTests {

        private final ElasticsearchOperations elasticsearchOperations;

        //构造注入我们的对象
        @Autowired
        public ElasticSearchOptionsTests(ElasticsearchOperations elasticsearchOperations) {
            this.elasticsearchOperations = elasticsearchOperations;
        }

        /*
            save: 索引一条文档 更新一条文档
                save方法当文档id不存在时 添加文档,当文档id存在时更新文档
         */
        @Test
        public void tetstIndex(){
            Product product = new Product();
            product.setId(1);
            product.setTitle("小浣熊");
            product.setPrice(1.5);
            product.setDescription("小浣熊干吃面真好吃");
            elasticsearchOperations.save(product);
        }

        /**
         * 查询一条文档
         */
        @Test
        public void testSearch(){
            Product product = elasticsearchOperations.get("1", Product.class);
            System.out.println(product.getId() + product.getTitle() + product.getPrice() + product.getDescription());
        }

        /**
         * 删除一条文档
         */
        @Test
        public void testDelete(){
            Product product = new Product();
            product.setId(1);
            elasticsearchOperations.delete(product);
        }

        /**
         * 删除所有
         */
        @Test
        public void testDeleteAll(){
            elasticsearchOperations.delete(Query.findAll(), Product.class);
        }

        /**
         * 查询所有
         */
        @Test
        public void testFindAll() throws JsonProcessingException {
            SearchHits<Product> productSearchHits = elasticsearchOperations.search(Query.findAll(), Product.class);
            System.out.println("总分数:" + productSearchHits.getMaxScore());
            System.out.println("符合条件的总条数:" + productSearchHits.getTotalHits());
            for (SearchHit<Product> productSearchHit : productSearchHits) {
                //productSearchHit.getContent()就是查询出来的对象
                System.out.println(new ObjectMapper().writeValueAsString(productSearchHit.getContent()));//通过ObjectMapper的方法将对象转化为字符串
            }
        }
    }
posted @ 2022-01-18 20:55  不是孩子了  阅读(305)  评论(0)    收藏  举报