Commit 8ee1c74b authored by Noel Alonso's avatar Noel Alonso
Browse files

Merge branch 'feature-parentChildIndex' into 'dev'

Feature parent child index

See merge request redmic-project/server/library/elasticsearch-lib!10
parents 64ec6e31 bee04248
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@
	<groupId>es.redmic.lib</groupId>
	<artifactId>elasticsearch-lib</artifactId>
	<packaging>jar</packaging>
	<version>0.10.0</version>
	<version>0.10.0-feature-parentChildIndex</version>
	<name>elasticsearch-lib</name>

	<properties>
+47 −0
Original line number Diff line number Diff line
package es.redmic.elasticsearchlib.common.model;

/*-
 * #%L
 * elasticsearch-lib
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

public class JoinIndex {

	public JoinIndex() {
	}

	private String name;

	private String parent;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getParent() {
		return parent;
	}

	public void setParent(String parent) {
		this.parent = parent;
	}
}
+8 −0
Original line number Diff line number Diff line
@@ -29,9 +29,17 @@ public interface IRWBaseESRepository<TModel extends BaseES<?>> {

	EventApplicationResult save(TModel modelToIndex);

	EventApplicationResult save(TModel modelToIndex, String parentId);

	EventApplicationResult update(TModel modelToIndex);

	EventApplicationResult update(TModel modelToIndex, String parentId);

	EventApplicationResult update(String id, XContentBuilder doc);

	EventApplicationResult update(String id, String parentId, XContentBuilder doc);

	EventApplicationResult delete(String id);

	EventApplicationResult delete(String id, String parentId);
}
+7 −21
Original line number Diff line number Diff line
@@ -308,14 +308,10 @@ public abstract class RBaseESRepository<TModel extends BaseES<?>, TQueryDTO exte
	protected abstract JavaType getSourceType(Class<?> wrapperClass);

	protected GetResponse getRequest(String id) {
		return getRequest(id, null, null);
		return getRequest(id, null);
	}

	protected GetResponse getRequest(String id, String parentId) {
		return getRequest(id, parentId, null);
	}

	protected GetResponse getRequest(String id, String parentId, String grandparentId) {

		LOGGER.debug("FindById en " + getIndex() + " " + getType() + " con id " + id);

@@ -324,10 +320,7 @@ public abstract class RBaseESRepository<TModel extends BaseES<?>, TQueryDTO exte
			GetRequest getRequest = new GetRequest(getIndex()[i], getType(), id.toString());

			if (parentId != null) {
				getRequest.parent(parentId);
			}
			if (grandparentId != null) {
				getRequest.routing(grandparentId);
				getRequest.routing(parentId);
			}

			GetResponse response;
@@ -335,7 +328,7 @@ public abstract class RBaseESRepository<TModel extends BaseES<?>, TQueryDTO exte
				response = ESProvider.getClient().get(getRequest, RequestOptions.DEFAULT);
			} catch (IOException e) {
				e.printStackTrace();
				throw new ItemNotFoundException("id", id + " en el servicio " + getType());
				throw new ItemNotFoundException("id", id + " en el servicio " + getIndex()[i] + " - " + getType());
			}

			if (response != null && response.isExists()) {
@@ -343,7 +336,7 @@ public abstract class RBaseESRepository<TModel extends BaseES<?>, TQueryDTO exte
			}
		}

		throw new ItemNotFoundException("id", id + " en el servicio " + getType());
		throw new ItemNotFoundException("id", id + " en el servicio " + getIndex()[0] + " - " + getType());
	}

	public List<String> suggest(TQueryDTO queryDTO) {
@@ -412,11 +405,6 @@ public abstract class RBaseESRepository<TModel extends BaseES<?>, TQueryDTO exte

	protected MultiGetResponse multigetRequest(MgetDTO dto, String parentId) {

		return multigetRequest(dto, parentId, null);
	}

	protected MultiGetResponse multigetRequest(MgetDTO dto, String parentId, String grandParentId) {

		LOGGER.debug("Mget en " + getIndex() + " " + getType() + " con fields " + dto.getFields() + " e ids "
				+ dto.getIds());

@@ -438,11 +426,9 @@ public abstract class RBaseESRepository<TModel extends BaseES<?>, TQueryDTO exte
				Item item = new Item(getIndex()[i], getType(), dto.getIds().get(k));

				if (parentId != null) {
					item.parent(parentId);
				}
				if (grandParentId != null) {
					item.routing(grandParentId);
					item.routing(parentId);
				}

				item.fetchSourceContext(fetchSourceContext);
				request.add(item);
			}
@@ -572,7 +558,7 @@ public abstract class RBaseESRepository<TModel extends BaseES<?>, TQueryDTO exte
			}
		}

		if (queryDTO.getText() != null && (queryDTO.getText().getHighlightFields() == null
		if (queryDTO.getText() != null && !(queryDTO.getText().getHighlightFields() == null
				|| queryDTO.getText().getHighlightFields().length == 0)) {

			searchSourceBuilder
+40 −16
Original line number Diff line number Diff line
@@ -47,10 +47,9 @@ import es.redmic.elasticsearchlib.config.EsClientProvider;
import es.redmic.exception.common.ExceptionType;
import es.redmic.exception.elasticsearch.ESUpdateException;
import es.redmic.models.es.common.dto.EventApplicationResult;
import es.redmic.models.es.common.model.BaseES;

@Component
public class ElasticPersistenceUtils<TModel extends BaseES<?>> {
public class ElasticPersistenceUtils {

	protected static Logger logger = LogManager.getLogger();

@@ -62,7 +61,11 @@ public class ElasticPersistenceUtils<TModel extends BaseES<?>> {

	protected static String SCRIPT_ENGINE = "groovy";

	public EventApplicationResult save(String index, String type, TModel model, String id) {
	public <TModel> EventApplicationResult save(String index, String type, TModel model, String id) {
		return save(index, type, model, id, null);
	}

	public <TModel> EventApplicationResult save(String index, String type, TModel model, String id, String parentId) {

		// @formatter:off
		
@@ -71,6 +74,10 @@ public class ElasticPersistenceUtils<TModel extends BaseES<?>> {
				.id(id)
				.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
		
		if (parentId != null) {
			request.routing(parentId);
		}
		
		// @formatter:on

		try {
@@ -83,7 +90,11 @@ public class ElasticPersistenceUtils<TModel extends BaseES<?>> {
		}
	}

	public EventApplicationResult update(String index, String type, TModel model, String id) {
	public <TModel> EventApplicationResult update(String index, String type, TModel model, String id) {
		return update(index, type, model, id, null);
	}

	public <TModel> EventApplicationResult update(String index, String type, TModel model, String id, String parentId) {

		// @formatter:off

@@ -92,6 +103,10 @@ public class ElasticPersistenceUtils<TModel extends BaseES<?>> {
				.fetchSource(false)
				.setRefreshPolicy(RefreshPolicy.IMMEDIATE);

		if (parentId != null) {
			updateRequest.routing(parentId);
		}
		
		// @formatter:on

		try {
@@ -108,6 +123,11 @@ public class ElasticPersistenceUtils<TModel extends BaseES<?>> {

	public EventApplicationResult update(String index, String type, String id, XContentBuilder doc) {

		return update(index, type, id, null, doc);
	}

	public EventApplicationResult update(String index, String type, String id, String parentId, XContentBuilder doc) {

		// @formatter:off
		
		UpdateRequest updateRequest = new UpdateRequest(index, type, id)
@@ -117,6 +137,10 @@ public class ElasticPersistenceUtils<TModel extends BaseES<?>> {
		
		// @formatter:on

		if (parentId != null) {
			updateRequest.routing(parentId);
		}

		try {
			ESProvider.getClient().update(updateRequest, RequestOptions.DEFAULT);
		} catch (Exception e) {
@@ -129,8 +153,17 @@ public class ElasticPersistenceUtils<TModel extends BaseES<?>> {

	public EventApplicationResult delete(String index, String type, String id) {

		return delete(index, type, id, null);
	}

	public EventApplicationResult delete(String index, String type, String id, String parentId) {

		DeleteRequest deleteRequest = new DeleteRequest(index, type, id).setRefreshPolicy(RefreshPolicy.IMMEDIATE);

		if (parentId != null) {
			deleteRequest.routing(parentId);
		}

		try {
			ESProvider.getClient().delete(deleteRequest, RequestOptions.DEFAULT);
			return new EventApplicationResult(true);
@@ -141,24 +174,18 @@ public class ElasticPersistenceUtils<TModel extends BaseES<?>> {
	}

	@SuppressWarnings("unchecked")
	protected Map<String, Object> convertTModelToSource(TModel modelToIndex) {
	protected <TModel> Map<String, Object> convertTModelToSource(TModel modelToIndex) {
		return objectMapper.convertValue(modelToIndex, Map.class);
	}

	public List<UpdateRequest> getUpdateRequest(String[] index, String[] type, String id, Map<String, Object> fields) {

		return getUpdateRequest(index, type, id, fields, null, null);
		return getUpdateRequest(index, type, id, fields, null);
	}

	public List<UpdateRequest> getUpdateRequest(String[] index, String[] type, String id, Map<String, Object> fields,
			String parentId) {

		return getUpdateRequest(index, type, id, fields, parentId, null);
	}

	public List<UpdateRequest> getUpdateRequest(String[] index, String[] type, String id, Map<String, Object> fields,
			String parentId, String grandParentId) {

		List<UpdateRequest> result = new ArrayList<UpdateRequest>();

		for (int i = 0; i < index.length; i++) {
@@ -169,10 +196,7 @@ public class ElasticPersistenceUtils<TModel extends BaseES<?>> {
				updateRequest.id(id);
				updateRequest.fetchSource(true);
				if (parentId != null)
					updateRequest.parent(grandParentId);

				if (grandParentId != null)
					updateRequest.routing(grandParentId);
					updateRequest.routing(parentId);

				updateRequest.doc(fields);
				result.add(updateRequest);
Loading