Skip to content

Commit

Permalink
feat: add sorting to elastic query
Browse files Browse the repository at this point in the history
  • Loading branch information
hegeaal committed Oct 6, 2023
1 parent 13b81e8 commit a20036d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ConceptSearchService(
private fun SearchOperation.toElasticQuery(orgNumber: String): Query {
val qb = NativeQuery.builder()
qb.withFilter { q -> q.match { m -> m.field("ansvarligVirksomhet.id").query(orgNumber) } }
qb.withSort { s -> s.field{ f -> f.field( sort.sortField()).order(sort.sortDirection()) }}
if (!query.isNullOrBlank()) qb.addFieldsQuery(fields, query)
qb.withPageable(Pageable.ofSize(pagination.getSize()).withPage(pagination.getPage()))
return qb.build()
Expand All @@ -49,6 +50,17 @@ class ConceptSearchService(
}
}

private fun SortField.sortDirection(): SortOrder =
when (direction) {
SortDirection.ASC -> SortOrder.Asc
else -> SortOrder.Desc
}
private fun SortField.sortField(): String =
when (field) {
SortFieldEnum.ANBEFALT_TERM_NB -> "anbefaltTerm.navn.nb.keyword"
else -> "endringslogelement.endringstidspunkt"
}

private fun QueryFields.paths(): List<String> =
listOf(
// Boosting hits in anbefaltTerm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,6 @@ class SearchConcepts : ApiTestContext() {
}

@Test
@Ignore
fun `Query returns sorted results ordered by sistEndret ascending`() {
val searchOp = SearchOperation(
query = "",
Expand All @@ -607,7 +606,6 @@ class SearchConcepts : ApiTestContext() {
}

@Test
@Ignore
fun `Query returns sorted results ordered by anbefaltTerm descending`() {
val searchOp = SearchOperation(
query = "",
Expand Down

0 comments on commit a20036d

Please sign in to comment.