@Override protected void onPersist(EntityMetadata entityMetadata, Object entity, Object id, List<RelationHolder> rlHolders) { try { Map<String, Object> values = new HashMap<String, Object>(); MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata() .getMetamodel(entityMetadata.getPersistenceUnit()); EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz()); String keyAsString = getKeyAsString(id, entityMetadata, metaModel); addSource(entity, values, entityType); addRelations(rlHolders, values); addDiscriminator(values, entityType); IndexResponse response = txClient .prepareIndex(entityMetadata.getSchema().toLowerCase(), entityMetadata.getTableName(), keyAsString) .setSource(values).setRefresh(isRefreshIndexes()).execute().actionGet(); assert response.getId() != null; } finally { // Nothing as of now. } }
protected void storeIndex(final String indexName, final String typeName, final Map<String, Object> dataMap) { dataMap.put(TIMESTAMP_FIELD, new Date()); if (logger.isDebugEnabled()) { logger.debug(indexName + "/" + typeName + " : dataMap" + dataMap); } try { esClient.prepareIndex(indexName, typeName).setRefresh(true).setSource(jsonBuilder().value(dataMap)).execute().actionGet(); } catch (final Exception e) { logger.warn("Could not write a content into index.", e); } }
@Override public void setPreference(final long userID, final long itemID, final float value) { createUserID(userID); createItemID(itemID); final Map<String, Object> source = new HashMap<>(); source.put(userIdField, userID); source.put(itemIdField, itemID); source.put(valueField, value); source.put(timestampField, new Date()); try { client.prepareIndex(preferenceIndex, preferenceType) .setSource(source).setRefresh(true).execute().actionGet(); } catch (final ElasticsearchException e) { throw new TasteException("Failed to set (" + userID + "," + itemID + "," + value + ")", e); } }
private void createUserID(final long userID) { final GetResponse getResponse = client .prepareGet(userIndex, userType, Long.toString(userID)) .setRefresh(true).execute().actionGet(); if (!getResponse.isExists()) { final Map<String, Object> source = new HashMap<>(); source.put("system_id", Long.toString(userID)); source.put(userIdField, userID); source.put(timestampField, new Date()); final IndexResponse response = client .prepareIndex(userIndex, userType, Long.toString(userID)) .setSource(source).setRefresh(true).execute().actionGet(); if (!response.isCreated()) { throw new TasteException("Failed to create " + source); } } }
private void createItemID(final long itemID) { final GetResponse getResponse = client .prepareGet(itemIndex, itemType, Long.toString(itemID)) .setRefresh(true).execute().actionGet(); if (!getResponse.isExists()) { final Map<String, Object> source = new HashMap<>(); source.put("system_id", Long.toString(itemID)); source.put(itemIdField, itemID); source.put(timestampField, new Date()); final IndexResponse response = client .prepareIndex(itemIndex, itemType, Long.toString(itemID)) .setSource(source).setRefresh(true).execute().actionGet(); if (!response.isCreated()) { throw new TasteException("Failed to create " + source); } } }
public void registerPercolateQuery(String indexName, String queryName, QueryBuilder queryBuilder) { try { client.prepareIndex(indexName, PERCOLATOR_TYPE, queryName) .setSource(XContentFactory.jsonBuilder() .startObject() .field("query", queryBuilder) .endObject()) .setRefresh(true) .execute().actionGet(); } catch (IOException e) { DTThrowable.rethrow(e); } }
@Override @SneakyThrows({ IOException.class }) public <T> void save(T data) { String indexName = getIndexForType(data.getClass()); String typeName = MappingBuilder.indexTypeFromClass(data.getClass()); updateDate(data); String json = getJsonMapper().writeValueAsString(data); getClient().prepareIndex(indexName, typeName).setOperationThreaded(false).setSource(json).setRefresh(true).execute().actionGet(); }
public void registerPercolateQuery(String indexName, String queryName, QueryBuilder queryBuilder) { try { client.prepareIndex(indexName, PERCOLATOR_TYPE, queryName) .setSource(XContentFactory.jsonBuilder() .startObject() .field("query", queryBuilder) .endObject()) .setRefresh(true) .execute().actionGet(); } catch (IOException e) { DTThrowable.rethrow(e); } }
public void createToken(final Set<String> roleSet, final ActionListener<String> listener) { if (roleSet == null || roleSet.isEmpty()) { listener.onFailure(new AuthException(RestStatus.BAD_REQUEST, "Role is empty.")); return; } final String token = generateToken(); final Map<String, Object> sourceMap = new HashMap<String, Object>(); sourceMap.put("roles", roleSet); sourceMap.put("lastModified", new Date()); client.prepareIndex(authTokenIndex, tokenType, token) .setSource(sourceMap).setRefresh(true) .execute(new ActionListener<IndexResponse>() { @Override public void onResponse(final IndexResponse response) { listener.onResponse(token); } @Override public void onFailure(final Throwable e) { listener.onFailure(e); } }); }
private void doItemUpdate(final Params params, final RequestHandler.OnErrorListener listener, final Map<String, Object> requestMap, final Map<String, Object> paramMap, final Map<String, Object> itemMap, final String index, final String type, final String itemIdField, final String timestampField, final Long itemId, final OpType opType, final RequestHandlerChain chain) { itemMap.put(itemIdField, itemId); itemMap.put(timestampField, new Date()); final OnResponseListener<IndexResponse> responseListener = response -> { paramMap.put(itemIdField, itemId); chain.execute(params, listener, requestMap, paramMap); }; final OnFailureListener failureListener = t -> { sleep(t); if (t instanceof DocumentAlreadyExistsException || t instanceof EsRejectedExecutionException) { execute(params, listener, requestMap, paramMap, chain); } else { listener.onError(t); } }; client.prepareIndex(index, type, itemId.toString()).setSource(itemMap) .setRefresh(true).setOpType(opType) .execute(on(responseListener, failureListener)); }
private void doUserUpdate(final Params params, final RequestHandler.OnErrorListener listener, final Map<String, Object> requestMap, final Map<String, Object> paramMap, final Map<String, Object> userMap, final String index, final String type, final String userIdField, final String timestampField, final Long userId, final OpType opType, final RequestHandlerChain chain) { userMap.put(userIdField, userId); userMap.put(timestampField, new Date()); final OnResponseListener<IndexResponse> responseListener = response -> { paramMap.put(userIdField, userId); chain.execute(params, listener, requestMap, paramMap); }; final OnFailureListener failureListener = t -> { if (t instanceof DocumentAlreadyExistsException || t instanceof EsRejectedExecutionException) { sleep(t); execute(params, listener, requestMap, paramMap, chain); } else { listener.onError(t); } }; client.prepareIndex(index, type, userId.toString()).setSource(userMap) .setRefresh(true).setOpType(opType) .execute(on(responseListener, failureListener)); }
private void createStoredQuery(String instanceId, String ttl, BooleanAgentDocument booleanAgentDocument, Optional<String> booleanRestriction) throws IOException { String reference = booleanAgentDocument.getReference() + "_" + instanceId; XContentBuilder storedQuery = prepareStoredQuery(instanceId, booleanAgentDocument, booleanRestriction, reference); // replaces an existing document with a new document by using the reference value as the Elasticsearch _id. getElasticClient() .prepareIndex(policyIndexName, percolatorTypeName, reference) .setSource(storedQuery) .setRefresh(true) .setTTL(ttl) .get(elasticsearchProperties.getElasticsearchSearchTimeout()); }
@Given("^i create a relationshiptype \"([^\"]*)\" in an archive name \"([^\"]*)\" version \"([^\"]*)\" with properties$") public void i_create_a_relationshiptype_in_an_archive_name_version_with_properties(String elementId, String archiveName, String archiveVersion, DataTable properties) throws Throwable { RelationshipType relationship = new RelationshipType(); relationship.setArchiveName(archiveName); relationship.setArchiveVersion(archiveVersion); relationship.setElementId(elementId); relationship.setWorkspace(AlienConstants.GLOBAL_WORKSPACE_ID); for (List<String> propertyObject : properties.raw()) { if (propertyObject.get(0).equals("validSource")) { relationship.setValidSources(propertyObject.get(1).split(",")); } else if (propertyObject.get(0).equals("validTarget")) { relationship.setValidTargets(propertyObject.get(1).split(",")); } else if (propertyObject.get(0).equals("abstract")) { relationship.setAbstract(Boolean.valueOf(propertyObject.get(1))); } } esClient.prepareIndex(ElasticSearchDAO.TOSCA_ELEMENT_INDEX, MappingBuilder.indexTypeFromClass(RelationshipType.class)) .setSource(JsonUtil.toString(relationship)).setRefresh(true).execute().actionGet(); }
@Override protected void onPersist(EntityMetadata entityMetadata, Object entity, Object id, List<RelationHolder> rlHolders) { try { Map<String, Object> values = new HashMap<String, Object>(); MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata() .getMetamodel(entityMetadata.getPersistenceUnit()); EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz()); String keyAsString = getKeyAsString(id, entityMetadata, metaModel); addSource(entity, values, entityType); addRelations(rlHolders, values); addDiscriminator(values, entityType); IndexResponse response = txClient .prepareIndex(entityMetadata.getSchema().toLowerCase(), entityMetadata.getTableName(), keyAsString) .setSource(values).setRefresh(isRefreshIndexes()).execute().actionGet(); assert response.getId() != null; } finally { // Nothing as of now. } }
@Override public void createUser(final String username, final String password, final String[] roles, final ActionListener<Void> listener) { try { final XContentBuilder builder = jsonBuilder() // .startObject() // .field("username", username) // .field("password", hashPassword(password)) // .field("roles", roles) // .endObject(); client.prepareIndex(authIndex, userType, getUserId(username)) .setSource(builder).setRefresh(true) .execute(new ActionListener<IndexResponse>() { @Override public void onResponse(final IndexResponse response) { listener.onResponse(null); } @Override public void onFailure(final Throwable e) { listener.onFailure(new AuthException( RestStatus.INTERNAL_SERVER_ERROR, "Could not create " + username, e)); } }); } catch (final Exception e) { listener.onFailure(new AuthException( RestStatus.INTERNAL_SERVER_ERROR, "Could not create " + username, e)); } }
private void createOneIndexNodeType(String elementId, String archiveVersion, List<CapabilityDefinition> capabilities, boolean refresh) throws IOException, IndexingServiceException { NodeType indexedNodeType = new NodeType(); indexedNodeType.setElementId(elementId); indexedNodeType.setArchiveVersion(archiveVersion); indexedNodeType.setCapabilities(capabilities); indexedNodeType.setWorkspace(AlienConstants.GLOBAL_WORKSPACE_ID); String typeName = MappingBuilder.indexTypeFromClass(NodeType.class); String serializeDatum = jsonMapper.writeValueAsString(indexedNodeType); esClient.prepareIndex(ElasticSearchDAO.TOSCA_ELEMENT_INDEX, typeName).setSource(serializeDatum).setRefresh(refresh).execute().actionGet(); } }
long version) { IndexRequestBuilder req = esTransportClient.prepareIndex(index, type, id).setSource(data).setOpType(opType) .setConsistencyLevel(WriteConsistencyLevel.DEFAULT).setRefresh(true); if (routingFlag) { req = req.setRouting(routingId);
@Given("^I create a \"([^\"]*)\" \"([^\"]*)\" in an archive name \"([^\"]*)\" version \"([^\"]*)\"$") public void I_create_a_in_an_archive_name_version(String componentType, String elementId, String archiveName, String archiveVersion) throws Throwable { AbstractInheritableToscaType element = new AbstractInheritableToscaType(); element.setAbstract(false); element.setElementId(elementId); element.setArchiveName(archiveName); element.setArchiveVersion(archiveVersion); element.setWorkspace(AlienConstants.GLOBAL_WORKSPACE_ID); Class<?> clazz = null; if (componentType.equals("capability") || componentType.equals("capabilities")) { clazz = CapabilityType.class; } else { throw new PendingException("creation of Type " + componentType + "not supported!"); } esClient.prepareIndex(ElasticSearchDAO.TOSCA_ELEMENT_INDEX, MappingBuilder.indexTypeFromClass(clazz)).setSource(JsonUtil.toString(element)) .setRefresh(true).execute().actionGet(); }
client.prepareIndex("test", BatchPercolatorService.TYPE_NAME, "kuku") .setSource(getSource(termQuery("field1", "value1"))) .setRefresh(true) .execute().actionGet();
if (options.getParent() != null) builder.setParent(options.getParent()); if (options.getOpType() != null) builder.setOpType(options.getOpType()); if (options.isRefresh() != null) builder.setRefresh(options.isRefresh()); if (options.getConsistencyLevel() != null) builder.setConsistencyLevel(options.getConsistencyLevel()); if (options.getVersion() != null) builder.setVersion(options.getVersion());