Skip to content

Commit

Permalink
Merge pull request #2 from surgeventures/bulk-api-routing-support
Browse files Browse the repository at this point in the history
Bulk api routing support
  • Loading branch information
Ptrboro authored May 14, 2024
2 parents 552135d + 588527e commit 65eda7c
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 101 deletions.
91 changes: 91 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Build & Publish

on:
pull_request:
push:
branches:
- "*"

# by default, permissions are read-only, read + write is required for git pushes
permissions:
contents: write

env:
MIX_ENV: test

jobs:
test:
runs-on: "ubuntu-latest"

name: Test Elixir ${{ matrix.elixir }}, OTP ${{ matrix.otp }}
strategy:
fail-fast: false
matrix:
os: ["ubuntu-latest"]
elixir: ["1.16"]
otp: ["26"]
include:
- elixir: "1.16"
otp: "26"
check_formatted: true
steps:
- uses: actions/checkout@v4
with:
submodules: true

- name: Configure sysctl limits
run: |
sudo swapoff -a
sudo sysctl -w vm.swappiness=1
sudo sysctl -w fs.file-max=262144
sudo sysctl -w vm.max_map_count=262144
- name: Runs Elasticsearch
uses: elastic/elastic-github-actions/elasticsearch@master
with:
stack-version: 7.6.0

- uses: erlef/setup-beam@v1
id: setup-elixir
with:
otp-version: ${{matrix.otp}}
elixir-version: ${{matrix.elixir}}

- name: Restore Dependency Cache
uses: actions/cache@v3
id: cache-deps
with:
path: |
deps
_build
key: |
mix-${{ runner.os }}-${{matrix.elixir}}-${{matrix.otp}}-${{ hashFiles('**/mix.lock') }}
restore-keys: |
mix-${{ runner.os }}-${{matrix.elixir}}-${{matrix.otp}}-
- name: Install Mix Dependencies
if: steps.mix-cache.outputs.cache-hit != 'true'
run: |
mix local.rebar --force
mix local.hex --force
mix deps.get
- run: mix deps.compile --warnings-as-errors
if: steps.cache-deps.outputs.cache-hit != 'true'

- name: Check for unused deps
run: mix deps.unlock --check-unused
if: matrix.check_formatted

- run: mix format --check-formatted
if: matrix.check_formatted

- run: mix test --include integration

- name: Build & Publish
run: |
mix hex.build
mix hex.publish --yes
if: matrix.check_formatted
env:
MIX_ENV: dev
HEX_API_KEY: ${{ secrets.HEX_ORGANIZATION_WRITE_KEY }}
79 changes: 0 additions & 79 deletions .github/workflows/test.yml

This file was deleted.

68 changes: 56 additions & 12 deletions lib/snap/bulk/action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ defmodule Snap.Bulk.Action.Create do
Represents a create step in a `Snap.Bulk` operation
"""
@enforce_keys [:doc]
defstruct [:_index, :_id, :require_alias, :doc]
defstruct [:_index, :_id, :require_alias, :routing, :doc]

@type t :: %__MODULE__{
_index: String.t() | nil,
_id: String.t() | nil,
require_alias: boolean() | nil,
routing: String.t() | nil,
doc: map()
}
end
Expand All @@ -18,12 +19,13 @@ defmodule Snap.Bulk.Action.Delete do
Represents a delete step in a `Snap.Bulk` operation
"""
@enforce_keys [:_id]
defstruct [:_index, :_id, :require_alias]
defstruct [:_index, :_id, :require_alias, :routing]

@type t :: %__MODULE__{
_index: String.t() | nil,
_id: String.t(),
require_alias: boolean() | nil
require_alias: boolean() | nil,
routing: String.t() | nil
}
end

Expand All @@ -32,12 +34,13 @@ defmodule Snap.Bulk.Action.Index do
Represents an index step in a `Snap.Bulk` operation
"""
@enforce_keys [:doc]
defstruct [:_index, :_id, :require_alias, :doc]
defstruct [:_index, :_id, :require_alias, :routing, :doc]

@type t :: %__MODULE__{
_index: String.t() | nil,
_id: String.t() | nil,
require_alias: boolean() | nil,
routing: String.t() | nil,
doc: map()
}
end
Expand All @@ -54,6 +57,7 @@ defmodule Snap.Bulk.Action.Update do
:doc,
:doc_as_upsert,
:require_alias,
:routing,
:script,
:upsert
]
Expand All @@ -65,6 +69,7 @@ defmodule Snap.Bulk.Action.Update do
doc: map(),
doc_as_upsert: boolean() | nil,
require_alias: boolean() | nil,
routing: String.t() | nil,
script: map() | nil,
upsert: map() | nil
}
Expand All @@ -73,8 +78,16 @@ end
defimpl Jason.Encoder, for: Snap.Bulk.Action.Create do
require Jason.Helpers

def encode(%Snap.Bulk.Action.Create{_index: index, _id: id, require_alias: require_alias}, opts) do
values = [_index: index, _id: id, require_alias: require_alias]
def encode(
%Snap.Bulk.Action.Create{
_index: index,
_id: id,
require_alias: require_alias,
routing: routing
},
opts
) do
values = [_index: index, _id: id, require_alias: require_alias, routing: routing]

values
|> Enum.reject(&is_nil(elem(&1, 1)))
Expand All @@ -86,8 +99,16 @@ end
defimpl Jason.Encoder, for: Snap.Bulk.Action.Delete do
require Jason.Helpers

def encode(%Snap.Bulk.Action.Delete{_index: index, _id: id, require_alias: require_alias}, opts) do
values = [_index: index, _id: id, require_alias: require_alias]
def encode(
%Snap.Bulk.Action.Delete{
_index: index,
_id: id,
require_alias: require_alias,
routing: routing
},
opts
) do
values = [_index: index, _id: id, require_alias: require_alias, routing: routing]

values
|> Enum.reject(&is_nil(elem(&1, 1)))
Expand All @@ -99,8 +120,23 @@ end
defimpl Jason.Encoder, for: Snap.Bulk.Action.Update do
require Jason.Helpers

def encode(%Snap.Bulk.Action.Update{_index: index, _id: id, require_alias: require_alias}, opts) do
values = [_index: index, _id: id, require_alias: require_alias]
def encode(
%Snap.Bulk.Action.Update{
_index: index,
_id: id,
doc_as_upsert: doc_as_upsert,
require_alias: require_alias,
routing: routing
},
opts
) do
values = [
_index: index,
_id: id,
doc_as_upsert: doc_as_upsert,
require_alias: require_alias,
routing: routing
]

values
|> Enum.reject(&is_nil(elem(&1, 1)))
Expand All @@ -112,8 +148,16 @@ end
defimpl Jason.Encoder, for: Snap.Bulk.Action.Index do
require Jason.Helpers

def encode(%Snap.Bulk.Action.Index{_index: index, _id: id, require_alias: require_alias}, opts) do
values = [_index: index, _id: id, require_alias: require_alias]
def encode(
%Snap.Bulk.Action.Index{
_index: index,
_id: id,
require_alias: require_alias,
routing: routing
},
opts
) do
values = [_index: index, _id: id, require_alias: require_alias, routing: routing]

values
|> Enum.reject(&is_nil(elem(&1, 1)))
Expand Down
11 changes: 6 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
defmodule Snap.MixProject do
use Mix.Project

@github_url "https://github.com/breakroom/snap"
@version "0.10.0"
@original_github_url "https://github.com/breakroom/snap"
@github_url "https://github.com/surgeventures/snap"
@version "0.10.2"

def project do
[
app: :snap,
app: :snap_fresha,
name: "Snap",
version: @version,
elixir: "~> 1.12",
Expand All @@ -18,7 +19,7 @@ defmodule Snap.MixProject do
preferred_cli_env: ["test.all": :test],

# Hex
description: "A modern Elasticsearch client",
description: "A modern Elasticsearch client, forked from [snap](#{@original_github_url})",
package: package(),

# Docs
Expand Down Expand Up @@ -74,9 +75,9 @@ defmodule Snap.MixProject do

defp package do
[
maintainers: ["Tom Taylor"],
licenses: ["MIT"],
links: %{
"Original project" => @original_github_url,
"GitHub" => @github_url
},
files: ~w(mix.exs lib LICENSE.md README.md CHANGELOG.md)
Expand Down
Empty file added test-results/.gitkeep
Empty file.
10 changes: 5 additions & 5 deletions test/bulk/actions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ defmodule Snap.Bulk.ActionsTest do
doc = %{foo: "bar"}

actions = [
%Action.Index{_index: "foo", doc: doc},
%Action.Create{_index: "foo", doc: doc, require_alias: true},
%Action.Update{_index: "foo", doc: doc, _id: 2},
%Action.Delete{_index: "foo", _id: 1}
%Action.Index{_index: "foo", doc: doc, routing: "bar"},
%Action.Create{_index: "foo", doc: doc, require_alias: true, routing: "bar"},
%Action.Update{_index: "foo", doc: doc, _id: 2, doc_as_upsert: true, routing: "bar"},
%Action.Delete{_index: "foo", _id: 1, routing: "bar"}
]

encoded = Actions.encode(actions) |> IO.chardata_to_string()

assert encoded ==
"{\"index\":{\"_index\":\"foo\"}}\n{\"foo\":\"bar\"}\n{\"create\":{\"_index\":\"foo\",\"require_alias\":true}}\n{\"foo\":\"bar\"}\n{\"update\":{\"_index\":\"foo\",\"_id\":2}}\n{\"doc\":{\"foo\":\"bar\"}}\n{\"delete\":{\"_index\":\"foo\",\"_id\":1}}\n"
"{\"index\":{\"_index\":\"foo\",\"routing\":\"bar\"}}\n{\"foo\":\"bar\"}\n{\"create\":{\"_index\":\"foo\",\"require_alias\":true,\"routing\":\"bar\"}}\n{\"foo\":\"bar\"}\n{\"update\":{\"_index\":\"foo\",\"_id\":2,\"doc_as_upsert\":true,\"routing\":\"bar\"}}\n{\"doc\":{\"foo\":\"bar\"}}\n{\"delete\":{\"_index\":\"foo\",\"_id\":1,\"routing\":\"bar\"}}\n"
end
end

0 comments on commit 65eda7c

Please sign in to comment.