public ResultMessage get(String messageId, String index) throws DocumentNotFoundException, IOException { final Get get = new Get.Builder(index, messageId).type(IndexMapping.TYPE_MESSAGE).build(); final DocumentResult result = client.execute(get); if (!result.isSucceeded()) { throw new DocumentNotFoundException(index, messageId); } @SuppressWarnings("unchecked") final Map<String, Object> message = (Map<String, Object>) result.getSourceAsObject(Map.class, false); return ResultMessage.parseFromSource(result.getId(), result.getIndex(), message); }
@Override public DocumentResult createNewElasticSearchResult(String responseBody, int statusCode, String reasonPhrase, Gson gson) { return createNewElasticSearchResult(new DocumentResult(gson), responseBody, statusCode, reasonPhrase, gson); } }
public Long getVersion() { return getAsLong(jsonObject.get("_version")); }
/** * This executes single Elasticsearch actions, creating indices as needed. * * @param client * @param action * @throws IOException */ private static void executeSingleAction(JestClient client, BulkableAction<DocumentResult> action) throws IOException { DocumentResult result = client.execute(action); if(result == null || result.getResponseCode() == 404){ // index doesn't exist for upsert command so create new index and try again if(LOG.isDebugEnabled()) { if (result == null) { logEsDebug(action.getRestMethodName(), action.getIndex(), action.getType(), null, -1, null); } else { logEsDebug(action.getRestMethodName(), action.getIndex(), action.getType(), result.getJsonString(), result.getResponseCode(), result.getErrorMessage()); } LOG.debug("index name "+action.getIndex() + " doesn't exist, creating new index"); } createIndex(client, action.getIndex(), action.getType()); result = client.execute(action); } if (result == null) { logEsError(action.getRestMethodName(), action.getIndex(), action.getType(), null, -1, null); } else if (!result.isSucceeded()){ logEsError(action.getRestMethodName(), action.getIndex(), action.getType(), result.getJsonString(), result.getResponseCode(), result.getErrorMessage()); } else if(LOG.isDebugEnabled()) { logEsDebug(action.getRestMethodName(), action.getIndex(), action.getType(), result.getJsonString(), result.getResponseCode(), result.getErrorMessage()); } }
@Override public UpdateResponse update(UpdateQuery updateQuery) { DocumentResult result = execute(prepareUpdate(updateQuery)); return new UpdateResponse(null, result.getType(), result.getId(), result.getJsonObject().get("_version").getAsLong(), DocWriteResponse.Result.CREATED); }
public String getType() { return getAsString(jsonObject.get("_type")); }
/** * Cache a result * * @param content */ private void cacheResult(final String content) { final JestClient client = getElasticSearchClient(); if (client != null) { final Index contentIndex = new Index.Builder(content).index(elasticIndex).type("logEntry").build(); try { final DocumentResult result = client.execute(contentIndex); log.debug("Completed indexation of content {} with succeeded={}", content, result.isSucceeded()); } catch (IOException e) { log.error("Error indexing content {}: {}", content, e.getMessage(), e); } //TODO: move to async at some point /*client.executeAsync(contentIndex, new JestResultHandler<JestResult>() { @Override public void completed(JestResult result) { log.debug("Completed indexation of content {} with succeeded={}", content, result.isSucceeded()); } @Override public void failed(Exception e) { log.error("Error indexing content {}: {}", content, e.getMessage(), e); } });*/ } else { log.warn("Content {} won't be cached, there is not target bucket", content); } }
public <T> T mapResult(DocumentResult response, Class<T> clazz) { T result = mapEntity(response.getSourceAsString(), clazz); if (result != null) { setPersistentEntityId(result, response.getId(), clazz); } return result; }
@Override public String index(IndexQuery query) { String documentId = execute(prepareIndex(query)).getId(); // We should call this because we are not going through a mapper. if (query.getObject() != null && isDocument(query.getObject().getClass())) { setPersistentEntityId(query.getObject(), documentId); } return documentId; }
private static void checkDocumentEquality(Map<String, Object> expectedDataOfDocument, Get request, DocumentResult dataOfDocumentResponse) { @SuppressWarnings("unchecked") final Map<String, Object> dataOfDocument = (Map<String, Object>) dataOfDocumentResponse.getSourceAsObject(Map.class, false); // Workaround because DeepEquals.deepEquals expects the types to be identical final Map<String, Object> actual = new HashMap<>(dataOfDocument); final Map<String, Object> expected = new HashMap<>(expectedDataOfDocument); if (!DeepEquals.deepEquals(actual, expected)) { throw FailureHandler.createFailure("Expected document for index: %s - type: %s - id: %s is %s, but %s was found.", request.getIndex(), request.getType(), request.getId(), GSON.toJson(expectedDataOfDocument), GSON.toJson(dataOfDocument)); } }
@Override public Explanation explain(int documentId) { if ( searchResult == null ) { execute(); } JsonObject hit = searchResult.getJsonObject() .get( "hits" ) .getAsJsonObject() .get( "hits" ) .getAsJsonArray() // TODO Is it right to use the document id that way? I am not quite clear about its semantics .get( documentId ) .getAsJsonObject(); try ( ServiceReference<JestClient> client = getExtendedSearchIntegrator().getServiceManager().requestReference( JestClient.class ) ) { Explain request = new Explain.Builder( hit.get( "_index" ).getAsString(), hit.get( "_type" ).getAsString(), hit.get( "_id" ).getAsString(), searcher.executedQuery ) .build(); DocumentResult response = client.get().executeRequest( request ); JsonObject explanation = response.getJsonObject().get( "explanation" ).getAsJsonObject(); return convertExplanation( explanation ); } }
public String getId() { return getAsString(jsonObject.get("_id")); }
private static void checkExistenceOfDocument(Get request, DocumentResult dataOfDocumentResponse) { if (!dataOfDocumentResponse.isSucceeded()) { throw FailureHandler.createFailure( "Document with index: %s - type: %s - id: %s has not returned any document.", request.getIndex(), request.getType(), request.getId()); } }
@Override public String delete(String indexName, String type, String id) { return execute(new Delete.Builder(id).index(indexName).type(type).build(), true).getId(); }
public ResultMessage get(String messageId, String index) throws DocumentNotFoundException, IOException { final Get get = new Get.Builder(index, messageId).type(IndexMapping.TYPE_MESSAGE).build(); final DocumentResult result = client.execute(get); if (!result.isSucceeded()) { throw new DocumentNotFoundException(index, messageId); } @SuppressWarnings("unchecked") final Map<String, Object> message = (Map<String, Object>) result.getSourceAsObject(Map.class, false); return ResultMessage.parseFromSource(result.getId(), result.getIndex(), message); }
public String getIndex() { return getAsString(jsonObject.get("_index")); }
@Override public DocumentResult createNewElasticSearchResult(String responseBody, int statusCode, String reasonPhrase, Gson gson) { return createNewElasticSearchResult(new DocumentResult(gson), responseBody, statusCode, reasonPhrase, gson); } }
public Long getVersion() { return getAsLong(jsonObject.get("_version")); }
public String getType() { return getAsString(jsonObject.get("_type")); }
@Override public DocumentResult createNewElasticSearchResult(String responseBody, int statusCode, String reasonPhrase, ObjectMapper objectMapper) throws IOException { return createNewElasticSearchResult(new DocumentResult(objectMapper), responseBody, statusCode, reasonPhrase, objectMapper); } }