浏览代码

spring data elasticsearch試し中

master
t.yamanaka 5 年前
父节点
当前提交
869992734d
共有 9 个文件被更改,包括 89 次插入60 次删除
  1. +1
    -2
      elasticsearchdemo/build.gradle.kts
  2. +1
    -1
      elasticsearchdemo/docker-compose.yml
  3. +27
    -0
      elasticsearchdemo/src/main/kotlin/com/example/elasticsearch/elasticsearchdemo/EsConfig.kt
  4. +4
    -55
      elasticsearchdemo/src/main/kotlin/com/example/elasticsearch/elasticsearchdemo/controller/APIController.kt
  5. +4
    -1
      elasticsearchdemo/src/main/kotlin/com/example/elasticsearch/elasticsearchdemo/model/Bank.kt
  6. +11
    -0
      elasticsearchdemo/src/main/kotlin/com/example/elasticsearch/elasticsearchdemo/repository/BankRepository.kt
  7. +12
    -0
      elasticsearchdemo/src/main/kotlin/com/example/elasticsearch/elasticsearchdemo/service/BankService.kt
  8. +28
    -0
      elasticsearchdemo/src/main/kotlin/com/example/elasticsearch/elasticsearchdemo/service/BankServiceImpl.kt
  9. +1
    -1
      elasticsearchdemo/src/main/resources/application.yml

+ 1
- 2
elasticsearchdemo/build.gradle.kts 查看文件

@@ -26,8 +26,7 @@ dependencies {
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
// implementation("org.elasticsearch:elasticsearch:5.1.2")
// implementation("org.elasticsearch.client:transport:5.1.2")

}

tasks.withType<Test> {

+ 1
- 1
elasticsearchdemo/docker-compose.yml 查看文件

@@ -2,7 +2,7 @@ version: '3.3'
services:
# elastic search.
es:
image: elasticsearch:7.1.0
image: elasticsearch:7.4.0
ports:
- "9200:9200"
- "9300:9300"

+ 27
- 0
elasticsearchdemo/src/main/kotlin/com/example/elasticsearch/elasticsearchdemo/EsConfig.kt 查看文件

@@ -0,0 +1,27 @@
package com.example.elasticsearch.elasticsearchdemo

import org.apache.http.HttpHost
import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestHighLevelClient
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.elasticsearch.core.ElasticsearchOperations
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories

@Configuration
@EnableElasticsearchRepositories
class EsConfig {

@Bean
fun client():RestHighLevelClient{
val client: RestHighLevelClient =
RestHighLevelClient(RestClient.builder(HttpHost("18.224.60.3",9200)))
return client
}

@Bean
fun elasticsearchTemplate():ElasticsearchOperations{
return ElasticsearchRestTemplate(client())
}
}

+ 4
- 55
elasticsearchdemo/src/main/kotlin/com/example/elasticsearch/elasticsearchdemo/controller/APIController.kt 查看文件

@@ -1,33 +1,16 @@
package com.example.elasticsearch.elasticsearchdemo.controller

import com.example.elasticsearch.elasticsearchdemo.model.Bank
import com.fasterxml.jackson.module.kotlin.isKotlinClass
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import java.net.InetAddress
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.elasticsearch.common.transport.TransportAddress
import org.elasticsearch.client.transport.TransportClient
import org.elasticsearch.transport.client.PreBuiltTransportClient
import org.elasticsearch.common.settings.Settings
import org.elasticsearch.action.search.SearchResponse
import org.elasticsearch.action.search.SearchType
import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestHighLevelClient
import org.elasticsearch.index.query.QueryBuilders
import org.elasticsearch.search.SearchHits
import org.elasticsearch.search.aggregations.AggregationBuilders
import com.example.elasticsearch.elasticsearchdemo.service.BankService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.elasticsearch.client.ClientConfiguration
import org.springframework.data.elasticsearch.core.ElasticsearchOperations
import org.springframework.data.elasticsearch.core.query.GetQuery
import org.springframework.web.bind.annotation.*
import kotlin.reflect.jvm.internal.impl.metadata.ProtoBuf

@RestController
@RequestMapping("v1")
class APIController {

@Autowired
lateinit var bankService:BankService

@GetMapping("hello")
fun hello(): String{
return """
@@ -35,39 +18,5 @@ class APIController {
"""
}

@GetMapping("/search/{keyword}")
fun index(@PathVariable("keyword")keyword: String): SearchResponse {
val settings: Settings = Settings.builder().put("cluster.name","docker-cluster").build()
val client:TransportClient = PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(TransportAddress(InetAddress.getByName("localhost"), 9300))


val response:SearchResponse = client.prepareSearch("bank")
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setQuery(
QueryBuilders.boolQuery()
.should(QueryBuilders.termQuery("account_number", keyword))
.should(QueryBuilders.termQuery("address",keyword))
.should(QueryBuilders.termQuery("age",keyword))
.should(QueryBuilders.termQuery("balance",keyword))
.should(QueryBuilders.termQuery("city",keyword))
.should(QueryBuilders.termQuery("email",keyword))
.should(QueryBuilders.termQuery("employer",keyword))
.should(QueryBuilders.termQuery("firstname",keyword))
.should(QueryBuilders.termQuery("gender",keyword))
.should(QueryBuilders.termQuery("lastname",keyword))
.should(QueryBuilders.termQuery("state",keyword))
)
.setFrom(0).setSize(10).setExplain(true)
.get()

println(response.hits)
println(response.hits.hits)

return response

}



}

+ 4
- 1
elasticsearchdemo/src/main/kotlin/com/example/elasticsearch/elasticsearchdemo/model/Bank.kt 查看文件

@@ -1,8 +1,11 @@
package com.example.elasticsearch.elasticsearchdemo.model

import org.springframework.data.annotation.Id
import org.springframework.data.elasticsearch.annotations.Document

@Document(indexName = "bank",type = "_doc")
data class Bank (
@Id
val accountNumber: Long
,val balance: Long
,val firstName: String
@@ -14,4 +17,4 @@ data class Bank (
,val email: String
,val city: String
,val state: String
)
)

+ 11
- 0
elasticsearchdemo/src/main/kotlin/com/example/elasticsearch/elasticsearchdemo/repository/BankRepository.kt 查看文件

@@ -0,0 +1,11 @@
package com.example.elasticsearch.elasticsearchdemo.repository

import com.example.elasticsearch.elasticsearchdemo.model.Bank
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository
import org.springframework.stereotype.Repository

@Repository
interface BankRepository : ElasticsearchRepository<Bank,String> {

fun findByName(name: String): List<Bank>
}

+ 12
- 0
elasticsearchdemo/src/main/kotlin/com/example/elasticsearch/elasticsearchdemo/service/BankService.kt 查看文件

@@ -0,0 +1,12 @@
package com.example.elasticsearch.elasticsearchdemo.service

import com.example.elasticsearch.elasticsearchdemo.model.Bank

interface BankService {

fun save(bank:Bank): Bank

fun delete(bank:Bank)

fun findByName(name:String):List<Bank>
}

+ 28
- 0
elasticsearchdemo/src/main/kotlin/com/example/elasticsearch/elasticsearchdemo/service/BankServiceImpl.kt 查看文件

@@ -0,0 +1,28 @@
package com.example.elasticsearch.elasticsearchdemo.service

import com.example.elasticsearch.elasticsearchdemo.model.Bank
import com.example.elasticsearch.elasticsearchdemo.repository.BankRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class BankServiceImpl : BankService{
lateinit var repository: BankRepository

@Autowired
fun setBankRepository(repository: BankRepository){
this.repository = repository
}

override fun save(bank: Bank): Bank {
return repository.save(bank)
}

override fun delete(bank: Bank) {
return repository.delete(bank)
}

override fun findByName(name: String): List<Bank> {
return repository.findByName(name)
}
}

+ 1
- 1
elasticsearchdemo/src/main/resources/application.yml 查看文件

@@ -2,7 +2,7 @@ spring:
data:
elasticsearch:
cluster-name: docker-cluster
network.host: 0.0.0.0
network.host: 127.0.0.0
transport.host: localhost
transport.tcp.port: 9300


正在加载...
取消
保存