Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T2118 fuzzy search #4401

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dev/script/generate-config/service-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ function getMediaApiConfig(config) {
|es6.replicas=${config.es6.replicas}
|quota.store.key="rcs-quota.json"
|security.cors.allowedOrigins="${getCorsAllowedOriginString(config)}"
|search.fuzziness={
| enabled=true
|}
|metrics.request.enabled=false
|syndication.review.useRuntimeFieldsFix=true
|`;
Expand Down
17 changes: 17 additions & 0 deletions media-api/app/lib/MediaApiConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ class MediaApiConfig(resources: GridConfigResources) extends CommonConfigWithEla
val cloudFrontPrivateKeyBucketKey: Option[String] = stringOpt("cloudfront.private-key.key")
val cloudFrontKeyPairId: Option[String] = stringOpt("cloudfront.keypair.id")

val fuzzySearchEnabled: Boolean = boolean("search.fuzziness.enabled")
val fuzzySearchPrefixLength: Int = stringOpt("search.fuzziness.prefixLength") match {
case Some(prefixLength) if convertToInt(prefixLength).isDefined => prefixLength.toInt
case _ => 1
}
val fuzzySearchEditDistance: String = stringOpt("search.fuzziness.editDistance") match {
case Some(editDistance) if convertToInt(editDistance).isDefined => editDistance
case Some(editDistance) if editDistance.contains("AUTO:") => editDistance //<- for non-default AUTO word boundaries
case _ => "AUTO"
}
val fuzzyMaxExpansions: Int = stringOpt("search.fuzziness.maxExpansions") match {
case Some(maxExpansions) if convertToInt(maxExpansions).isDefined => maxExpansions.toInt
case _ => 50
}

val rootUri: String = services.apiBaseUri
val kahunaUri: String = services.kahunaBaseUri
val cropperUri: String = services.cropperBaseUri
Expand Down Expand Up @@ -61,4 +76,6 @@ class MediaApiConfig(resources: GridConfigResources) extends CommonConfigWithEla

val restrictDownload: Boolean = boolean("restrictDownload")

private def convertToInt(s: String): Option[Int] = Try { s.toInt }.toOption

}
17 changes: 14 additions & 3 deletions media-api/app/lib/elasticsearch/QueryBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,21 @@ class QueryBuilder(matchFields: Seq[String], overQuotaAgencies: () => List[Agenc
private def multiMatchPhraseQuery(value: String, fields: Seq[String]): MultiMatchQuery =
ElasticDsl.multiMatchQuery(value).fields(fields).matchType(MultiMatchQueryBuilderType.PHRASE)

private def multiMatchWordQuery(value: String, fields: Seq[String]): MultiMatchQuery = {
val multiMatchQuery = ElasticDsl.multiMatchQuery(value).fields(fields).operator(Operator.AND)

if (config.fuzzySearchEnabled) {
multiMatchQuery.matchType(MultiMatchQueryBuilderType.BEST_FIELDS)
.fuzziness(config.fuzzySearchEditDistance)
.maxExpansions(config.fuzzyMaxExpansions)
.prefixLength(config.fuzzySearchPrefixLength)
} else {
multiMatchQuery.matchType(MultiMatchQueryBuilderType.CROSS_FIELDS)
}
}

private def makeMultiQuery(value: Value, fields: Seq[String]): MultiMatchQuery = value match {
case Words(value) => ElasticDsl.multiMatchQuery(value).fields(fields).
operator(Operator.AND).
matchType(MultiMatchQueryBuilderType.CROSS_FIELDS)
case Words(value) => multiMatchWordQuery(value, fields)
case Phrase(string) => multiMatchPhraseQuery(string, fields)
// That's OK, we only do date queries on a single field at a time
case e => throw InvalidQuery(s"Cannot do multiQuery on $e")
Expand Down
26 changes: 25 additions & 1 deletion media-api/test/lib/elasticsearch/QueryBuilderTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class QueryBuilderTest extends AnyFunSpec with Matchers with ConditionFixtures w

val matchFields: Seq[String] = Seq("afield", "anothermatchfield")

private val commonConfigurations = USED_CONFIGS_IN_TEST ++ MOCK_CONFIG_KEYS.map(_ -> NOT_USED_IN_TEST).toMap

private val mediaApiConfig = new MediaApiConfig(GridConfigResources(
Configuration.from(USED_CONFIGS_IN_TEST ++ MOCK_CONFIG_KEYS.map(_ -> NOT_USED_IN_TEST).toMap),
Configuration.from(commonConfigurations),
null,
new ApplicationLifecycle {
override def addStopHook(hook: () => Future[_]): Unit = {}
Expand Down Expand Up @@ -132,6 +134,28 @@ class QueryBuilderTest extends AnyFunSpec with Matchers with ConditionFixtures w
multiMatchClause.`type` shouldBe Some(MultiMatchQueryBuilderType.CROSS_FIELDS)
}

it("any field words queries should be applied to all of the match fields with best fields type and fuzziness, operator and analyzers set") {
val mediaApiConfigWithFuzzySearch = new MediaApiConfig(GridConfigResources(
Configuration.from(commonConfigurations ++ Map("search.fuzziness.enabled" -> true)),
null,
new ApplicationLifecycle {
override def addStopHook(hook: () => Future[_]): Unit = {}
override def stop(): Future[_] = Future.successful(())
}
))
val queryBuilder = new QueryBuilder(matchFields, () => Nil, mediaApiConfigWithFuzzySearch)
val query = queryBuilder.makeQuery(List(anyFieldWordsCondition)).asInstanceOf[BoolQuery]

query.must.size shouldBe 1
val multiMatchClause = query.must.head.asInstanceOf[MultiMatchQuery]
multiMatchClause.text shouldBe "cats dogs"
multiMatchClause.fields.map(_.field) shouldBe matchFields
multiMatchClause.operator shouldBe Some(Operator.AND)
multiMatchClause.`type` shouldBe Some(MultiMatchQueryBuilderType.BEST_FIELDS)
multiMatchClause.fuzziness shouldBe defined
multiMatchClause.fuzziness shouldBe Some("AUTO")
}

it("multiple field queries should query against the requested fields only") {
val query = queryBuilder.makeQuery(List(multipleFieldWordsCondition)).asInstanceOf[BoolQuery]

Expand Down
Loading