org.eclipse.jnosql.databases.elasticsearch.communication.ElasticsearchEntry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jnosql-elasticsearch Show documentation
Show all versions of jnosql-elasticsearch Show documentation
The Eclipse JNoSQL layer to Elasticsearch
The newest version!
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* 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
* Maximillian Arruda
*/
package org.eclipse.jnosql.databases.elasticsearch.communication;
import org.eclipse.jnosql.communication.semistructured.CommunicationEntity;
import org.eclipse.jnosql.communication.semistructured.Element;
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;
class ElasticsearchEntry {
private final String id;
private final Map map;
private final String collection;
private static final Function, Element> ENTRY_DOCUMENT = entry ->
Element.of(entry.getKey().toString(), entry.getValue());
private ElasticsearchEntry(String id, Map map) {
this.id = id;
this.collection = map == null ? null : map.getOrDefault(EntityConverter.ENTITY, "_doc").toString();
this.map = map;
}
boolean isEmpty() {
return isNull(id) || isNull(collection) || isNull(map);
}
boolean isNotEmpty() {
return !isEmpty();
}
CommunicationEntity toEntity() {
var id = Element.of(EntityConverter.ID_FIELD, this.id);
List documents = map.keySet().stream()
.map(k -> toDocument(k, map))
.toList();
CommunicationEntity entity = CommunicationEntity.of(collection);
documents.forEach(d-> entity.add(d.name(),d.value()));
entity.remove(EntityConverter.ID_FIELD);
entity.remove(EntityConverter.ENTITY);
entity.add(id);
return entity;
}
private Element toDocument(String key, Map properties) {
Object value = properties.get(key);
if (Map.class.isInstance(value)) {
Map map = Map.class.cast(value);
return Element.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 Element.of(key, documents);
}
return Element.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(String id, Map data) {
return new ElasticsearchEntry(id, data);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy