ElasticSearch在微服务中使用demo

ElasticSearch在微服务中使用demo,第1张

ElasticSearch在微服务中使用demo

项目层级

escommon导包


     org.springframework.boot
     spring-boot-starter-data-elasticsearch

在common中创建封装类
用于查询

@Resource
public class Query {

    private String keyword;
    private String sortField;
    private String sortType;
    private int page;
    private int size;

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public String getSortField() {
        return sortField;
    }

    public void setSortField(String sortField) {
        this.sortField = sortField;
    }

    public String getSortType() {
        return sortType;
    }

    public void setSortType(String sortType) {
        this.sortType = sortType;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}

用于添加进库

@document(indexName = "log")
public class Log {

    @Id
    private String id;

    private String time;

    private String logContent;

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getLogContent() {
        return logContent;
    }

    public void setLogContent(String logContent) {
        this.logContent = logContent;
    }

    public String getId() {
        return id;
    }

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

用来响应结果

//Ajax请求响应对象的类
public class AjaxResult {
    private boolean success = true;
    private String message = " *** 作成功!";


    //返回到前台对象
    private Object resultObj;

    public boolean isSuccess() {
        return success;
    }

    public AjaxResult setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public AjaxResult setMessage(String message) {
        this.message = message;
        return this;
    }

    public Object getResultObj() {
        return resultObj;
    }

    public AjaxResult setResultObj(Object resultObj) {
        this.resultObj = resultObj;
        return this;
    }

    //AjaxResult.me()成功
    //AjaxResult.me().setMessage()成功
    //AjaxResult.me().setSuccess(false),setMessage("失败");
    public  static AjaxResult me(){
        return new AjaxResult();
    }



    
}
接下来是esserver

导包


        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
        

        
            xxxx
            es-common
            1.0-SNAPSHOT
        

        
        
            org.springframework.cloud
            spring-cloud-starter-config
        

        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        

controller

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/es")
public class EsController {

    //注入CourseDocRepository
    @Resource
    private LogRepository rep;
//    @Autowired
//    private ElasticsearchTemplate elasticsearchTemplate;

    @PostMapping("/save")
    public AjaxResult save(@RequestBody Log log){
        rep.save(log);
        return AjaxResult.me();
    }

    @PostMapping("/query")
    public AjaxResult queryCourses(@RequestBody Query docQuery){
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //关键字查询
        String keyword = docQuery.getKeyword();
        if(StringUtils.isNotBlank(keyword)){
            //只支持一个字段
            //boolQueryBuilder.must(QueryBuilders.matchQuery("time", keyword));
            //多个字段
            boolQueryBuilder.must(QueryBuilders.multiMatchQuery(keyword,"id","time","logContent"));
        }

        //排序
        //获取排序字段  排序的类型
//        String sortField = docQuery.getSortField();
        //  前端如果没有排序字段 默认使用价格排序
//        sortField = "time";
//        String sortType = docQuery.getSortType();
//        SortOrder type = (sortType == null||SortOrder.DESC.toString().equals(sortType)) ? SortOrder.DESC:SortOrder.ASC;
//        //以哪个字段排序
//        FieldSortBuilder sortBuilder = new FieldSortBuilder(sortField);
//        //指定排序类型
//        sortBuilder.order(type);
//        builder.withSort(sortBuilder);
        //分页
        PageRequest pageRequest = PageRequest.of(docQuery.getPage()-1, docQuery.getSize());
        builder.withPageable(pageRequest);
        Page search = rep.search(builder.build());
        return AjaxResult.me().setResultObj(search);
//        return null;
    }
}

然后还有es的仓库基本的增删改查的方法已经有了,如果要自定义就在继承的接口中定义(根据命名)

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface LogRepository extends ElasticsearchRepository {
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5665408.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存