aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/api/ESGenericSearchDAO.java
blob: 4dc57201fbd114dcf9905193cd5dab8c8cdcd92e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.dao.api;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.openecomp.sdc.be.dao.es.ElasticSearchClient;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Elastic search dao that manages search operations.
 *
 * @author luc boutier
 */
public class ESGenericSearchDAO extends ESGenericIdDAO implements IGenericSearchDAO {

	private static final int MAX_SEARCH_SIZE = 1000;

	@Resource(name = "elasticsearch-client")
	private ElasticSearchClient esClient;

	@Override
	public long count(String indexName, String typeName, QueryBuilder query) {

		SearchRequestBuilder searchRequestBuilder = esClient.getClient().prepareSearch(indexName).setTypes(typeName)
				.setSize(0);
		if (query != null) {
			searchRequestBuilder.setQuery(query);
		}

		SearchResponse response = searchRequestBuilder.execute().actionGet();
		if (!somethingFound(response)) {
			return 0;
		} else {
			return response.getHits().getTotalHits();
		}
	}

	/**
	 * Convert a SearchResponse into a list of objects (json deserialization.)
	 *
	 * @param searchResponse
	 *            The actual search response from elastic-search.
	 * @param clazz
	 *            The type of objects to de-serialize.
	 * @return A list of instances that contains de-serialized data.
	 */
	public <T> List<T> toGetListOfData(SearchResponse searchResponse, Class<T> clazz) {
		// return null if no data has been found in elastic search.
		if (!somethingFound(searchResponse)) {
			return null;
		}

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

		for (int i = 0; i < searchResponse.getHits().getHits().length; i++) {
			try {
				result.add(getJsonMapper().readValue(searchResponse.getHits().getAt(i).getSourceAsString(), clazz));
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

		return result;
	}

	public <T> List<T> doCustomFind(Class<T> clazz, String indexName, String typeName, QueryBuilder query,
			SortBuilder sortBuilder) {

		List<T> result = new ArrayList<>();
		SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(indexName).setTypes(typeName)
				.setSize(MAX_SEARCH_SIZE);
		if (query != null) {
			searchRequestBuilder.setQuery(query);
		}
		if (sortBuilder != null) {
			searchRequestBuilder.addSort(sortBuilder);
		}
		SearchResponse response = searchRequestBuilder.execute().actionGet();
		if (!somethingFound(response)) {
			return null;
		} else {
			for (int i = 0; i < response.getHits().getHits().length; i++) {
				String hit = response.getHits().getAt(i).sourceAsString();

				T val = null;
				try {
					val = getJsonMapper().readValue(hit, clazz);
					result.add(val);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return result;
		}
	}

	private boolean somethingFound(final SearchResponse searchResponse) {
		if (searchResponse == null || searchResponse.getHits() == null || searchResponse.getHits().getHits() == null
				|| searchResponse.getHits().getHits().length == 0) {
			return false;
		}
		return true;
	}

}