ElasticSearch--Springboot 통합 ElasticSearch

9953 단어 ElasticSearch

기사 목록

  • 5, Springboot 통합 ElasticSearch

  • [실리콘밸리 수업을 공부하는 데 필기]

    5, Springboot 통합 ElasticSearch


    1. SpringBoot 프로젝트 빠르게 만들기
    2. 가져오기 의존
    <dependency>
                <groupId>org.elasticsearch.clientgroupId>
                <artifactId>elasticsearch-rest-high-level-clientartifactId>
                <version>7.4.2version>
    dependency>
    
    
    <properties>
            <java.version>1.8java.version>
            <elasticsearch.version>7.4.2elasticsearch.version>
        properties>
    

    3. GulimallElasticSearchConfig 구성 파일을 만듭니다.java
    // RestHighLevelClient
    @Configuration
    public class GulimallElasticSearchConfig {
    
            // 
        public  static  final RequestOptions COMMON_OPTIONS;
    
        static {
            RequestOptions.Builder builder =RequestOptions.DEFAULT.toBuilder();
    
    
    
            COMMON_OPTIONS = builder.build();
        }
        
        @Bean
        public RestHighLevelClient esRestClient() {
            RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(
                            new HttpHost("192.168.196.128", 9200, "http")));
    
            return client;
        }
    }
    

    4. 테스트, 테스트 테스트에서 테스트
    @SpringBootTest
    class GulimallSearchApplicationTests {
    
        @Autowired
        private RestHighLevelClient client;
    
        @Test
        void test(){
            System.out.println(client);
        }
    
        /**
         *  
         */
        @SneakyThrows
        @Test
        void test1 (){
            IndexRequest indexRequest = new IndexRequest("users");
            indexRequest.id("1");
            User user = new User();
            user.setName("li");
            user.setAge(18);
            String json = JSON.toJSONString(user);
            indexRequest.source(json, XContentType.JSON);
    
            IndexResponse index = client.index(indexRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);
    
            System.out.println(index);
        }
    
        @Data
        class User{
            private  String name;
            private  Integer age;
        }
    }
    

    좋은 웹페이지 즐겨찾기