diff --git a/lib/xarango/domain/document.ex b/lib/xarango/domain/document.ex index 641673d..aae6fdf 100644 --- a/lib/xarango/domain/document.ex +++ b/lib/xarango/domain/document.ex @@ -68,6 +68,53 @@ defmodule Xarango.Domain.Document do |> to_document end + @doc """ + ## Wthin geo search in collection + + Make sure you have a geo index on collection module + + ``` + defmodule Station do + index :geo, :location + end + ``` + + First argument it's a list with lat & lon, + Second argument represent the radius within arango will search + Third argument define a new column with distance in meters (filled by arango) + + `Station.within({47.632014, -122.207210}, 100, :distance)` + """ + def within([latitude, longitude], radius \\ 0, column \\ :distance) do + %Xarango.Query{query: "FOR doc IN WITHIN(#{_collection().name}, #{latitude}, #{longitude}, #{radius}, \"#{Atom.to_string(column)}\") RETURN doc", batchSize: 3} + |> Xarango.Query.query(_database()) + |> Map.get(:result) + |> to_document + end + + @doc """ + ## Near geo search in collection + + Make sure you have a geo index on collection module + + ``` + defmodule Station do + index :geo, :location + end + ``` + First argument it's a list with lat & lon, + Second argument limit results to only first n results + Third argument define a new column with distance in meters (filled by arango) + + `Station.near({47.632014, -122.207210}, 10, :distance)` + """ + def near([latitude, longitude], limit \\ 0, column \\ :distance) do + %Xarango.Query{query: "FOR doc IN NEAR(#{_collection().name}, #{latitude}, #{longitude}, #{limit}, \"#{Atom.to_string(column)}\") RETURN doc", batchSize: 3} + |> Xarango.Query.query(_database()) + |> Map.get(:result) + |> to_document + end + def fetch(document, field) do case field do :id -> {:ok, document.doc._id} @@ -88,10 +135,6 @@ defmodule Xarango.Domain.Document do defp to_document(doc) do struct(__MODULE__, doc: Document.to_document(doc)) end - - end end - - end diff --git a/mix.lock b/mix.lock index c97d665..3775a31 100644 --- a/mix.lock +++ b/mix.lock @@ -1,13 +1,15 @@ -%{"certifi": {:hex, :certifi, "0.7.0", "861a57f3808f7eb0c2d1802afeaae0fa5de813b0df0979153cbafcd853ababaf", [], [], "hexpm"}, - "earmark": {:hex, :earmark, "1.0.2", "a0b0904d74ecc14da8bd2e6e0248e1a409a2bc91aade75fcf428125603de3853", [], [], "hexpm"}, - "ex_doc": {:hex, :ex_doc, "0.14.3", "e61cec6cf9731d7d23d254266ab06ac1decbb7651c3d1568402ec535d387b6f7", [], [{:earmark, "~> 1.0", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"}, +%{ + "certifi": {:hex, :certifi, "0.7.0", "861a57f3808f7eb0c2d1802afeaae0fa5de813b0df0979153cbafcd853ababaf", [:rebar3], [], "hexpm"}, + "earmark": {:hex, :earmark, "1.0.2", "a0b0904d74ecc14da8bd2e6e0248e1a409a2bc91aade75fcf428125603de3853", [:mix], [], "hexpm"}, + "ex_doc": {:hex, :ex_doc, "0.14.3", "e61cec6cf9731d7d23d254266ab06ac1decbb7651c3d1568402ec535d387b6f7", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"}, "faker": {:hex, :faker, "0.8.0", "91880d7aa0882ceca77849a28cecddaa337e274ac35197b65c7ad38cfa4517ab", [:mix], [], "hexpm"}, - "hackney": {:hex, :hackney, "1.6.3", "d489d7ca2d4323e307bedc4bfe684323a7bf773ecfd77938f3ee8074e488e140", [], [{:certifi, "0.7.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "1.2.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"}, - "httpoison": {:hex, :httpoison, "0.9.2", "a211a8e87403a043c41218e64df250d321f236ac57f786c6a0ccf3e9e817c819", [], [{:hackney, "~> 1.6.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"}, + "hackney": {:hex, :hackney, "1.6.3", "d489d7ca2d4323e307bedc4bfe684323a7bf773ecfd77938f3ee8074e488e140", [:mix, :rebar3], [{:certifi, "0.7.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "1.2.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"}, + "httpoison": {:hex, :httpoison, "0.9.2", "a211a8e87403a043c41218e64df250d321f236ac57f786c6a0ccf3e9e817c819", [:mix], [{:hackney, "~> 1.6.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"}, "httpotion": {:hex, :httpotion, "3.0.2", "525b9bfeb592c914a61a8ee31fdde3871e1861dfe805f8ee5f711f9f11a93483", [:mix], [{:ibrowse, "~> 4.2", [hex: :ibrowse, optional: false]}]}, "ibrowse": {:hex, :ibrowse, "4.2.2", "b32b5bafcc77b7277eff030ed32e1acc3f610c64e9f6aea19822abcadf681b4b", [:rebar3], []}, - "idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [], [], "hexpm"}, - "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [], [], "hexpm"}, - "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [], [], "hexpm"}, + "idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [:rebar3], [], "hexpm"}, + "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"}, + "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"}, "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"}, - "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [], [], "hexpm"}} + "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm"}, +} diff --git a/test/domain/document_test.exs b/test/domain/document_test.exs index f8ad6d8..78861ce 100644 --- a/test/domain/document_test.exs +++ b/test/domain/document_test.exs @@ -147,11 +147,21 @@ defmodule DomainDocumentTest do assert model[:id] == Enum.at(result, 0)[:id] end - defp _database do - %Xarango.Database{name: "test_db"} + test "create geo index and run within query" do + model = TestDbIndexModel.create(%{jabba: "test", location: [47.618336,-122.201141]}) + result = TestDbIndexModel.within([47.619240, -122.203019], 200, :distance) + assert model[:id] == Enum.at(result, 0)[:id] end + test "create geo index and run near query" do + model = TestDbIndexModel.create(%{jabba: "test", location: [47.618336,-122.201141]}) + result = TestDbIndexModel.near([47.619240, -122.203019], 10, :distance) + assert model[:id] == Enum.at(result, 0)[:id] + end + defp _database do + %Xarango.Database{name: "test_db"} + end end defmodule TestModel do @@ -166,4 +176,5 @@ defmodule TestDbIndexModel do use Xarango.Domain.Document, db: :test_db index :fulltext, :jabba + index :geo, :location end