Elasticsearch는 기본적으로 전체 텍스트 검색에서 무적이며 빅 데이터에서도 매우 성공적입니다. NOSQL (또는 원래 NOSQL)으로 사용할 수 있습니다.
이 기사는 Spring Boot를 간략하게 소개하여 Kotlin Language를 사용하여 Elasticsearch에 연결합니다. 그러나 나는 자세한 소개를하지 않을 것입니다. Java/Kotlin에서 Elasticsearch의 사용에 대해 더 깊이 이해하고 싶다면 https://gitee.com/quanke/elasticsearch-java/ 전에 쓴 "Elasticsearch Java API 매뉴얼"을 참조하십시오.
Elasticsearch를 전혀 이해하지 못하면 먼저 Elasticsearch Service를 이해하고 설치하십시오.
Elasticsearch에 연결하는 방법에는 여러 가지가 있습니다
Spring Data ElasticSearch를 사용하는 경우 Spring Boot 1.5 ++ 버전은 최신 버전의 Elasticsearch를 지원하지 않습니다.
다음은 Spring Data Elasticsearch 및 Elasticsearch의 해당 버전입니다.
| 스프링 데이터 Elasticsearch | Elasticsearch |
|---|---|
| 3.0.0.RC2 | 5.5.0 |
| 3.0.0.m4 | 5.4.0 |
| 2.0.4. release | 2.4.0 |
| 2.0.0 | 2.2.0 |
| 1.4.0.m1 | 1.7.3 |
| 1.3.0. release | 1.5.2 |
| 1.2.0. release | 1.4.4 |
| 1.1.0 | 1.3.2 |
| 1.0.0 | 1.1.1 |
우리가 사용하는 Elasticsearch 버전은 5.5.6이고 Spring Boot 버전은 1.5.6이며 Elasticsearch 5.0 이상을 지원합니다. Spring Data Elasticsearch는 릴리스 버전이 아닙니다. 우리는 Elasticsearch Java 클라이언트를 사용합니다. 그러나 공식 권장 사항이 더 나은 방법입니다. "Elasticsearch Java REST API 매뉴얼"을 참조 할 수 있습니다. https://gitee.com/quanke/elasticsearch-java-rest를 썼지만이 기사는 여전히 Elasticsearch Java 클라이언트를 사용합니다.
봄 부트 Kotlin 프로젝트 구축
프로젝트를 구축하는 데 문제가있는 경우 이전 기사 "Spring Boot 및 Kotlin을 사용하여 Restfull API 만들기"를 참조하십시오.
Gradle을 사용하여 빌드, build.gradle 파일을 추가하십시오
종속성 {compile "org.elasticsearch : elasticsearch : $ elasticsearch_version"compile "org.elasticsearch.client : 전송 : $ elasticsearch_version"}}완전한 빌드. 그레이드 파일
Group 'name.quanke.kotlin'version'1.0-snapshot'buildscript {ext.kotlin_version = '1.2.10'ext.spring_boot_version = '1.5.4. Release'ext.springfox_swagger2_version = '2.7.0'ext.mysql_version = '51.21'ext.10.110.1 '1.10.10.21'. ext.elasticsearch_version = '5.5.1'ext.fastjson_version = '1.2.7'리포지토리 {mavencentral ()} 종속성 {classpath "org.jetbrains.kotlin : Kotlin-gradle-Plugin : $ kotlin_version" ClassPath ( "org.springframework.boot : spring-boot-gradle-plugin : $ spring_boot_version") // Kotlin은 springboot의 기본 매개 변수가없는 생성자를 통합하고 기본적으로 모든 클래스를 기본 열기 클래스 플러그인 클래스 경로 ( "org.jetbrains.kotlin : Kotlin-Noarg : $ kotlin_version")로 설정합니다. classpath ( "org.jetbrains.kotlin : kotlin-allopen : $ kotlin_version")}}}} 플러그인 적용 : 'kotlin'Apply 플러그인 : "Kotlin-Spring"// https://kotlinlang.org/Reference/Compiler-plugins.html#kotlin-compoller-compoler-compontler-compoller-compoller-compontler-plugin 'org.springframework.boot'Apply 플러그인 : "kotlin-jpa"//https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-helljar {vasename ='챕터 11-6-8-- service '버전 ='0.1.0 '}}} mavencentral ()} 종속성 {compile "org.jetbrains.kotlin : kotlin-stdlib-jre8 : $ kotlin_version"compile ( "org.jetbrains.kotlin : kotlin-reflect : $ {kotlin_version}") compile "org.springframework.boot : spring-boot-starter-web : $ spring_boot_version"compile "org.elasticsearch : elasticsearch : elasticsearch : $ elasticsearch_version"compile "org.elasticsearch.client : 송신 : $ elasticsearch_version"compile "com.alibaba:fastjson: 그들은 compile "org.apache.commons : commons-lang3 : 3.6"testcompile "org.springframework.boot : spring-boot-starter-test : $ spring_boot_version"testcompile "org.jetbrains.kotlin : kotlin-test-junit : $ kotlin_version"} compilekotlin {kotlinoptions "1.8"} compiletestkotlin {kotlinoptions.jvmtarget = "1.8"}테스트 기본 클래스 ElasticsearchClient를 먼저 작성하십시오
import com.alibaba.fastjson.jsonimport com.alibaba.fastjson.serializer.serializerfeatureimport org.elasticsearch.action.search.searchresponseimport org.elasticsearch.client.transport.transportclientimport org.elasticsearch.common.setsimport org.elasticsearch.common.transport.inetsockettransporttaddressimport org.elasticsearch.transport.client.prebuilttransportclientimport org.junit.afterimport org.junit.beforeimport java.net.intaddress/** elastical versitor 5.1 's Client 및 Enrastictemplert의 초기화. ES를 요청하는 클러스터는 클러스터의 외부 요인입니다. * 2017/11/10에 http://quanke.name에 의해 만들어졌습니다. */Open Class ElasticsearchClient {Protected var Client : TransportClient? = null @Therows (예외 :: class) fun setup () {val eSsettings = settings.builder () .put ( "cluster.name", "utan-es") // es instance.put의 이름을 설정하십시오. List.Build () /*** 여기서 연결 방법은 X-Pack 플러그인이 설치되지 않았다는 사실을 나타냅니다. X-Pack이 설치된 경우 [ElasticsearchXPackClient]* 1을 참조하십시오. Java 클라이언트 방법은 TCP 프로토콜* 2를 사용하여 포트 9300에서 통신하는 것입니다. .addtransportAddress (inetSocketTransportAddress (inetAddress.getByName ( "192.168.1.10"), 9300))) println ( "ElasticsearchClient 연결이 성공")} @After @Throws (예외 :: class) fun inulsdown () {client! if (client!)}} println (searchResponse : searchResponse) {val searchHits = searchResponse.hits.hits for (searchHit in searchHits) {println (json.tojsonstring (searchHit.Source, SerializerFeature.prettyFormat))}}}단위 테스트를 실행하십시오
import org.elasticsearch.query.querybuilders.matchallqueryimport org.junit.testimport org.junit.runner.runner.runwithimport org.springframewort.test.context.springboottestestimport org.springframework.test.test.context.junit.sxt.test.junit.test.contet.test.context.test.test.context http://quanke.name 2018/1/9. */@runwith (springRunner :: class) @SpringBoottestClass ApplicationTests : elasticsearchClient () {@test fun`es test "`() {val qb = matchallQuery () val 응답 = 클라이언트 !!. Preparesearch ("Twitter ") //가 여러 인덱스가 될 수 있습니다. 조건 () () println (응답)}}우리는 dayu-spring-boot-starter-es를 썼고 그것을 오픈 소스를 할 기회가 있습니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.