package com.cadrlife.devsearch.esplugin;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.name.Named;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.OK;
public class InitRestHandler extends BaseRestHandler {
private final String codeIndex;
@Inject
public InitRestHandler(Settings settings, Client client, RestController restController, @Named("codeIndex") String codeIndex) {
super(settings, client);
this.codeIndex = codeIndex;
restController.registerHandler(GET, "/_dev-search/init", this);
}
@Override
public void handleRequest(final RestRequest request, final RestChannel channel) {
try {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> value = new HashMap<>();
ArrayList<String> actions = new ArrayList<>();
ensureIndex(actions);
value.put("changed", !actions.isEmpty());
value.put("actions", actions);
channel.sendResponse(new BytesRestResponse(OK, mapper.writeValueAsString(value)));
} catch (Exception e) {
logger.error("Failed to get projects", e);
channel.sendResponse(new BytesRestResponse(RestStatus.INTERNAL_SERVER_ERROR, e.getMessage()));
}
}
private void ensureIndex(List<String> actions) {
logger.info("Running ensure index");
IndicesExistsResponse response = client.admin().indices().prepareExists(codeIndex).execute().actionGet();
if (!response.isExists()) {
logger.info("Index missing. Creating...");
CreateIndexResponse createResponse = client.admin().indices().prepareCreate(codeIndex).setSource("{\"settings\":{\n" +
" \"analysis\": {\n" +
" \"analyzer\": {\n" +
" \"sourcecode\":{\n" +
" \"type\": \"pattern\",\n" +
" \"pattern\":\"[^\\\\w\\\\$\\\\p{L}]\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }").execute().actionGet();
if (!createResponse.isAcknowledged()) {
throw new RuntimeException("Create index request not acknowledged");
}
logger.info("Created " + codeIndex + "");
actions.add("Created " + codeIndex + "");
PutMappingResponse docResponse = client.admin().indices().preparePutMapping(codeIndex).setType("doc").setSource("{\"doc\" : {\n" +
" \"properties\" : {\n" +
" \"project\" : { \"type\": \"string\", \"index\" : \"not_analyzed\" },\n" +
" \"repo\" : { \"type\": \"string\", \"index\" : \"not_analyzed\" },\n" +
" \"content\" : { \"type\": \"string\", \"analyzer\" : \"sourcecode\" },\n" +
" \"lastIndexed\" : {\"type\" : \"date\"},\n" +
" \"lastChanged\": {\"type\" : \"date\"},\n" +
" \"javaName\" : { \"type\": \"string\", \"index\" : \"not_analyzed\" },\n" +
" \"javaPackage\" : { \"type\": \"string\", \"index\" : \"not_analyzed\" }\n" +
" }\n" +
" }\n" +
" }").execute().actionGet();
if (!docResponse.isAcknowledged()) {
throw new RuntimeException("Create doc mappings request not acknowledged");
}
logger.info("Created doc mappings");
actions.add("Created doc mappings");
PutMappingResponse projectResponse = client.admin().indices().preparePutMapping(codeIndex).setType("project").setSource("{\"project\": {\n" +
" \"properties\" : {\n" +
" \"lastIndexed\": {\"type\" : \"date\"},\n" +
" \"lastChanged\": {\"type\" : \"date\"},\n" +
" \"repo\" : { \"type\": \"string\", \"index\" : \"not_analyzed\" },\n" +
" \"name\" : { \"type\": \"string\", \"index\" : \"not_analyzed\" }\n" +
" }\n" +
" }\n" +
" }").execute().actionGet();
if (!projectResponse.isAcknowledged()) {
throw new RuntimeException("Create project mappings request not acknowledged");
}
logger.info("Created project mappings");
actions.add("Created project mappings");
}
// System.out.println("Response " + response.isExists());
}
}