Skip to content

Commit

Permalink
Merge pull request #318 from github/jherns/add-reindex
Browse files Browse the repository at this point in the history
add reindex support
  • Loading branch information
jherns authored Nov 6, 2024
2 parents b9684ab + 496306c commit 324f0a9
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 8 deletions.
8 changes: 1 addition & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ jobs:
strategy:
matrix:
ruby-version: ['3.2']
ES_VERSION: ['5.6.15', '8.13.2']
ES_VERSION: ['8.13.2']
include:
- ES_VERSION: '5.6.15'
ES_DOWNLOAD_URL: >-
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.15.tar.gz
- ES_VERSION: '8.13.2'
ES_DOWNLOAD_URL: >-
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.13.2-linux-x86_64.tar.gz
Expand All @@ -39,9 +36,6 @@ jobs:
run: |
wget ${{ matrix.ES_DOWNLOAD_URL }}
tar -xzf elasticsearch-*.tar.gz
- if: ${{ matrix.ES_VERSION == '5.6.15' }}
name: Run Elasticsearch
run: './elasticsearch-${{ matrix.ES_VERSION }}/bin/elasticsearch -d'
- if: ${{ matrix.ES_VERSION != '5.6.15' }}
name: Run Elasticsearch
run: './elasticsearch-${{ matrix.ES_VERSION }}/bin/elasticsearch -d -Expack.security.enabled=false'
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 6.2.0 (2024-11-06)
- Added support for reindex API
- Removed CI checks for ES 5.6.15

## 6.1.1 (2024-06-05)
- Unlock faraday_middleware version to allow 1.x

Expand Down
29 changes: 29 additions & 0 deletions lib/elastomer_client/client/reindex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module ElastomerClient
class Client

# Returns a Reindex instance
def reindex
Reindex.new(self)
end

class Reindex
# Create a new Reindex for initiating reindex tasks.
# More context: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html
#
# client - ElastomerClient::Client used for HTTP requests to the server
def initialize(client)
@client = client
end

attr_reader :client

def reindex(body, params = {})
response = client.post "/_reindex", params.merge(params, body:, action: "reindex", rest_api: "reindex")
response.body
end

end
end
end
2 changes: 1 addition & 1 deletion lib/elastomer_client/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module ElastomerClient
VERSION = "6.1.1"
VERSION = "6.2.0"

def self.version
VERSION
Expand Down
76 changes: 76 additions & 0 deletions test/client/reindex_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# frozen_string_literal: true

require_relative "../test_helper"

describe ElastomerClient::Client::Reindex do
before do
@source_index = $client.index("source_index")
@dest_index = $client.index("dest_index")
@non_existent_index = $client.index("non_existent_index")
if @source_index.exists?
@source_index.delete
end
if @dest_index.exists?
@dest_index.delete
end
if @non_existent_index.exists?
@non_existent_index.delete
end
@source_index.create(default_index_settings)
@dest_index.create(default_index_settings)
wait_for_index(@source_index.name, "green")
wait_for_index(@dest_index.name, "green")

# Index a document in the source index
@source_index.docs.index(document_wrapper("book", { _id: 1, title: "Book 1" }))
@source_index.refresh
end

after do
@source_index.delete if @source_index.exists?
@dest_index.delete if @dest_index.exists?
@non_existent_index.delete if @non_existent_index.exists?
end

it "reindexes documents from one index to another" do
reindex = $client.reindex
body = {
source: { index: @source_index.name },
dest: { index: @dest_index.name }
}
reindex.reindex(body)

# Refresh the destination index to make sure the document is searchable
@dest_index.refresh

# Verify that the document has been reindexed
doc = @dest_index.docs.get(id: 1, type: "book")

assert_equal "Book 1", doc["_source"]["title"]
end

it "creates a new index when the destination index does not exist" do
reindex = $client.reindex
body = {
source: { index: @source_index.name },
dest: { index: "non_existent_index" }
}
reindex.reindex(body)
new_index = $client.index("non_existent_index")

assert_predicate(new_index, :exists?)
end

it "fails when the source index does not exist" do
reindex = $client.reindex
body = {
source: { index: "non_existent_index" },
dest: { index: @dest_index.name }
}

exception = assert_raises(ElastomerClient::Client::RequestError) do
response = reindex.reindex(body)
end
assert_equal(404, exception.status)
end
end

0 comments on commit 324f0a9

Please sign in to comment.