Skip to content
This repository has been archived by the owner on May 22, 2021. It is now read-only.

Commit

Permalink
Fix #61: Change DataSource and ScoredResults ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-myltsev committed Aug 14, 2018
1 parent 3800d8a commit 0dacad1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
29 changes: 15 additions & 14 deletions common/src/main/scala/org/globalnames/index/util/DataSource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ object DataSource {
private val colDsId = 1
private val gbifDsId = 11

override def compare(x: t.DataSource, y: t.DataSource): Int = {
(x.id, y.id) match {
case (`colDsId`, `colDsId`) => 0
case (`gbifDsId`, `gbifDsId`) => 0
case (`colDsId`, _) => 1
case (_, `colDsId`) => -1
case (`gbifDsId`, _) => 1
case (_, `gbifDsId`) => -1
case (_, _) =>
if (x.quality == y.quality) {
Ordering.String.compare(y.title.toLowerCase, x.title.toLowerCase)
} else {
y.quality.getValue - x.quality.getValue
}
private def databaseLevel(ds: t.DataSource): Int = ds.id match {
case `colDsId` => 50
case _ if ds.quality.value == t.DataSourceQuality.Curated.value => 40
case `gbifDsId` => 30
case _ if ds.quality.value == t.DataSourceQuality.AutoCurated.value => 20
case _ => 10
}

override def compare(ds1: t.DataSource, ds2: t.DataSource): Int = {
val dbl1 = databaseLevel(ds1)
val dbl2 = databaseLevel(ds2)
if (dbl1 != dbl2) {
Ordering.Int.compare(dbl1, dbl2)
} else {
Ordering.String.compare(ds1.title, ds2.title)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import index.dao.{DBResultObj, Tables => T}
import index.dao.Projections._
import index.{thrift => t}
import thrift.{nameresolver => nr, matcher => m, MatchKind => MK}
import util.DataSource
import util.UuidEnhanced._
import slick.jdbc.PostgresProfile.api._

Expand Down Expand Up @@ -252,17 +253,36 @@ class NameResolver(request: nr.Request)
contexts = contexts)
}

private implicit val resultScoredOrdering: Ordering[nr.ResultScored] =
private val scoreOrdering: Ordering[t.Score] = new Ordering[t.Score] {
private val epsilon = 1e-6
private val doubleOrdering: Ordering[Double] = new Ordering[Double] {
override def compare(x: Double, y: Double): Int = {
if (Math.abs(x - y) < epsilon) 0
else Ordering.Double.compare(x, y)
}
}

override def compare(s1: t.Score, s2: t.Score): Int = {
Ordering.Option[Double](doubleOrdering).compare(s1.value, s2.value)
}
}

private val resultScoredOrdering: Ordering[nr.ResultScored] =
new Ordering[nr.ResultScored] {
override def compare(x: nr.ResultScored, y: nr.ResultScored): Int = {
if (x.result.dataSource.quality == y.result.dataSource.quality) {
if (x.score.value == y.score.value) {
Ordering.Int.compare(x.result.dataSource.recordCount, y.result.dataSource.recordCount)
val dataSourceCompare =
DataSource.ordering.compare(x.result.dataSource, y.result.dataSource)

if (dataSourceCompare == 0) {
val scoreOrderingCompare = scoreOrdering.compare(x.score, y.score)
if (scoreOrderingCompare == 0) {
Ordering.Int.reverse.compare(x.result.dataSource.recordCount,
y.result.dataSource.recordCount)
} else {
Ordering.Option[Double].compare(y.score.value, x.score.value)
Ordering.Option[Double].compare(x.score.value, y.score.value)
}
} else {
Ordering.Int.compare(x.result.dataSource.quality.value, y.result.dataSource.quality.value)
dataSourceCompare
}
}
}
Expand All @@ -272,9 +292,9 @@ class NameResolver(request: nr.Request)
responses.map { response =>
val results =
request.bestMatchOnly ?
{ response.results.nonEmpty ? Seq(response.results.min) | Seq() } |
response.results.sorted
val preferredResultsSorted = response.preferredResults.sorted
{ response.results.nonEmpty ? Seq(response.results.max(resultScoredOrdering)) | Seq() } |
response.results.sorted(resultScoredOrdering.reverse)
val preferredResultsSorted = response.preferredResults.sorted(resultScoredOrdering.reverse)
val preferredResults =
if (request.bestMatchOnly) {
for {
Expand Down

0 comments on commit 0dacad1

Please sign in to comment.