Skip to content

Commit

Permalink
search-api: Try different grep query for different query results
Browse files Browse the repository at this point in the history
  • Loading branch information
jnatten committed Dec 9, 2024
1 parent 15d8dbf commit 35448aa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,36 @@ trait GrepSearchService {
searchDecompounded = true
)

val codeQueries = boolQuery().should(
prefixQuery("code", q.underlying).boost(50),
matchQuery("code", q.underlying).boost(10),
termQuery("code", q.underlying).boost(100)
)
val titleQuery = langQueryFunc("title", 6)

val onlyCodeQuery = boolQuery()
.must(codeQueries)
.not(titleQuery)

boolQuery()
.should(
langQueryFunc("title", 6),
prefixQuery("code", q.underlying).boost(50),
matchQuery("code", q.underlying).boost(10),
termQuery("code", q.underlying).boost(100)
)
.must(boolQuery().should(titleQuery, onlyCodeQuery))
}
.getOrElse(boolQuery())
query.filter(
Seq(
idsFilter(input),
prefixFilter(input)
).flatten
)
query.filter(getFilters(input))
}

def idsFilter(input: GrepSearchInput): Option[Query] = input.codes match {
private def getFilters(input: GrepSearchInput): List[Query] =
List(
idsFilter(input),
prefixFilter(input)
).flatten

private def idsFilter(input: GrepSearchInput): Option[Query] = input.codes match {
case Some(ids) if ids.nonEmpty => termsQuery("code", ids).some
case _ => None
}

def prefixFilter(input: GrepSearchInput): Option[Query] = input.prefixFilter match {
private def prefixFilter(input: GrepSearchInput): Option[Query] = input.prefixFilter match {
case Some(prefixes) if prefixes.nonEmpty =>
Some(
boolQuery().should(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ class GrepSearchServiceTest extends IntegrationSuite(EnableElasticsearchContaine
result.results.map(_.code).sorted should be(grepTestBundle.grepContext.map(_.kode).sorted)
}

test("That querying grep codes with prefixes returns nothing") {
grepIndexService.indexDocuments(1.some, Some(grepTestBundle)).get
blockUntil(() => grepIndexService.countDocuments == grepTestBundle.grepContext.size)

val result = grepSearchService
.searchGreps(emptyInput.copy(query = NonEmptyString.fromString("kakepenger"), prefixFilter = Some(List("TT"))))
.get

result.results.map(_.code).sorted should be(Seq.empty)
}

test("That searching for all grep prefixes works as expected") {
grepIndexService.indexDocuments(1.some, Some(grepTestBundle)).get
blockUntil(() => grepIndexService.countDocuments == grepTestBundle.grepContext.size)
Expand Down

0 comments on commit 35448aa

Please sign in to comment.