Skip to content

Commit

Permalink
remove unnecessary use of NoopQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
kenoir committed Nov 21, 2024
1 parent ba2634f commit ae21bd5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.sksamuel.elastic4s._
import com.sksamuel.elastic4s.requests.searches._
import com.sksamuel.elastic4s.requests.searches.aggs.TermsAggregation
import com.sksamuel.elastic4s.requests.searches.queries.{
NoopQuery,
Query,
RangeQuery
}
Expand Down Expand Up @@ -54,10 +53,10 @@ class ImagesRequestBuilder()
searchOptions.searchQuery.isDefined || searchOptions.color.isDefined,
includes = Seq("display", "vectorValues.reducedFeatures"),
aggs = filteredAggregationBuilder(pairables).filteredAggregations,
preFilter = buildImageFilterQuery(unpairables),
preFilter = unpairables.map(buildImageFilterQuery),
postFilter = Some(
must(
buildImageFilterQuery(searchOptions.filters)
searchOptions.filters.map(buildImageFilterQuery)
)
),
knn = searchOptions.color.map(ColorQuery(_))
Expand Down Expand Up @@ -130,29 +129,36 @@ class ImagesRequestBuilder()
conceptIds
)
case GenreFilter(genreLabels) =>
termsQuery("filterableValues.source.genres.label", genreLabels)
case GenreConceptFilter(conceptIds) =>
if (conceptIds.isEmpty) NoopQuery
else
termsQuery("filterableValues.source.genres.concepts.id", conceptIds)
termsQuery(
"filterableValues.source.genres.label",
genreLabels
)
case GenreConceptFilter(conceptIds) if conceptIds.nonEmpty =>
termsQuery(
"filterableValues.source.genres.concepts.id",
conceptIds
)
case SubjectLabelFilter(subjectLabels) =>
termsQuery("filterableValues.source.subjects.label", subjectLabels)
case SubjectConceptFilter(conceptIds) =>
if (conceptIds.isEmpty) NoopQuery
else termsQuery("filterableValues.source.subjects.concepts.id", conceptIds)
termsQuery(
"filterableValues.source.subjects.label",
subjectLabels
)
case SubjectConceptFilter(conceptIds) if conceptIds.nonEmpty =>
termsQuery(
"filterableValues.source.subjects.concepts.id",
conceptIds
)
case DateRangeFilter(fromDate, toDate) =>
val (gte, lte) =
(fromDate map ElasticDate.apply, toDate map ElasticDate.apply)

RangeQuery(
"filterableValues.source.production.dates.range.from",
lte = lte,
gte = gte
)
}

private def buildImageFilterQuery(filters: Seq[ImageFilter]): Seq[Query] =
filters.map(buildImageFilterQuery) filter (_ != NoopQuery)

def requestWithSimilarFeatures
: (Index, String, IndexedImage, Int, Double) => SearchRequest =
rawSimilarityRequest(ImageSimilarity.features)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ object WorksRequestBuilder
sortByScore = searchOptions.searchQuery.isDefined,
includes = Seq("display", "type"),
aggs = filteredAggregationBuilder(pairables).filteredAggregations,
preFilter = buildWorkFilterQuery(VisibleWorkFilter :: unpairables),
preFilter = (VisibleWorkFilter :: unpairables).map(buildWorkFilterQuery),
postFilter = Some(
must(
buildWorkFilterQuery(pairables)
pairables.map(buildWorkFilterQuery)
)
)
)
Expand Down Expand Up @@ -139,11 +139,6 @@ object WorksRequestBuilder
searchOptions.sortOrder
}

private def buildWorkFilterQuery(filters: Seq[WorkFilter]): Seq[Query] =
filters.map {
buildWorkFilterQuery
} filter (_ != NoopQuery)

private def buildWorkFilterQuery(workFilter: WorkFilter): Query =
workFilter match {
case VisibleWorkFilter =>
Expand All @@ -161,20 +156,30 @@ object WorksRequestBuilder
gte = gte
)
case LanguagesFilter(languageIds) =>
termsQuery("filterableValues.languages.id", languageIds)
termsQuery(
"filterableValues.languages.id",
languageIds
)
case GenreFilter(genreQueries) =>
termsQuery("filterableValues.genres.label", genreQueries)
case GenreConceptFilter(conceptIds) =>
if (conceptIds.isEmpty) NoopQuery
else termsQuery("filterableValues.genres.concepts.id", conceptIds)

termsQuery(
"filterableValues.genres.label",
genreQueries
)
case GenreConceptFilter(conceptIds) if conceptIds.nonEmpty =>
termsQuery(
"filterableValues.genres.concepts.id",
conceptIds
)
case SubjectLabelFilter(labels) =>
termsQuery("filterableValues.subjects.label", labels)

case SubjectConceptFilter(conceptIds) =>
if (conceptIds.isEmpty) NoopQuery
else termsQuery("filterableValues.subjects.concepts.id", conceptIds)

termsQuery(
"filterableValues.subjects.label",
labels
)
case SubjectConceptFilter(conceptIds) if conceptIds.nonEmpty =>
termsQuery(
"filterableValues.subjects.concepts.id",
conceptIds
)
case ContributorsFilter(contributorQueries) =>
termsQuery(
"filterableValues.contributors.agent.label",
Expand Down

0 comments on commit ae21bd5

Please sign in to comment.