4 Star 55 Fork 36

X-Pacific / esclientrhlDemo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

esclientrhl使用说明

这个demo集成了ESClientRHL,是个简单的springboot工程,只要拥有一个es服务和本demo就可以完成客户端与es的交互

单测目录中还包含各种esclientRHL的高级功能使用示例

快速入门

前置准备

本地部署一个es服务,端口默认9200,即访问http://localhost:9200可以访问

可以下载本例中的es版本7.3.1

步骤一、pom添加maven依赖

<properties>
    <java.version>1.8</java.version>
    <elasticsearch.version>7.3.1</elasticsearch.version>
</properties>
<dependency>
    <groupId>cn.zxporz</groupId>
    <artifactId>esclientrhl</artifactId>
    <version>7.0.2</version>
</dependency>

步骤二、springboot启动类添加注解EnableESTools

@SpringBootApplication
@EnableESTools(basePackages={"org.zxp.esclientrhl.demo.repository"},entityPath = {"org.zxp.esclientrhl.demo.domain"})
public class EsclientrhlDemoApplication

配置application.properties

server.port=8888
elasticsearch.host=127.0.0.1:9200

步骤三、创建es索引对应的实体类

@ESMetaData(indexName = "index_demo",number_of_shards = 3,number_of_replicas = 0,printLog = true)
public class IndexDemo {
    @ESID
    private String proposal_no;
    @ESMapping(datatype = DataType.keyword_type)
    private String risk_code;
    @ESMapping(datatype = DataType.text_type)
    private String risk_name;
    @ESMapping(keyword = true)
    private String business_nature;
    @ESMapping(datatype = DataType.text_type)
    private String business_nature_name;
    private String appli_code;//可以用默认值,这样会有appli_code.keyword可以直接搜
    @ESMapping(suggest = true)
    private String appli_name;
    private String insured_code;
    @ESMapping(ngram = true)
    private String insured_name;
    @ESMapping(datatype = DataType.date_type)
    private Date operate_date;
    @ESMapping(datatype = DataType.text_type)
    private String operate_date_format;
    @ESMapping(datatype = DataType.date_type)
    private Date start_date;
    @ESMapping(datatype = DataType.date_type)
    private Date end_date;
    @ESMapping(datatype = DataType.double_type)
    private double sum_amount;
    @ESMapping(datatype = DataType.double_type)
    private double sum_premium;
    @ESMapping(datatype = DataType.keyword_type)
    private String com_code;
……

步骤四、创建Repository接口

public interface IndexDemoRepository extends ESCRepository<IndexDemo,String> {
}

步骤五、调用

@RestController
public class IndexDemoController {
    @Autowired
    private IndexDemoRepository indexDemoRepository;
    //http://127.0.0.1:8888/demo/add
    @GetMapping("/demo/add")
    public String add() throws Exception {
        IndexDemo indexDemo = new IndexDemo();
        indexDemo.setProposal_no("1");
        indexDemo.setAppli_name("a1");
        indexDemo.setRisk_code("aa1");
        indexDemo.setSum_premium(1);
        indexDemoRepository.save(indexDemo);
        return "新增成功";
    }
    //http://127.0.0.1:8888/demo/add_list
    @GetMapping("/demo/add_list")
    public String addList() throws Exception {
        IndexDemo indexDemo2 = new IndexDemo();
        indexDemo2.setProposal_no("2");
        indexDemo2.setAppli_name("a2");
        indexDemo2.setRisk_code("aa2");
        indexDemo2.setSum_premium(2);

        IndexDemo indexDemo3 = new IndexDemo();
        indexDemo3.setProposal_no("3");
        indexDemo3.setAppli_name("a3");
        indexDemo3.setRisk_code("aa3");
        indexDemo3.setSum_premium(3);
        indexDemoRepository.save(Arrays.asList(indexDemo2,indexDemo3));
        return "新增成功";
    }
    //http://127.0.0.1:8888/demo/update
    @GetMapping("/demo/update")
    public String update() throws Exception {
        IndexDemo indexDemo = new IndexDemo();
        indexDemo.setProposal_no("1");
        indexDemo.setAppli_name("a999999");
        indexDemo.setRisk_code("aa9999999");
        indexDemo.setSum_premium(99999);
        indexDemoRepository.update(indexDemo);
        return "修改成功";
    }

    //http://127.0.0.1:8888/demo/delete
    @GetMapping("/demo/delete")
    public String delete() throws Exception {
        IndexDemo indexDemo = new IndexDemo();
        indexDemo.setProposal_no("1");
        indexDemo.setAppli_name("a999999");
        indexDemo.setRisk_code("a9999999");
        indexDemo.setSum_premium(99999);
        indexDemoRepository.delete(indexDemo);
        return "删除成功";
    }
    //http://127.0.0.1:8888/demo/query
    @GetMapping("/demo/query")
    public List<IndexDemo> query() throws Exception {
        List<IndexDemo> search = indexDemoRepository.search(QueryBuilders.matchAllQuery());
        return search;
    }
}

测试访问

  1. 访问http://127.0.0.1:8888/demo/add,增加一条数据
  2. 访问http://127.0.0.1:8888/demo/add_list,增加多条数据
  3. 访问http://127.0.0.1:8888/demo/query,查询列表
  4. 访问http://127.0.0.1:8888/demo/update,修改一条数据
  5. 访问http://127.0.0.1:8888/demo/query,查询列表
  6. 访问http://127.0.0.1:8888/demo/delete,删除一条数据
  7. 访问http://127.0.0.1:8888/demo/query,查询列表

更多使用demo见test包下代码

  1. TestCRUD 普通增删改查
  2. TestAggs 聚合的使用
  3. TestIndex 索引管理
  4. TestLowLevelClient LowLevelClient的使用
  5. TestNewClient 直接使用RestHighLevelClient操作
  6. TestNested 嵌套对象的使用
  7. TestQueryBuilder 各种QueryBuilder的使用
  8. TestRepository Repository的使用
  9. TestAliases 别名的使用
  10. TestRollover Rollover的使用
  11. TestSugg 搜索建议的使用
  12. TestGeo GEO的使用
MIT License Copyright (c) 2021 X-Pacific Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

esclientrhl的使用示例 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/zxporz/esclientrhlDemo.git
git@gitee.com:zxporz/esclientrhlDemo.git
zxporz
esclientrhlDemo
esclientrhlDemo
master

搜索帮助