1 /** 2 * Indice API 3 * 4 * Copyright: © 2015 David Monagle 5 * License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. 6 * Authors: David Monagle 7 */ 8 module elasticsearch.api.actions.indices; 9 10 import elasticsearch.api.parameters; 11 import elasticsearch.transport.response; 12 import elasticsearch.transport.exceptions; 13 import elasticsearch.client; 14 15 /** Create an index. 16 * 17 * Creates a new index 18 * 19 * Params: 20 * arguments = A ESParams dictionary list with the following 21 * index: The index name (required) 22 * body: Optional json string configuration for the index (`settings` and `mappings`) 23 * timeout: Explicit operation timeout 24 * 25 * Returns: Response object with the server response. 26 * 27 * Throws: ArgumentError 28 * 29 * See_Also: http://www.elasticsearch.org/guide/reference/api/admin-indices-create-index/ 30 * 31 */ 32 Response createIndex(Client client, ESParams arguments = ESParams()) { 33 arguments.enforceParameter("index"); 34 35 auto index = arguments["index"]; 36 auto params = arguments.validateAndExtract("timeout"); 37 auto requestBody = arguments["body"]; 38 39 return client.performRequest(RequestMethod.PUT, esPathify(index), params, requestBody); 40 } 41 42 /// 43 Response createIndex(Client client, string indexName, string indexBody = "{}", ESParams params = ESParams()) { 44 params["index"] = indexName; 45 params["body"] = indexBody; 46 return createIndex(client, params); 47 } 48 49 /// 50 Response deleteIndex(Client client, string[] indexes, ESParams arguments = ESParams()) { 51 auto params = arguments.validateAndExtract("timeout"); 52 return client.performRequest(RequestMethod.DELETE, esPathify(esListify(indexes)), params); 53 } 54 55 /// 56 Response deleteIndex(Client client, string index, ESParams params = ESParams()) { 57 return deleteIndex(client, [index], params); 58 } 59 60 /// 61 alias refreshIndex = elasticsearch.api.actions.indices.refresh_; 62 63 /// 64 Response refresh_(Client client, string[] indexes, ESParams arguments = ESParams()) { 65 auto params = arguments.validateAndExtract("timeout"); 66 indexes ~= "_refresh"; 67 return client.performRequest(RequestMethod.POST, esPathify(indexes), params); 68 } 69 70 /// 71 Response refresh_(Client client, string index, ESParams params = ESParams()) { 72 return refresh_(client, [index], params); 73 } 74 75 76 /// 77 bool indexExists(Client client, ESParams arguments) { 78 arguments.enforceParameter("index"); 79 80 auto params = arguments.validateAndExtract( 81 "ignore_indices", "ignore_unavailable", "allow_no_indices", 82 "expand_wildcards", "local" 83 ); 84 85 try { 86 Response response = client.performRequest(RequestMethod.HEAD, esPathify(arguments["index"]), params); 87 return (response.status == 200); 88 } 89 catch (RequestException e) { 90 return false; 91 } 92 } 93 94 /// 95 bool indexExists(Client client, string indexName, ESParams params = ESParams()) { 96 params["index"] = indexName; 97 98 return indexExists(client, params); 99 }