Skip to content

Commit

Permalink
Remove unused contextFilters and add traits filter
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarvelle committed Dec 13, 2024
1 parent dee1657 commit 33d3cd1
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import no.ndla.searchapi.integration.SearchApiClient
import no.ndla.searchapi.model.api.grep.GrepSearchResultsDTO
import no.ndla.searchapi.model.api.{ErrorHandling, GroupSearchResultDTO, MultiSearchResultDTO, SubjectAggregationsDTO}
import no.ndla.searchapi.model.domain.{LearningResourceType, Sort}
import no.ndla.searchapi.model.search.SearchType
import no.ndla.searchapi.model.search.{SearchTrait, SearchType}
import no.ndla.searchapi.model.search.settings.{MultiDraftSearchSettings, SearchSettings}
import no.ndla.searchapi.service.search.{
GrepSearchService,
Expand Down Expand Up @@ -125,19 +125,15 @@ trait SearchController {
|If subjects are specified the learning resource must have specified relevances in relation to a specified subject.
|If levels are specified the learning resource must have specified relevances in relation to a specified level.""".stripMargin
)
private val contextFilters = listQuery[String]("context-filters")
.description(
"""A comma separated list of resource-types the learning resources should be filtered by.
|Used in conjunction with the parameter resource-types to filter additional resource-types.
""".stripMargin
)
private val includeMissingResourceTypeGroup = query[Boolean]("missing-group")
.description(
"Whether to include group without resource-types for group-search. Defaults to false."
)
.default(false)
private val grepCodes = listQuery[String]("grep-codes")
.description("A comma separated list of codes from GREP API the resources should be filtered by.")
private val traits = listQuery[String]("traits")
.description("A comma separated list of traits the resources should be filtered by.")
private val scrollId = query[Option[String]]("search-context")
.description(
s"""A unique string obtained from a search you want to keep scrolling in. To obtain one from a search, provide one of the following values: ${InitialScrollContextKeywords
Expand Down Expand Up @@ -199,10 +195,10 @@ trait SearchController {
.in(contextTypes)
.in(languageFilter)
.in(relevanceFilter)
.in(contextFilters)
.in(includeMissingResourceTypeGroup)
.in(aggregatePaths)
.in(grepCodes)
.in(traits)
.in(embedResource)
.in(embedId)
.in(filterInactive)
Expand All @@ -223,10 +219,10 @@ trait SearchController {
contextTypes,
languageFilter,
relevanceFilter,
anotherResourceTypes,
includeMissingResourceTypeGroup,
aggregatePaths,
grepCodes,
traits,
embedResource,
embedId,
filterInactive,
Expand All @@ -249,11 +245,12 @@ trait SearchController {
sort = sort,
withIdIn = learningResourceIds.values,
subjects = subjects.values,
resourceTypes = groupTypes.values ++ anotherResourceTypes.values,
resourceTypes = groupTypes.values,
learningResourceTypes = contextTypes.values.flatMap(LearningResourceType.valueOf),
supportedLanguages = languageFilter.values,
relevanceIds = relevanceFilter.values,
grepCodes = grepCodes.values,
traits = traits.values.flatMap(SearchTrait.valueOf),
shouldScroll = false,
filterByNoResourceType = false,
aggregatePaths = aggregatePaths.values,
Expand Down Expand Up @@ -380,9 +377,9 @@ trait SearchController {
.in(subjects)
.in(languageFilter)
.in(relevanceFilter)
.in(contextFilters)
.in(scrollId)
.in(grepCodes)
.in(traits)
.in(aggregatePaths)
.in(embedResource)
.in(embedId)
Expand All @@ -404,9 +401,9 @@ trait SearchController {
subjects,
languageFilter,
relevanceFilter,
contextFilters,
scrollId,
grepCodes,
traits,
aggregatePaths,
embedResource,
embedId,
Expand All @@ -427,11 +424,12 @@ trait SearchController {
sort = sort.getOrElse(Sort.ByRelevanceDesc),
withIdIn = learningResourceIds.values,
subjects = subjects.values,
resourceTypes = resourceTypes.values ++ contextFilters.values,
resourceTypes = resourceTypes.values,
learningResourceTypes = contextTypes.values.flatMap(LearningResourceType.valueOf),
supportedLanguages = languageFilter.values,
relevanceIds = relevanceFilter.values,
grepCodes = grepCodes.values,
traits = traits.values.flatMap(SearchTrait.valueOf),
shouldScroll = shouldScroll,
filterByNoResourceType = false,
aggregatePaths = aggregatePaths.values,
Expand Down Expand Up @@ -552,6 +550,7 @@ trait SearchController {
draftStatus = stringListParam("draft-status").some,
users = stringListParam("users").some,
grepCodes = stringListParam("grep-codes").some,
traits = stringListParam("traits").flatMap(SearchTrait.withNameOption).some,
aggregatePaths = stringListParam("aggregate-paths").some,
embedResource = stringListParam("embed-resource").some,
embedId = stringParamOrNone("embed-id"),
Expand Down Expand Up @@ -652,6 +651,7 @@ trait SearchController {
supportedLanguages = params.languageFilter.getOrElse(List.empty),
relevanceIds = params.relevance.getOrElse(List.empty),
grepCodes = params.grepCodes.getOrElse(List.empty),
traits = params.traits.getOrElse(List.empty),
shouldScroll = shouldScroll,
filterByNoResourceType = false,
aggregatePaths = params.aggregatePaths.getOrElse(List.empty),
Expand Down Expand Up @@ -690,6 +690,7 @@ trait SearchController {
statusFilter = params.draftStatus.getOrElse(List.empty).flatMap(DraftStatus.valueOf),
userFilter = params.users.getOrElse(List.empty),
grepCodes = params.grepCodes.getOrElse(List.empty),
traits = params.traits.getOrElse(List.empty),
shouldScroll = shouldScroll,
searchDecompounded = false,
aggregatePaths = params.aggregatePaths.getOrElse(List.empty),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.circe.{Decoder, Encoder}
import no.ndla.common.model.NDLADate
import no.ndla.network.tapir.NonEmptyString
import no.ndla.searchapi.model.domain.Sort
import no.ndla.searchapi.model.search.SearchType
import no.ndla.searchapi.model.search.{SearchTrait, SearchType}
import sttp.tapir.Schema
import sttp.tapir.Schema.annotations.description

Expand Down Expand Up @@ -68,6 +68,8 @@ case class DraftSearchParamsDTO(
users: Option[List[String]],
@description("A list of codes from GREP API the resources should be filtered by.")
grepCodes: Option[List[String]],
@description("A comma separated list of traits the resources should be filtered by.")
traits: Option[List[SearchTrait]],
@description("List of index-paths that should be term-aggregated and returned in result.")
aggregatePaths: Option[List[String]],
@description(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
import no.ndla.network.tapir.NonEmptyString
import no.ndla.searchapi.model.domain.Sort
import no.ndla.searchapi.model.search.SearchTrait
import sttp.tapir.Schema
import sttp.tapir.Schema.annotations.description

Expand Down Expand Up @@ -58,6 +59,8 @@ case class SearchParamsDTO(
languageFilter: Option[List[String]],
@description("A list of codes from GREP API the resources should be filtered by.")
grepCodes: Option[List[String]],
@description("A comma separated list of traits the resources should be filtered by.")
traits: Option[List[SearchTrait]],
@description("List of index-paths that should be term-aggregated and returned in result.")
aggregatePaths: Option[List[String]],
@description(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import no.ndla.common.model.NDLADate
import no.ndla.common.model.api.draft.CommentDTO
import no.ndla.searchapi.model.domain.LearningResourceType
import no.ndla.searchapi.model.search.SearchType
import no.ndla.searchapi.model.search.{SearchTrait, SearchType}
import sttp.tapir.Schema.annotations.description

@description("Object describing matched field with matching words emphasized")
Expand All @@ -38,7 +38,7 @@ case class MultiSearchSummaryDTO(
@description("Languages the resource exists in") supportedLanguages: Seq[String],
@description("Learning resource type") learningResourceType: LearningResourceType,
@description("Status information of the resource") status: Option[StatusDTO],
@description("Traits for the resource") traits: List[String],
@description("Traits for the resource") traits: List[SearchTrait],
@description("Relevance score. The higher the score, the better the document matches your search criteria.") score: Float,
@description("List of objects describing matched field with matching words emphasized") highlights: List[HighlightedFieldDTO],
@description("The taxonomy paths for the resource") paths: List[String],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ sealed abstract class SearchTrait(override val entryName: String) extends EnumEn
override def toString: String = entryName
}
object SearchTrait extends Enum[SearchTrait] with CirceEnumWithErrors[SearchTrait] {
case object Video extends SearchTrait("VIDEO")
case object H5p extends SearchTrait("H5P")
case object Audio extends SearchTrait("AUDIO")
case object File extends SearchTrait("FILE")
case object Video extends SearchTrait("VIDEO")
case object H5p extends SearchTrait("H5P")
case object Audio extends SearchTrait("AUDIO")
case object Podcast extends SearchTrait("PODCAST")

def all: List[String] = SearchTrait.values.map(_.toString).toList
override def values: IndexedSeq[SearchTrait] = findValues

implicit def schema: Schema[SearchTrait] = schemaForEnumEntry[SearchTrait]
def valueOf(s: String): Option[SearchTrait] = SearchTrait.values.find(_.entryName == s)
implicit def schema: Schema[SearchTrait] = schemaForEnumEntry[SearchTrait]

implicit val enumTsType: TSNamedType[SearchTrait] =
TSType.alias[SearchTrait]("SearchTrait", TSUnion(values.map(e => TSLiteralString(e.entryName))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import no.ndla.common.model.domain.draft.DraftStatus
import no.ndla.language.Language
import no.ndla.network.tapir.NonEmptyString
import no.ndla.searchapi.model.domain.{LearningResourceType, Sort}
import no.ndla.searchapi.model.search.SearchType
import no.ndla.searchapi.model.search.{SearchTrait, SearchType}

case class MultiDraftSearchSettings(
query: Option[NonEmptyString],
Expand All @@ -33,6 +33,7 @@ case class MultiDraftSearchSettings(
statusFilter: List[DraftStatus],
userFilter: List[String],
grepCodes: List[String],
traits: List[SearchTrait],
shouldScroll: Boolean,
searchDecompounded: Boolean,
aggregatePaths: List[String],
Expand Down Expand Up @@ -72,6 +73,7 @@ object MultiDraftSearchSettings {
statusFilter = List.empty,
userFilter = List.empty,
grepCodes = List.empty,
traits = List.empty,
shouldScroll = false,
searchDecompounded = false,
aggregatePaths = List.empty,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import no.ndla.common.model.domain.Availability
import no.ndla.language.Language
import no.ndla.network.tapir.NonEmptyString
import no.ndla.searchapi.model.domain.{LearningResourceType, Sort}
import no.ndla.searchapi.model.search.SearchTrait

case class SearchSettings(
query: Option[NonEmptyString],
Expand All @@ -27,6 +28,7 @@ case class SearchSettings(
supportedLanguages: List[String],
relevanceIds: List[String],
grepCodes: List[String],
traits: List[SearchTrait],
shouldScroll: Boolean,
filterByNoResourceType: Boolean,
aggregatePaths: List[String],
Expand All @@ -53,6 +55,7 @@ object SearchSettings {
supportedLanguages = List.empty,
relevanceIds = List.empty,
grepCodes = List.empty,
traits = List.empty,
shouldScroll = false,
filterByNoResourceType = false,
aggregatePaths = List.empty,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ trait MultiDraftSearchService {
if (settings.grepCodes.nonEmpty) Some(termsQuery("grepContexts.code", settings.grepCodes))
else None

val traitsFilter =
if (settings.traits.nonEmpty) Some(termsQuery("traits", settings.traits.map(_.entryName)))
else None

val embedResourceAndIdFilter =
buildNestedEmbedField(settings.embedResource, settings.embedId, settings.language, settings.fallback)

Expand Down Expand Up @@ -321,6 +325,7 @@ trait MultiDraftSearchService {
statusFilter,
usersFilter,
grepCodesFilter,
traitsFilter,
embedResourceAndIdFilter,
revisionDateFilter,
publishedDateFilter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ trait MultiSearchService {
Some(termsQuery("grepContexts.code", settings.grepCodes))
else None

val traitsFilter =
if(settings.traits.nonEmpty)
Some(termsQuery("traits", settings.traits.map(_.entryName)))
else None

val embedResourceAndIdFilter =
buildNestedEmbedField(settings.embedResource, settings.embedId, settings.language, settings.fallback)

Expand Down Expand Up @@ -181,6 +186,7 @@ trait MultiSearchService {
taxonomyRelevanceFilter,
taxonomyContextActiveFilter,
grepCodesFilter,
traitsFilter,
embedResourceAndIdFilter,
availabilityFilter
).flatten
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ trait SearchConverterService {
traits += SearchTrait.Video
}
case "audio" => traits += SearchTrait.Audio
case "podcast" => traits += SearchTrait.Audio
case "file" => traits += SearchTrait.File
case "podcast" => traits += SearchTrait.Podcast
case _ => // Do nothing
}
})
Expand Down
2 changes: 2 additions & 0 deletions search-api/src/test/scala/no/ndla/searchapi/TestData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,7 @@ object TestData {
supportedLanguages = List.empty,
relevanceIds = List.empty,
grepCodes = List.empty,
traits = List.empty,
shouldScroll = false,
filterByNoResourceType = false,
aggregatePaths = List.empty,
Expand Down Expand Up @@ -1628,6 +1629,7 @@ object TestData {
statusFilter = List.empty,
userFilter = List.empty,
grepCodes = List.empty,
traits = List.empty,
shouldScroll = false,
searchDecompounded = false,
aggregatePaths = List.empty,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import no.ndla.scalatestsuite.IntegrationSuite
import no.ndla.searchapi.TestData.*
import no.ndla.searchapi.model.api.MetaImageDTO
import no.ndla.searchapi.model.domain.{IndexingBundle, LearningResourceType, Sort}
import no.ndla.searchapi.model.search.SearchPagination
import no.ndla.searchapi.model.search.{SearchPagination, SearchTrait}
import no.ndla.searchapi.{TestData, TestEnvironment, UnitSuite}

import scala.util.Success
Expand Down Expand Up @@ -705,9 +705,18 @@ class MultiSearchServiceTest
multiSearchService.matchingQuery(searchSettings.copy(query = Some(NonEmptyString.fromString("Ekstrastoff").get)))
search.totalCount should be(1)
search.results.head.id should be(12)
search.results.head.traits should be(List("H5P"))
search.results.head.traits should be(List(SearchTrait.H5p))
}

test("That search can be filtered by traits") {
val Success(search) =
multiSearchService.matchingQuery(searchSettings.copy(traits = List(SearchTrait.H5p)))
search.totalCount should be(1)
search.results.head.id should be(12)
search.results.head.traits should be(List(SearchTrait.H5p))
}


test("That searches for embed attributes matches") {
val Success(search) =
multiSearchService.matchingQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ class SearchConverterServiceTest extends UnitSuite with TestEnvironment {
article,
IndexingBundle(Some(TestData.emptyGrepBundle), Some(emptyBundle), None)
)
searchableArticle.traits should equal(List(SearchTrait.H5p, SearchTrait.File))
searchableArticle.traits should equal(List(SearchTrait.H5p))

val article2 =
TestData.emptyDomainArticle.copy(
Expand Down

0 comments on commit 33d3cd1

Please sign in to comment.