Search in sources :

Example 16 with DocumentField

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();
}
Also used : DocumentField(org.elasticsearch.common.document.DocumentField) SearchHit(org.elasticsearch.search.SearchHit) Text(org.elasticsearch.common.text.Text) LinkedHashMap(java.util.LinkedHashMap) SearchDocument(org.springframework.data.elasticsearch.core.document.SearchDocument) Test(org.junit.jupiter.api.Test)

Example 17 with DocumentField

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);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) DocumentField(org.elasticsearch.common.document.DocumentField) TrinoException(io.trino.spi.TrinoException)

Example 18 with DocumentField

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;
}
Also used : DocumentField(org.elasticsearch.common.document.DocumentField) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ElasticSearchClassInfo(org.meveo.service.index.ElasticSearchClassInfo) CustomModelObject(org.meveo.model.customEntities.CustomModelObject) Map(java.util.Map) HashMap(java.util.HashMap) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 19 with DocumentField

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);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) DocumentField(org.elasticsearch.common.document.DocumentField) PrestoException(com.facebook.presto.spi.PrestoException)

Example 20 with DocumentField

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);
}
Also used : DocumentField(org.elasticsearch.common.document.DocumentField)

Aggregations

DocumentField (org.elasticsearch.common.document.DocumentField)20 SearchHit (org.elasticsearch.search.SearchHit)8 Test (org.junit.jupiter.api.Test)6 SearchDocument (org.springframework.data.elasticsearch.core.document.SearchDocument)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 Text (org.elasticsearch.common.text.Text)5 LinkedHashMap (java.util.LinkedHashMap)4 LocalDateTime (java.time.LocalDateTime)3 HashMap (java.util.HashMap)3 GetResponse (org.elasticsearch.action.get.GetResponse)3 PrestoException (com.facebook.presto.spi.PrestoException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 GetResult (org.elasticsearch.index.get.GetResult)2 SearchHits (org.elasticsearch.search.SearchHits)2 Document (org.springframework.data.elasticsearch.core.document.Document)2 TrinoException (io.trino.spi.TrinoException)1 IOException (java.io.IOException)1 List (java.util.List)1 MessageSearchIndex (org.apache.james.mailbox.store.search.MessageSearchIndex)1