Skip to content

Commit 2d75884

Browse files
Initial elasticsearch changes
1 parent 0816489 commit 2d75884

7 files changed

Lines changed: 157 additions & 2 deletions

File tree

config/dev.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ config :code_corps, :analytics, CodeCorps.Analytics.InMemoryAPI
5151
config :code_corps, :stripe, Stripe
5252
config :code_corps, :stripe_env, :dev
5353

54+
# Configure elasticsearch
55+
config :code_corps, :elasticsearch_url, "http://0.0.0.0:9200"
56+
config :code_corps, :elasticsearch_index, "skills"
57+
5458
config :sentry,
5559
environment_name: Mix.env || :dev
5660

config/prod.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ config :code_corps, :analytics, CodeCorps.Analytics.SegmentAPI
5050
config :code_corps, :stripe, Stripe
5151
config :code_corps, :stripe_env, :prod
5252

53+
# Configure elasticsearch
54+
config :code_corps, :elasticsearch_url, "http://0.0.0.0:9200"
55+
config :code_corps, :elasticsearch_index, "skills"
56+
5357
config :sentry,
5458
environment_name: Mix.env || :prod
5559

config/test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,17 @@ config :code_corps, :stripe_env, :test
3636

3737
config :code_corps, :icon_color_generator, CodeCorps.RandomIconColor.TestGenerator
3838

39+
# Configure elasticsearch
40+
config :code_corps, :elasticsearch_url, "http://0.0.0.0:9200"
41+
config :code_corps, :elasticsearch_index, "skills"
42+
3943
# Set Corsica logging to output no console warning when rejecting a request
4044
config :code_corps, :corsica_log_level, [rejected: :debug]
4145

46+
# Configure elasticsearch
47+
config :code_corps, :elasticsearch_url, "http://0.0.0.0:9200"
48+
config :code_corps, :elasticsearch_index, "skills"
49+
4250
config :sentry,
4351
environment_name: Mix.env || :test
4452

mix.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule CodeCorps.Mixfile do
44
def project do
55
[app: :code_corps,
66
version: "0.0.1",
7-
elixir: "1.3.4",
7+
elixir: "1.4.0",
88
elixirc_paths: elixirc_paths(Mix.env),
99
compilers: [:phoenix, :gettext] ++ Mix.compilers,
1010
build_embedded: Mix.env == :prod,
@@ -94,7 +94,8 @@ defmodule CodeCorps.Mixfile do
9494
{:timber, "~> 0.4"}, # Logging
9595
{:timex, "~> 3.0"},
9696
{:timex_ecto, "~> 3.0"},
97-
{:ecto_ordered, "0.2.0-beta1"}
97+
{:ecto_ordered, "0.2.0-beta1"},
98+
{:elastix, git: "https://github.com/paulsullivanjr/elastix.git"} # for elastic search
9899
]
99100
end
100101

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"earmark": {:hex, :earmark, "1.0.3", "89bdbaf2aca8bbb5c97d8b3b55c5dd0cff517ecc78d417e87f1d0982e514557b", [:mix], []},
2020
"ecto": {:hex, :ecto, "2.1.1", "fa8bdb14be9992b777036e20f183b8c4300cc012a0fae748529ff89b5423f2dd", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]},
2121
"ecto_ordered": {:hex, :ecto_ordered, "0.2.0-beta1", "cb066bc608f1c8913cea85af8293261720e6a88e3c99061e6877d7025352f045", [:mix], [{:ecto, "~> 2.0", [hex: :ecto, optional: false]}]},
22+
"elastix": {:git, "https://github.com/paulsullivanjr/elastix.git", "72441f08d59491ec1101b8bb9afe56463a5cbd75", []},
2223
"ex_aws": {:hex, :ex_aws, "1.0.0", "509368ffbd38b405faefda95eb30012fc7b791fad6574ef427cc4ad0f4f5b533", [:mix], [{:configparser_ex, "~> 0.2.1", [hex: :configparser_ex, optional: true]}, {:gen_stage, "~> 0.8.0", [hex: :gen_stage, optional: false]}, {:hackney, "1.6.1", [hex: :hackney, optional: true]}, {:jsx, "~> 2.8", [hex: :jsx, optional: true]}, {:poison, ">= 1.2.0", [hex: :poison, optional: true]}, {:sweet_xml, "~> 0.5", [hex: :sweet_xml, optional: true]}]},
2324
"ex_doc": {:hex, :ex_doc, "0.14.5", "c0433c8117e948404d93ca69411dd575ec6be39b47802e81ca8d91017a0cf83c", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]},
2425
"ex_machina": {:hex, :ex_machina, "1.0.2", "1cc49e1a09e3f7ab2ecb630c17f14c2872dc4ec145d6d05a9c3621936a63e34f", [:mix], [{:ecto, "~> 2.0", [hex: :ecto, optional: true]}]},
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
defmodule SkillControllerSearchIntegrationTest do
2+
use ExUnit.Case, async: true
3+
alias CodeCorps.ElasticSearchHelper
4+
5+
@test_url Application.get_env(:code_corps, :elasticsearch_url)
6+
@test_index Application.get_env(:code_corps, :elasticsearch_index)
7+
8+
setup do
9+
Elastix.Index.delete(Application.get_env(:code_corps, :elasticsearch_url),
10+
Application.get_env(:code_corps, :elasticsearch_index))
11+
:ok
12+
end
13+
14+
test "search partial word" do
15+
results = ElasticSearchHelper.search("ru")
16+
assert results == ["Ruby"]
17+
end
18+
19+
test "search whole word" do
20+
results = ElasticSearchHelper.search("css")
21+
assert results == ["CSS"]
22+
end
23+
24+
test "search no matches" do
25+
results = ElasticSearchHelper.search("foo")
26+
assert results == []
27+
end
28+
29+
end
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
defmodule CodeCorps.ElasticSearchHelper do
2+
alias Elastix.Search
3+
alias Elastix.Index
4+
alias Elastix.Document
5+
6+
@test_url Application.get_env(:code_corps, :elasticsearch_url)
7+
@test_index Application.get_env(:code_corps, :elasticsearch_index)
8+
9+
def delete(url, index) do
10+
Index.delete(url, index)
11+
end
12+
13+
def create_index(url, index, type) do
14+
Index.settings(url, index, settings_map)
15+
Index.settings(url, "#{index}/_mapping/#{type}", field_filter)
16+
end
17+
18+
def init do
19+
Index.settings(@test_url, @test_index, settings_map)
20+
Index.settings(@test_url, "skills/_mapping/title", field_filter)
21+
22+
data = %{
23+
title: "CSS"
24+
}
25+
Document.index @test_url, @test_index, "title", 1, data, [refresh: true]
26+
data = %{
27+
title: "Elixir"
28+
}
29+
Document.index @test_url, @test_index, "title", 2, data, [refresh: true]
30+
data = %{
31+
title: "Ruby"
32+
}
33+
Document.index @test_url, @test_index, "title", 3, data, [refresh: true]
34+
35+
data = %{
36+
title: "Rails"
37+
}
38+
Document.index @test_url, @test_index, "title", 4, data, [refresh: true]
39+
end
40+
41+
def search(search_query) do
42+
init
43+
44+
data = %{
45+
query: %{
46+
match: %{ title: search_query }
47+
}
48+
}
49+
50+
response = Search.search @test_url, @test_index, [], data
51+
52+
#response.status_code == 200 do
53+
#count = response.body["hits"]["total"]
54+
55+
hits = response.body["hits"]["hits"] || []
56+
Enum.map(hits, fn(x) -> x["_source"]["title"] end)
57+
end
58+
59+
defp settings_map do
60+
%{
61+
settings: %{
62+
number_of_shards: 5,
63+
analysis: %{
64+
filter: %{
65+
autocomplete_filter: %{
66+
type: "edge_ngram",
67+
min_gram: 2,
68+
max_gram: 20
69+
}
70+
},
71+
analyzer: %{
72+
autocomplete: %{
73+
type: "custom",
74+
tokenizer: "standard",
75+
filter: [
76+
"lowercase",
77+
"autocomplete_filter"
78+
]
79+
}
80+
}
81+
}
82+
}
83+
}
84+
end
85+
86+
defp field_filter do
87+
%{
88+
title: %{
89+
properties: %{
90+
title: %{
91+
"type": "string",
92+
"analyzer": "autocomplete"
93+
}
94+
}
95+
}
96+
}
97+
end
98+
end
99+
100+
# elastix index.ex gets:
101+
# custom method
102+
# @doc false
103+
# def settings(elastic_url, name, data) do
104+
# elastic_url <> make_path(name)
105+
# |> HTTP.put(Poison.encode!(data))
106+
# |> process_response
107+
# end
108+

0 commit comments

Comments
 (0)