This library allows you to connect to a Elasticsearch server from your Vapor application, perform searches and get back Codable/Content results. Currently only searching is supported, not indices nor data manipulation.
Setup
Add Elasticsearch Service to your Package.swift file:
Register the configuration object and the provider inside your configure.swift file:
let elasticURL = URL(string: "http://localhost:9200")
if let elasticURL = elasticURL {
let elasticConfig = ElasticsearchConfig(serverURL: elasticURL)
services.register(elasticConfig)
try services.register(ElasticsearchProvider())
}
Using the service
After importing ElasticsearchService and obtaining instance of ElasticsearchClient from your container, you can send your search request (either wrapped in QueryContainer, or a simple [AnyHashable: Any] in case of complicated queries) and receive search results (again either as custom Codable structure, or as an [AnyHashable: Any]).
QueryContainer and structs that conform to the Query protocol allow you to model your queries in a type-safe, Swift-y way. However, not the entirety of Elasticsearch’s query language has been converted, only the most commonly used queries. In case a query you require is missing, feel free to submit a pull request.
For more examples, please feel free to look into the test suite or comments.
Known issues
BoolQuery’s usefulness is significantly hampered by the fact that only one type of queries can be passed as an argument, for example if you pass type [MatchPhrase] as an must argument, other arguments (filter, mustNot, etc.) must also be of type [MatchPhrase]. If you know how to solve this (type erasure?), a pull request is most appreciated!
Elasticsearch Service for Vapor
This library allows you to connect to a Elasticsearch server from your Vapor application, perform searches and get back
Codable
/Content
results. Currently only searching is supported, not indices nor data manipulation.Setup
Add Elasticsearch Service to your
Package.swift
file:Register the configuration object and the provider inside your
configure.swift
file:Using the service
After importing
ElasticsearchService
and obtaining instance ofElasticsearchClient
from your container, you can send your search request (either wrapped inQueryContainer
, or a simple[AnyHashable: Any]
in case of complicated queries) and receive search results (again either as customCodable
structure, or as an[AnyHashable: Any]
).QueryContainer
andstruct
s that conform to theQuery
protocol allow you to model your queries in a type-safe, Swift-y way. However, not the entirety of Elasticsearch’s query language has been converted, only the most commonly used queries. In case a query you require is missing, feel free to submit a pull request.For more examples, please feel free to look into the test suite or comments.
Known issues
BoolQuery
’s usefulness is significantly hampered by the fact that only one type of queries can be passed as an argument, for example if you pass type[MatchPhrase]
as anmust
argument, other arguments (filter
,mustNot
, etc.) must also be of type[MatchPhrase]
. If you know how to solve this (type erasure?), a pull request is most appreciated!