springboot默认支持两种技术来和ES交互

1、Jest(默认不生效) 需要导入jest工具包(io.searchbox.client.JestClient)

2、SpringData ElasticSearch【ES版本可能不合适】

       如果版本不适配:

                1)、升级SpringBoot版本

                2)、安装对应版本的ES

     1)、Client节点信息clusterNodes;clusterName

     2)、ElasticsearchTemplate 操作es

     3)、编写一个ElasticsearchRepository的子接口来操作Es

springboot对elasticsearch的版本支持不太友好,1.5.20依赖的2.1.20的springdata-elasticsearch,仅支持2.x版本的elasticsearch服务,这里用jest进行测试

 例:

@GetMapping("/indexArticle")
    public void indexArticleUseElastic() {
        Article article = new Article();
        article.setId(1);
        article.setTitle("wc");
        article.setContent("减肥减肥");
        article.setCreateDate("2019-07-07");
        article.setRemark("fjsd");
        //构建一个索引功能
        Index index = new Index.Builder(article).index("suxdindex").type("suxdtype").build();
        //执行
        try {
            //执行
            jestClient.execute(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/searchArticle")
    public String searchArticleUseElastic() {
        String json = "{\n" +
                "  \"query\" : {\n" +
                "     \"match\" : {\n" +
                "        \"title\" : \"wc\"\n" +
                "     }\n" +
                "  }\n" +
                "}";
        Search search = new Search.Builder(json).addIndex("suxdindex").addType("suxdtype").build();

        try {
            SearchResult result = jestClient.execute(search);
            return result.getJsonString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
spring  
  elasticsearch:
    jest:
      uris: http://47.94.145.253:9200/
<dependency>
      <groupId>io.searchbox</groupId>
      <artifactId>jest</artifactId>
      <version>6.3.1</version>
</dependency>