Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/src/main/kotlin/edu/wgu/osmt/PropertyLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import java.util.stream.StreamSupport


@Component
@Profile("dev")
@Profile("debug")
class PropertyLogger {
@EventListener
fun handleContextRefresh(event: ContextRefreshedEvent) {
Expand Down Expand Up @@ -43,4 +43,4 @@ class PropertyLogger {
companion object {
private val LOGGER = LoggerFactory.getLogger(PropertyLogger::class.java)
}
}
}
1 change: 1 addition & 0 deletions api/src/main/kotlin/edu/wgu/osmt/RoutePaths.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ object RoutePaths {
const val COLLECTION_REMOVE = "$COLLECTION_DETAIL/remove"

const val WORKSPACE_PATH = "/workspace"
const val WORKSPACE_LIST = WORKSPACE_PATH

private const val TASKS_PATH = "/results"
const val TASK_DETAIL_TEXT = "$TASKS_PATH/text/{uuid}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class CustomCollectionQueriesImpl @Autowired constructor(
.flatten()
.map { it.uuid }
.distinct()
return getCollectionFromUuids(pageable, filterDslQuery, (innerHitCollectionUuids + collectionMultiPropertyResults).distinct())
return getCollectionFromUuids(pageable, filterDslQuery, (innerHitCollectionUuids + collectionMultiPropertyResults).distinct(), "CustomCollectionQueriesImpl.byApiSearch()2", log)
}

@Deprecated("Upgrade to ES v8.x queries", ReplaceWith("createNestQueryDslQuery"), DeprecationLevel.WARNING )
Expand All @@ -161,9 +161,9 @@ class CustomCollectionQueriesImpl @Autowired constructor(

private fun getCollectionUuids(pageable: Pageable, filter: co.elastic.clients.elasticsearch._types.query_dsl.Query?, collectionName: String) : List<String> {
return if (collectionName.contains("\""))
getCollectionUuidsFromComplexName(pageable, filter, collectionName)
getCollectionUuidsFromComplexName(pageable, filter, collectionName, "getCollectionUuids", log)
else
getCollectionUuidsFromName(pageable, filter, collectionName)
getCollectionUuidsFromName(pageable, filter, collectionName, "getCollectionUuids", log)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,66 @@ import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders.matchAll
import edu.wgu.osmt.collection.CollectionDoc
import edu.wgu.osmt.db.PublishStatus
import edu.wgu.osmt.elasticsearch.WguQueryHelper.createMatchPhrasePrefixDslQuery
import edu.wgu.osmt.elasticsearch.WguQueryHelper.createQuery
import edu.wgu.osmt.elasticsearch.WguQueryHelper.createNativeQuery
import edu.wgu.osmt.elasticsearch.WguQueryHelper.createSimpleQueryDslQuery
import edu.wgu.osmt.elasticsearch.WguQueryHelper.createTermsDslQuery
import edu.wgu.osmt.richskill.RichSkillDoc
import org.slf4j.LoggerFactory
import org.slf4j.Logger
import org.springframework.data.domain.Pageable
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate
import org.springframework.data.elasticsearch.client.elc.NativeQuery
import org.springframework.data.elasticsearch.client.elc.NativeQueryBuilder
import org.springframework.data.elasticsearch.core.SearchHits
import java.util.stream.Collectors

/**
* This have been partially converted to use the ElasticSearch 8.7.X apis. Need to do full conversion to use
* the v8.7.x ES Java API client, https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/8.10/searching.html
*/
interface FindsAllByPublishStatus<T> {
val elasticSearchTemplate: ElasticsearchTemplate
val javaClass: Class<T>

fun findAllFilteredByPublishStatus(publishStatus: Set<PublishStatus>, pageable: Pageable): SearchHits<T> {
val nqb = createQueryBuilder(pageable, publishStatus)
val query = createQuery("FindsAllByPublishStatus.findAllFilteredByPublishStatus()", nqb, LoggerFactory.getLogger(FindsAllByPublishStatus::class.java))
return elasticSearchTemplate.search(query, javaClass)
val nativeQuery = createMatchAllRichSkillNativeQuery(pageable, publishStatus)
return elasticSearchTemplate.search(nativeQuery, javaClass)
}

fun countAllFilteredByPublishStatus(publishStatus: Set<PublishStatus>, pageable: Pageable): Long {
val nqb = createQueryBuilder(pageable, publishStatus)
val query = createQuery("FindsAllByPublishStatus.countAllFilteredByPublishStatus()", nqb, LoggerFactory.getLogger(FindsAllByPublishStatus::class.java))
return elasticSearchTemplate.count(query, javaClass)
val nativeQuery = createMatchAllRichSkillNativeQuery(pageable, publishStatus)
return elasticSearchTemplate.count(nativeQuery, javaClass)
}

fun createQueryBuilder(pageable: Pageable, publishStatus: Set<PublishStatus>): NativeQueryBuilder {
fun createMatchAllRichSkillNativeQuery(pageable: Pageable, publishStatus: Set<PublishStatus>): NativeQuery {
val MATCH_ALL = matchAll().build()._toQuery()
var filterValues = publishStatus
.stream()
.map { ps -> ps.name}
.collect(Collectors.toList())
.stream()
.map { ps -> ps.name}
.collect(Collectors.toList())
var filter = createTermsDslQuery( RichSkillDoc::publishStatus.name, filterValues, false)
return NativeQueryBuilder()
.withPageable(pageable)
.withQuery(MATCH_ALL)
.withFilter(filter)

return createNativeQuery(pageable, filter, MATCH_ALL)
}

fun getCollectionUuidsFromComplexName(pageable: Pageable, filter: co.elastic.clients.elasticsearch._types.query_dsl.Query?, collectionName: String) : List<String> {
val query = NativeQuery
.builder()
.withFilter(filter)
.withQuery(createSimpleQueryDslQuery("${CollectionDoc::name.name}.raw", collectionName))
.withPageable(pageable)
.build()
fun getCollectionUuidsFromComplexName(pageable: Pageable, dslFilter: co.elastic.clients.elasticsearch._types.query_dsl.Query?, collectionName: String, msgPrefix: String, log: Logger) : List<String> {
var dslQuery = createSimpleQueryDslQuery("${CollectionDoc::name.name}.raw", collectionName)
var nativeQuery = createNativeQuery(pageable, dslFilter, dslQuery, msgPrefix, log)

return elasticSearchTemplate
.search( query, CollectionDoc::class.java )
.searchHits
.map { it.content.uuid }
.search( nativeQuery, CollectionDoc::class.java )
.searchHits
.map { it.content.uuid }
}

fun getCollectionUuidsFromName(pageable: Pageable, filter: co.elastic.clients.elasticsearch._types.query_dsl.Query?, collectionName: String) : List<String> {
val query = NativeQuery
.builder()
.withFilter(filter)
.withQuery(createMatchPhrasePrefixDslQuery(CollectionDoc::name.name, collectionName))
.withPageable(pageable)
.build()
fun getCollectionUuidsFromName(pageable: Pageable, dslFilter: co.elastic.clients.elasticsearch._types.query_dsl.Query?, collectionName: String, msgPrefix: String, log: Logger) : List<String> {
var dslQuery = createMatchPhrasePrefixDslQuery(CollectionDoc::name.name, collectionName)
var nativeQuery = createNativeQuery(pageable, dslFilter, dslQuery, msgPrefix, log)

return elasticSearchTemplate
.search( query, CollectionDoc::class.java )
.search( nativeQuery, CollectionDoc::class.java )
.searchHits
.map { it.content.uuid }
}

fun getCollectionFromUuids(pageable: Pageable, filter: co.elastic.clients.elasticsearch._types.query_dsl.Query?, uuids: List<String> ): SearchHits<CollectionDoc> {
val query = NativeQuery
.builder()
.withFilter(filter)
.withQuery(createTermsDslQuery("_id", uuids))
.withPageable(pageable)
.build()
return elasticSearchTemplate.search(query, CollectionDoc::class.java)
fun getCollectionFromUuids(pageable: Pageable, dslFilter: co.elastic.clients.elasticsearch._types.query_dsl.Query?, uuids: List<String>, msgPrefix: String, log: Logger ): SearchHits<CollectionDoc> {
var dslQuery = createTermsDslQuery("_id", uuids)
var nativeQuery = createNativeQuery(pageable, dslFilter, dslQuery, msgPrefix, log)

return elasticSearchTemplate.search(nativeQuery, CollectionDoc::class.java)
}
}
56 changes: 29 additions & 27 deletions api/src/main/kotlin/edu/wgu/osmt/elasticsearch/WguQueryHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import co.elastic.clients.elasticsearch.core.search.InnerHits
import org.slf4j.Logger
import org.springframework.data.domain.Pageable
import org.springframework.data.elasticsearch.client.elc.NativeQuery
import org.springframework.data.elasticsearch.client.elc.NativeQueryBuilder
import org.springframework.data.elasticsearch.client.erhlc.NativeSearchQueryBuilder
import org.springframework.data.elasticsearch.core.query.Query
import org.springframework.data.elasticsearch.core.query.StringQuery
Expand All @@ -22,39 +21,42 @@ import java.util.stream.Collectors
* Utility class for leveraging latest ElasticSearch v8.7.X Java API
*/
object WguQueryHelper {
/**
* Stepping stone to 100% migration to ES v8.7.x apis; see KeywordEsRepo.kt
*/
fun convertToNativeQuery(pageable: Pageable, filter: co.elastic.clients.elasticsearch._types.query_dsl.Query?, nsqb: NativeSearchQueryBuilder, msgPrefix: String, log: Logger): Query {
val oldQuery = nsqb.build()

val nuQuery = NativeQuery.builder()
.withFilter(filter)
.withQuery(StringQuery(oldQuery.query.toString()))
.withPageable(pageable)
// .withSort(createSort("blah"))
.build()
log.debug(String.Companion.format("\n%s springDataQuery:\n\t\t%s", msgPrefix, (nuQuery.springDataQuery as StringQuery).source))
log.debug(String.Companion.format("\n%s filter:\n\t\t%s", msgPrefix, nuQuery.filter.toString()))
return nuQuery
@Deprecated("Upgrade to ES v8.x queries", ReplaceWith("createNativeQuery"), DeprecationLevel.WARNING )
fun convertToNativeQuery(pageable: Pageable, dslFilter: co.elastic.clients.elasticsearch._types.query_dsl.Query?, nsqb: NativeSearchQueryBuilder, msgPrefix: String, log: Logger): Query {
val springDataQuery = StringQuery(nsqb.build().query.toString())
return createNativeQuery(pageable, dslFilter, springDataQuery, msgPrefix, log)
}

@Deprecated("Upgrade to ES v8.x queries", ReplaceWith("createQuery"), DeprecationLevel.WARNING )
fun createStringQuery(msgPrefix: String, nqb: NativeSearchQueryBuilder, log: Logger): Query {
val query = nqb.build()
log.debug(String.Companion.format("\n%s query:\n\t\t%s", msgPrefix, query.query.toString()))
log.debug(String.Companion.format("\n%s filter:\n\t\t%s", msgPrefix, query.filter.toString()))
//NOTE: this is causing us to lose the filter query
return StringQuery(query.query.toString())
@Deprecated("Upgrade to ES v8.x queries", ReplaceWith("createNativeQuery"), DeprecationLevel.WARNING )
fun createNativeQuery(pageable: Pageable, dslFilter: co.elastic.clients.elasticsearch._types.query_dsl.Query?, springDataQuery: Query, msgPrefix: String? = null, log: Logger? = null): NativeQuery {
val query = NativeQuery
.builder()
.withFilter(dslFilter)
.withQuery(springDataQuery)
.withPageable(pageable)
.build()
log(query, msgPrefix, log)
return query;
}

fun createQuery(msgPrefix: String, nqb: NativeQueryBuilder, log: Logger): Query {
val query = nqb.build()
log.debug(String.Companion.format("\n%s query:\n\t\t%s", msgPrefix, query.query.toString()))
log.debug(String.Companion.format("\n%s filter:\n\t\t%s", msgPrefix, query.filter.toString()))
fun createNativeQuery(pageable: Pageable, dslFilter: co.elastic.clients.elasticsearch._types.query_dsl.Query?, dslQuery: co.elastic.clients.elasticsearch._types.query_dsl.Query, msgPrefix: String? = null, log: Logger? = null): NativeQuery {
val query = NativeQuery
.builder()
.withFilter(dslFilter)
.withQuery(dslQuery)
.withPageable(pageable)
// .withSort(createSort("blah"))
.build()
log(query, msgPrefix, log)
return query;
}

private fun log(nativeQuery: NativeQuery, msgPrefix: String?, log: Logger?) {
if (nativeQuery.springDataQuery == null || msgPrefix == null || log == null) return
log.debug(String.Companion.format("\n%s springDataQuery:\n\t\t%s", msgPrefix, (nativeQuery.springDataQuery as StringQuery).source))
log.debug(String.Companion.format("\n%s dslFilter:\n\t\t%s", msgPrefix, nativeQuery.filter.toString()))
}

fun createMatchPhrasePrefixDslQuery(fieldName: String, searchStr: String, boostVal : Float? = null): co.elastic.clients.elasticsearch._types.query_dsl.Query {
return QueryBuilders.matchPhrasePrefix { qb -> qb.field(fieldName).query(searchStr).boost(boostVal) }
}
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/kotlin/edu/wgu/osmt/jobcode/JobCodeEsRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package edu.wgu.osmt.jobcode

import edu.wgu.osmt.config.INDEX_JOBCODE_DOC
import edu.wgu.osmt.elasticsearch.OffsetPageable
import edu.wgu.osmt.elasticsearch.WguQueryHelper.createStringQuery
import edu.wgu.osmt.elasticsearch.WguQueryHelper.convertToNativeQuery
import org.elasticsearch.index.query.BoolQueryBuilder
import org.elasticsearch.index.query.Operator
import org.elasticsearch.index.query.QueryBuilders.*
Expand Down Expand Up @@ -47,7 +47,7 @@ class CustomJobCodeRepositoryImpl @Autowired constructor(override val elasticSea
.withPageable(createOffsetPageable(query))
.withQuery(disjunctionQuery)
.withSort(SortBuilders.fieldSort("${JobCode::code.name}.keyword").order(SortOrder.ASC))
val query = createStringQuery("CustomJobCodeRepositoryImpl.typeAheadSearch()", nqb, log)
val query = convertToNativeQuery(createOffsetPageable(query), null, nqb, "CustomJobCodeRepositoryImpl.typeAheadSearch()", log)
return elasticSearchTemplate.search(query, JobCode::class.java)
}

Expand Down
18 changes: 11 additions & 7 deletions api/src/main/kotlin/edu/wgu/osmt/keyword/KeywordEsRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import co.elastic.clients.elasticsearch._types.query_dsl.*
import edu.wgu.osmt.config.INDEX_KEYWORD_DOC
import edu.wgu.osmt.config.SORT_INSENSITIVE
import edu.wgu.osmt.elasticsearch.OffsetPageable
import edu.wgu.osmt.elasticsearch.WguQueryHelper.createStringQuery
import edu.wgu.osmt.elasticsearch.WguQueryHelper
import edu.wgu.osmt.elasticsearch.WguQueryHelper.convertToNativeQuery
import edu.wgu.osmt.jobcode.CustomJobCodeRepositoryImpl
import org.elasticsearch.index.query.QueryBuilders
import org.elasticsearch.search.sort.SortBuilders
Expand All @@ -14,7 +15,6 @@ import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Configuration
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate
import org.springframework.data.elasticsearch.client.elc.NativeQuery
import org.springframework.data.elasticsearch.client.erhlc.NativeSearchQueryBuilder
import org.springframework.data.elasticsearch.core.SearchHits
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates
Expand Down Expand Up @@ -70,7 +70,7 @@ class CustomKeywordRepositoryImpl @Autowired constructor(override val elasticSea
).minimumShouldMatch(1)
}

val query = createStringQuery("CustomKeywordRepositoryImpl.typeAheadSearch()", nqb, log)
val query = convertToNativeQuery( limitedPageable, null, nqb, "CustomKeywordRepositoryImpl.typeAheadSearch()", log )
return elasticSearchTemplate.search(query, Keyword::class.java)
}

Expand All @@ -88,10 +88,14 @@ class CustomKeywordRepositoryImpl @Autowired constructor(override val elasticSea
pageable = OffsetPageable(0, 20, null)
criteria = searchSpecific(searchStr, type)
}
log.debug(String.Companion.format("\ntypeAheadSearchNu query:\n\t\t%s", criteria.bool().toString()))
return elasticSearchTemplate.search( NativeQuery.builder()
.withPageable(pageable)
.withQuery(criteria).build(), Keyword::class.java )
// log.debug(String.Companion.format("\ntypeAheadSearchNu query:\n\t\t%s", criteria.bool().toString()))
// return elasticSearchTemplate.search( NativeQuery.builder()
// .withPageable(pageable)
// .withQuery(criteria)
// .build(), Keyword::class.java )

var nativeQuery = WguQueryHelper.createNativeQuery(pageable, null, criteria)
return elasticSearchTemplate.search(nativeQuery, Keyword::class.java)
}

fun searchAll(type: KeywordTypeEnum): Query {
Expand Down
Loading