Spring data Elasticsearch自定义查询

1.关键字查询:match
•查询条件构建器:NativeSearchQueryBuilder
•match条件:QueryBuilders.matchQuery(字段,值);
•查询条件:queryBuilder.withQuery( … )
•查询:repository.search( 条件 )
•Page 返回类型,本身就是容器,可以直接遍历获得数据
–总条数: page.getTotalElements()
–总页数:page.getTotalPages()
Spring data Elasticsearch自定义查询_第1张图片

package com.czxy.changgou3.elasticsearch;

import com.czxy.changgou3.TestApplication;
import com.czxy.changgou3.repository.ESBookRepository;
import com.czxy.changgou3.vo.ESBook;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
 * Created by liangtong.
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class TestESQuery {
     

    @Resource
    private ESBookRepository esBookRepository;


    //#需求:查询title为 “小曾讲爱情故事”
    @Test
    public void demo01(){
     
        // 查询条件构建器(存放所有的查询条件)
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();

        // 关键字查询 match
        queryBuilder.withQuery(QueryBuilders.matchQuery("title","小曾讲爱情故事") );

        // 查询
        Page<ESBook> page = esBookRepository.search(queryBuilder.build());

        // 处理结果
        System.out.println("总条数:" + page.getTotalElements());
        System.out.println("总页数:" + page.getTotalPages());
        for(ESBook esBook : page ){
     
            System.out.println(esBook);
        }

    }
}

1.1多条件查询:bool – must/mustNot (交集)
•多条件拼凑,使用BoolQueryBuilder对象
–QueryBuilders.boolQuery()
•并集操作:must() / mustNot()
Spring data Elasticsearch自定义查询_第2张图片

   @Test
    public void demo02(){
     
        // 查询条件构建器
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();

        // 查询条件--多条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        // 条件1:必须
        boolQueryBuilder.must( QueryBuilders.matchQuery("title","小曾讲爱情故事"));
        // 条件2:不需要
        boolQueryBuilder.mustNot( QueryBuilders.matchQuery("title","II"));


        queryBuilder.withQuery( boolQueryBuilder );

        // 查询
        Page<ESBook> page = esBookRepository.search(queryBuilder.build());


        // 处理解雇
        System.out.println("总条数:" + page.getTotalElements());
        System.out.println("总页数:" + page.getTotalPages());
        for(ESBook esBook : page) {
     
            System.out.println(esBook);
        }

    }

1.2多条件查询:bool–should (并集)
Spring data Elasticsearch自定义查询_第3张图片

//#需求:查询“小曾讲爱情故事” ,以及 “阿叶不堪回首的往事”相关数据
    @Test
    public void demo03(){
     
        // 查询条件构建器
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();

        // 查询条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.should( QueryBuilders.matchQuery("title","小曾讲爱情故事"));
        boolQueryBuilder.should( QueryBuilders.matchQuery("title", "阿叶不堪回首的往事"));

        queryBuilder.withQuery(boolQueryBuilder);

        // 查询
        Page<ESBook> page = esBookRepository.search(queryBuilder.build());

        // 处理结果
        for (ESBook esBook : page) {
     
            System.out.println(esBook);
        }



    }

1.3精确查询:term
Spring data Elasticsearch自定义查询_第4张图片

//#需求:查询价格为68.8 信息
    @Test
    public void demo04(){
     
        // 构建器
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        // 条件
        queryBuilder.withQuery( QueryBuilders.termQuery("price", 456));

        // 查询
        Page<ESBook> search = esBookRepository.search(queryBuilder.build());
        // 结果
        Page<ESBook> page = esBookRepository.search(queryBuilder.build());

        // 处理结果
        System.out.println("总条数:" + page.getTotalElements());
        System.out.println("总页数:" + page.getTotalPages());
        for (ESBook esBook : page) {
     
            System.out.println(esBook);
        }
    }

1.4区间查询
Spring data Elasticsearch自定义查询_第5张图片


//#需求:查询价格在50-300之间
    @Test
    public void demo05(){
     
        // 构建器
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        // 条件
        queryBuilder.withQuery( QueryBuilders.rangeQuery("price").gte(50).lte(300)  );

        // 查询
        Page<ESBook> search = esBookRepository.search(queryBuilder.build());
        // 结果
        Page<ESBook> page = esBookRepository.search(queryBuilder.build());

        // 处理结果
        System.out.println("总条数:" + page.getTotalElements());
        System.out.println("总页数:" + page.getTotalPages());
        for (ESBook esBook : page) {
     
            System.out.println(esBook);
        }
    }

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