Java Code Examples for org.elasticsearch.action.get.GetResponse#isSourceEmpty()

The following examples show how to use org.elasticsearch.action.get.GetResponse#isSourceEmpty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TraceServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 6 votes vote down vote up
@Override
public Trace getFragment(String tenantId, String id) {
    Trace ret = null;

    GetResponse response = client.getClient().prepareGet(
            client.getIndex(tenantId), TRACE_TYPE, id).setRouting(id)
            .execute()
            .actionGet();
    if (!response.isSourceEmpty()) {
        try {
            ret = mapper.readValue(response.getSourceAsString(), Trace.class);
        } catch (Exception e) {
            msgLog.errorFailedToParse(e);
        }
    }

    if (msgLog.isTraceEnabled()) {
        msgLog.tracef("Get fragment with id[%s] is: %s", id, ret);
    }

    return ret;
}
 
Example 2
Source File: ElasticSearchRepository.java    From elastic-crud with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public List<T> findAll(final List<String> ids) {
  if (ids.isEmpty()) {
    return ImmutableList.of();
  }

  final Builder<T> builder = ImmutableList.builder();

  final MultiGetResponse response = client
    .prepareMultiGet()
    .add(index, type, ids)
    .execute()
    .actionGet();

  for(final MultiGetItemResponse item : response.getResponses()) {
    final GetResponse get = item.getResponse();
    if(get.isSourceEmpty()) {
      continue;
    }

    final String json = get.getSourceAsString();
    final T entity = deserializer.apply(json);
    builder.add((T) entity.withId(get.getId()));
  }

  return builder.build();
}
 
Example 3
Source File: ElasticsearchHistory.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected ElasticsearchDocumentHistory loadExistingDocumentHistory(String documentId)
    throws BaleenException {
  try {
    GetResponse response =
        new GetRequestBuilder(elasticsearch.getClient(), GetAction.INSTANCE)
            .setId(documentId)
            .setIndex(esIndex)
            .setType(esType)
            .get();

    if (!response.isExists() || response.isSourceEmpty()) {
      // If we don't have any data, then let parent implementation create a new history
      return null;
    } else {
      ESHistory esh = mapper.readValue(response.getSourceAsBytes(), ESHistory.class);
      if (esh == null) {
        return new ElasticsearchDocumentHistory(
            this, documentId, new LinkedBlockingDeque<HistoryEvent>(Collections.emptyList()));
      } else {
        return new ElasticsearchDocumentHistory(
            this, documentId, new LinkedBlockingDeque<HistoryEvent>(esh.getEvents()));
      }
    }
  } catch (IOException e) {
    throw new BaleenException(e);
  }
}
 
Example 4
Source File: DatumFromMetadataAsDocumentProcessor.java    From streams with Apache License 2.0 5 votes vote down vote up
@Override
public List<StreamsDatum> process(StreamsDatum entry) {
  List<StreamsDatum> result = new ArrayList<>();

  ObjectNode metadataObjectNode;
  try {
    metadataObjectNode = mapper.readValue((String) entry.getDocument(), ObjectNode.class);
  } catch (IOException ex) {
    return result;
  }

  Map<String, Object> metadata = ElasticsearchMetadataUtil.asMap(metadataObjectNode);

  if (entry.getMetadata() == null) {
    return result;
  }

  String index = ElasticsearchMetadataUtil.getIndex(metadata, config);
  String type = ElasticsearchMetadataUtil.getType(metadata, config);
  String id = ElasticsearchMetadataUtil.getId(metadata);

  GetRequestBuilder getRequestBuilder = elasticsearchClientManager.client().prepareGet(index, type, id);
  getRequestBuilder.setFields("*", "_timestamp");
  getRequestBuilder.setFetchSource(true);
  GetResponse getResponse = getRequestBuilder.get();

  if ( getResponse == null || !getResponse.isExists() || getResponse.isSourceEmpty()) {
    return result;
  }

  entry.setDocument(getResponse.getSource());
  if ( getResponse.getField("_timestamp") != null) {
    DateTime timestamp = new DateTime(((Long) getResponse.getField("_timestamp").getValue()).longValue());
    entry.setTimestamp(timestamp);
  }

  result.add(entry);

  return result;
}
 
Example 5
Source File: DatumFromMetadataProcessor.java    From streams with Apache License 2.0 5 votes vote down vote up
@Override
public List<StreamsDatum> process(StreamsDatum entry) {
  List<StreamsDatum> result = new ArrayList<>();

  if (entry == null || entry.getMetadata() == null) {
    return result;
  }

  Map<String, Object> metadata = entry.getMetadata();

  String index = ElasticsearchMetadataUtil.getIndex(metadata, config);
  String type = ElasticsearchMetadataUtil.getType(metadata, config);
  String id = ElasticsearchMetadataUtil.getId(entry);

  GetRequestBuilder getRequestBuilder = elasticsearchClientManager.client().prepareGet(index, type, id);
  getRequestBuilder.setFields("*", "_timestamp");
  getRequestBuilder.setFetchSource(true);
  GetResponse getResponse = getRequestBuilder.get();

  if ( getResponse == null || !getResponse.isExists() || getResponse.isSourceEmpty() ) {
    return result;
  }

  entry.setDocument(getResponse.getSource());
  if ( getResponse.getField("_timestamp") != null) {
    DateTime timestamp = new DateTime(((Long) getResponse.getField("_timestamp").getValue()).longValue());
    entry.setTimestamp(timestamp);
  }

  result.add(entry);

  return result;
}
 
Example 6
Source File: ElasticsearchStateManager.java    From elasticsearch-imap with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized State getRiverState(final Folder folder) throws MessagingException {

    try {

        waitForCluster();

        if (client.admin().indices().prepareExists(index()).execute().actionGet().isExists()) {

            final GetResponse response = client
                    .prepareGet(index(), RIVERSTATE_TYPE, FOLDERSTATE_ID + "_" + folder.getURLName().toString().hashCode()).execute()
                    .get();

            if (!response.isSourceEmpty()) {
                return mapper.readValue(response.getSourceAsString(), new TypeReference<State>() {
                });

            }
        }
    } catch (final Exception ex) {
        throw new MessagingException("Unable to get river state", ex);
    }

    final State rs = new State();
    rs.setFolderUrl(folder.getURLName().toString());
    // rs.setLastUid(1L);
    rs.setExists(true);
    return rs;

}
 
Example 7
Source File: ConfigurationServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 4 votes vote down vote up
@Override
public TransactionConfig getTransaction(String tenantId, String name) {
    TransactionConfig ret = null;

    if (msgLog.isTraceEnabled()) {
        msgLog.tracef("Get transaction config with name[%s]", name);
    }

    try {
        String index = client.getIndex(tenantId);

        RefreshRequestBuilder refreshRequestBuilder =
                client.getClient().admin().indices().prepareRefresh(index);
        client.getClient().admin().indices().refresh(refreshRequestBuilder.request()).actionGet();

        // First check if an invalid config exists
        GetResponse response = client.getClient().prepareGet(
                index, TXN_CONFIG_INVALID_TYPE, name).setRouting(name)
                .execute()
                .actionGet();

        if (response.isSourceEmpty()) {
            // Retrieve the valid configuration
            response = client.getClient().prepareGet(
                    index, TXN_CONFIG_TYPE, name).setRouting(name)
                    .execute()
                    .actionGet();
        }

        if (!response.isSourceEmpty()) {
            try {
                ret = mapper.readValue(response.getSourceAsString(), TransactionConfig.class);

                // Check if config was deleted
                if (ret.isDeleted()) {
                    ret = null;
                }
            } catch (Exception e) {
                msgLog.errorFailedToParse(e);
            }
        }

    } catch (org.elasticsearch.indices.IndexMissingException t) {
        // Ignore, as means that no transaction configurations have
        // been stored yet
        if (msgLog.isTraceEnabled()) {
            msgLog.tracef("No index found, so unable to retrieve transaction config [%s]", name);
        }
    }

    if (msgLog.isTraceEnabled()) {
        msgLog.tracef("Get transaction config with name[%s] is: %s", name, ret);
    }

    return ret;
}