ElasticSearch 通过idea 上传数据
 <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.4.2</version>
        </dependency>
 <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-smile</artifactId>
            <version>2.10.0</version>
        </dependency>
这个jackson 太高 版本可能会导致冲突 无法正常运行

 

覆盖掉默认的版本

添加对应的elasticSearch配置类

@Configuration public class ElasticSearchConfiguration { @Bean public RestHighLevelClient restHighLevelClient(){ //可以在builder 中添加多个主机 RestClientBuilder builder = RestClient.builder(new HttpHost("192.168.1.137", 9200, "http")); RestHighLevelClient client = new RestHighLevelClient(builder); return client; } }

 
package com.msb.mall.mallsearch; import com.fasterxml.jackson.databind.ObjectMapper; import com.msb.mall.mallsearch.config.ElasticSearchConfiguration; import lombok.Data; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.io.IOException; @SpringBootTest class MallSearchApplicationTests { @Autowired private RestHighLevelClient restHighLevelClient; @Test void contextLoads() { System.out.println("restHighLevelClient---->"+restHighLevelClient); } /** * 测试保存文档 */ @Test void saveIndex() throws IOException { IndexRequest indexRequest = new IndexRequest("system"); indexRequest.id("1"); //indexRequest.source("name","lcc","age","18","gender","男");//测试 文档属性 User user = new User(); user.setName("lcc"); user.setAge(22); user.setGender("男"); //用jackson中的对象转换json数据 ObjectMapper objectMapper = new ObjectMapper(); String json = objectMapper.writeValueAsString(user); indexRequest.source(json, XContentType.JSON); //执行操作 IndexResponse index = restHighLevelClient.index(indexRequest, ElasticSearchConfiguration.COMMON_OPTIONS); //提取有用的的返回信息 System.out.println(index); } @Data class User{ private String name; private Integer age; private String gender; } }

 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号