Elasticsearch查询

本文详细介绍了一个基于RESTful的图书搜索API实现,包括分页查询、按名查询、排序、提取特定字段、高亮显示、多条件查询及范围查询等功能。通过具体的Java代码示例,展示了如何使用Elasticsearch进行高效的数据检索。
摘要由CSDN通过智能技术生成

1.BookController.java

@RestController
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping(value = {"/loadAll/{page}/{size}"})
    public String loadAll(@PathVariable("page")int page, @PathVariable("size")int size){
        return bookService.loadAll(page,size);
    }

    @GetMapping(value = {"/loadByName/{name}"})
    public String loadByName(@PathVariable("name")String name){
        return bookService.loadByName(name);
    }

    @GetMapping(value = {"/loadBySort/{fieldName}"})
    public String loadBySort(@PathVariable("fieldName")String fieldName){
        return bookService.loadBySort(fieldName);
    }

    @GetMapping(value = {"/loadByFetchField"})
    public String loadBySort(String[] fieldNames){
        String[] fieldName = new String[2];
        fieldName[0] = "bookId";
        fieldName[1] = "bookAuthor";
        return bookService.loadByFetchField(fieldName);
    }

    @GetMapping(value = {"/loadByNameHight/{name}"})
    public String loadByNameHight(@PathVariable("name")String name){
        return bookService.loadByNameHight(name);
    }

    @GetMapping(value = {"/loadByMutil/{name}/{author}"})
    public String loadByMutil(@PathVariable("name")String name,@PathVariable("author")String author){
        return bookService.loadByMutil(new String[]{name,author});
    }

    @GetMapping(value = {"/loadByFilter/{name}/{price}"})
    public String loadByFilter(@PathVariable("name")String name,@PathVariable("price")String price){
        return bookService.loadByFilter(new String[]{name,price});
    }
}

2.BookService.java


public interface BookService {
    String loadAll(int page,int size);
    public String loadByName(String name);
    public String loadBySort(String fieldName);
    public String loadByFetchField(String... fieldNames);
    public String loadByNameHight(String name);
    public String loadByMutil(String...values);
    public String loadByFilter(String...values);
}

3.BookServiceImpl.java


@Service("bookService")
public class BookServiceImpl implements BookService {
    @Autowired
    private RestHighLevelClient client;


    //分页查询
    @Override
    public String loadAll(int page, int size) {
        SearchRequest request = new SearchRequest();
        request.indices("book");
        SearchSourceBuilder searchSource = new SearchSourceBuilder();
        searchSource.from((page-1)*size);
        searchSource.size(size);

        request.source(searchSource);

        SearchResponse response = null;
        try{
            response = client.search(request, RequestOptions.DEFAULT);
            System.out.println(response);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }

        String result = "";
        SearchHits hits = response.getHits();
        for (SearchHit hit:hits) {
            result += hit.getSourceAsString();
        }
        return result;
    }

    //根据名字查询
    @Override
    public String loadByName(String name) {
        SearchRequest request = new SearchRequest();
        request.indices("book");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery("bookName",name));

        request.source(searchSourceBuilder);

        SearchResponse response = null;
        try{
            response = client.search(request,RequestOptions.DEFAULT);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }

        String result = "";
        SearchHits hits = response.getHits();
        for (SearchHit hit:hits) {
            result += hit.getSourceAsString();
        }

        return result;
    }

    //排序
    @Override
    public String loadBySort(String fieldName) {
        SearchRequest request = new SearchRequest();
        request.indices("book");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.sort(fieldName, SortOrder.DESC);

        request.source(searchSourceBuilder);

        SearchResponse response = null;
        try{
            response = client.search(request,RequestOptions.DEFAULT);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }

        String result = null;
        SearchHits hits = response.getHits();
        for (SearchHit hit:hits) {
            result += hit;
        }
        return result;
    }

    //提取部分列
    @Override
    public String loadByFetchField(String... fieldNames) {
        SearchRequest request = new SearchRequest();
        request.indices("book");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.fetchSource(fieldNames,null);
        searchSourceBuilder.sort("bookId",SortOrder.DESC);
        request.source(searchSourceBuilder);

        SearchResponse response = null;
        try{
            response = client.search(request,RequestOptions.DEFAULT);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }

        String result = null;
        SearchHits hits = response.getHits();
        for (SearchHit hit:hits) {
            result += hit.getSourceAsString();
        }
        return result;
    }

    //高亮显示
    @Override
    public String loadByNameHight(String name) {
        SearchRequest request = new SearchRequest();
        request.indices("book");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        highlightBuilder.field("bookName");
        searchSourceBuilder.highlighter(highlightBuilder);
        searchSourceBuilder.query(QueryBuilders.matchQuery("bookName",name));

        request.source(searchSourceBuilder);

        SearchResponse response = null;
        try{
            response = client.search(request,RequestOptions.DEFAULT);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }

        String result = "";
        SearchHits hits = response.getHits();
        for (SearchHit hit:hits) {
            result += hit.getHighlightFields();
            System.out.println(hit.getHighlightFields());
        }
        return result;
    }

    //条件查询
    @Override
    public String loadByMutil(String... values) {
        SearchRequest request = new SearchRequest();
        request.indices("book");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        QueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery("bookName",values[0]);
        QueryBuilder queryBuilder1 = QueryBuilders.matchPhraseQuery("bookAuthor",values[1]);
        searchSourceBuilder.query(QueryBuilders.boolQuery().must(queryBuilder).mustNot(queryBuilder1));//需要满足queryBuilder条件,而满足queryBuilder1条件的内容
        request.source(searchSourceBuilder);
        SearchResponse response = null;
        try{
            response = client.search(request,RequestOptions.DEFAULT);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
        SearchHits hits = response.getHits();
        String result = "";
        for (SearchHit hit:hits) {
            result += hit.getSourceAsString();
        }
        return result;
    }

    //范围查询
    @Override
    public String loadByFilter(String... values) {
        SearchRequest request = new SearchRequest();
        request.indices("book");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        QueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery("bookName",values[0]);
        QueryBuilder queryBuilder1 = QueryBuilders.rangeQuery("bookPrice").gte(values[1]);
        //must().filter() 先执行must()结果再filter()
        searchSourceBuilder.query(QueryBuilders.boolQuery().mustNot(queryBuilder).filter(queryBuilder1));
        request.source(searchSourceBuilder);
        SearchResponse response = null;
        try{
            response = client.search(request,RequestOptions.DEFAULT);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
        SearchHits hits = response.getHits();
        String result = "";
        for (SearchHit hit:hits) {
            result += hit.getSourceAsString();
        }
        return result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值