1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.core
21 package post
22 package indexed
23 
24 import com.sksamuel.elastic4s.{ElasticApi, ElasticDsl}
25 import com.sksamuel.elastic4s.requests.searches.queries.Query
26 import com.sksamuel.elastic4s.requests.searches.sort.{FieldSort, SortOrder}
27 import org.make.core.technical.Pagination
28 import org.make.core.reference.Country
29 
30 final case class PostSearchQuery(
31   filters: Option[PostSearchFilters] = None,
32   limit: Option[Pagination.Limit] = None,
33   offset: Option[Pagination.Offset] = None,
34   sort: Option[String] = None,
35   order: Option[Order] = None
36 )
37 
38 final case class PostSearchFilters(
39   postIds: Option[PostIdsSearchFilter] = None,
40   displayHome: Option[DisplayHomeSearchFilter] = None,
41   countries: Option[PostCountriesFilter] = None
42 )
43 
44 object PostSearchFilters extends ElasticDsl {
45 
46   def parse(
47     postIds: Option[PostIdsSearchFilter] = None,
48     displayHome: Option[DisplayHomeSearchFilter] = None,
49     postCountries: Option[PostCountriesFilter] = None
50   ): Option[PostSearchFilters] = {
51     (postIds, displayHome, postCountries) match {
52       case (None, None, None) => None
53       case _                  => Some(PostSearchFilters(postIds, displayHome, postCountries))
54     }
55   }
56 
57   def getPostSearchFilters(postSearchQuery: PostSearchQuery): Seq[Query] = {
58     Seq(
59       buildPostIdsSearchFilter(postSearchQuery.filters),
60       buildDisplayHomeSearchFilter(postSearchQuery.filters),
61       buildPostCountriesSearchFilter(postSearchQuery.filters)
62     ).flatten
63   }
64 
65   def getOffsetSearch(postSearchQuery: PostSearchQuery): Int =
66     postSearchQuery.offset.fold(0)(_.extractInt)
67 
68   def getLimitSearch(postSearchQuery: PostSearchQuery): Int =
69     postSearchQuery.limit.fold(10)(_.extractInt)
70 
71   def getSort(postSearchQuery: PostSearchQuery): Option[FieldSort] = {
72     val order: SortOrder = postSearchQuery.order.map(_.sortOrder).getOrElse(SortOrder.Asc)
73 
74     postSearchQuery.sort.map { sort =>
75       FieldSort(field = sort, order = order)
76     }
77   }
78 
79   def buildPostIdsSearchFilter(maybeFilters: Option[PostSearchFilters]): Option[Query] = {
80     for {
81       filters                      <- maybeFilters
82       PostIdsSearchFilter(postIds) <- filters.postIds
83     } yield ElasticApi.termsQuery(PostElasticsearchFieldNames.postId, postIds.map(_.value))
84   }
85 
86   def buildDisplayHomeSearchFilter(maybeFilters: Option[PostSearchFilters]): Option[Query] = {
87     for {
88       filters                              <- maybeFilters
89       DisplayHomeSearchFilter(displayHome) <- filters.displayHome
90     } yield ElasticApi.termQuery(PostElasticsearchFieldNames.displayHome, displayHome)
91   }
92 
93   def buildPostCountriesSearchFilter(maybeFilters: Option[PostSearchFilters]): Option[Query] = {
94     for {
95       filters                        <- maybeFilters
96       PostCountriesFilter(countries) <- filters.countries
97     } yield ElasticApi.termsQuery(PostElasticsearchFieldNames.country, countries.map(_.value))
98   }
99 
100 }
101 
102 final case class PostIdsSearchFilter(postIds: Seq[PostId])
103 final case class DisplayHomeSearchFilter(displayHome: Boolean)
104 final case class PostCountriesFilter(countries: Seq[Country])
Line Stmt Id Pos Tree Symbol Tests Code
52 5308 1828 - 1832 Select scala.None scala.None
53 4257 1871 - 1925 Apply org.make.core.post.indexed.PostSearchFilters.apply PostSearchFilters.apply(postIds, displayHome, postCountries)
53 2207 1866 - 1926 Apply scala.Some.apply scala.Some.apply[org.make.core.post.indexed.PostSearchFilters](PostSearchFilters.apply(postIds, displayHome, postCountries))
59 5391 2055 - 2078 Select org.make.core.post.indexed.PostSearchQuery.filters org.scalatest.testsuite postSearchQuery.filters
59 3617 2030 - 2079 Apply org.make.core.post.indexed.PostSearchFilters.buildPostIdsSearchFilter org.scalatest.testsuite PostSearchFilters.this.buildPostIdsSearchFilter(postSearchQuery.filters)
60 1701 2116 - 2139 Select org.make.core.post.indexed.PostSearchQuery.filters org.scalatest.testsuite postSearchQuery.filters
60 636 2087 - 2140 Apply org.make.core.post.indexed.PostSearchFilters.buildDisplayHomeSearchFilter org.scalatest.testsuite PostSearchFilters.this.buildDisplayHomeSearchFilter(postSearchQuery.filters)
61 1798 2148 - 2203 Apply org.make.core.post.indexed.PostSearchFilters.buildPostCountriesSearchFilter org.scalatest.testsuite PostSearchFilters.this.buildPostCountriesSearchFilter(postSearchQuery.filters)
61 3882 2179 - 2202 Select org.make.core.post.indexed.PostSearchQuery.filters org.scalatest.testsuite postSearchQuery.filters
62 5242 2210 - 2210 TypeApply scala.Predef.$conforms org.scalatest.testsuite scala.Predef.$conforms[Option[com.sksamuel.elastic4s.requests.searches.queries.Query]]
62 3358 2019 - 2217 ApplyToImplicitArgs scala.collection.IterableOps.flatten org.scalatest.testsuite scala.`package`.Seq.apply[Option[com.sksamuel.elastic4s.requests.searches.queries.Query]](PostSearchFilters.this.buildPostIdsSearchFilter(postSearchQuery.filters), PostSearchFilters.this.buildDisplayHomeSearchFilter(postSearchQuery.filters), PostSearchFilters.this.buildPostCountriesSearchFilter(postSearchQuery.filters)).flatten[com.sksamuel.elastic4s.requests.searches.queries.Query](scala.Predef.$conforms[Option[com.sksamuel.elastic4s.requests.searches.queries.Query]])
66 5526 2321 - 2333 Select org.make.core.technical.Pagination.extractInt x$1.extractInt
66 3624 2290 - 2334 Apply scala.Option.fold org.scalatest.testsuite postSearchQuery.offset.fold[Int](0)(((x$1: org.make.core.technical.Pagination.Offset) => x$1.extractInt))
66 2218 2318 - 2319 Literal <nosymbol> org.scalatest.testsuite 0
69 3893 2402 - 2446 Apply scala.Option.fold org.scalatest.testsuite postSearchQuery.limit.fold[Int](10)(((x$2: org.make.core.technical.Pagination.Limit) => x$2.extractInt))
69 492 2433 - 2445 Select org.make.core.technical.Pagination.extractInt x$2.extractInt
69 1627 2429 - 2431 Literal <nosymbol> org.scalatest.testsuite 10
72 1775 2572 - 2583 Select org.make.core.Order.sortOrder org.scalatest.testsuite x$3.sortOrder
72 3282 2546 - 2609 Apply scala.Option.getOrElse org.scalatest.testsuite postSearchQuery.order.map[com.sksamuel.elastic4s.requests.searches.sort.SortOrder](((x$3: org.make.core.Order) => x$3.sortOrder)).getOrElse[com.sksamuel.elastic4s.requests.searches.sort.SortOrder](com.sksamuel.elastic4s.requests.searches.sort.SortOrder.Asc)
72 5249 2595 - 2608 Select com.sksamuel.elastic4s.requests.searches.sort.SortOrder.Asc org.scalatest.testsuite com.sksamuel.elastic4s.requests.searches.sort.SortOrder.Asc
74 3144 2615 - 2700 Apply scala.Option.map org.scalatest.testsuite postSearchQuery.sort.map[com.sksamuel.elastic4s.requests.searches.sort.FieldSort](((sort: String) => { <artifact> val x$1: String = sort; <artifact> val x$2: com.sksamuel.elastic4s.requests.searches.sort.SortOrder = order; <artifact> val x$3: Option[Any] @scala.reflect.internal.annotations.uncheckedBounds = com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$2; <artifact> val x$4: Option[String] @scala.reflect.internal.annotations.uncheckedBounds = com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$3; <artifact> val x$5: Option[com.sksamuel.elastic4s.requests.searches.queries.Query] @scala.reflect.internal.annotations.uncheckedBounds = com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$4; <artifact> val x$6: Option[String] @scala.reflect.internal.annotations.uncheckedBounds = com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$5; <artifact> val x$7: Option[com.sksamuel.elastic4s.requests.searches.sort.SortMode] @scala.reflect.internal.annotations.uncheckedBounds = com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$6; <artifact> val x$8: Option[String] @scala.reflect.internal.annotations.uncheckedBounds = com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$8; <artifact> val x$9: Option[com.sksamuel.elastic4s.requests.searches.sort.NestedSort] @scala.reflect.internal.annotations.uncheckedBounds = com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$9; com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply(x$1, x$3, x$4, x$5, x$6, x$7, x$2, x$8, x$9) }))
75 3429 2656 - 2656 Select com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$4 org.scalatest.testsuite com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$4
75 1784 2656 - 2656 Select com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$9 org.scalatest.testsuite com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$9
75 1637 2656 - 2656 Select com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$5 org.scalatest.testsuite com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$5
75 2145 2656 - 2656 Select com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$2 org.scalatest.testsuite com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$2
75 5533 2656 - 2656 Select com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$3 org.scalatest.testsuite com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$3
75 500 2656 - 2656 Select com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$6 org.scalatest.testsuite com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$6
75 5060 2656 - 2694 Apply com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply org.scalatest.testsuite com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply(x$1, x$3, x$4, x$5, x$6, x$7, x$2, x$8, x$9)
75 3822 2656 - 2656 Select com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$8 org.scalatest.testsuite com.sksamuel.elastic4s.requests.searches.sort.FieldSort.apply$default$8
81 3832 2801 - 3003 Apply scala.Option.flatMap org.scalatest.testsuite maybeFilters.flatMap[com.sksamuel.elastic4s.requests.searches.queries.Query](((filters: org.make.core.post.indexed.PostSearchFilters) => filters.postIds.withFilter(((check$ifrefutable$1: org.make.core.post.indexed.PostIdsSearchFilter) => (check$ifrefutable$1: org.make.core.post.indexed.PostIdsSearchFilter @unchecked) match { case (postIds: Seq[org.make.core.post.PostId]): org.make.core.post.indexed.PostIdsSearchFilter((postIds @ _)) => true case _ => false })).map[com.sksamuel.elastic4s.requests.searches.queries.Query](((x$5: org.make.core.post.indexed.PostIdsSearchFilter) => (x$5: org.make.core.post.indexed.PostIdsSearchFilter @unchecked) match { case (postIds: Seq[org.make.core.post.PostId]): org.make.core.post.indexed.PostIdsSearchFilter((postIds @ _)) => com.sksamuel.elastic4s.ElasticApi.termsQuery[String](PostElasticsearchFieldNames.postId, postIds.map[String](((x$4: org.make.core.post.PostId) => x$4.value))) }))))
82 4790 2864 - 3003 Apply scala.Option.WithFilter.map org.scalatest.testsuite filters.postIds.withFilter(((check$ifrefutable$1: org.make.core.post.indexed.PostIdsSearchFilter) => (check$ifrefutable$1: org.make.core.post.indexed.PostIdsSearchFilter @unchecked) match { case (postIds: Seq[org.make.core.post.PostId]): org.make.core.post.indexed.PostIdsSearchFilter((postIds @ _)) => true case _ => false })).map[com.sksamuel.elastic4s.requests.searches.queries.Query](((x$5: org.make.core.post.indexed.PostIdsSearchFilter) => (x$5: org.make.core.post.indexed.PostIdsSearchFilter @unchecked) match { case (postIds: Seq[org.make.core.post.PostId]): org.make.core.post.indexed.PostIdsSearchFilter((postIds @ _)) => com.sksamuel.elastic4s.ElasticApi.termsQuery[String](PostElasticsearchFieldNames.postId, postIds.map[String](((x$4: org.make.core.post.PostId) => x$4.value))) }))
83 2156 2946 - 2980 Select org.make.core.post.indexed.PostElasticsearchFieldNames.postId org.scalatest.testsuite PostElasticsearchFieldNames.postId
83 5645 2994 - 3001 Select org.make.core.post.PostId.value org.scalatest.testsuite x$4.value
83 1434 2924 - 3003 Apply com.sksamuel.elastic4s.api.QueryApi.termsQuery org.scalatest.testsuite com.sksamuel.elastic4s.ElasticApi.termsQuery[String](PostElasticsearchFieldNames.postId, postIds.map[String](((x$4: org.make.core.post.PostId) => x$4.value)))
83 3436 2982 - 3002 Apply scala.collection.IterableOps.map org.scalatest.testsuite postIds.map[String](((x$4: org.make.core.post.PostId) => x$4.value))
88 5330 3108 - 3325 Apply scala.Option.flatMap org.scalatest.testsuite maybeFilters.flatMap[com.sksamuel.elastic4s.requests.searches.queries.Query](((filters: org.make.core.post.indexed.PostSearchFilters) => filters.displayHome.withFilter(((check$ifrefutable$2: org.make.core.post.indexed.DisplayHomeSearchFilter) => (check$ifrefutable$2: org.make.core.post.indexed.DisplayHomeSearchFilter @unchecked) match { case (displayHome: Boolean): org.make.core.post.indexed.DisplayHomeSearchFilter((displayHome @ _)) => true case _ => false })).map[com.sksamuel.elastic4s.requests.searches.queries.Query](((x$6: org.make.core.post.indexed.DisplayHomeSearchFilter) => (x$6: org.make.core.post.indexed.DisplayHomeSearchFilter @unchecked) match { case (displayHome: Boolean): org.make.core.post.indexed.DisplayHomeSearchFilter((displayHome @ _)) => com.sksamuel.elastic4s.ElasticApi.termQuery(PostElasticsearchFieldNames.displayHome, displayHome) }))))
89 2098 3179 - 3325 Apply scala.Option.WithFilter.map org.scalatest.testsuite filters.displayHome.withFilter(((check$ifrefutable$2: org.make.core.post.indexed.DisplayHomeSearchFilter) => (check$ifrefutable$2: org.make.core.post.indexed.DisplayHomeSearchFilter @unchecked) match { case (displayHome: Boolean): org.make.core.post.indexed.DisplayHomeSearchFilter((displayHome @ _)) => true case _ => false })).map[com.sksamuel.elastic4s.requests.searches.queries.Query](((x$6: org.make.core.post.indexed.DisplayHomeSearchFilter) => (x$6: org.make.core.post.indexed.DisplayHomeSearchFilter @unchecked) match { case (displayHome: Boolean): org.make.core.post.indexed.DisplayHomeSearchFilter((displayHome @ _)) => com.sksamuel.elastic4s.ElasticApi.termQuery(PostElasticsearchFieldNames.displayHome, displayHome) }))
89 2037 3227 - 3227 Literal <nosymbol> org.scalatest.testsuite true
90 5067 3272 - 3311 Select org.make.core.post.indexed.PostElasticsearchFieldNames.displayHome org.scalatest.testsuite PostElasticsearchFieldNames.displayHome
90 3353 3251 - 3325 Apply com.sksamuel.elastic4s.api.QueryApi.termQuery org.scalatest.testsuite com.sksamuel.elastic4s.ElasticApi.termQuery(PostElasticsearchFieldNames.displayHome, displayHome)
95 3279 3432 - 3643 Apply scala.Option.flatMap org.scalatest.testsuite maybeFilters.flatMap[com.sksamuel.elastic4s.requests.searches.queries.Query](((filters: org.make.core.post.indexed.PostSearchFilters) => filters.countries.withFilter(((check$ifrefutable$3: org.make.core.post.indexed.PostCountriesFilter) => (check$ifrefutable$3: org.make.core.post.indexed.PostCountriesFilter @unchecked) match { case (countries: Seq[org.make.core.reference.Country]): org.make.core.post.indexed.PostCountriesFilter((countries @ _)) => true case _ => false })).map[com.sksamuel.elastic4s.requests.searches.queries.Query](((x$8: org.make.core.post.indexed.PostCountriesFilter) => (x$8: org.make.core.post.indexed.PostCountriesFilter @unchecked) match { case (countries: Seq[org.make.core.reference.Country]): org.make.core.post.indexed.PostCountriesFilter((countries @ _)) => com.sksamuel.elastic4s.ElasticApi.termsQuery[String](PostElasticsearchFieldNames.country, countries.map[String](((x$7: org.make.core.reference.Country) => x$7.value))) }))))
96 3692 3539 - 3539 Literal <nosymbol> org.scalatest.testsuite true
96 5075 3497 - 3643 Apply scala.Option.WithFilter.map org.scalatest.testsuite filters.countries.withFilter(((check$ifrefutable$3: org.make.core.post.indexed.PostCountriesFilter) => (check$ifrefutable$3: org.make.core.post.indexed.PostCountriesFilter @unchecked) match { case (countries: Seq[org.make.core.reference.Country]): org.make.core.post.indexed.PostCountriesFilter((countries @ _)) => true case _ => false })).map[com.sksamuel.elastic4s.requests.searches.queries.Query](((x$8: org.make.core.post.indexed.PostCountriesFilter) => (x$8: org.make.core.post.indexed.PostCountriesFilter @unchecked) match { case (countries: Seq[org.make.core.reference.Country]): org.make.core.post.indexed.PostCountriesFilter((countries @ _)) => com.sksamuel.elastic4s.ElasticApi.termsQuery[String](PostElasticsearchFieldNames.country, countries.map[String](((x$7: org.make.core.reference.Country) => x$7.value))) }))
97 1623 3583 - 3618 Select org.make.core.post.indexed.PostElasticsearchFieldNames.country org.scalatest.testsuite PostElasticsearchFieldNames.country
97 1722 3561 - 3643 Apply com.sksamuel.elastic4s.api.QueryApi.termsQuery org.scalatest.testsuite com.sksamuel.elastic4s.ElasticApi.termsQuery[String](PostElasticsearchFieldNames.country, countries.map[String](((x$7: org.make.core.reference.Country) => x$7.value)))
97 3763 3620 - 3642 Apply scala.collection.IterableOps.map org.scalatest.testsuite countries.map[String](((x$7: org.make.core.reference.Country) => x$7.value))
97 4988 3634 - 3641 Select org.make.core.reference.Country.value org.scalatest.testsuite x$7.value