@Override public IndexResponse get() { Profiler profiler = Profiler.createIfTrace(EsClient.LOGGER).start(); try { return super.execute().actionGet(); } catch (Exception e) { throw new IllegalStateException(String.format("Fail to execute %s", toString()), e); } finally { if (profiler.isTraceEnabled()) { profiler.stopTrace(toString()); } } }
/** * Insert a record in the database. Any field/value pairs in the specified * values HashMap will be written into the record with the specified record * key. * * @param table * The name of the table * @param key * The record key of the record to insert. * @param values * A HashMap of field/value pairs to insert in the record * @return Zero on success, a non-zero error code on error. See this class's * description for a discussion of error codes. */ @Override public Status insert(String table, String key, Map<String, ByteIterator> values) { try { final XContentBuilder doc = jsonBuilder().startObject(); for (Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) { doc.field(entry.getKey(), entry.getValue()); } doc.endObject(); client.prepareIndex(indexKey, table, key).setSource(doc).execute().actionGet(); return Status.OK; } catch (Exception e) { e.printStackTrace(); return Status.ERROR; } }
client.prepareIndex(indexKey, table, key).setSource(response.getSource()).execute().actionGet();
.setVersion(version == null ? 1 : version.longValue()) .setVersionType(version == null ? VersionType.FORCE : VersionType.EXTERNAL) .execute() .actionGet(); if (version != null) jsonMap.put("_version", version); // to prevent side effects
@Test public void do_not_support_execute_method() { try { es.client().prepareIndex(FakeIndexDefinition.INDEX_TYPE_FAKE).execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); } }
@Override public void execute(Tuple tuple) { try { String index = mapper.getIndex(tuple); String type = mapper.getType(tuple); String id = mapper.getId(tuple); String source = mapper.getSource(tuple); OpType opType = mapper.getOpType(); client.prepareIndex(index, type).setId(id).setSource(source) .setOpType(opType).execute(); collector.ack(tuple); } catch (Exception e) { collector.fail(tuple); } }
private void doStoreResult(Iterator<TimeValue> backoff, IndexRequestBuilder index, ActionListener<Void> listener) { index.execute(new ActionListener<IndexResponse>() { @Override public void onResponse(IndexResponse indexResponse) { listener.onResponse(null); } @Override public void onFailure(Exception e) { if (false == (e instanceof EsRejectedExecutionException) || false == backoff.hasNext()) { listener.onFailure(e); } else { TimeValue wait = backoff.next(); logger.warn(() -> new ParameterizedMessage("failed to store task result, retrying in [{}]", wait), e); threadPool.schedule(wait, ThreadPool.Names.SAME, () -> doStoreResult(backoff, index, listener)); } } }); }
.setSource(json).execute(); IndexResponse response = listenableActionFuture.actionGet();
/** * Write a json document into the search index. * Writing using a XContentBuilder is the most efficient way to add content to elasticsearch * * @param jsonMap * the json document to be indexed in elasticsearch * @param id * the unique identifier of a document * @param indexName * the name of the index * @param typeName * the type of the index */ public IndexResponse writeSource(String indexName, XContentBuilder json, String id, String typeName, long version, VersionType versionType) { // put this to the index IndexResponse r = elasticsearchClient.prepareIndex(indexName, typeName, id).setSource(json) .setVersion(version).setVersionType(versionType).execute() .actionGet(); // documentation about the versioning is available at // https://www.elastic.co/blog/elasticsearch-versioning-support // TODO: error handling return r; }
@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. } }
private void insertIndexes(List<IndexRequestBuilder> indexes, Map<String, Object> dataOfDocument) { for (IndexRequestBuilder indexRequestBuilder : indexes) { indexRequestBuilder.setSource(dataOfDocument).execute().actionGet(); } }
private void insertIndexes(List<IndexRequestBuilder> indexes, Map<String, Object> dataOfDocument) { for (IndexRequestBuilder indexRequestBuilder : indexes) { indexRequestBuilder.setSource(dataOfDocument).execute().actionGet(); } }
public void index(String index, String type, long id, String source) { client.prepareIndex(index, type, Long.toString(id)) .setSource(source) .execute().actionGet(); }
@Override public boolean add(String indexName, String type, String id, String json) { IndexResponse response = client.prepareIndex(indexName, type, id).setSource(json, XContentType.JSON).execute().actionGet(); return true; }
/** Add the document (provided as JSON) to Elasticsearch, using the id provided. */ public void addDocument(String id, Map<String, Object> json) { try { esResource.getClient().prepareIndex(index, type, id).setSource(json).execute().actionGet(); } catch (ElasticsearchException ee) { getMonitor().error("Couldn't persist document to Elasticsearch", ee); } }
/** Add the document (provided as JSON) to Elasticsearch, using the id provided. */ public void addDocument(String id, Map<String, Object> json) { try { esResource.getClient().prepareIndex(index, type, id).setSource(json).execute().actionGet(); } catch (ElasticsearchException ee) { getMonitor().error("Couldn't persist document to Elasticsearch", ee); } }
/** * Syntactic sugar for: * <pre> * return client().prepareIndex(index, type, id).setSource(source).execute().actionGet(); * </pre> * <p> * where source is a JSON String. */ protected final IndexResponse index(String index, String type, String id, String source) { return client().prepareIndex(index, type, id).setSource(source, XContentType.JSON).execute().actionGet(); }
@Override public void onError(final String errmsg, final Message msg, final Exception e) { try { logger.error("Message " + ((MimeMessage) msg).getMessageID() + " throws an error: " + errmsg + e, e); client.prepareIndex(index(), RIVERSTATE_TYPE, ERRORS_ID + "_" + ((MimeMessage) msg).getMessageID().hashCode()) .setSource(mapper.writeValueAsString(new IndexableError(((MimeMessage) msg).getMessageID(), null, errmsg + e))) .execute().actionGet(); } catch (final Exception ex) { logger.error("Unable to log an error because of " + ex + errmsg, 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(); } }
@Test public void testMixedQuery() throws IOException { this.init(); String input = "{\"name\":\"Radar Networks\",\"description\":\"Offers a personal recommendation engine\",\"location\":\"san francisco\",\"funding\":[{\"round\":\"a\",\"year\":2006,\"investor\":[\"ny-angels\"]},{\"round\":\"b\",\"year\":2007,\"investor\":[\"sequoia\"]},{\"round\":\"c\",\"year\":2009,\"investor\":[\"kleiner\"]}]}"; client().prepareIndex(INDEX_NAME, TYPE_NAME, "1").setSource(input).execute().actionGet(); flushAndRefresh(); String query = "{\"tree\":{\"boolean\":{\"slop\":1,\"clause\":[{\"twig\":{\"root\":\"funding\",\"child\":[{\"node\":{\"attribute\":\"investor\",\"query\":\"kleiner\"}}]}},{\"twig\":{\"root\":\"funding\",\"child\":[{\"node\":{\"attribute\":\"investor\",\"query\":\"sequoia\"}}]}}]}}}"; SearchResponse response = client().prepareSearch(INDEX_NAME).setTypes(TYPE_NAME).setQuery(query).get(); ElasticsearchAssertions.assertNoFailures(response); SearchHits hits = response.getHits(); assertThat(hits.getTotalHits(), equalTo(1l)); }