Skip to content

Commit

Permalink
📝 Elastic search engine implemented # 2
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbatovK committed Mar 24, 2024
1 parent e500aa6 commit 9e89d2c
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import org.springframework.data.elasticsearch.annotations.FieldType
class Tender(
@Field(type = FieldType.Text, name = "api_tender_info")
var apiTenderInfo: String?,
@Field(type = FieldType.Keyword, name = "category")
@Field(type = FieldType.Text, name = "category")
var category: String?,
@Field(type = FieldType.Keyword, name = "customer")
@Field(type = FieldType.Text, name = "customer")
var customer: String?,
@Field(type = FieldType.Text, name = "date")
var date: String?,
Expand All @@ -35,7 +35,7 @@ class Tender(
var tenderInnerLink: String?,
@Field(type = FieldType.Text, name = "tender_name")
var tenderName: String?,
@Field(type = FieldType.Keyword, name = "tender_num_outer")
@Field(type = FieldType.Text, name = "tender_num_outer")
var tenderNumOuter: String?,
@Field(type = FieldType.Text, name = "user_id")
var userId: String?,
Expand All @@ -60,5 +60,9 @@ fun TenderDto.toDomainObject() =
tenderName = this.TenderName,
tenderNumOuter = this.TenderNumOuter,
userId = this.User_id,
metaData = this.searchFragmentXML?.fragment?.joinToString() ?: ""
metaData = (
this.ApiTenderInfo + this.Category + this.Customer + this.Etp + this.ID + this.Price +
this.Region + this.TenderName + this.User_id +
this.searchFragmentXML?.fragment?.joinToString()
)
)
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
package com.albatros.springsecurity.data.model.dto
package com.albatros.springsecurity.data.model.dto

class FullTextSearchRequest(
val include: String,
val exclude: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,77 @@ package com.albatros.springsecurity.data.repository
import com.albatros.springsecurity.data.model.database.Tender
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.elasticsearch.annotations.Query
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository
import org.springframework.stereotype.Repository

@Repository
interface TenderRepository : ElasticsearchRepository<Tender, String> {
interface TenderSearchRepository : ElasticsearchRepository<Tender, String> {

@Query(
"""
{
"bool": {
"must": [
{
"multi_match": {
"query": "?0",
"fields": ["tender_name^5", "tender_id^15", "region^5", "etp^10", "fz", "meta_data", "user_id", "category^10", "customer^15", "date"],
"operator": "AND",
"fuzziness": "2",
"minimum_should_match": "2",
"type": "best_fields"
}
}
],
"must_not": [
{
"multi_match": {
"query": "?1",
"fields": ["tender_name^5", "tender_id^15", "region^5", "etp^10", "fz", "meta_data", "user_id", "category^10", "customer^15", "date"],
"operator": "AND",
"fuzziness": "2",
"type": "best_fields"
}
}
]
}
}
"""
)
fun fullTextSearchAnd(keywords: String): List<Tender>

@Query(
"""
{
"bool": {
"must": [
{
"multi_match": {
"query": "?0",
"fields": ["tender_name^5", "tender_id^15", "region^5", "etp^10", "fz", "meta_data", "user_id", "category^10", "customer^15", "date"],
"operator": "OR",
"fuzziness": "2",
"type": "best_fields"
}
}
],
"must_not": [
{
"multi_match": {
"query": "?1",
"fields": ["tender_name^5", "tender_id^15", "region^5", "etp^10", "fz", "meta_data", "user_id", "category^10", "customer^15", "date"],
"operator": "AND",
"fuzziness": "2",
"type": "best_fields"
}
}
]
}
}
"""
)
fun fullTextSearchOr(keywords: String, exclude: String): List<Tender>
fun findAllByTenderId(tenderId: String, pageable: Pageable): Page<Tender>

fun findAllByCustomerContainsIgnoreCase(customer: String, pageable: Pageable): Page<Tender>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.albatros.springsecurity.presentation.controller

import com.albatros.springsecurity.data.model.database.Tender
import com.albatros.springsecurity.data.repository.TenderRepository
import com.albatros.springsecurity.data.model.dto.FullTextSearchRequest
import com.albatros.springsecurity.data.repository.TenderSearchRepository
import com.albatros.springsecurity.data.service.TenderApiService
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
Expand All @@ -13,31 +18,42 @@ import org.springframework.web.bind.annotation.RestController
@RequestMapping("/api")
class TenderController(
private val apiService: TenderApiService,
private val tenderRepository: TenderRepository
private val tenderRepository: TenderSearchRepository,
) {

@PostMapping("/full-text-search")
fun getAll(@RequestBody fullTextSearchRequest: FullTextSearchRequest): List<Tender> {
return tenderRepository.fullTextSearchOr(
fullTextSearchRequest.include, fullTextSearchRequest.exclude
)
}

@GetMapping("/provider/")
fun getAll() = apiService.getAllTenderProviders()

@GetMapping("/provider/{id}/tender")
fun getTendersByProviderId(@PathVariable id: Int) = apiService.getTendersByProviders(id)

@GetMapping("/tender/customer")
fun findAllByCustomerContainsIgnoreCase(@RequestParam customer: String): List<Tender> =
tenderRepository.findAllByCustomerContainsIgnoreCase(customer)
fun findAllByCustomerContainsIgnoreCase(@RequestParam customer: String, pageable: Pageable): Page<Tender> =
tenderRepository.findAllByCustomerContainsIgnoreCase(customer, pageable)

@GetMapping("/tender/region")
fun findAllByRegionContainsIgnoreCase(@RequestParam region: String): List<Tender> =
tenderRepository.findAllByRegionContainsIgnoreCase(region)
fun findAllByRegionContainsIgnoreCase(@RequestParam region: String, pageable: Pageable): Page<Tender> =
tenderRepository.findAllByRegionContainsIgnoreCase(region, pageable)

@GetMapping("/tender/category")
fun findAllByCategory(@RequestParam category: String): List<Tender> = tenderRepository.findAllByCategory(category)
fun findAllByCategory(@RequestParam category: String, pageable: Pageable): Page<Tender> =
tenderRepository.findAllByCategory(category, pageable)

@GetMapping("/tender/etp")
fun findAllByEtpEqualsIgnoreCase(@RequestParam etp: String): List<Tender> =
tenderRepository.findAllByEtpEqualsIgnoreCase(etp)
fun findAllByEtpEqualsIgnoreCase(@RequestParam etp: String, pageable: Pageable): Page<Tender> =
tenderRepository.findAllByEtpEqualsIgnoreCase(etp, pageable)

@GetMapping("/tender/name")
fun findAllByTenderNameContainingIgnoreCase(@RequestParam(value = "query") query: String): List<Tender> =
tenderRepository.findAllByTenderNameContainingIgnoreCase(query)
fun findAllByTenderNameContainingIgnoreCase(
@RequestParam(value = "query") query: String,
pageable: Pageable
): Page<Tender> =
tenderRepository.findAllByTenderNameContainingIgnoreCase(query, pageable)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.albatros.springsecurity.data.model.database.toDomainObject
import com.albatros.springsecurity.data.model.dto.TenderDto
import com.albatros.springsecurity.data.model.dto.TenderProviderDto
import com.albatros.springsecurity.data.repository.TenderProviderRepository
import com.albatros.springsecurity.data.repository.TenderRepository
import com.albatros.springsecurity.data.repository.TenderSearchRepository
import com.albatros.springsecurity.data.service.TenderApiService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
Expand All @@ -15,7 +15,7 @@ import org.springframework.scheduling.annotation.Scheduled
@Configuration
class Scheduler(
private val tenderProviderRepository: TenderProviderRepository,
private val tenderRepository: TenderRepository,
private val tenderRepository: TenderSearchRepository,
private val apiService: TenderApiService,
) {

Expand Down

0 comments on commit 9e89d2c

Please sign in to comment.