Skip to content

Commit

Permalink
Merge pull request #9 from imdrasil/fix_ci_elastic_setup
Browse files Browse the repository at this point in the history
Add elastic as a service
  • Loading branch information
imdrasil authored Oct 23, 2020
2 parents 7d99d27 + 20da6d5 commit c7ad8c1
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 46 deletions.
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ addons:
packages:
- openjdk-8-jdk
- openjdk-8-jre
services:
- elasticsearch
before_script:
- curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.1.1.deb
- sudo dpkg -i --force-confnew elasticsearch-5.1.1.deb
- sudo service elasticsearch start
- sleep 10
- crystal ./script/runner.cr -- es:index:create_all
- crystal ./script/runner.cr es:index:create_all
script:
- ./bin/ameba
- crystal spec

notifications:
on_failure: never
Expand Down
9 changes: 9 additions & 0 deletions sam.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "sam"

# Here you can define your tasks
# desc "with description to be used by help command"
# task "test" do
# puts "ping"
# end

Sam.help
7 changes: 5 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
name: hermes
version: 0.2.2
version: 0.3.0

authors:
- Roman Kalnytskyi <[email protected]>

crystal: 0.31.1
crystal: 0.35.1

license: MIT
development_dependencies:
sam:
github: imdrasil/sam.cr
version: "~> 0.3.0"
ameba:
github: crystal-ameba/ameba
version: "= 0.12.1"
22 changes: 11 additions & 11 deletions spec/hermes/repository_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe Hermes::Repository do
it "update given doc by given description" do
post = create_post
Hermes.refresh
r = PostRepository.update(post._id, {
PostRepository.update(post._id, {
script: {
inline: "ctx._source.likes += params.count",
lang: "painless",
Expand Down Expand Up @@ -85,8 +85,8 @@ describe Hermes::Repository do

describe "::search" do
it "correctly searches by given query" do
p1 = PostRepository.save(build_post(tag: "search", user: "kim"), true)
p2 = PostRepository.save(build_post(user: "kim", tag: "elastic"), true)
PostRepository.save(build_post(tag: "search", user: "kim"), true)
PostRepository.save(build_post(user: "kim", tag: "elastic"), true)
PostRepository.save(build_post(user: "eddy", tag: "elastic"), true)
r = PostRepository.search({
query: {
Expand All @@ -109,8 +109,8 @@ describe Hermes::Repository do

describe "::aggregate" do
it "correctly aggregates" do
p1 = PostRepository.save(build_post(tag: "search", user: "kim", likes: 2), true)
p2 = PostRepository.save(build_post(user: "kim", tag: "elastic", likes: 3), true)
PostRepository.save(build_post(tag: "search", user: "kim", likes: 2), true)
PostRepository.save(build_post(user: "kim", tag: "elastic", likes: 3), true)
r = PostRepository.aggregate({total_likes: {sum: {field: "likes"}}})

r.aggs["total_likes"]["value"].should eq(5)
Expand Down Expand Up @@ -178,8 +178,8 @@ describe Hermes::Repository do

describe "::count" do
it "counts objects by given query" do
post1 = create_post(user: "kim")
post2 = create_post(user: "eddy")
create_post(user: "kim")
create_post(user: "eddy")
TestIndex.refresh
PostRepository.count({query: {term: {user: "kim"}}}).should eq(1)
end
Expand All @@ -205,7 +205,7 @@ describe Hermes::Repository do
describe "::multi_get" do
it "returns array of retrieved objects" do
post1 = create_post(user: "kim")
post2 = create_post(user: "yao")
create_post(user: "yao")
post3 = create_post(user: "eddy")
TestIndex.refresh
res = PostRepository.multi_get([post1._id, post3._id])
Expand All @@ -215,9 +215,9 @@ describe Hermes::Repository do

describe "::all" do
it "retrieves all" do
post1 = create_post(user: "kim")
post2 = create_post(user: "yao")
post3 = create_post(user: "eddy")
create_post(user: "kim")
create_post(user: "yao")
create_post(user: "eddy")
TestIndex.refresh
PostRepository.all.entries.size.should eq(3)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/hermes_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "./spec_helper"
describe Hermes do
describe "::bulk" do
it "loads all to es" do
r = Hermes.bulk([
Hermes.bulk([
{index: {_index: "test_index", _type: "posts"}},
{
title: "test t3",
Expand Down
6 changes: 3 additions & 3 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ require "../src/hermes"
require "./config"
require "./factories"

Hermes::Config.configure do |c|
#c.host = "127.0.0.1"
end
# Hermes::Config.configure do |c|
# c.host = "127.0.0.1"
# end

Spec.before_each do
query = {query: {match_all: {} of String => String}}
Expand Down
2 changes: 2 additions & 0 deletions src/hermes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ require "./hermes/client"
require "./hermes/*"

module Hermes
VERSION = "0.3.0"

def self.status
client.get("/")
end
Expand Down
4 changes: 2 additions & 2 deletions src/hermes/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Hermes
@headers = HTTP::Headers.new
@headers.add("Accept", "application/json")
@headers.add("Content-Type", "application/json")

@http = HTTP::Client.new(URI.new(Config.schema, Config.host, Config.port))
end

Expand All @@ -16,7 +16,7 @@ module Hermes
Response.new(res)
end
{% end %}

{% for method in [:get, :post, :put, :delete] %}
def {{method.id}}!(url, headers = nil, body = nil)
res = @http.{{method.id}}(url, @headers, body)
Expand Down
2 changes: 1 addition & 1 deletion src/hermes/repository.cr
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ module Hermes

def self.save(obj : Persistent, refresh = false)
body = obj.to_json
unless obj.es_new_record?
if !obj.es_new_record?
Hermes.client.put(path(obj._id, refresh), nil, body)
else
res = Hermes.client.post(path(refresh), nil, body)
Expand Down
4 changes: 2 additions & 2 deletions src/hermes/search_response.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Hermes
@_source._id = @_id
@_source._type = @_type
@_source._index = @_index

@_source
end

Expand Down Expand Up @@ -65,7 +65,7 @@ module Hermes

@[JSON::Field(key: "aggregations")]
property aggregations : Hash(String, JSON::Any)

def aggs
@aggregations
end
Expand Down
2 changes: 1 addition & 1 deletion src/hermes/types/circle.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Hermes

@[JSON::Field(key: "coordinates")]
property coordinates : Array(Float64)

@[JSON::Field(key: "radius")]
property radius : String

Expand Down
2 changes: 1 addition & 1 deletion src/hermes/types/point.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Hermes

@[JSON::Field(key: "coordinates")]
property coordinates : Array(Float64)

def initialize(@coordinates)
@type = "point"
end
Expand Down
2 changes: 1 addition & 1 deletion src/hermes/types/range.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Hermes

@[JSON::Field(key: "lte")]
property lte : T

@[JSON::Field(key: "gte")]
property gte : T

Expand Down
3 changes: 0 additions & 3 deletions src/hermes/version.cr

This file was deleted.

28 changes: 14 additions & 14 deletions src/sam.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,30 @@ Sam.namespace "es" do
end
end

task "update" do |t, args|
k = Hermes::Index::INDEXES.find { |k| k.index_name == args[0].as(String) }
if k
k.not_nil!.update
task "update" do |_, args|
klass = Hermes::Index::INDEXES.find { |k| k.index_name == args[0].as(String) }
if klass
klass.not_nil!.update
puts "Index is updated"
else
puts "No such index"
end
end

task "create" do |t, args|
k = Hermes::Index::INDEXES.find { |k| k.index_name == args[0].as(String) }
if k
k.not_nil!.create
task "create" do |_, args|
klass = Hermes::Index::INDEXES.find { |k| k.index_name == args[0].as(String) }
if klass
klass.not_nil!.create
puts "Index is created"
else
puts "No such index"
end
end

task "destroy" do |t, args|
k = Hermes::Index::INDEXES.find { |k| k.index_name == args[0].as(String) }
if k
k.not_nil!.destroy
task "destroy" do |_, args|
klass = Hermes::Index::INDEXES.find { |k| k.index_name == args[0].as(String) }
if klass
klass.not_nil!.destroy
puts "Index is updated"
else
puts "No such index"
Expand All @@ -59,11 +59,11 @@ Sam.namespace "es" do
end

namespace "alias" do
task "add" do |t, args|
task "add" do |_, args|
Hermes::Cluster.add_alias(args[0].as(String), args[1].as(String))
end

task "remove" do |t, args|
task "remove" do |_, args|
Hermes::Cluster.remove_alias(args[0].as(String), args[1].as(String))
end
end
Expand Down

0 comments on commit c7ad8c1

Please sign in to comment.