2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.dao.api;
23 import org.elasticsearch.action.search.SearchRequestBuilder;
24 import org.elasticsearch.action.search.SearchResponse;
25 import org.elasticsearch.index.query.QueryBuilder;
26 import org.elasticsearch.search.sort.SortBuilder;
27 import org.openecomp.sdc.be.dao.es.ElasticSearchClient;
29 import javax.annotation.Resource;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
35 * Elastic search dao that manages search operations.
39 public class ESGenericSearchDAO extends ESGenericIdDAO implements IGenericSearchDAO {
41 private static final int MAX_SEARCH_SIZE = 1000;
43 @Resource(name = "elasticsearch-client")
44 private ElasticSearchClient esClient;
47 public long count(String indexName, String typeName, QueryBuilder query) {
49 SearchRequestBuilder searchRequestBuilder = esClient.getClient().prepareSearch(indexName).setTypes(typeName)
52 searchRequestBuilder.setQuery(query);
55 SearchResponse response = searchRequestBuilder.execute().actionGet();
56 if (!somethingFound(response)) {
59 return response.getHits().getTotalHits();
64 * Convert a SearchResponse into a list of objects (json deserialization.)
66 * @param searchResponse
67 * The actual search response from elastic-search.
69 * The type of objects to de-serialize.
70 * @return A list of instances that contains de-serialized data.
72 public <T> List<T> toGetListOfData(SearchResponse searchResponse, Class<T> clazz) {
73 // return null if no data has been found in elastic search.
74 if (!somethingFound(searchResponse)) {
78 List<T> result = new ArrayList<>();
80 for (int i = 0; i < searchResponse.getHits().getHits().length; i++) {
82 result.add(getJsonMapper().readValue(searchResponse.getHits().getAt(i).getSourceAsString(), clazz));
83 } catch (IOException e) {
84 throw new RuntimeException(e);
91 public <T> List<T> doCustomFind(Class<T> clazz, String indexName, String typeName, QueryBuilder query,
92 SortBuilder sortBuilder) {
94 List<T> result = new ArrayList<>();
95 SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(indexName).setTypes(typeName)
96 .setSize(MAX_SEARCH_SIZE);
98 searchRequestBuilder.setQuery(query);
100 if (sortBuilder != null) {
101 searchRequestBuilder.addSort(sortBuilder);
103 SearchResponse response = searchRequestBuilder.execute().actionGet();
104 if (!somethingFound(response)) {
107 for (int i = 0; i < response.getHits().getHits().length; i++) {
108 String hit = response.getHits().getAt(i).sourceAsString();
112 val = getJsonMapper().readValue(hit, clazz);
114 } catch (IOException e) {
115 throw new RuntimeException(e);
122 private boolean somethingFound(final SearchResponse searchResponse) {
123 if (searchResponse == null || searchResponse.getHits() == null || searchResponse.getHits().getHits() == null
124 || searchResponse.getHits().getHits().length == 0) {