Last active
November 26, 2019 16:44
-
-
Save duanebester/162dd215e3a47d5450a0efe71a0cc909 to your computer and use it in GitHub Desktop.
Elastic example class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Elastic(props: ElasticProperties) extends ElasticHelpers { | |
implicit val ec: ExecutionContext = ExecutionContext.global | |
override val elasticProps: ElasticProperties = props | |
override val elasticClient = ElasticClient(JavaClient(elasticProps)) | |
def searchUsers(filter: Filter) = | |
elasticClient.execute { | |
search(USER_INDEX).bool(buildQuery(filter)) | |
}.map(resp => UsersResponse(resp.result.to[User], resp.result.totalHits)) | |
def searchCoffeeShops(filter: Filter) = | |
elasticClient.execute { | |
search(COFFEE_SHOPS_INDEX).bool(buildQuery(filter)) | |
}.map(resp => CoffeeShopsResponse(resp.result.to[CoffeeShop], resp.result.totalHits)) | |
def buildQuery(queryFilter: Filter): BoolQuery = { | |
var qMusts = mutable.ListBuffer[Query]() | |
var qFilters = mutable.ListBuffer[Query]() | |
qMusts += matchAllQuery() | |
if (queryFilter.bbox.isDefined) { | |
val geom = queryFilter.bbox.get | |
qFilters += geoQuery(geom) | |
} | |
return must(qMusts.toSeq).filter(qFilters) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment