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> { |
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" |
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()) | |||||
} | |||||
} |
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 | |||||
} | |||||
} | } |
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 | ||||
) | |||||
) |
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> | |||||
} |
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> | |||||
} |
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) | |||||
} | |||||
} |
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 | ||||