use of org.elasticsearch.common.document.DocumentField in project spring-data-elasticsearch by spring-projects.
the class DocumentAdaptersUnitTests method searchResponseShouldReturnContainsValue.
// DATAES-628
@Test
public void searchResponseShouldReturnContainsValue() {
Map<String, DocumentField> fields = new LinkedHashMap<>();
fields.put("string", new DocumentField("string", Collections.singletonList("value")));
fields.put("bool", new DocumentField("bool", Arrays.asList(true, true, false)));
fields.put("null", new DocumentField("null", Collections.emptyList()));
SearchHit searchHit = new SearchHit(123, "my-id", new Text("type"), fields, null);
SearchDocument document = DocumentAdapters.from(searchHit);
assertThat(document.containsValue("value")).isTrue();
assertThat(document.containsValue(Arrays.asList(true, true, false))).isTrue();
assertThat(document.containsValue(null)).isTrue();
}
use of org.elasticsearch.common.document.DocumentField in project trino by trinodb.
the class TimestampDecoder method decode.
@Override
public void decode(SearchHit hit, Supplier<Object> getter, BlockBuilder output) {
DocumentField documentField = hit.getFields().get(path);
Object value;
if (documentField != null) {
if (documentField.getValues().size() > 1) {
throw new TrinoException(TYPE_MISMATCH, format("Expected single value for column '%s', found: %s", path, documentField.getValues().size()));
}
value = documentField.getValue();
} else {
value = getter.get();
}
if (value == null) {
output.appendNull();
} else {
LocalDateTime timestamp;
if (value instanceof String) {
String valueString = (String) value;
Long epochMillis = Longs.tryParse(valueString);
if (epochMillis != null) {
timestamp = LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), UTC);
} else {
timestamp = ISO_DATE_TIME.parse(valueString, LocalDateTime::from);
}
} else if (value instanceof Number) {
timestamp = LocalDateTime.ofInstant(Instant.ofEpochMilli(((Number) value).longValue()), UTC);
} else {
throw new TrinoException(NOT_SUPPORTED, format("Unsupported representation for field '%s' of type TIMESTAMP: %s [%s]", path, value, value.getClass().getSimpleName()));
}
long epochMicros = timestamp.atOffset(UTC).toInstant().toEpochMilli() * MICROSECONDS_PER_MILLISECOND;
TIMESTAMP_MILLIS.writeLong(output, epochMicros);
}
}
use of org.elasticsearch.common.document.DocumentField in project meveo by meveo-org.
the class CustomTableService method search.
/**
* Execute a search on given fields for given query values. See ElasticClient.search() for a query format.
*
* @param cetCodeOrTablename Custom entity template code, or custom table name to query
* @param queryValues Fields and values to match
* @param from Pagination - starting record. Defaults to 0.
* @param size Pagination - number of records per page. Defaults to ElasticClient.DEFAULT_SEARCH_PAGE_SIZE.
* @param sortFields - Fields to sort by. If omitted, will sort by score. If search query contains a 'closestMatch' expression, sortFields and sortOrder will be overwritten
* with a corresponding field and descending order.
* @param sortOrders Sorting orders
* @param returnFields Return only certain fields - see Elastic Search documentation for details
* @return Search result
* @throws BusinessException General business exception
*/
public List<Map<String, Object>> search(String cetCodeOrTablename, Map<String, Object> queryValues, Integer from, Integer size, String[] sortFields, SortOrder[] sortOrders, String[] returnFields) throws BusinessException {
ElasticSearchClassInfo classInfo = new ElasticSearchClassInfo(CustomTableRecord.class, cetCodeOrTablename);
SearchResponse searchResult = elasticClient.search(queryValues, from, size, sortFields, sortOrders, returnFields, Collections.singletonList(classInfo));
if (searchResult == null) {
return new ArrayList<>();
}
List<Map<String, Object>> responseValues = new ArrayList<>();
searchResult.getHits().forEach(hit -> {
Map<String, Object> values = new HashMap<>();
responseValues.add(values);
if (hit.getFields() != null && !hit.getFields().values().isEmpty()) {
for (DocumentField field : hit.getFields().values()) {
if (field.getValues() != null) {
if (field.getValues().size() > 1) {
values.put(field.getName(), field.getValues());
} else {
values.put(field.getName(), field.getValue());
}
}
}
} else if (hit.getSourceAsMap() != null) {
values.putAll(hit.getSourceAsMap());
}
});
// log.debug("AKK ES search result values are {}", responseValues);
return responseValues;
}
use of org.elasticsearch.common.document.DocumentField in project presto by prestodb.
the class TimestampDecoder method decode.
@Override
public void decode(SearchHit hit, Supplier<Object> getter, BlockBuilder output) {
DocumentField documentField = hit.getFields().get(path);
Object value = null;
if (documentField != null) {
if (documentField.getValues().size() > 1) {
throw new PrestoException(ELASTICSEARCH_TYPE_MISMATCH, format("Expected single value for column '%s', found: %s", path, documentField.getValues().size()));
}
value = documentField.getValue();
} else {
value = getter.get();
}
if (value == null) {
output.appendNull();
} else {
LocalDateTime timestamp;
if (value instanceof String) {
timestamp = ISO_DATE_TIME.parse((String) value, LocalDateTime::from);
} else if (value instanceof Number) {
timestamp = LocalDateTime.ofInstant(Instant.ofEpochMilli(((Number) value).longValue()), ZULU);
} else {
throw new PrestoException(NOT_SUPPORTED, format("Unsupported representation for field '%s' of type TIMESTAMP: %s [%s]", path, value.getClass().getSimpleName(), value));
}
long epochMillis = timestamp.atZone(zoneId).toInstant().toEpochMilli();
TIMESTAMP.writeLong(output, epochMillis);
}
}
use of org.elasticsearch.common.document.DocumentField in project vertexium by visallo.
the class IdStrategy method extendedDataRowIdFromSearchHit.
public ExtendedDataRowId extendedDataRowIdFromSearchHit(SearchHit hit) {
DocumentField elementTypeField = hit.getFields().get(Elasticsearch7SearchIndex.ELEMENT_TYPE_FIELD_NAME);
if (elementTypeField == null) {
throw new VertexiumException("Could not find field: " + Elasticsearch7SearchIndex.ELEMENT_TYPE_FIELD_NAME);
}
ElementType elementType = ElasticsearchDocumentType.parse(elementTypeField.getValue().toString()).toElementType();
DocumentField elementIdField = hit.getFields().get(Elasticsearch7SearchIndex.ELEMENT_ID_FIELD_NAME);
if (elementIdField == null) {
throw new VertexiumException("Could not find field: " + Elasticsearch7SearchIndex.ELEMENT_ID_FIELD_NAME);
}
String elementId = elementIdField.getValue();
DocumentField tableNameField = hit.getFields().get(Elasticsearch7SearchIndex.EXTENDED_DATA_TABLE_NAME_FIELD_NAME);
if (tableNameField == null) {
throw new VertexiumException("Could not find field: " + Elasticsearch7SearchIndex.EXTENDED_DATA_TABLE_NAME_FIELD_NAME);
}
String tableName = tableNameField.getValue();
DocumentField rowIdField = hit.getFields().get(Elasticsearch7SearchIndex.EXTENDED_DATA_TABLE_ROW_ID_FIELD_NAME);
if (rowIdField == null) {
throw new VertexiumException("Could not find field: " + Elasticsearch7SearchIndex.EXTENDED_DATA_TABLE_ROW_ID_FIELD_NAME);
}
String rowId = rowIdField.getValue();
return new ExtendedDataRowId(elementType, elementId, tableName, rowId);
}
Aggregations