Springboo整合elasticsearch实现查询与复杂查询(内配图文一眼看懂)

Springboot整合elasticsearch实现简单查询,高级查询等,简单查询在dao层通过继承ElasticsearchRepository复杂查询需要用到ElasticsearchRestTemplate模板类,请看操作,配图文解析lasticsearchRestTemplate模板类复杂查询

Elasticsearch学习:

Springboo整合elasticsearch实现查询与复杂查询(内配图文一眼看懂)
ElasticSearch 字段数据存在,term查询不到指定的数据
以下用到的是elasticsearch-7.8为例子

1配置pom文件

注:elasticsearch的版本和Springboot的版本要匹配(不是版本匹配),是兼容问题,如果es用的是7.8但是springboot用的低版本会出现莫名的错误(踩坑注意)

<?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.bj.ft</groupId>
    <artifactId>es7</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.6.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
    </dependencies>



</project>

2.配置配置文件

把地址放在类里面就不用在properties去配置了,图省事

@Configuration
class ElasticsearchConfigextends  extends AbstractElasticsearchConfiguration {


    //重写父类方法
    @Override
    public RestHighLevelClient elasticsearchClient() {
        RestClientBuilder builder = RestClient.builder(new HttpHost("127.0.0.1", 1001));
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
        return restHighLevelClient;
    }
}

3.配置实体类

用到@Document(indexName = “shopping”, shards = 3, replicas = 1) 指明索引名称,分片和备份 ,下面的@id是必有的,这个字段可以理解为索引的_id,用户可以通过_id去查看单个数据

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "shopping", shards = 3, replicas = 1)
public class Product {

    @Id
    private Long id;//商品唯一标识
    private String name;
    private String sex;
    private String jiage;//商品名称


}

4.配置dao层

放入对应的实体映射和id类型

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

5.配置Springboot启动

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

**6.编写测试类

因为配置了config和dao直接自动装配即可

@RunWith(SpringRunner.class)
@SpringBootTest
public class test {

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    ProductDao productDao;


    @Test
    public void createIndex() {     //可以什么都不写,运行的时候系统默认创建
        System.out.println("创建索引");
    }

    @Test                            //删除索引
    public void deleteIndex() {
        boolean flg = elasticsearchRestTemplate.deleteIndex(Product.class);
        System.out.println("删除索引 = " + flg);
    }
    
      //以下都是最简单的用法,增删改查,就不一一打了,把最常用的方法列举了出来
     @Test
    public void queryByid() {
        productDao.saveAll();   //批量查询,传入List<实体类>进行一个批量的添加
        productDao.save();       //添加数据,传入实体类
        productDao.findAll();     //查询全部,返回迭代器,直接Foreach可拿到数据
        productDao.findById();    //查询一条,需要传递id也就是在实体类定义的@id,把id内容放进去
        productDao.delete();    //删除
        productDao.deleteAll();  //批量删除,传入List<实体类
        productDao.save()   //这里的添加和修改是一个防范save,如果id不变就是修改数据,id发生改变就是添加数据
        
    }

    //复杂查询数据,本次查询的内容是, where  name =张(模糊匹配)  or sex=男   jiage>1888的数据
如果看不懂请看下面的图,一看就懂了
 @Test
    public void test1(){
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.matchAllQuery());
        builder.withQuery(QueryBuilders.boolQuery()
                .should(QueryBuilders.matchQuery("name","张")
                )
                .should(QueryBuilders.matchQuery("sex","男")
                ).filter(QueryBuilders.rangeQuery("jiage").gt(1888))

        );
        SearchHits<Product> search = elasticsearchRestTemplate.search(builder.build(), Product.class);
        for (org.springframework.data.elasticsearch.core.SearchHit<Product> searchHit : search) {
            System.out.println(searchHit.getContent());
        }
    }
}

Springboo整合elasticsearch实现查询与复杂查询(内配图文一眼看懂)_第1张图片

这是代码和在json查询的区别,无异于就是封装了一下,还有别的功能就按照作用域调用其方法即可,这是最简单的入门复杂查询,如果上述描述不正确,还望多多指教,后续还会更新es的高级查询和语法用法,敬请期待

你可能感兴趣的:(elasticsearch,java,大数据)