Browse Source

spring data elasticsearch試し中

master
t.yamanaka 5 years ago
parent
commit
869992734d
9 changed files with 89 additions and 60 deletions
  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 View File

testImplementation("org.springframework.boot:spring-boot-starter-test") { testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine") 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> { tasks.withType<Test> {

+ 1
- 1
elasticsearchdemo/docker-compose.yml View File

services: services:
# elastic search. # elastic search.
es: es:
image: elasticsearch:7.1.0
image: elasticsearch:7.4.0
ports: ports:
- "9200:9200" - "9200:9200"
- "9300:9300" - "9300:9300"

+ 27
- 0
elasticsearchdemo/src/main/kotlin/com/example/elasticsearch/elasticsearchdemo/EsConfig.kt View File

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 View File

package com.example.elasticsearch.elasticsearchdemo.controller 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.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 org.springframework.web.bind.annotation.*
import kotlin.reflect.jvm.internal.impl.metadata.ProtoBuf


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


@Autowired
lateinit var bankService:BankService

@GetMapping("hello") @GetMapping("hello")
fun hello(): String{ fun hello(): String{
return """ return """
""" """
} }


@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 View File

package com.example.elasticsearch.elasticsearchdemo.model package com.example.elasticsearch.elasticsearchdemo.model


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


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

+ 11
- 0
elasticsearchdemo/src/main/kotlin/com/example/elasticsearch/elasticsearchdemo/repository/BankRepository.kt View File

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 View File

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 View File

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 View File

data: data:
elasticsearch: elasticsearch:
cluster-name: docker-cluster cluster-name: docker-cluster
network.host: 0.0.0.0
network.host: 127.0.0.0
transport.host: localhost transport.host: localhost
transport.tcp.port: 9300 transport.tcp.port: 9300



Loading…
Cancel
Save