SpringBoot集成ElasticSearch对API的实际应用封装(七)

第一步.添加配置文件

在resources创建elasticsearch.yml配置文件并添加一下配置:

#elasticsearch集群名称,默认是elasticsearch
spring.data.elasticsearch.cluster.name=wali

#节点的地址,注意api模式下的端口号是9300,千万不要写成9200等
spring.date.elasticsearch.cluster-nodes=127.0.0.1:9300

#是否开启本地存储
spring.data.elasticsearch.repositories.enable=true

第二步.创建实体类并配置

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import java.util.Date;
/**
 * Created by ${ligh} on 2018/12/28 下午5:19
 */
 
@Document(indexName = "people",type = "person",shards = 5,replicas = 1,refreshInterval = "1")
public class People {

    @Id
    private String id;
    private String name;
    private String password;
    private Integer age;
//    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birth;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}

第三步.创建Dao层,并设置为组件

import com.ligh.domain.People;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
/**
 * Created by ${ligh} on 2018/12/28 下午5:21
 */
 
@Component
public interface PeopleDao extends ElasticsearchRepository<People,String>{
}

第四步.创建结果返回状态和返回信息

创建返回信息类:

/**
 * Created by ${ligh} on 2018/12/28 下午4:26
 */
public class RespInfo {

    private Object content;
    private Integer status;
    private String message;

    public Object getContent() {
        return content;
    }
    public void setContent(Object content) {
        this.content = content;
    }
    public Integer getStatus() {
        return status;
    }
    public void setStatus(Integer status) {
        this.status = status;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

创建返回状态类:

/**
 * Created by ${ligh} on 2018/12/28 下午4:28
 */
public class InfoCode {

    public static Integer SUCCESS=200;

    public static Integer ERROR = 500;

    //其他状态...
}

第五步.Controller类中进行接口调用

import com.alibaba.fastjson.JSON;
import com.ligh.dao.PeopleDao;
import com.ligh.domain.People;
import com.ligh.entity.InfoCode;
import com.ligh.entity.RespInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by ${ligh} on 2018/12/28 下午5:23
 */
@RequestMapping("people")
@RestController
public class PeopleController {

    @Autowired
    private PeopleDao peopleDao;

    /**
     *  添加方法也是修改方法
     *
     * @param people
     * @return
     */
    @RequestMapping("add")
    public String add(@RequestBody People people){
        RespInfo respInfo = new RespInfo();
        Date date = new Date();
        people.setBirth(date);
        People save = peopleDao.save(people);
        if(save != null){
            respInfo.setMessage("存储成功!");
            respInfo.setStatus(InfoCode.SUCCESS);
        }else {
            respInfo.setMessage("存储失败!");
            respInfo.setStatus(InfoCode.ERROR);
        }
        return JSON.toJSONString(respInfo);
    }

    /**
     *  删除方法
     *
     * @param people
     * @return
     */
    @RequestMapping("delete")
    public String delete(@RequestBody(required = false) People people){
        peopleDao.delete(people);
        return "success";
    }

    /**
     *  通过id查询
     *
     * @param people
     * @return
     */
    @RequestMapping("query")
    public String update(@RequestBody People people){
        RespInfo respInfo = new RespInfo();
        //查询全部
        Iterable<People> all = peopleDao.findAll();
        Map map = new ConcurrentHashMap();
        map.put("all",all);
        //根据指定参数进行查询
        Optional<People> list = peopleDao.findById(people.getId());
        if(list.isPresent()){
            respInfo.setStatus(InfoCode.SUCCESS);
            map.put("list",list);
            respInfo.setContent(map);
        }else {
            respInfo.setMessage("没有数据!");
            respInfo.setStatus(InfoCode.ERROR);
        }
        return JSON.toJSONString(respInfo);
    }
}

以上就完成了springboot实际开发中的使用,也可以在service业务逻辑层进行业务代码编写,直接在Controller中进行注入service业务接口。

只有不断的学习才能变得很强,更强…

对于Spring Boot集成Elasticsearch Java API Client,你可以按照以下步骤进行操作: 1. 首先,你需要在你的项目中添加Elasticsearch的依赖。可以在官方文档()中找到相关的依赖信息。 2. 接下来,你可以通过创建低级别的RestClient来连接到Elasticsearch。你可以使用以下代码片段来创建一个基于RestClient的传输对象: ```java RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper()); ElasticsearchClient client = new ElasticsearchClient(transport); ``` 3. 一旦你建立了与Elasticsearch的连接,你就可以使用ElasticsearchClient来执行各种操作。比如,你可以使用以下代码来创建一个索引: ```java CreateIndexResponse createIndexResponse = client.indices().create(c -> c.index("newapi")); ``` 在这个例子中,"newapi"是你要创建的索引的名称。 所以,以上是使用Spring Boot集成Elasticsearch Java API Client的基本步骤。你可以根据你的具体需求进一步使用ElasticsearchAPI来完成其他操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [springboo整合elasticSearch8 java client api](https://blog.csdn.net/A434534658/article/details/125239480)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值