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