
org.jnosql.diana.elasticsearch.document.ElasticsearchEntry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch-driver Show documentation
Show all versions of elasticsearch-driver Show documentation
The Eclipse JNoSQL communication layer, Diana, implementation to Eclasticsearch
The newest version!
/*
* Copyright (c) 2017 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.jnosql.diana.elasticsearch.document;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.search.SearchHit;
import org.jnosql.diana.api.document.Document;
import org.jnosql.diana.api.document.DocumentEntity;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.util.Objects.isNull;
import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;
import static org.jnosql.diana.elasticsearch.document.EntityConverter.ID_FIELD;
class ElasticsearchEntry {
private final String id;
private final Map map;
private final String collection;
private static final Function, Document> ENTRY_DOCUMENT = entry ->
Document.of(entry.getKey().toString(), entry.getValue());
private ElasticsearchEntry(String id, String collection, Map map) {
this.id = id;
this.collection = collection;
this.map = map;
}
boolean isEmpty() {
return isNull(id) || isNull(collection) || isNull(map);
}
boolean isNotEmpty() {
return !isEmpty();
}
DocumentEntity toEntity() {
Document id = Document.of(ID_FIELD, this.id);
List documents = map.keySet().stream()
.map(k -> toDocument(k, map))
.collect(Collectors.toList());
DocumentEntity entity = DocumentEntity.of(collection, documents);
entity.remove(ID_FIELD);
entity.add(id);
return entity;
}
private Document toDocument(String key, Map properties) {
Object value = properties.get(key);
if (Map.class.isInstance(value)) {
Map map = Map.class.cast(value);
return Document.of(key, map.keySet()
.stream().map(k -> toDocument(k.toString(), map))
.collect(Collectors.toList()));
}
if (isADocumentIterable(value)) {
List> documents = new ArrayList<>();
for (Object object : Iterable.class.cast(value)) {
Map, ?> map = Map.class.cast(object);
documents.add(map.entrySet().stream().map(ENTRY_DOCUMENT).collect(toList()));
}
return Document.of(key, documents);
}
return Document.of(key, value);
}
private boolean isADocumentIterable(Object value) {
return Iterable.class.isInstance(value) &&
stream(Iterable.class.cast(value).spliterator(), false)
.allMatch(Map.class::isInstance);
}
static ElasticsearchEntry of(SearchHit searchHit) {
return new ElasticsearchEntry(searchHit.getId(),
searchHit.getType(),
searchHit.getSourceAsMap());
}
static ElasticsearchEntry of(GetResponse searchHit) {
return new ElasticsearchEntry(searchHit.getId(),
searchHit.getType(),
searchHit.getSourceAsMap());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy