Skip to content

Commit

Permalink
Merge pull request #957 from adpi2/tweak-scala-perc
Browse files Browse the repository at this point in the history
Tweak scala percentage
  • Loading branch information
adpi2 authored Mar 16, 2022
2 parents a9075d6 + b465a86 commit 35d2e69
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ import scaladex.infra.elasticsearch.RawProjectDocument
*/
class ElasticsearchEngine(esClient: ElasticClient, index: String)(implicit ec: ExecutionContext)
extends SearchEngine
with QueryDocumentFormat
with LazyLogging
with Closeable {
import ElasticsearchEngine._

private val maxLanguagesOrPlatforms = 20

Expand Down Expand Up @@ -307,7 +307,7 @@ class ElasticsearchEngine(esClient: ElasticClient, index: String)(implicit ec: E
val scalaPercentageField = fieldAccess("githubInfo.scalaPercentage", default = "100")
val scorer = scriptScore(
Script(
script = s"Math.log($githubStarField * $scalaPercentageField)"
script = s"Math.log($githubStarField * $scalaPercentageField + 1)"
)
)
functionScoreQuery()
Expand Down Expand Up @@ -499,4 +499,10 @@ object ElasticsearchEngine extends LazyLogging {
val esClient = ElasticClient(JavaClient(props))
new ElasticsearchEngine(esClient, config.index)
}

def fieldAccess(name: String): String =
s"doc['$name'].value"

def fieldAccess(name: String, default: String): String =
s"(doc['$name'].size() != 0 ? doc['$name'].value : $default)"
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ class ElasticsearchEngineTests extends AsyncFreeSpec with Matchers with BeforeAn
}
}

"should evaluate to field access syntax of the given field" in {
val field = "githubInfo.stars"
val accessExpr = ElasticsearchEngine.fieldAccess(field)
accessExpr shouldBe "doc['githubInfo.stars'].value"
}

"should evaluate to a field access that checks for nullability, and provides a default value" in {
val field = "githubInfo.stars"
val accessExpr = ElasticsearchEngine.fieldAccess(field, default = "0")
accessExpr shouldBe "(doc['githubInfo.stars'].size() != 0 ? doc['githubInfo.stars'].value : 0)"
}

private def projectDocument(ref: String, stars: Int, scalaPercentage: Int): ProjectDocument = {
val githubInfo = GithubInfoDocument.default.copy(stars = Some(stars), scalaPercentage = Some(scalaPercentage))
ProjectDocument.default(Project.Reference.from(ref)).copy(githubInfo = Some(githubInfo))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class AwesomePages(env: Env, searchEngine: SearchEngine)(implicit ec: ExecutionC
case (languageParams, platformParams, sortParam) =>
val scalaVersions = languageParams.flatMap(Language.fromLabel).collect { case v: Scala => v }.toSeq
val platforms = platformParams.flatMap(Platform.fromLabel).toSeq
val sorting = sortParam.flatMap(Sorting.byLabel.get).getOrElse(Sorting.Stars)
val sorting = sortParam.flatMap(Sorting.byLabel.get).getOrElse(Sorting.Relevance)
Tuple1(AwesomeParams(scalaVersions, platforms, sorting))
}

Expand Down

0 comments on commit 35d2e69

Please sign in to comment.