Verified Commit a7404b33 authored by Raphaël Flores's avatar Raphaël Flores
Browse files

Upgrade Elasticsearch to 6.8.x and Spring Data Elasticsearch accordingly. GNP-5494.

parent 8bb0470a
......@@ -114,7 +114,7 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.cloud:spring-cloud-starter-config")
implementation("org.springframework.data:spring-data-commons")
implementation("org.springframework.data:spring-data-elasticsearch:3.2.0.M1") // to replace by GA version in June 2019
implementation("org.springframework.data:spring-data-elasticsearch:3.2.0.RC2") // to replace by GA version in June 2019
implementation("org.elasticsearch:elasticsearch:${project.ext["ELASTICSEARCH_VERSION"]}")
implementation("org.elasticsearch.client:elasticsearch-rest-high-level-client:${project.ext["ELASTICSEARCH_VERSION"]}")
......
package fr.inra.urgi.datadiscovery.config;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.elasticsearch.core.EntityMapper;
import org.springframework.data.mapping.MappingException;
/**
* Re-implements the ElasticSearch entity mapper as the default one
......@@ -28,4 +31,34 @@ public class CustomElasticSearchEntityMapper implements EntityMapper {
public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
return objectMapper.readValue(source, clazz);
}
/*
* (non-Javadoc)
* @see org.springframework.data.elasticsearch.core.EntityMapper#mapObject(java.lang.Object)
*/
@SuppressWarnings("unchecked")
@Override
public Map<String, Object> mapObject(Object source) {
try {
return objectMapper.readValue(mapToString(source), HashMap.class);
} catch (IOException e) {
throw new MappingException(e.getMessage(), e);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.elasticsearch.core.EntityMapper#readObject(java.util.Map, java.lang.Class)
*/
@SuppressWarnings("unchecked")
@Override
public <T> T readObject (Map<String, Object> source, Class<T> targetType) {
try {
return mapToObject(mapToString(source), targetType);
} catch (IOException e) {
throw new MappingException(e.getMessage(), e);
}
}
}
package fr.inra.urgi.datadiscovery.dao;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import fr.inra.urgi.datadiscovery.domain.Document;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
......@@ -15,6 +11,10 @@ import org.springframework.data.elasticsearch.core.SearchResultMapper;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* A base class for highlight mappers. There is one subclass for each app (RARe, WheatIS, etc.)
* @author JB Nizet
......@@ -44,18 +44,9 @@ public abstract class AbstractDocumentHighlightMapper<D extends Document>
SearchHit hit = response.getHits().getAt(i);
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
String newDescription = document.getDescription();
boolean hightlightFound = false;
if (highlightFields != null) {
HighlightField descriptionHighlight = highlightFields.get("description");
if (descriptionHighlight != null && descriptionHighlight.getFragments().length == 1) {
newDescription = descriptionHighlight.getFragments()[0].string();
hightlightFound = true;
}
}
String newDescription = getHighlightedDescription(highlightFields);
if (hightlightFound) {
if (null != newDescription) {
D newDocument = clone(document, newDescription);
newContent.set(i, newDocument);
}
......@@ -68,6 +59,36 @@ public abstract class AbstractDocumentHighlightMapper<D extends Document>
page.getScrollId());
}
@SuppressWarnings("unchecked")
@Override
public <T> T mapSearchHit(SearchHit searchHit, Class<T> type) {
if (type != getDocumentClass()) {
throw new IllegalArgumentException("The only supported class is " + getDocumentClass());
}
T document = defaultResultMapper.mapEntity(searchHit.getSourceAsString(), type);
Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
String newDescription = getHighlightedDescription(highlightFields);
if (null != newDescription) {
D newDocument = clone((D) document, newDescription);
return (T) newDocument;
}
return (T) document;
}
private String getHighlightedDescription(Map<String, HighlightField> highlightFields) {
if (highlightFields != null) {
HighlightField descriptionHighlight = highlightFields.get("description");
if (descriptionHighlight != null && descriptionHighlight.getFragments().length == 1) {
return descriptionHighlight.getFragments()[0].string();
}
}
return null;
}
protected abstract Class<D> getDocumentClass();
protected abstract D clone(D original, String newDescription);
}
......
ELASTICSEARCH_VERSION=6.6.2
ELASTICSEARCH_VERSION=6.8.2
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment