1 module elasticsearch.api.actions.index;
2 
3 import elasticsearch.api.parameters;
4 import elasticsearch.transport;
5 import elasticsearch.client;
6 
7 import vibe.http.common;
8 
9 alias create = elasticsearch.api.actions.index.index;
10 
11 /// Create or update a document.
12 ///
13 /// The `index` API will either _create_ a new document, or _update_ an existing one, when a document `:id`
14 /// is passed. When creating a document, an ID will be auto-generated, when it's not passed as an argument.
15 ///
16 /// You can specifically enforce the _create_ operation by settint the `op_type` argument to `create`, or
17 /// by using the {Actions#create} method.
18 ///
19 /// Optimistic concurrency control is performed, when the `version` argument is specified. By default,
20 /// no version checks are performed.
21 ///
22 /// By default, the document will be available for {Actions#get} immediately, for {Actions#search} only
23 /// after an index refresh operation has been performed (either automatically or manually).
24 ///
25 /// @example Create or update a document `myindex/mytype/1`
26 ///
27 ///     client.index index: 'myindex',
28 ///                  type: 'mytype',
29 ///                  id: '1',
30 ///                  body: {
31 ///                   title: 'Test 1',
32 ///                   tags: ['y', 'z'],
33 ///                   published: true,
34 ///                   published_at: Time.now.utc.iso8601,
35 ///                   counter: 1
36 ///                 }
37 ///
38 /// @example Refresh the index after the operation (useful e.g. in integration tests)
39 ///
40 ///     client.index index: 'myindex', type: 'mytype', id: '1', body: { title: 'TEST' }, refresh: true
41 ///     client.search index: 'myindex', q: 'title:test'
42 ///
43 /// @example Create a document with a specific expiration time (TTL)
44 ///
45 ///     # Decrease the default housekeeping interval first:
46 ///     client.cluster.put_settings body: { transient: { 'indices.ttl.interval' => '1s' } }
47 ///
48 ///     # Enable the `_ttl` property for all types within the index
49 ///     client.indices.create index: 'myindex', body: { mappings: { mytype: { _ttl: { enabled: true } }  } }
50 ///
51 ///     client.index index: 'myindex', type: 'mytype', id: '1', body: { title: 'TEST' }, ttl: '5s'
52 ///
53 ///     sleep 3 and client.get index: 'myindex', type: 'mytype', id: '1'
54 ///     # => {"_index"=>"myindex" ... "_source"=>{"title"=>"TEST"}}
55 ///
56 ///     sleep 3 and client.get index: 'myindex', type: 'mytype', id: '1'
57 ///     # => Elasticsearch::Transport::Transport::Errors::NotFound: [404] ...
58 ///
59 /// @option arguments [String] :id Document ID (optional, will be auto-generated if missing)
60 /// @option arguments [String] :index The name of the index (*Required*)
61 /// @option arguments [String] :type The type of the document (*Required*)
62 /// @option arguments [Hash] :body The document
63 /// @option arguments [String] :consistency Explicit write consistency setting for the operation
64 ///                                         (options: one, quorum, all)
65 /// @option arguments [String] :op_type Explicit operation type (options: index, create)
66 /// @option arguments [String] :parent ID of the parent document
67 /// @option arguments [String] :percolate Percolator queries to execute while indexing the document
68 /// @option arguments [Boolean] :refresh Refresh the index after performing the operation
69 /// @option arguments [String] :replication Specific replication type (options: sync, async)
70 /// @option arguments [String] :routing Specific routing value
71 /// @option arguments [Time] :timeout Explicit operation timeout
72 /// @option arguments [Time] :timestamp Explicit timestamp for the document
73 /// @option arguments [Duration] :ttl Expiration time for the document
74 /// @option arguments [Number] :version Explicit version number for concurrency control
75 /// @option arguments [String] :version_type Specific version type (options: internal, external, external_gte, force)
76 ///
77 /// @see http://elasticsearch.org/guide/reference/api/index_/
78 ///
79 Response index(Client client, ESParams arguments = ESParams()) {
80 	RequestMethod method;
81 	
82 	arguments.enforceParameter("index");
83 	arguments.enforceParameter("type");
84 	
85 	auto requestBody = arguments["body"];
86 	auto params = arguments.validateAndExtract(
87 		"consistency", "op_type", "parent", "percolate", "refresh", "replication", "routing",
88 		"timeout", "timestamp", "ttl", "version", "version_type"
89 		);
90 	string[] path = [arguments["index"], arguments["type"]];
91 	
92 	if (arguments.hasField("id")) {
93 		method = RequestMethod.PUT;
94 		path ~= arguments["id"];
95 	}
96 	else {
97 		method = RequestMethod.POST;
98 	}
99 	
100 	return client.performRequest(method, esPathify(path), params, requestBody);
101 }
102 
103 /// ditto
104 Response index(Client client, string indexName, string type, string id, string requestBody, ESParams p = ESParams()) {
105 	p["index"] = indexName;
106 	p["type"] = type;
107 	p["id"] = id;
108 	p["body"] = requestBody;
109 	
110 	return index(client, p);
111 }
112 
113 /// ditto
114 Response index(Client client, string indexName, string type, string requestBody, ESParams p = ESParams()) {
115 	p["index"] = indexName;
116 	p["type"] = type;
117 	p["body"] = requestBody;
118 	
119 	return index(client, p);
120 }