diff --git a/CHANGELOG b/CHANGELOG index 9bf1e29..3d536ef 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +v0.10.0 + - update remap to removing fields from the mapping that are not explicitly + defined. v0.9.1 - fix search enumerator, missing first result set v0.8.3 diff --git a/lib/elasticity/strategies/alias_index.rb b/lib/elasticity/strategies/alias_index.rb index c62b033..3cd224d 100644 --- a/lib/elasticity/strategies/alias_index.rb +++ b/lib/elasticity/strategies/alias_index.rb @@ -66,10 +66,17 @@ def remap(index_def) docs = @client.mget(body: { docs: id_docs }, refresh: true)["docs"] break if docs.empty? + # Modify document hashes to match the mapping definition so that legacy fields aren't added + defined_mapping_fields = index_def[:mappings][docs.first["_type"]]["properties"].keys + # Move only documents that still exists on the old index, into the new index. ops = [] docs.each do |doc| - ops << { index: { _index: new_index, _type: doc["_type"], _id: doc["_id"], data: doc["_source"] } } if doc["found"] + if doc["found"] + legacy_fields = doc["_source"].keys - defined_mapping_fields + legacy_fields.each { |field| doc["_source"].delete(field) } + ops << { index: { _index: new_index, _type: doc["_type"], _id: doc["_id"], data: doc["_source"] } } + end end @client.bulk(body: ops) diff --git a/lib/elasticity/version.rb b/lib/elasticity/version.rb index 7ee2dfb..e1dc4b6 100644 --- a/lib/elasticity/version.rb +++ b/lib/elasticity/version.rb @@ -1,3 +1,3 @@ module Elasticity - VERSION = "0.9.1" + VERSION = "0.10.0" end diff --git a/spec/functional/persistence_spec.rb b/spec/functional/persistence_spec.rb index 104cb28..62dedf8 100644 --- a/spec/functional/persistence_spec.rb +++ b/spec/functional/persistence_spec.rb @@ -16,7 +16,7 @@ def self.name c.strategy = Elasticity::Strategies::SingleIndex c.mapping = { - properties: { + "properties" => { name: { type: "string", index: "not_analyzed" }, birthdate: { type: "date" }, }, @@ -85,7 +85,7 @@ class Cat < Animal c.index_base_name = "cats_and_dogs" c.strategy = Elasticity::Strategies::SingleIndex c.document_type = "cat" - c.mapping = { properties: { + c.mapping = { "properties" => { name: { type: "string", index: "not_analyzed" }, age: { type: "integer" } } } @@ -103,7 +103,7 @@ class Dog < Animal c.index_base_name = "cats_and_dogs" c.strategy = Elasticity::Strategies::SingleIndex c.document_type = "dog" - c.mapping = { properties: { + c.mapping = { "properties" => { name: { type: "string", index: "not_analyzed" }, age: { type: "integer" }, hungry: { type: "boolean" } @@ -165,7 +165,7 @@ def self.name c.strategy = Elasticity::Strategies::AliasIndex c.mapping = { - properties: { + "properties" => { id: { type: "integer" }, name: { type: "string", index: "not_analyzed" }, birthdate: { type: "date" }, @@ -243,6 +243,50 @@ def to_document expect(results.total).to eq(2010) end + it "does not copy over fields not defined in the mapping" do + john = subject.new(_id: 1, id: 1, name: "John", birthdate: "1985-10-31", sort: ['john']) + mari = subject.new(_id: 2, id: 2, name: "Mari", birthdate: "1986-09-24", sort: ['mari']) + + john.update + mari.update + + subject.flush_index + results = subject.search({}) + expect(results.first.birthdate).to be + + # no birthdate + subject = Class.new(Elasticity::Document) do + def self.name + "SomeClass" + end + + configure do |c| + c.index_base_name = "users" + c.document_type = "user" + c.strategy = Elasticity::Strategies::AliasIndex + + c.mapping = { + "properties" => { + id: { type: "integer" }, + name: { type: "string", index: "not_analyzed" }, + }, + } + end + + attr_accessor :id, :name + + def to_document + { id: id, name: name } + end + end + + subject.remap! + subject.flush_index + + results = subject.search({}) + expect(results.first.respond_to?(:birthdate)).to be false + end + it "recover from remap interrupts" do number_of_docs = 2000 docs = number_of_docs.times.map do |i|