From d7a705c3b7cd579151d96c7ef8f47222dd12bc98 Mon Sep 17 00:00:00 2001 From: Paul Butcher Date: Fri, 15 Sep 2023 11:57:02 +0100 Subject: [PATCH] join knn filter terms with AND (#716) --- .../services/TemplateSearchBuilder.scala | 5 ++-- .../api/search/images/ImagesFiltersTest.scala | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/search/src/main/scala/weco/api/search/services/TemplateSearchBuilder.scala b/search/src/main/scala/weco/api/search/services/TemplateSearchBuilder.scala index 3650cafc3..dc3c18cbf 100644 --- a/search/src/main/scala/weco/api/search/services/TemplateSearchBuilder.scala +++ b/search/src/main/scala/weco/api/search/services/TemplateSearchBuilder.scala @@ -43,8 +43,9 @@ trait TemplateSearchBuilder extends Encoders { // on whether the document contains the term in any searchable field. // If there are fields that should be ignored, then this default will // be inappropriate, and can be overridden. - protected val knnFilter: String = """{ - | "multi_match": { "query": "{{query}}" } + protected val knnFilter: String = + """{ + | "multi_match": { "query": "{{query}}", "operator": "AND", "type": "cross_fields"} | }""".stripMargin // Importantly, this is *not* JSON, due to the `{{#` sequences, so must be created and sent as a string. diff --git a/search/src/test/scala/weco/api/search/images/ImagesFiltersTest.scala b/search/src/test/scala/weco/api/search/images/ImagesFiltersTest.scala index 7d713d81b..ee0511758 100644 --- a/search/src/test/scala/weco/api/search/images/ImagesFiltersTest.scala +++ b/search/src/test/scala/weco/api/search/images/ImagesFiltersTest.scala @@ -354,6 +354,7 @@ class ImagesFiltersTest extends AnyFunSpec with ApiImagesTestBase { imagesIndex, "images.examples.color-filter-tests.even-less-blue-foot", "images.examples.color-filter-tests.blue-foot", + "images.examples.color-filter-tests.blue", "images.examples.color-filter-tests.orange-foot", "images.examples.color-filter-tests.slightly-less-blue-foot" ) @@ -374,5 +375,28 @@ class ImagesFiltersTest extends AnyFunSpec with ApiImagesTestBase { } } } + it("combines multi-token query terms with AND") { + withImagesApi { + case (imagesIndex, routes) => + indexTestDocuments( + imagesIndex, + "images.examples.color-filter-tests.blue", // Green + Dye (subjects) + "images.examples.color-filter-tests.blue-foot", // Dye but not Green + "images.examples.color-filter-tests.even-less-blue-foot" // Neither Dye nor Green + ) + + assertJsonResponse( + routes, + path = f"$rootPath/images?query=green+dye&color=22bbff" + ) { + Status.OK -> imagesListResponse( + ids = Seq( + "images.examples.color-filter-tests.blue" + ), + strictOrdering = true + ) + } + } + } } }