-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #318 from github/jherns/add-reindex
add reindex support
- Loading branch information
Showing
5 changed files
with
111 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |