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

The following examples show how to use org.elasticsearch.action.get.GetResponse#getField() . 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: MinHashPluginTest.java    From elasticsearch-minhash with Apache License 2.0 6 votes vote down vote up
private void test_get(final Client client, final String index,
        final String type, final String id, final byte[] hash1,
        final byte[] hash2, final byte[] hash3) {
    final GetResponse response = client.prepareGet(index, type, id)
            .setStoredFields(new String[] { "_source", "minhash_value1", "minhash_value2", "minhash_value3" }).execute()
            .actionGet();
    assertTrue(response.isExists());
    final Map<String, Object> source = response.getSourceAsMap();
    assertEquals("test " + Integer.parseInt(id) % 100, source.get("msg"));

    final DocumentField field1 = response.getField("minhash_value1");
    final BytesArray value1 = (BytesArray) field1.getValue();
    assertEquals(hash1.length, value1.length());
    Assert.assertArrayEquals(hash1, value1.array());

    final DocumentField field2 = response.getField("minhash_value2");
    final BytesArray value2 = (BytesArray) field2.getValue();
    assertEquals(hash2.length, value2.length());
    Assert.assertArrayEquals(hash2, value2.array());

    final DocumentField field3 = response.getField("minhash_value3");
    final BytesArray value3 = (BytesArray) field3.getValue();
    assertEquals(hash3.length, value3.length());
    Assert.assertArrayEquals(hash3, value3.array());
}
 
Example 2
Source File: ESIndex.java    From pyramid with Apache License 2.0 6 votes vote down vote up
public List<Object> getListField(String id, String field){
    GetResponse response = client.prepareGet(this.indexName, this.documentType, id).
            setStoredFields(field)
            .execute()
            .actionGet();
    if (response==null){
        if (logger.isWarnEnabled()){
            logger.warn("no response from document "+id+" when fetching field "+field+"!");
        }
        return new ArrayList<>();
    }else if (response.getField(field)==null){
        if (logger.isWarnEnabled()) {
            logger.warn("document " + id + " has no field " + field + "!");
        }
        return new ArrayList<>();
    }
    return response.getField(field).getValues();
}
 
Example 3
Source File: ProductQueryServiceImpl.java    From elasticsearch-tutorial with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Product getProduct(ElasticSearchIndexConfig config, Long productId)
{
    GetResponse getResponse = searchClientService.getClient().prepareGet(config.getIndexAliasName(), config.getDocumentType(), String.valueOf(productId))
                                                             .setFields(SearchDocumentFieldName.productDocumentFields)
                                                             .get();
    if(getResponse.isExists())
    {
        Product product = new Product();
        product.setId(Long.valueOf(getResponse.getId()));
        product.setTitle(getResponse.getField(SearchDocumentFieldName.TITLE.getFieldName()).getValue().toString());
        product.setDescription(getResponse.getField(SearchDocumentFieldName.DESCRIPTION.getFieldName()).getValue().toString());
        product.setSoldOut(Boolean.valueOf(getResponse.getField(SearchDocumentFieldName.SOLD_OUT.getFieldName()).getValue().toString()));
        product.setAvailableOn(SearchDateUtils.getFormattedDate(getResponse.getField(SearchDocumentFieldName.AVAILABLE_DATE.getFieldName()).getValue().toString()));
        product.setKeywords(getListFieldValueOrNull(getResponse.getField(SearchDocumentFieldName.KEYWORDS.getFieldName())));
        product.setPrice(BigDecimal.valueOf(Double.valueOf(getResponse.getField(SearchDocumentFieldName.PRICE.getFieldName()).getValue().toString())));
        product.setBoostFactor(Float.valueOf(getResponse.getField(SearchDocumentFieldName.BOOSTFACTOR.getFieldName()).getValue().toString()));
        GetField catField = getResponse.getField(SearchDocumentFieldName.CATEGORIES_ARRAY.getFieldName());
        if(catField !=null)
        {
            for (Object ListOfMapValues : catField.getValues())
            {
                for (final java.util.Map.Entry<String, String> entry : ((Map<String, String>)ListOfMapValues).entrySet())
                {
                    //Only main facet values should be set.key ending with a number.
                    if(entry.getKey().matches("^.*.facet$"))
                    {
                        product.addCategory(new Category(entry.getValue(), null, entry.getKey().split("\\.facet")[0]));
                    }
                }
            }
        }
        
        return product;
    }
    return null;
}
 
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: ProductQueryServiceImpl.java    From searchanalytics-bigdata with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Product getProduct(final ElasticSearchIndexConfig config,
		final Long productId) {
	final GetResponse getResponse = searchClientService
			.getClient()
			.prepareGet(config.getIndexAliasName(),
					config.getDocumentType(), String.valueOf(productId))
			.setFields(SearchDocumentFieldName.productDocumentFields).get();
	if (getResponse.isExists()) {
		final Product product = new Product();
		product.setId(Long.valueOf(getResponse.getId()));
		product.setTitle(getResponse
				.getField(SearchDocumentFieldName.TITLE.getFieldName())
				.getValue().toString());
		product.setDescription(getResponse
				.getField(
						SearchDocumentFieldName.DESCRIPTION.getFieldName())
				.getValue().toString());
		product.setSoldOut(Boolean.valueOf(getResponse
				.getField(SearchDocumentFieldName.SOLD_OUT.getFieldName())
				.getValue().toString()));
		product.setAvailableOn(SearchDateUtils.getFormattedDate(getResponse
				.getField(
						SearchDocumentFieldName.AVAILABLE_DATE
								.getFieldName()).getValue().toString()));
		product.setKeywords(getListFieldValueOrNull(getResponse
				.getField(SearchDocumentFieldName.KEYWORDS.getFieldName())));
		product.setPrice(BigDecimal.valueOf(Double.valueOf(getResponse
				.getField(SearchDocumentFieldName.PRICE.getFieldName())
				.getValue().toString())));
		product.setBoostFactor(Float.valueOf(getResponse
				.getField(
						SearchDocumentFieldName.BOOSTFACTOR.getFieldName())
				.getValue().toString()));
		final GetField catField = getResponse
				.getField(SearchDocumentFieldName.CATEGORIES_ARRAY
						.getFieldName());
		if (catField != null) {
			for (final Object ListOfMapValues : catField.getValues()) {
				for (final java.util.Map.Entry<String, String> entry : ((Map<String, String>) ListOfMapValues)
						.entrySet()) {
					// Only main facet values should be set.key ending with
					// a number.
					if (entry.getKey().matches("^.*.facet$")) {
						product.addCategory(new Category(entry.getValue(),
								null, entry.getKey().split("\\.facet")[0]));
					}
				}
			}
		}
		return product;
	}
	return null;
}
 
Example 7
Source File: ElasticsearchMailDestination.java    From elasticsearch-imap with Apache License 2.0 4 votes vote down vote up
@Override
public int getFlaghashcode(final String id) throws IOException, MessagingException {

    createIndexIfNotExists();
    
    client.admin().indices().refresh(new RefreshRequest()).actionGet();

    final GetResponse getResponse = client.prepareGet().setIndex(index).setType(type).setId(id)
            .setFields(new String[] { "flaghashcode" }).execute().actionGet();

    if (getResponse == null || !getResponse.isExists()) {
        return -1;
    }

    final GetField flaghashcodeField = getResponse.getField("flaghashcode");

    if (flaghashcodeField == null || flaghashcodeField.getValue() == null || !(flaghashcodeField.getValue() instanceof Number)) {
        throw new IOException("No flaghashcode field for id " + id+ " ("+(flaghashcodeField==null?"null":"Val: "+flaghashcodeField.getValue())+")");
    }

    return ((Number) flaghashcodeField.getValue()).intValue();

}