public static void uploadFile(Client tc, String filepath, String index, String id) throws Exception { LOGGER.info("Will update '" + id + "' with " + filepath); try (Reader reader = new FileReader(filepath)) { final String res = tc .index(new IndexRequest(index).type("sg").id(id).setRefreshPolicy(RefreshPolicy.IMMEDIATE) .source(id, readXContent(reader, XContentType.YAML))).actionGet().getId(); if (!id.equals(res)) { throw new Exception(" FAIL: Configuration for '" + id + "' failed for unknown reasons. Pls. consult logfile of elasticsearch"); } } catch (Exception e) { throw e; } }
@Override public void addMessage(String queue, Message message) { Map<String, Object> doc = new HashMap<>(); doc.put("messageId", message.getId()); doc.put("payload", message.getPayload()); doc.put("queue", queue); doc.put("created", System.currentTimeMillis()); IndexRequest request = new IndexRequest(logIndexName, MSG_DOC_TYPE); request.source(doc); try { new RetryUtil<>().retryOnException( () -> elasticSearchClient.index(request).actionGet(), null, null, RETRY_COUNT, "Indexing document in for docType: message", "addMessage" ); } catch (Exception e) { logger.error("Failed to index message: {}", message.getId(), e); } }
private static boolean uploadFile(final Client tc, final String filepath, final String index, final String _id, final boolean legacy) { String type = "sg"; String id = _id; if(legacy) { type = _id; id = "0"; } System.out.println("Will update '"+type+"/" + id + "' with " + filepath+" "+(legacy?"(legacy mode)":"")); try (Reader reader = new FileReader(filepath)) { final String res = tc .index(new IndexRequest(index).type(type).id(id).setRefreshPolicy(RefreshPolicy.IMMEDIATE) .source(_id, readXContent(reader, XContentType.YAML))).actionGet().getId(); if (id.equals(res)) { System.out.println(" SUCC: Configuration for '" + _id + "' created or updated"); return true; } else { System.out.println(" FAIL: Configuration for '" + _id + "' failed for unknown reasons. Please consult the Elasticsearch logfile."); } } catch (Exception e) { System.out.println(" FAIL: Configuration for '" + _id + "' failed because of " + e.toString()); } return false; }
private SearchGuardLicense createOrGetTrial(String msg) { long created = System.currentTimeMillis(); ThreadContext threadContext = threadPool.getThreadContext(); try(StoredContext ctx = threadContext.stashContext()) { threadContext.putHeader(ConfigConstants.SG_CONF_REQUEST_HEADER, "true"); GetResponse get = client.prepareGet(searchguardIndex, "sg", "tattr").get(); if(get.isExists()) { created = (long) get.getSource().get("val"); } else { try { client.index(new IndexRequest(searchguardIndex) .type("sg") .id("tattr") .setRefreshPolicy(RefreshPolicy.IMMEDIATE) .create(true) .source("{\"val\": "+System.currentTimeMillis()+"}", XContentType.JSON)).actionGet(); } catch (VersionConflictEngineException e) { //ignore } catch (Exception e) { LOGGER.error("Unable to index tattr", e); } } } return SearchGuardLicense.createTrialLicense(formatDate(created), clusterService, msg); } }
@Override protected ActionFuture<IndexResponse> doExecute(IndexRequest request) { return client.index(request); }
@Override public IndexResponse execute(final Client client) { return client.index(indexRequest).actionGet(); } }).getId();
@Override public Void execute(final Client client) throws ElasticsearchException { try { IndexResponse response = client.index(request).get(); } catch (Exception e) { throw new EsSetupRuntimeException(e); } return null; }
@Override public Void execute(final Client client) throws ElasticsearchException { try { IndexResponse response = client.index(request).get(); } catch (Exception e) { throw new EsSetupRuntimeException(e); } return null; }
@Override public IndexResponse index(IndexRequest request) { try { return client.index(request).actionGet(); } catch (VersionConflictEngineException e) { throw new ConcurrentUpdateException(e); } }
@VisibleForTesting protected void recover(int itemId, BulkRequest request) { client.index(createIndexRequest((Message) request.payloads().get(itemId))).actionGet(); }
private static boolean uploadFile(final Client tc, final String filepath, final String index, final String _id, final boolean legacy) { String type = "sg"; String id = _id; if(legacy) { type = _id; id = "0"; } System.out.println("Will update '"+type+"/" + id + "' with " + filepath+" "+(legacy?"(legacy mode)":"")); try (Reader reader = new FileReader(filepath)) { final String res = tc .index(new IndexRequest(index).type(type).id(id).setRefreshPolicy(RefreshPolicy.IMMEDIATE) .source(_id, readXContent(reader, XContentType.YAML))).actionGet().getId(); if (id.equals(res)) { System.out.println(" SUCC: Configuration for '" + _id + "' created or updated"); return true; } else { System.out.println(" FAIL: Configuration for '" + _id + "' failed for unknown reasons. Please consult the Elasticsearch logfile."); } } catch (Exception e) { System.out.println(" FAIL: Configuration for '" + _id + "' failed because of " + e.toString()); } return false; }
public static void uploadFile(Client tc, String filepath, String index, String id) throws Exception { LOGGER.info("Will update '" + id + "' with " + filepath); try (Reader reader = new FileReader(filepath)) { final String res = tc .index(new IndexRequest(index).type("sg").id(id).setRefreshPolicy(RefreshPolicy.IMMEDIATE) .source(id, readXContent(reader, XContentType.YAML))).actionGet().getId(); if (!id.equals(res)) { throw new Exception(" FAIL: Configuration for '" + id + "' failed for unknown reasons. Pls. consult logfile of elasticsearch"); } } catch (Exception e) { throw e; } }
private void storeLastUpdatedTimestamp(String exportTimestamp) { String json = "{ \"lastUpdatedTimestamp\" : \"" + exportTimestamp + "\" }"; IndexRequest updateTimestampRequest = indexRequest(riverIndexName).type(riverName.name()).id("lastUpdatedTimestamp").source(json); client.index(updateTimestampRequest).actionGet(); }
@Override public ActionFuture<IndexResponse> execute(final Client client) { final IndexRequest request = Requests.indexRequest(nodeTemplate.getIndexName()) .source(content).type("log"); return client.index(request); } });
private SearchGuardLicense createOrGetTrial(String msg) { long created = System.currentTimeMillis(); ThreadContext threadContext = threadPool.getThreadContext(); try(StoredContext ctx = threadContext.stashContext()) { threadContext.putHeader(ConfigConstants.SG_CONF_REQUEST_HEADER, "true"); GetResponse get = client.prepareGet(searchguardIndex, "sg", "tattr").get(); if(get.isExists()) { created = (long) get.getSource().get("val"); } else { try { client.index(new IndexRequest(searchguardIndex) .type("sg") .id("tattr") .setRefreshPolicy(RefreshPolicy.IMMEDIATE) .create(true) .source("{\"val\": "+System.currentTimeMillis()+"}", XContentType.JSON)).actionGet(); } catch (VersionConflictEngineException e) { //ignore } catch (Exception e) { LOGGER.error("Unable to index tattr", e); } } } return SearchGuardLicense.createTrialLicense(formatDate(created), clusterService, msg); } }
@Override public InsertResponse insert(InsertRequest insertRequest) throws ClientException { checkClient(); Map<String, Object> storableMap = modelContext.marshal(insertRequest.getStorable()); logger.debug("Insert - converted object: '{}'", storableMap); org.elasticsearch.action.index.IndexRequest idxRequest = new org.elasticsearch.action.index.IndexRequest(insertRequest.getTypeDescriptor().getIndex(), insertRequest.getTypeDescriptor().getType()).source(storableMap); if (insertRequest.getId() != null) { idxRequest.id(insertRequest.getId()).version(1).versionType(VersionType.EXTERNAL); } org.elasticsearch.action.index.IndexResponse response = esClientProvider.getClient().index(idxRequest).actionGet(getQueryTimeout()); return new InsertResponse(response.getId(), insertRequest.getTypeDescriptor()); }
@Override public InsertResponse insert(InsertRequest insertRequest) throws ClientException { checkClient(); Map<String, Object> storableMap = modelContext.marshal(insertRequest.getStorable()); logger.debug("Insert - converted object: '{}'", storableMap); org.elasticsearch.action.index.IndexRequest idxRequest = new org.elasticsearch.action.index.IndexRequest(insertRequest.getTypeDescriptor().getIndex(), insertRequest.getTypeDescriptor().getType()).source(storableMap); if (insertRequest.getId() != null) { idxRequest.id(insertRequest.getId()).version(1).versionType(VersionType.EXTERNAL); } org.elasticsearch.action.index.IndexResponse response = esClientProvider.getClient().index(idxRequest).actionGet(getQueryTimeout()); return new InsertResponse(response.getId(), insertRequest.getTypeDescriptor()); }
public void putScriptToIndex(PutIndexedScriptRequest request, ActionListener<IndexResponse> listener) { String scriptLang = validateScriptLanguage(request.scriptLang()); //verify that the script compiles validate(request.source(), scriptLang); IndexRequest indexRequest = new IndexRequest(request).index(SCRIPT_INDEX).type(scriptLang).id(request.id()) .version(request.version()).versionType(request.versionType()) .source(request.source()).opType(request.opType()).refresh(true); //Always refresh after indexing a template client.index(indexRequest, listener); }
@Override public void onMessage(final Message msg) throws IOException, MessagingException { if (closed) { if (logger.isTraceEnabled()) { logger.trace("Is closed, will not index"); } return; } if (isError()) { if (logger.isTraceEnabled()) { logger.trace("error, not indexing"); } return; } createIndexIfNotExists(); final IndexableMailMessage imsg = IndexableMailMessage.fromJavaMailMessage(msg, withTextContent, withHtmlContent, preferHtmlContent, withAttachments, stripTagsFromTextContent, headersToFields); if (logger.isTraceEnabled()) { logger.trace("Process mail " + imsg.getUid() + "/" + imsg.getPopId() + " :: " + imsg.getSubject() + "/" + imsg.getSentDate()); } client.index(createIndexRequest(imsg)).actionGet(); }
indexRequest.consistencyLevel(WriteConsistencyLevel.fromString(consistencyLevel)); client.index(indexRequest, new RestBuilderListener<IndexResponse>(channel) { @Override public RestResponse buildResponse(IndexResponse response, XContentBuilder builder) throws Exception {