Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added a setup endpoint GET _zentity/_setup to create the .zentity-mod…
…els index manually. Allows number_of_shards and number_of_replicas to be specified in the URI parameters.
- Loading branch information
1 parent
ccd995a
commit 79a00e3
Showing
3 changed files
with
167 additions
and
52 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
115 changes: 115 additions & 0 deletions
115
src/main/java/org/elasticsearch/plugin/zentity/SetupAction.java
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,115 @@ | ||
package org.elasticsearch.plugin.zentity; | ||
|
||
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; | ||
import org.elasticsearch.client.node.NodeClient; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentFactory; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.rest.BaseRestHandler; | ||
import org.elasticsearch.rest.BytesRestResponse; | ||
import org.elasticsearch.rest.RestController; | ||
import org.elasticsearch.rest.RestRequest; | ||
import org.elasticsearch.rest.RestStatus; | ||
|
||
import static org.elasticsearch.rest.RestRequest.Method; | ||
import static org.elasticsearch.rest.RestRequest.Method.POST; | ||
|
||
public class SetupAction extends BaseRestHandler { | ||
|
||
public static final int DEFAULT_NUMBER_OF_SHARDS = 1; | ||
public static final int DEFAULT_NUMBER_OF_REPLICAS = 1; | ||
public static final String INDEX_MAPPING = "{\n" + | ||
" \"doc\": {\n" + | ||
" \"dynamic\": \"strict\",\n" + | ||
" \"properties\": {\n" + | ||
" \"attributes\": {\n" + | ||
" \"type\": \"object\",\n" + | ||
" \"enabled\": false\n" + | ||
" },\n" + | ||
" \"resolvers\": {\n" + | ||
" \"type\": \"object\",\n" + | ||
" \"enabled\": false\n" + | ||
" },\n" + | ||
" \"matchers\": {\n" + | ||
" \"type\": \"object\",\n" + | ||
" \"enabled\": false\n" + | ||
" },\n" + | ||
" \"indices\": {\n" + | ||
" \"type\": \"object\",\n" + | ||
" \"enabled\": false\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}"; | ||
|
||
@Inject | ||
public SetupAction(Settings settings, RestController controller) { | ||
super(settings); | ||
controller.registerHandler(POST, "_zentity/_setup", this); | ||
} | ||
|
||
/** | ||
* Create the .zentity-models index. | ||
* | ||
* @param client The client that will communicate with Elasticsearch. | ||
* @param numberOfShards The value of index.number_of_shards. | ||
* @param numberOfReplicas The value of index.number_of_replicas. | ||
* @return | ||
*/ | ||
public static CreateIndexResponse createIndex(NodeClient client, int numberOfShards, int numberOfReplicas) { | ||
return client.admin().indices().prepareCreate(ModelsAction.INDEX_NAME) | ||
.setSettings(Settings.builder() | ||
.put("index.number_of_shards", numberOfShards) | ||
.put("index.number_of_replicas", numberOfReplicas) | ||
) | ||
.addMapping("doc", INDEX_MAPPING, XContentType.JSON) | ||
.get(); | ||
} | ||
|
||
/** | ||
* Create the .zentity-models index using the default index settings. | ||
* | ||
* @param client The client that will communicate with Elasticsearch. | ||
* @return | ||
*/ | ||
public static CreateIndexResponse createIndex(NodeClient client) { | ||
return createIndex(client, DEFAULT_NUMBER_OF_SHARDS, DEFAULT_NUMBER_OF_REPLICAS); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "zentity_setup_action"; | ||
} | ||
|
||
@Override | ||
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) { | ||
|
||
// Parse request | ||
Boolean pretty = restRequest.paramAsBoolean("pretty", false); | ||
int numberOfShards = restRequest.paramAsInt("number_of_shards", 1); | ||
int numberOfReplicas = restRequest.paramAsInt("number_of_replicas", 1); | ||
Method method = restRequest.method(); | ||
|
||
return channel -> { | ||
try { | ||
if (method == POST) { | ||
|
||
CreateIndexResponse response = createIndex(client, numberOfShards, numberOfReplicas); | ||
XContentBuilder content = XContentFactory.jsonBuilder(); | ||
if (pretty) | ||
content.prettyPrint(); | ||
content = response.toXContent(content, ToXContent.EMPTY_PARAMS); | ||
channel.sendResponse(new BytesRestResponse(RestStatus.OK, content)); | ||
|
||
} else { | ||
throw new NotImplementedException("Method and endpoint not implemented."); | ||
} | ||
} catch (NotImplementedException e) { | ||
channel.sendResponse(new BytesRestResponse(channel, RestStatus.NOT_IMPLEMENTED, e)); | ||
} | ||
}; | ||
} | ||
} |
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