Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jgaskins/elasticsearch
Elasticsearch client for Crystal
https://github.com/jgaskins/elasticsearch
Last synced: 22 days ago
JSON representation
Elasticsearch client for Crystal
- Host: GitHub
- URL: https://github.com/jgaskins/elasticsearch
- Owner: jgaskins
- License: mit
- Created: 2022-11-28T02:38:50.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-03T00:18:05.000Z (over 1 year ago)
- Last Synced: 2024-12-09T10:55:41.650Z (25 days ago)
- Language: Crystal
- Homepage: https://jgaskins.dev/elasticsearch/
- Size: 249 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
elasticsearch:
github: jgaskins/elasticsearch
```2. Run `shards install`
## Usage
Require the shard into your app and set up a client:
```crystal
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:
```crystal
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:
```crystal
struct Product
include ES::Serializablegetter name : String
getter quantity_sold : Int64
getter description : String
@[ES::Field(type: ES::Property.new(:keyword))]
getter category : String
getter created_at : Time
endProduct.provision_index "products", client: es
```### Searching
```crystal
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
1. Fork it ()
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request## Contributors
- [Jamie Gaskins](https://github.com/jgaskins) - creator and maintainer