Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elasticsearch implementation #658

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open

Conversation

paulsullivanjr
Copy link
Contributor

@paulsullivanjr paulsullivanjr commented Jan 22, 2017

What's in this PR?

This is currently work in progress. But wanted to get an initial PR out.

References

Fixes #60

Copy link
Contributor

@joshsmith joshsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple comments/questions here. I'm completely ignorant to how elastix works so these are get-my-feet-wet questions.

config/dev.exs Outdated
@@ -51,6 +51,10 @@ config :code_corps, :analytics, CodeCorps.Analytics.InMemoryAPI
config :code_corps, :stripe, Stripe
config :code_corps, :stripe_env, :dev

# Configure elasticsearch
config :code_corps, :elasticsearch_url, "http://0.0.0.0:9200"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe should set these URLs via ENV vars?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I had put these in the config files initially for convenience.

config/dev.exs Outdated
@@ -51,6 +51,10 @@ config :code_corps, :analytics, CodeCorps.Analytics.InMemoryAPI
config :code_corps, :stripe, Stripe
config :code_corps, :stripe_env, :dev

# Configure elasticsearch
config :code_corps, :elasticsearch_url, "http://0.0.0.0:9200"
config :code_corps, :elasticsearch_index, "skills"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we should be setting these indexes in the config?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't. I had put these in the config files initially for convenience. But it needs to go away.

@paulsullivanjr
Copy link
Contributor Author

Elastix provides basic functionality for working with elasticsearch. It's a pretty small library (4 modules) and not very long or complex.

mix.exs Outdated
@@ -4,7 +4,7 @@ defmodule CodeCorps.Mixfile do
def project do
[app: :code_corps,
version: "0.0.1",
elixir: "1.3.4",
elixir: "1.4.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the circleci is failing because of this, would undo for the PR as the upgrade to 1.4 should probably be in its own PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, overlooked that. I'll do that this evening.

def to_map(foo, bar), do: %{ String.to_atom(foo) => bar}

defp settings_map do
%{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation

@npendery
Copy link
Contributor

This looks awesome! Great job. One thing I would suggest is maybe making the field_filter extensible for other fields. Maybe have a map as an input with %{field: "type"} and field_filter is generated from this map so if we want to add multiple fields at once we can. Also, not an elasticsearch expert but I believe just by adding more mapping types we could put other models in the same index so maybe have another layer to the input map with type? Doesnt seem like we need it right now but just a thought. This is definitely a good step forward towards a higher level elixir elasticsearch client!

@paulsullivanjr
Copy link
Contributor Author

Thanks for the kind words. I like the ideas you proposed and think they should work. I'll start working on those ideas soon.

@joshsmith
Copy link
Contributor

Hey @paulsullivanjr I just wanted to check in on this and see if there's anything we should be doing here?

@paulsullivanjr
Copy link
Contributor Author

@joshsmith I want to discuss what other steps and pieces we'll need to integrate this. I'll catch up with you on slack tonight or tomorrow. I want to get this done so I can get started other other issues.

@joshsmith
Copy link
Contributor

Awesome, I will be around to discuss.

@joshsmith
Copy link
Contributor

Hey @paulsullivanjr just adding that suggestions comment here for context to this PR:

  1. Adding a skills/search endpoint for searching, handled directly in SkillController
  2. Endpoint point needs to return a collection of Skill structures, need to research how to create from elasticsearch results. The Elasticsearch indexes will need to contain the Skill records from the database including id's.
  3. Keep an ELASTICSEARCH_URL in .env

@@ -47,7 +47,7 @@ defmodule CodeCorps.ElasticSearchHelper do
end

def process_response(%HTTPoison.Response{status_code: 200} = response, type) do
response.body["hits"]["hits"] |> Enum.map(fn(x) -> x["_source"][type] end)
response.body["hits"]["hits"] |> Enum.map(fn(x) -> x["_source"] end)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is x here? I think this variable could benefit from a longer name.

@@ -18,4 +22,9 @@ defmodule CodeCorps.SkillController do
|> title_filter(params)
|> limit_filter(params)
end

def search(_conn, params) do
CodeCorps.ElasticSearchHelper.search(@elasticsearch_url, @elasticsearch_index, @elasticsearch_type, query)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like query is undefined here.

@@ -23,7 +23,7 @@ defmodule CodeCorps.SkillController do
|> limit_filter(params)
end

def search(_conn, params) do
def search(_conn, %{"query" => query} = params) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the = params portion here if you're not using the params as a whole object.

@joshsmith
Copy link
Contributor

joshsmith commented Sep 28, 2017

We could easily get Skills back by simply:

  • using results = [%{"id" => 1}, %{"id" => 2}] |> Enum.map(&(&1["id"]))
  • with a where clause: skills = Skill |> where([s], s.id in [1, 2]) |> Repo.all where [1, 2] are the results above

paulsullivanjr and others added 2 commits November 25, 2017 20:24
- Removed duplicate entry
- Moved some code and a couple small refactors of the helper module
- Updated to not pass the document index when creating
- cleaning up some unneeded changes
- Updated search function
- Changed elixir version back to 1.3.4
- Updates per comments, added add_documents() function to helper
- added some convenience functions
- Removing unnecessary code, eliminated 1.4 warning messages so it's done when we upgrade
- Added tests for fuzzy search, updates some map formats
- Added match all function
- Added search endpoint for elasticsearch, updated to return maps
- Added comment about using -- with lists
@begedin begedin force-pushed the develop branch 8 times, most recently from e075407 to 6d6cc63 Compare February 12, 2018 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants