lib/elasticsearch/model/indexing.rb in elasticsearch-model-6.1.2 vs lib/elasticsearch/model/indexing.rb in elasticsearch-model-7.0.0.pre
- old
+ new
@@ -1,5 +1,22 @@
+# Licensed to Elasticsearch B.V. under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Elasticsearch B.V. licenses this file to you under
+# the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
module Elasticsearch
module Model
# Provides the necessary support to set up index options (mappings, settings)
# as well as instance methods to create, update or delete documents in the index.
@@ -37,13 +54,11 @@
attr_accessor :options, :type
# @private
TYPES_WITH_EMBEDDED_PROPERTIES = %w(object nested)
- def initialize(type, options={})
- raise ArgumentError, "`type` is missing" if type.nil?
-
+ def initialize(type = nil, options={})
@type = type
@options = options
@mapping = {}
end
@@ -70,11 +85,15 @@
self
end
def to_hash
- { @type.to_sym => @options.merge( properties: @mapping ) }
+ if @type
+ { @type.to_sym => @options.merge( properties: @mapping ) }
+ else
+ @options.merge( properties: @mapping )
+ end
end
def as_json(options={})
to_hash
end
@@ -227,13 +246,15 @@
mappings = options.delete(:mappings) || self.mappings.to_hash
delete_index!(options.merge index: target_index) if options[:force]
unless index_exists?(index: target_index)
- self.client.indices.create index: target_index,
- body: {
- settings: settings,
- mappings: mappings }
+ options.delete(:force)
+ self.client.indices.create({ index: target_index,
+ body: {
+ settings: settings,
+ mappings: mappings }
+ }.merge(options))
end
end
# Returns true if the index exists
#