日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

kotlin + springboot启用elasticsearch搜索

發布時間:2023/11/27 生活经验 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 kotlin + springboot启用elasticsearch搜索 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考自:

http://how2j.cn/k/search-engine/search-engine-springboot/1791.html?p=78908

工具版本: elasticsearch 6.2.2、 kibana 6.2.2,? 下載地址: elasticsearch、kibana

下載demo

1、kotlin版springboot項目創建

訪問https://start.spring.io/, 創建項目demo(maven + kotlin + springboot 2.1.7, 其他默認)。

添加web支持、elasticsearch搜索及kotlin測試所需依賴

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-test-junit5 --><dependency><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-test-junit5</artifactId><version>1.2.70</version><scope>test</scope></dependency>

最終pom.xml文件為

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.7.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><kotlin.version>1.2.71</kotlin.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-reflect</artifactId></dependency><dependency><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-stdlib-jdk8</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-test-junit5 --><dependency><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-test-junit5</artifactId><version>1.2.70</version><scope>test</scope></dependency></dependencies><build><sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory><testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-maven-plugin</artifactId><configuration><args><arg>-Xjsr305=strict</arg></args><compilerPlugins><plugin>spring</plugin></compilerPlugins></configuration><dependencies><dependency><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-maven-allopen</artifactId><version>${kotlin.version}</version></dependency></dependencies></plugin></plugins></build></project>
View Code

2、創建實體類Category.kt

package com.example.demo.entityimport org.springframework.data.elasticsearch.annotations.Document@Document(indexName = "test", type = "category")
class Category {var id : Int? = null;var name : String? = null;
}

es操作dao類CategoryESDAO.kt

package com.example.demo.esimport com.example.demo.entity.Category
import org.springframework.data.elasticsearch.repository.ElasticsearchRepositoryinterface CategoryESDAO : ElasticsearchRepository<Category, Int>

創建類SearchController.kt,并實現搜索方法,這里根據keyword作為前綴進行搜索

package com.example.demo.controllerimport com.example.demo.entity.Category
import com.example.demo.es.CategoryESDAO
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery
import org.elasticsearch.index.query.QueryBuilders
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import javax.annotation.Resource@RestController
class SearchController {@Resourceprivate lateinit var categoryESDAO: CategoryESDAO@GetMapping("/search")fun search(@RequestParam(value = "keyword") keyword: String, @RequestParam(value = "start", defaultValue = "0") start: Int,@RequestParam(value = "size", defaultValue = "5") size: Int): List<Category> {val queryBuilder = QueryBuilders.matchPhrasePrefixQuery("name", keyword)val sort = Sort(Sort.Direction.DESC, "id")val pageable = PageRequest.of(start, size, sort)val searchQuery = NativeSearchQueryBuilder().withPageable(pageable).withQuery(queryBuilder).build()val page = categoryESDAO.search(searchQuery)return page.content.filter {category ->  category != null}.toList()}}

在類DemoApplication.kt中指定包名,

package com.example.demoimport org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = ["com.example.demo.es"])
class DemoApplicationfun main(args: Array<String>) {runApplication<DemoApplication>(*args)
}

在resources目錄下application.properties中增加elasticsearch的參數

#ElasticSearch
spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300

3、創建測試類

在test/kotlin目錄下com.example.demo包下創建類TestSearchController.kt測試搜索

package com.example.demoimport com.example.demo.entity.Category
import com.example.demo.es.CategoryESDAO
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.HttpMethod
import org.springframework.test.context.web.WebAppConfiguration
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.context.WebApplicationContext
import javax.annotation.Resource@SpringBootTest
@WebAppConfiguration
class TestSearchController {@Resourceprivate lateinit var wac : WebApplicationContext@Resourceprivate lateinit var categoryESDAO: CategoryESDAO@BeforeEachfun add() {for (ch in 'a'..'z') {val category = Category()category.id = ch - 'a'category.name = ch.toUpperCase().toString().plus(ch)categoryESDAO.save(category)}}@Testfun test() {val mockMvc = MockMvcBuilders.webAppContextSetup(wac).build()val result = mockMvc.perform(MockMvcRequestBuilders.request(HttpMethod.GET, "/search?keyword=Aa")).andExpect(MockMvcResultMatchers.status().isOk).andDo(::println).andReturn().response.contentAsString;println(result)}
}

依次啟動elasticsearch6.2.2、kibana6.2.2,

在瀏覽器中打開http://localhost:5601/app/kibana#/dev_tools/console?_g=()

執行

PUT test

創建test(對應Category類Document注解 indexName)索引,然后執行TestSearchController類進行測試,

控制臺輸出結果為:

[{"id":0,"name":"Aa"}]

?

轉載于:https://www.cnblogs.com/wushengwuxi/p/11349523.html

總結

以上是生活随笔為你收集整理的kotlin + springboot启用elasticsearch搜索的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。