Commit a9b5bf51 authored by Noel Alonso's avatar Noel Alonso
Browse files

Reestructura código y añade test de consulta

parent 6a22200f
Loading
Loading
Loading
Loading
+19 −17
Original line number Diff line number Diff line
@@ -110,14 +110,15 @@ public class RTaxonDistributionRepository extends RBaseESRepository<Distribution
		GeoJSONFeatureCollectionDTO res = new GeoJSONFeatureCollectionDTO();

		List<Integer> confidence = getConfidenceValues(dto);

		if (confidence == null || confidence.isEmpty() || confidence.size() > 4)
			return res;

		Map<String, Object> scriptParams = new HashMap<>();
		scriptParams.put("taxons", ids);
		scriptParams.put("confidences", confidence);
		SearchResponse response = findAll(createQuery(dto, ids, confidence), INCLUDE_DEFAULT, EXCLUDE_DEFAULT,
				scriptParams);
		SearchResponse response = findAll(createQuery(dto, ids, confidence, INCLUDE_DEFAULT, EXCLUDE_DEFAULT,
				scriptParams));

		if (response != null) {
			List<Object> result = mapperHitsArrayToClass(response.getHits().getHits());
@@ -150,19 +151,7 @@ public class RTaxonDistributionRepository extends RBaseESRepository<Distribution
		return ret;
	}

	public SearchResponse findAll(QueryBuilder query, String[] include, String[] exclude,
			Map<String, Object> scriptParams) {

		SearchRequest searchRequest = new SearchRequest(getIndex());
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

		searchSourceBuilder.query(query);
		searchSourceBuilder.size(10000);
		searchSourceBuilder.fetchSource(include, exclude);
		searchSourceBuilder.scriptField("taxons",
			new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, ElasticSearchUtils.getScriptFile(AGGS_DISTRIBUTION_SCRIPT_PATH), scriptParams));

		searchRequest.source(searchSourceBuilder);
	public SearchResponse findAll(SearchRequest searchRequest) {

		SearchResponse searchResponse;

@@ -176,7 +165,8 @@ public class RTaxonDistributionRepository extends RBaseESRepository<Distribution
		return searchResponse;
	}

	private QueryBuilder createQuery(GeoDataQueryDTO queryDTO, List<String> ids, List<Integer> confidence) {
	public SearchRequest createQuery(GeoDataQueryDTO queryDTO, List<String> ids, List<Integer> confidence,
		String[] include, String[] exclude, Map<String, Object> scriptParams) {

		NestedQueryBuilder idsFilter;
		if (ids != null && !ids.isEmpty()) // TODO: quitar, siempre debe ids
@@ -212,7 +202,19 @@ public class RTaxonDistributionRepository extends RBaseESRepository<Distribution
			throw new ESBBoxQueryException(e);
		}

		return query;
		SearchRequest searchRequest = new SearchRequest(getIndex());
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

		searchSourceBuilder.query(query);
		searchSourceBuilder.size(10000);
		searchSourceBuilder.fetchSource(include, exclude);
		searchSourceBuilder.scriptField("taxons",
			new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG,
				ElasticSearchUtils.getScriptFile(AGGS_DISTRIBUTION_SCRIPT_PATH), scriptParams));

		searchRequest.source(searchSourceBuilder);

		return searchRequest;
	}

	public TaxonDistribution findByRegisterId(String id) {
+3 −7
Original line number Diff line number Diff line
@@ -26,8 +26,6 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.fasterxml.jackson.databind.ObjectMapper;

import es.redmic.es.common.repository.SelectionWorkRepository;
import es.redmic.es.common.service.RBaseESService;
import es.redmic.es.geodata.common.service.GridServiceItfc;
@@ -58,8 +56,6 @@ public abstract class RTaxonDistributionService extends RBaseESService<Distribut
	@Autowired
	SelectionWorkRepository selectionWorkRepository;

	@Autowired
	ObjectMapper objectMapper;

	protected RTaxonDistributionService(RTaxonDistributionRepository repository, GridServiceItfc gridUtil) {
		this.repository = repository;
@@ -80,8 +76,8 @@ public abstract class RTaxonDistributionService extends RBaseESService<Distribut

		List<String> taxonsIds = getIdsBySelection(queryDTO);

		if (taxonsIds == null || taxonsIds.size() <= 0)
			return new ArrayList<TaxonDistributionRegistersDTO>();
		if (taxonsIds == null || taxonsIds.isEmpty())
			return new ArrayList<>();

		return repository.findByGridIdAndTaxons(queryDTO, gridId, taxonsIds);
	}
@@ -94,7 +90,7 @@ public abstract class RTaxonDistributionService extends RBaseESService<Distribut
			ids = selectionWorkRepository.getSelectedIds(queryDTO.getTerms().get("selection").toString());
		}

		if ((ids == null || ids.size() <= 0)
		if ((ids == null || ids.isEmpty())
				&& (queryDTO.getTerms() == null && queryDTO.getTerms().get("taxonId") == null))
			return null;

+80 −0
Original line number Diff line number Diff line
package es.redmic.test.unit.tools.distribution;

/*-
 * #%L
 * ElasticSearch
 * %%
 * 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%
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.json.JSONException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.boot.test.mock.mockito.MockBean;

import es.redmic.es.common.queryFactory.geodata.AnimalTrackingQueryUtils;
import es.redmic.es.tools.distributions.species.repository.TaxonDist100MRepository;
import es.redmic.models.es.common.query.dto.BboxQueryDTO;
import es.redmic.models.es.common.query.dto.GeoDataQueryDTO;
import es.redmic.test.unit.queryFactory.common.GeoDataQueryTest;

@RunWith(MockitoJUnitRunner.class)
public class SpeciesDistributionQueryTest extends GeoDataQueryTest {

	TaxonDist100MRepository repository = new TaxonDist100MRepository();

	@Test
	public void getQuery_ReturnInternalQuery_IfIsDefaultQueryDTO() throws IOException, JSONException {

		GeoDataQueryDTO dto = new GeoDataQueryDTO();

		BboxQueryDTO bbox = new BboxQueryDTO();
		bbox.setBottomRightLat(27.806396484375);
		bbox.setBottomRightLon(-12.80181884765625);
		bbox.setTopLeftLat(30.0421142578125);
		bbox.setTopLeftLon(-18.63104248046875);

		dto.setBbox(bbox);

		List<String> ids = new ArrayList<>();
		ids.add("root.1.13.148.19923.455.1570.4833.14618");
		List<Integer> confidence = new ArrayList<>();
		confidence.add(4);

		Map<String, Object> scriptParams = new HashMap<>();
		scriptParams.put("taxons", ids);
		scriptParams.put("confidences", confidence);


		SearchRequest query = repository.createQuery(dto, ids, confidence, new String[] { "*" }, new String[] {}, scriptParams);

		String queryExpected = getExpectedQuery("/queryfactory/tools/distribution/distributionQuery.json");

		JSONAssert.assertEquals(queryExpected, query.source().toString(), false);
	}
}
+110 −0
Original line number Diff line number Diff line
{
	"size": 10000,
	"query": {
		"bool": {
			"filter": [{
				"bool": {
					"must": [{
						"nested": {
							"query": {
								"bool": {
									"should": [{
										"terms": {
											"properties.taxons.path": ["root.1.13.148.19923.455.1570.4833.14618"],
											"boost": 1.0
										}
									}, {
										"terms": {
											"properties.taxons.equivalent": ["root.1.13.148.19923.455.1570.4833.14618"],
											"boost": 1.0
										}
									}, {
										"nested": {
											"query": {
												"bool": {
													"must": [{
														"terms": {
															"properties.taxons.registers.misidentification": ["root.1.13.148.19923.455.1570.4833.14618"],
															"boost": 1.0
														}
													}],
													"adjust_pure_negative": true,
													"boost": 1.0
												}
											},
											"path": "properties.taxons.registers",
											"ignore_unmapped": false,
											"score_mode": "avg",
											"boost": 1.0
										}
									}],
									"adjust_pure_negative": true,
									"boost": 1.0
								}
							},
							"path": "properties.taxons",
							"ignore_unmapped": false,
							"score_mode": "avg",
							"boost": 1.0
						}
					}, {
						"nested": {
							"query": {
								"bool": {
									"must": [{
										"terms": {
											"properties.taxons.registers.confidence": [4],
											"boost": 1.0
										}
									}],
									"adjust_pure_negative": true,
									"boost": 1.0
								}
							},
							"path": "properties.taxons.registers",
							"ignore_unmapped": false,
							"score_mode": "avg",
							"boost": 1.0
						}
					}, {
						"geo_shape": {
							"geometry": {
								"shape": {
									"type": "envelope",
									"coordinates": [
										[-18.63104248046875, 30.0421142578125],
										[-12.80181884765625, 27.806396484375]
									]
								},
								"relation": "intersects"
							},
							"ignore_unmapped": false,
							"boost": 1.0
						}
					}],
					"adjust_pure_negative": true,
					"boost": 1.0
				}
			}],
			"adjust_pure_negative": true,
			"boost": 1.0
		}
	},
	"_source": {
		"includes": ["*"],
		"excludes": []
	},
	"script_fields": {
		"taxons": {
			"script": {
				"source": "boolean filterByConfidence(def confidence, def params) {\n\tparams.confidences.contains(confidence);\n}\nboolean filterByPathTaxon(def pathTaxon, def params) {\n\tparams.taxons.contains(pathTaxon);\n}\n\nboolean filterByMisidentification(def taxon, def params) {\n\n\tfor (reg in taxon.registers) {\n\t\tif (params.taxons.contains(reg.misidentification))\n\t\t\treturn true;\n\t}\n\treturn false;\n}\n\ndef getRegisters(def taxon, def params) {\n\treturn [\"registers\": taxon.registers.findAll(reg -> filterByConfidence(reg.confidence, params))];\n}\n\nvoid checkRegister(def result, def taxon, def params) {\n\tif (filterByPathTaxon(taxon.path, params) || filterByPathTaxon(taxon.equivalent, params) || filterByMisidentification(taxon, params)) {\n\t\t\tdef taxonRegisters = getRegisters(taxon, params);\n\n\t\t\tif (taxonRegisters.registers.size() > 0) {\n\t\t\t\tresult['properties.registerCount'] += taxon.registers.size();\n\t\t\t\tresult['properties.taxonCount'] ++;\n\t\t\t}\n\t}\n}\n\nMap result = new HashMap();\n\nresult['properties.registerCount'] = 0;\nresult['properties.taxonCount'] = 0;\n\nfor (tax in params._source.properties.taxons) {\n\tcheckRegister(result, tax, params);\n}\n\nreturn result;\n",
				"lang": "painless",
				"params": {
					"confidences": [4],
					"taxons": ["root.1.13.148.19923.455.1570.4833.14618"]
				}
			},
			"ignore_failure": false
		}
	}
}
+41 −0
Original line number Diff line number Diff line
boolean filterByConfidence(def confidence, def params) {
	params.confidences.contains(confidence);
}
boolean filterByPathTaxon(def pathTaxon, def params) {
	params.taxons.contains(pathTaxon);
}

boolean filterByMisidentification(def taxon, def params) {

	for (reg in taxon.registers) {
		if (params.taxons.contains(reg.misidentification))
			return true;
	}
	return false;
}

def getRegisters(def taxon, def params) {
	return ["registers": taxon.registers.findAll(reg -> filterByConfidence(reg.confidence, params))];
}

void checkRegister(def result, def taxon, def params) {
	if (filterByPathTaxon(taxon.path, params) || filterByPathTaxon(taxon.equivalent, params) || filterByMisidentification(taxon, params)) {
			def taxonRegisters = getRegisters(taxon, params);

			if (taxonRegisters.registers.size() > 0) {
				result['properties.registerCount'] += taxon.registers.size();
				result['properties.taxonCount'] ++;
			}
	}
}

Map result = new HashMap();

result['properties.registerCount'] = 0;
result['properties.taxonCount'] = 0;

for (tax in params._source.properties.taxons) {
	checkRegister(result, tax, params);
}

return result;