Elasticsearch is basically invincible in full-text search, and it is also very successful in big data. It can be used as nosql (or originally nosql).
This article briefly introduces Spring Boot to use Kotlin language to connect to Elasticsearch. However, I will not give a detailed introduction. If you want to have a deeper understanding of the use of Elasticsearch in Java/kotlin, please refer to the "Elasticsearch Java API Manual" I wrote before https://gitee.com/quanke/elasticsearch-java/ contains usage examples, including the pitfalls we have struck with when we use it.
If you don't understand Elasticsearch at all, please first understand and install the Elasticsearch service
There are many ways to connect to Elasticsearch
It should be noted that if Spring Data Elasticsearch is used, spring boot 1.5++ version does not support the latest version of elasticsearch.
Below are the corresponding versions of spring data elasticsearch and elasticsearch
| spring data 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.RELEASE | 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.RELEASE | 1.3.2 |
| 1.0.0.RELEASE | 1.1.1 |
The Elasticsearch version we are using is 5.5.6, and the spring boot version is 1.5.6, and supports Elasticsearch 5.0 or above. Spring data elasticsearch is not the RELEASE version. We use the elasticsearch Java client. However, the official recommendation is a better way. You can refer to the "Elasticsearch Java Rest API Manual" I wrote https://gitee.com/quanke/elasticsearch-java-rest, but this article still uses elasticsearch Java client.
Building Spring Boot Kotlin Projects
If you have problems building the project, you can refer to my previous article "Creating a RESTfull API with Spring Boot and Kotlin"
Build with Gradle, add in the build.gradle file
dependencies { compile "org.elasticsearch:elasticsearch:$elasticsearch_version" compile "org.elasticsearch.client:transport:$elasticsearch_version"}Complete build.gradle file
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 = '5.1.21' ext.mybatis_version = '1.1.1' ext.elasticsearch_version = '5.5.1' ext.fastjson_version = '1.2.7' repositories { mavenCentral() } Dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath("org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version")// Kotlin integrates SpringBoot's default parameterless constructor, and sets all classes by default open class plugin classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlin_version") classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version") }}apply plugin: 'kotlin'apply plugin: "kotlin-spring" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-pluginapply plugin: 'org.springframework.boot'apply plugin: "kotlin-jpa" //https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-helljar { baseName = 'chapter11-6-8-service' version = '0.1.0'}repositories { mavenCentral()}dependencies { 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_version" compile "org.elasticsearch.client:transport:$elasticsearch_version" compile "com.alibaba:fastjson:$fastjson_version" 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.jvmTarget = "1.8"} compileTestKotlin { kotlinOptions.jvmTarget = "1.8"}Write the test base class ElasticsearchClient first
import com.alibaba.fastjson.JSONimport com.alibaba.fastjson.serializer.SerializerFeatureimport org.elasticsearch.action.search.SearchResponseimport org.elasticsearch.client.transport.TransportClientimport org.elasticsearch.common.settings.Settingsimport org.elasticsearch.common.transport.InetSocketTransportAddressimport org.elasticsearch.transport.client.PreBuiltTransportClientimport org.junit.Afterimport org.junit.Beforeimport java.net.IntAddress/** * Initialization of Elasticsearch 5.5.1's client and ElasticsearchTemplate* As an external visitor, the cluster requesting ES is an external factor for the cluster. * Created by http://quanke.name on 2017/11/10. */open class ElasticsearchClient { protected var client: TransportClient? = null @Before @Throws(Exception::class) fun setUp() { val esSettings = Settings.builder() .put("cluster.name", "utan-es") //Set the name of the ES instance.put("client.transport.sniff", true) //Originally sniff the status of the entire cluster and add the ips of other ES nodes in the cluster to the local client list.build() /** * The connection method here refers to the fact that the x-pack plug-in is not installed. If x-pack is installed, refer to [ElasticsearchXPackClient] * 1. The Java client method is to communicate on port 9300 using the tcp protocol* 2. The http client method is to communicate on port 9200 using the http protocol*/ client = PreBuiltTransportClient(esSettings) .addTransportAddress(InetSocketTransportAddress(InetAddress.getByName("192.168.1.10"), 9300)) println("ElasticsearchClient connection is successful") } @After @Throws(Exception::class) fun tearsDown() { if (client != null) { client!!.close() } } protected fun println(searchResponse: SearchResponse) { val searchHits = searchResponse.hits.hits for (searchHit in searchHits) { println(JSON.toJSONString(searchHit.source, SerializerFeature.PrettyFormat)) } }}Run unit tests
import org.elasticsearch.index.query.QueryBuilders.matchAllQueryimport org.junit.Testimport org.junit.runner.RunWithimport org.springframework.boot.test.context.SpringBootTestimport org.springframework.test.context.junit4.SpringRunner/** * Created by http://quanke.name on 2018/1/9. */@RunWith(SpringRunner::class)@SpringBootTestclass ApplicationTests: ElasticsearchClient() { @Test fun `es test"`() { val qb = matchAllQuery() val response = client!!.prepareSearch("twitter")//can be multiple indexes .setTypes("tweet")//can be multiple types.setQuery(qb) //Query query conditions.get() println(response) }}We wrote a dayu-spring-boot-starter-es, and we have the opportunity to open source it.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.