Skip to content

Commit

Permalink
Merge pull request #132 from Zatvobor/v0.x.0
Browse files Browse the repository at this point in the history
v0.6.0
  • Loading branch information
Zatvobor committed Mar 8, 2015
2 parents 0f7d2be + bc67213 commit 3cd5635
Show file tree
Hide file tree
Showing 28 changed files with 157 additions and 130 deletions.
15 changes: 7 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
language: erlang
sudo: false
language: elixir
elixir:
- 1.0.3
notifications:
recipients:
- [email protected]
otp_release:
- 17.0
before_install:
- git clone https://github.com/elixir-lang/elixir
- cd elixir && git checkout v0.13.1 && make && cd ..
before_script: "export PATH=`pwd`/elixir/bin:$PATH"
script: "MIX_ENV=test mix do deps.get, compile, travis"
services:
- elasticsearch
script: "MIX_ENV=test mix test"
51 changes: 27 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,21 @@ An Elixir based DSL for operating the ElasticSearch cluster related stuff, such

releases:

- `v0.5.0` tested on elastisearch `1.1.1`, elixir `0.13.1`
- `v0.4` tested on elastisearch `0.90.3`, elixir `0.12.5`
- `v0.6.0` tested on elastcisearch `1.4.4`, elixir `1.0.3`
- `v0.5.0` tested on elastcisearch `1.1.1`, `1.4.2`, elixir `0.13.1`
- `v0.4` tested on elasticsearch `0.90.3`, elixir `0.12.5`

Walk-through a code
-------------------

Let's create an `articles` index:

```elixir
import Tirexs.Bulk

settings = Tirexs.ElasticSearch.Config.new()

Tirexs.Bulk.store [index: "articles", refresh: true], settings do
create id: 1, title: "One", tags: ["elixir"], type: "article"
create id: 2, title: "Two", tags: ["elixir", "ruby"], type: "article"
create id: 3, title: "Three", tags: ["java"], type: "article"
create id: 4, title: "Four", tags: ["erlang"], type: "article"
end
```

Ability to customize the mapping for specific document type:
Let's define the mapping for specific document type:

```elixir
import Tirexs.Mapping

index = [index: "articles", type: "article"]
mappings do
indexes "id", type: "string", index: "not_analyzed", include_in_all: false
indexes "id", type: "long", index: "not_analyzed", include_in_all: false
indexes "title", type: "string", boost: 2.0, analyzer: "snowball"
indexes "tags", type: "string", analyzer: "keyword"
indexes "content", type: "string", analyzer: "snowball"
Expand All @@ -48,10 +34,27 @@ end
{ :ok, status, body } = Tirexs.Mapping.create_resource(index)
```

Then let's populate an `articles` index:

```elixir
import Tirexs.Bulk
require Tirexs.ElasticSearch

settings = Tirexs.ElasticSearch.config()

Tirexs.Bulk.store [index: "articles", refresh: true], settings do
create id: 1, title: "One", tags: ["elixir"], type: "article"
create id: 2, title: "Two", tags: ["elixir", "ruby"], type: "article"
create id: 3, title: "Three", tags: ["java"], type: "article"
create id: 4, title: "Four", tags: ["erlang"], type: "article"
end
```

Now, let's go further. We will be searching for articles whose title begins with letter “T”, sorted by title in descending order, filtering them for ones tagged “elixir”, and also retrieving some facets:

```elixir
import Tirexs.Search
require Tirexs.Query

articles = search [index: "articles"] do
query do
Expand Down Expand Up @@ -81,16 +84,16 @@ end

result = Tirexs.Query.create_resource(articles)

Enum.each result.hits, fn(item) ->
Enum.each Tirexs.Query.result(result, :hits), fn(item) ->
IO.puts inspect(item)
#=> [{"_index","articles"},{"_type","article"},{"_id","2"},{"_score",1.0},{"_source",[{"id",2}, {"title","Two"},{"tags",["elixir","r uby"]},{"type","article"}]}]
end
```

Let's display the global facets:
```elixir
Enum.each result.facets["global_tags"]["terms"], fn(f) ->
IO.puts "#{f["term"]} #{f["count"]}"
Enum.each Tirexs.Query.result(result, :facets).global_tags.terms, fn(f) ->
IO.puts "#{f.term} #{f.count}"
end

#=> elixir 2
Expand All @@ -100,8 +103,8 @@ end
```
Now, let's display the facets based on current query (notice that count for articles tagged with 'java' is included, even though it's not returned by our query; count for articles tagged 'erlang' is excluded, since they don't match the current query):
```elixir
Enum.each result.facets["current_tags"]["terms"], fn(f) ->
IO.puts "#{f["term"]} #{f["count"]}"
Enum.each Tirexs.Query.result(result, :facets).current_tags.terms, fn(f) ->
IO.puts "#{f.term} #{f.count}"
end

#=> ruby 1
Expand Down
7 changes: 4 additions & 3 deletions examples/mapping.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
# Tirexs.Loader.load Path.expand("examples/mapping.exs")
#
import Tirexs.Mapping
require Tirexs.ElasticSearch

Tirexs.DSL.define [type: "dsl", index: "test_dsl_index"], fn(index, _) ->

elastic_settings = Tirexs.ElasticSearch.Config.new([user: "new_user"])
elastic_settings = Tirexs.ElasticSearch.config(user: "new_user")

mappings do
indexes "mn_opts_", [type: "nested"] do
Expand All @@ -31,8 +32,8 @@ Tirexs.DSL.define [type: "dsl", index: "test_dsl_index"], fn(index, _) ->
# Below a couple of code which could be useful for debugging

# url = Tirexs.ElasticSearch.make_url(index[:index], elastic_settings)
# json = JSEX.prettify!(Tirexs.Mapping.to_resource_json(index))
# json = JSX.prettify!(Tirexs.Mapping.to_resource_json(index))
# IO.puts "\n # => curl -X PUT -d '#{json}' #{url}"

{ index, elastic_settings }
end
end
4 changes: 2 additions & 2 deletions examples/river/couchdb_river.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ Tirexs.DSL.define [name: "tets_river_dsl"], fn(river, elastic_settings) ->

# Below a couple of code which could be useful for debugging
# url = Tirexs.ElasticSearch.make_url("_river/" <> river[:name] <>"/_meta", elastic_settings)
# json = JSEX.prettify!(Tirexs.River.to_resource_json(river))
# json = JSX.prettify!(Tirexs.River.to_resource_json(river))
# IO.puts "\n # => curl -X PUT -d '#{json}' #{url}"

{ river, elastic_settings }
end
end
4 changes: 2 additions & 2 deletions examples/search.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Tirexs.DSL.define fn(elastic_settings) ->

# Below a couple of code which could be useful for debugging
# url = Tirexs.ElasticSearch.make_url(search[:index] <> "/_search", elastic_settings)
# json = JSEX.prettify!(Tirexs.Query.to_resource_json(search))
# json = JSX.prettify!(Tirexs.Query.to_resource_json(search))
# IO.puts "\n # => curl -X POST -d '#{json}' #{url}"

{ search, elastic_settings }
end
end
9 changes: 0 additions & 9 deletions lib/mix/tasks/travis.ex

This file was deleted.

4 changes: 2 additions & 2 deletions lib/tirexs/bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ defmodule Tirexs.Bulk do
header = Dict.put([], action, meta)

output = []
output = output ++ [JSEX.encode!(header)]
output = output ++ [JSX.encode!(header)]
unless action == :delete do
output = output ++ [convert_document_to_json(document)]
end
Expand Down Expand Up @@ -80,7 +80,7 @@ defmodule Tirexs.Bulk do
end

def convert_document_to_json(document) do
JSEX.encode!(document)
JSX.encode!(document)
end

def get_type_from_document(document) do
Expand Down
8 changes: 5 additions & 3 deletions lib/tirexs/dsl.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require Tirexs.ElasticSearch

defmodule Tirexs.DSL do
@moduledoc """
This module represents a main entry point for defining DSL scenarios.
Expand All @@ -6,15 +8,15 @@ defmodule Tirexs.DSL do

@doc false
def define(type, resource) do
elastic_settings = Tirexs.ElasticSearch.Config.new()
elastic_settings = Tirexs.ElasticSearch.config()
case resource.(type, elastic_settings) do
{ type, elastic_settings } -> create_resource(type, elastic_settings)
end
end

@doc false
def define(resource) do
elastic_settings = Tirexs.ElasticSearch.Config.new()
elastic_settings = Tirexs.ElasticSearch.config()
case resource.(elastic_settings) do
{ type, elastic_settings } -> create_resource(type, elastic_settings)
end
Expand All @@ -28,4 +30,4 @@ defmodule Tirexs.DSL do
type[:search] -> Tirexs.Query.create_resource(type, opts)
end
end
end
end
6 changes: 4 additions & 2 deletions lib/tirexs/dsl/logic.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Tirexs.DSL.Logic do
require Record

@moduledoc """
Defines a main module which provides a common contract for DSL handlers and common utilities.
"""
Expand Down Expand Up @@ -31,12 +33,12 @@ defmodule Tirexs.DSL.Logic do

@doc false
def to_atom(value) when is_atom(value), do: value
def to_atom(value) when is_binary(value), do: binary_to_atom(value)
def to_atom(value) when is_binary(value), do: String.to_atom(value)
def to_atom(value), do: value

@doc false
def is_dict?(dict) do
is_record(dict, Dict) || false
Record.is_record(dict, Dict) || false
end

@doc false
Expand Down
12 changes: 7 additions & 5 deletions lib/tirexs/elastic_search.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require Record

defmodule Tirexs.ElasticSearch do

@doc """
This module provides a simple convenience for connection options such as `port`, `uri`, `user`, `pass`
and functions for doing a `HTTP` request to `ElasticSearch` engine directly.
"""
defrecord Config, [port: 9200, uri: "127.0.0.1", user: nil, pass: nil]
Record.defrecord :config, [port: 9200, uri: "127.0.0.1", user: nil, pass: nil]

@doc false
def get(query_url, config) do
Expand Down Expand Up @@ -54,7 +56,7 @@ defmodule Tirexs.ElasticSearch do
@doc false
def do_request(url, method, body \\ []) do
:inets.start()
{ url, content_type, options } = { String.to_char_list!(url), 'application/json', [{:body_format, :binary}] }
{ url, content_type, options } = { String.to_char_list(url), 'application/json', [{:body_format, :binary}] }
case method do
:get -> response(:httpc.request(method, {url, []}, [], []))
:head -> response(:httpc.request(method, {url, []}, [], []))
Expand All @@ -80,13 +82,13 @@ defmodule Tirexs.ElasticSearch do
end
end

def get_body_json(body), do: JSEX.decode!(to_string(body), [{:labels, :atom}])
def get_body_json(body), do: JSX.decode!(to_string(body), [{:labels, :atom}])

def make_url(query_url, config) do
if config.port == nil || config.port == 80 do
if config(config, :port) == nil || config(config, :port) == 80 do
"http://#{config.uri}/#{query_url}"
else
"http://#{config.uri}:#{config.port}/#{query_url}"
"http://#{config(config, :uri)}:#{config(config, :port)}/#{query_url}"
end
end

Expand Down
9 changes: 6 additions & 3 deletions lib/tirexs/elastic_search/settings.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
require Tirexs.ElasticSearch

defmodule Tirexs.ElasticSearch.Settings do
@moduledoc false

import Tirexs.ElasticSearch
require Tirexs.ElasticSearch

@doc false
def create_resource(definition) do
create_resource(definition, Tirexs.ElasticSearch.Config.new)
create_resource(definition, Tirexs.ElasticSearch.config())
end

@doc false
Expand All @@ -16,6 +19,6 @@ defmodule Tirexs.ElasticSearch.Settings do

@doc false
def to_resource_json(definition) do
JSEX.encode! [settings: definition[:settings]]
JSX.encode! [settings: definition[:settings]]
end
end
end
8 changes: 4 additions & 4 deletions lib/tirexs/logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ defmodule Tirexs.Logger do
@moduledoc false

def to_curl(data) do
case JSEX.is_json?(data) do
case JSX.is_json?(data) do
true -> log(data)
false -> log(JSEX.encode!(data))
false -> log(JSX.encode!(data))
end
end

defp log(json) do
:error_logger.info_msg(JSEX.prettify!(json))
:error_logger.info_msg(JSX.prettify!(json))
end

end
end
10 changes: 5 additions & 5 deletions lib/tirexs/manage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Tirexs.Manage do
end

def delete_by_query(options, settings) do
_body = JSEX.encode!(options[:filter] || options[:query] || [])
_body = JSX.encode!(options[:filter] || options[:query] || [])
#To do add DELETE with body
Tirexs.ElasticSearch.delete(make_url("_query", options), settings)
end
Expand All @@ -24,7 +24,7 @@ defmodule Tirexs.Manage do
end

def update(options, update_params, settings) do
Tirexs.ElasticSearch.post(make_url("_update", options), JSEX.encode!(update_params), settings)
Tirexs.ElasticSearch.post(make_url("_update", options), JSX.encode!(update_params), settings)
end

def create(:warmer, options, settings) do
Expand All @@ -33,7 +33,7 @@ defmodule Tirexs.Manage do
Tirexs.ElasticSearch.put("bear_test/_warmer/warmer_1", settings)
Enum.each Dict.keys(warmers), fn(key) ->
url = make_url(to_string(key), options)
body = JSEX.encode!(warmers[key][:source])
body = JSX.encode!(warmers[key][:source])
Tirexs.ElasticSearch.put(url, body, settings)
end
end
Expand Down Expand Up @@ -80,9 +80,9 @@ defmodule Tirexs.Manage do
options[:query] -> [query: options[:query]]
true -> []
end
case JSEX.encode!(body) do
case JSX.encode!(body) do
"[]" -> Tirexs.ElasticSearch.get(make_url(url_suffix, options), settings)
body -> Tirexs.ElasticSearch.post(make_url(url_suffix, options), body, settings)
end
end
end
end
Loading

0 comments on commit 3cd5635

Please sign in to comment.