Giter VIP home page Giter VIP logo

elasticsearch-service's Introduction

Elasticsearch Service for Vapor

Swift 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:

.package(url: "https://github.com/monstar-lab/elasticsearch-service.git", from: "0.9.0")

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.

func getSearchHandler(_ req: Request) throws -> Future<[Product]> {
  let elasticClient = try req.make(ElasticsearchClient.self)
  let query = QueryContainer(
    Query(
      Fuzzy(key: "name", value: "bolster", transpositions: true)
    )
  )

  return try elasticClient
    .search(index: "product", query, decodeTo: Product.self)
    .map(to: [Product].self) { result in
      return result.hits.hits.map { $0.source }
    }
}

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!
  • Indices manipulation is not supported.
  • Data manipulation is not supported.
  • Aggregation is not supported.

elasticsearch-service's People

Contributors

cellane avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

ryangrimm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.