Skip to content

Commit

Permalink
add pagination to coloc, filter study types with opensearch
Browse files Browse the repository at this point in the history
formatting
  • Loading branch information
jdhayhurst committed Oct 9, 2024
1 parent 7fab2c9 commit fdbec7b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
27 changes: 11 additions & 16 deletions app/models/Backend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,21 @@ class Backend @Inject() (implicit
}

def getColocalisation(studyLocusId: String,
studyTypes: Option[Seq[StudyTypeEnum.Value]]
studyTypes: Option[Seq[StudyTypeEnum.Value]],
pagination: Option[Pagination]
): Future[IndexedSeq[Colocalisation]] = {
val indexName = getIndexOrDefault("colocalisation")
val pag = pagination.getOrElse(Pagination.mkDefault)
val terms = Map("leftStudyLocusId.keyword" -> Seq(studyLocusId),
"rightStudyLocusId.keyword" -> Seq(studyLocusId)
).filter(_._2.nonEmpty)
val filter = Seq(
termsQuery("rightStudyType.keyword", studyTypes.getOrElse(StudyTypeEnum.values))
)
val colocs = esRetriever
.getByIndexedQueryShould(indexName,
Map("leftStudyLocusId.keyword" -> studyLocusId,
"rightStudyLocusId.keyword" -> studyLocusId
),
Pagination.mkDefault,
fromJsValue[Colocalisation]
)
.getByIndexedTermsShould(indexName, terms, pag, fromJsValue[Colocalisation], filter = filter)
.map(_._1)
val colocsFilteredByStudyType = if (studyTypes.isDefined) {
colocs.map(
_.filter(coloc => studyTypes.get.contains(StudyTypeEnum.withName(coloc.rightStudyType)))
)
} else {
colocs
}
colocsFilteredByStudyType.map(_.map { coloc =>
colocs.map(_.map { coloc =>
val otherStudyLocusId: String = if (coloc.leftStudyLocusId != studyLocusId) {
coloc.leftStudyLocusId
} else {
Expand Down
28 changes: 26 additions & 2 deletions app/models/ElasticRetriever.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,13 @@ class ElasticRetriever @Inject() (
buildF: JsValue => Option[A],
aggs: Iterable[AbstractAggregation] = Iterable.empty,
sortByField: Option[sort.FieldSort] = None,
excludedFields: Seq[String] = Seq.empty
excludedFields: Seq[String] = Seq.empty,
filter: Seq[Query] = Seq.empty
): Future[(IndexedSeq[A], JsValue)] = {
// just log and execute the query
val indexQuery: IndexQuery[V] = IndexQuery(esIndex = esIndex,
kv = kv,
filters = filter,
pagination = pagination,
aggs = aggs,
excludedFields = excludedFields
Expand All @@ -182,6 +184,28 @@ class ElasticRetriever @Inject() (
getByIndexedQuery(searchRequest, sortByField, buildF)
}

def getByIndexedTermsShould[A, V](
esIndex: String,
kv: Map[String, V],
pagination: Pagination,
buildF: JsValue => Option[A],
aggs: Iterable[AbstractAggregation] = Iterable.empty,
sortByField: Option[sort.FieldSort] = None,
excludedFields: Seq[String] = Seq.empty,
filter: Seq[Query] = Seq.empty
): Future[(IndexedSeq[A], JsValue)] = {
// just log and execute the query
val indexQuery: IndexQuery[V] = IndexQuery(esIndex = esIndex,
kv = kv,
filters = filter,
pagination = pagination,
aggs = aggs,
excludedFields = excludedFields
)
val searchRequest: SearchRequest = IndexTermsShould(indexQuery)
getByIndexedQuery(searchRequest, sortByField, buildF)
}

/** This fn represents a query where each kv from the map is used in
* a bool 'should'. Based on the query asked by `getByIndexedQuery` and aggregation is applied
*/
Expand Down Expand Up @@ -229,7 +253,7 @@ class ElasticRetriever @Inject() (
case None => searchRequest
}

logger.debug(s"Elasticsearch query: ${client.show(sortedSearchRequest)}")
logger.info(s"Elasticsearch query: ${client.show(sortedSearchRequest)}")
sortedSearchRequest
}

Expand Down
5 changes: 5 additions & 0 deletions app/models/ElasticRetrieverQueryBuilders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ trait ElasticRetrieverQueryBuilders extends QueryApi with Logging {
): SearchRequest =
getByIndexTermsBuilder(indexQuery, must)

def IndexTermsShould[V](
indexQuery: IndexQuery[V]
): SearchRequest =
getByIndexTermsBuilder(indexQuery, should)

def getByIndexQueryBuilder[V](
indexQuery: IndexQuery[V],
f: Iterable[Query] => BoolQuery
Expand Down
6 changes: 3 additions & 3 deletions app/models/entities/CredibleSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import sangria.schema.{
StringType,
fields
}
import models.gql.Arguments.studyTypes
import models.gql.Arguments.{studyTypes, pageArg}

case class Locus(
variantId: Option[String],
Expand Down Expand Up @@ -279,10 +279,10 @@ object CredibleSet extends Logging {
"colocalisation",
OptionType(ListType(colocalisationImp)),
description = None,
arguments = studyTypes :: Nil,
arguments = studyTypes :: pageArg :: Nil,
resolve = js => {
val id = (js.value \ "studyLocusId").as[String]
js.ctx.getColocalisation(id, js.arg(studyTypes))
js.ctx.getColocalisation(id, js.arg(studyTypes), js.arg(pageArg))
}
)
)
Expand Down

0 comments on commit fdbec7b

Please sign in to comment.