elasticsearch
Crystal client for Elasticsearch, allowing querying of documents and deserialization into Crystal objects.
NOTE This library is usable, but a bit rough at the moment. And some things will change.
Installation
-
Add the dependency to your
shard.yml
:dependencies: elasticsearch: github: jgaskins/elasticsearch
-
Run
shards install
Usage
Require the shard into your app and set up a client:
require "elasticsearch"
# Uses ELASTICSEARCH_URL environment variable or defaults to http://localhost:9200
es = Elasticsearch::Client.new
# You can also specify an Elasticsearch URI
es = Elasticsearch::Client.new(URI.parse("https://search.example.com:9200"))
# You can also abbreviate the Elasticsearch namespace as ES
es = ES::Client.new
Create an index
You can use the Elasticsearch index API directly:
es.indices.create "products",
mappings: ES::Mapping.new(
properties: ES::Properties{
"name" => ES::Property.new(:text),
"quantity_sold" => ES::Property.new(:long),
"description" => ES::Property.new(:text),
"category" => ES::Property.new(:keyword),
"created_at" => ES::Property.new(:date),
},
)
Or you can provision an index from a document model type:
struct Product
include ES::Serializable
getter name : String
getter quantity_sold : Int64
getter description : String
@[ES::Field(type: ES::Property.new(:keyword))]
getter category : String
getter created_at : Time
end
Product.provision_index "products", client: es
Searching
results = es.search("products", simple_query_string: "computers", as: Product)
results.each do |hit|
hit # => ES::SearchResult::Hit(Product)
hit.source # => Product
end
Development
TODO Write development instructions here
Contributing
- Fork it (https://github.com/jgaskins/elasticsearch/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Jamie Gaskins - creator and maintainer