use of com.thinkbiganalytics.search.rest.model.es.ElasticSearchRestSearchHit in project kylo by Teradata.
the class ElasticSearchRestSearchResultTransform method transformRestResult.
public SearchResult transformRestResult(String query, int size, int start, ElasticSearchRestSearchResponse restSearchResponse) {
final String KYLO_DATA = "kylo-data";
final String KYLO_SCHEMA_METADATA = "kylo-schema-metadata";
final String KYLO_FEEDS = "kylo-feeds";
final String KYLO_CATEGORIES = "kylo-categories";
final String ELASTIC_SEARCH_RC = "Elasticsearch (rc)";
List<SearchResultData> searchResultData = new ArrayList<>();
for (ElasticSearchRestSearchHit elasticSearchRestSearchHit : restSearchResponse.getElasticSearchRestSearchHits()) {
if (elasticSearchRestSearchHit.getIndexName().equals(KYLO_DATA)) {
searchResultData.add(getTableSearchResultData(elasticSearchRestSearchHit));
} else if (elasticSearchRestSearchHit.getIndexName().equals(SearchIndex.DATASOURCES)) {
searchResultData.add(getSchemaSearchResultData(elasticSearchRestSearchHit));
} else if (elasticSearchRestSearchHit.getIndexName().contains(KYLO_FEEDS)) {
// This is dependent upon ModeShape configuration. The prefix will remain the same.
// Hence only checking part of index name
searchResultData.add(getFeedMetadataSearchResultData(elasticSearchRestSearchHit));
} else if (elasticSearchRestSearchHit.getIndexName().contains(KYLO_CATEGORIES)) {
// This is dependent upon ModeShape configuration. The prefix will remain the same.
// Hence only checking part of index name
searchResultData.add(getCategoryMetadataSearchResultData(elasticSearchRestSearchHit));
} else {
searchResultData.add(getUnknownTypeSearchResultData(elasticSearchRestSearchHit));
}
}
SearchResult elasticSearchRestResult = new SearchResult();
elasticSearchRestResult.setQuery(query);
Long totalHits = restSearchResponse.getTotalResults();
elasticSearchRestResult.setTotalHits(totalHits);
elasticSearchRestResult.setFrom((long) (start + 1));
elasticSearchRestResult.setTo((long) (start + size));
if (totalHits < (start + size)) {
elasticSearchRestResult.setTo(totalHits);
}
if (totalHits == 0) {
elasticSearchRestResult.setFrom(0L);
}
elasticSearchRestResult.setTookInMillis(restSearchResponse.getTookInMillis());
elasticSearchRestResult.setEngine(ELASTIC_SEARCH_RC);
elasticSearchRestResult.setSearchResults(searchResultData);
elasticSearchRestResult.setSearchResultsSummary(getSearchResultSummary());
return elasticSearchRestResult;
}
use of com.thinkbiganalytics.search.rest.model.es.ElasticSearchRestSearchHit in project kylo by Teradata.
the class ElasticSearchRestService method transformElasticSearchRestResponse.
private ElasticSearchRestSearchResponse transformElasticSearchRestResponse(Response response) {
if (response == null) {
log.warn("Null response from Elasticsearch (rest client)");
return null;
}
ElasticSearchRestSearchResponse elasticSearchRestSearchResponse = new ElasticSearchRestSearchResponse();
elasticSearchRestSearchResponse.setRequestLine(response.getRequestLine().toString());
elasticSearchRestSearchResponse.setHost(response.getHost().toString());
elasticSearchRestSearchResponse.setStatusLine(response.getStatusLine().toString());
elasticSearchRestSearchResponse.setRawEntity(response.getEntity());
for (int i = 0; i < response.getHeaders().length; i++) {
List<String> currentHeaders = elasticSearchRestSearchResponse.getHeaders();
currentHeaders.add(response.getHeaders()[i].toString());
elasticSearchRestSearchResponse.setHeaders(currentHeaders);
}
HttpEntity entity = response.getEntity();
if (entity == null) {
return null;
}
try {
String entityString = EntityUtils.toString(response.getEntity());
JSONObject entityStringJsonObject = new JSONObject(entityString);
String tookInMs = entityStringJsonObject.getString("took");
elasticSearchRestSearchResponse.setTookInMillis(Long.parseLong(tookInMs));
JSONObject hitsJsonObject = entityStringJsonObject.getJSONObject("hits");
elasticSearchRestSearchResponse.setTotalResults(hitsJsonObject.getLong("total"));
JSONArray hitsJsonArray = hitsJsonObject.getJSONArray("hits");
List<ElasticSearchRestSearchHit> elasticSearchRestSearchHits = new ArrayList<>();
for (int i = 0; i < hitsJsonArray.length(); i++) {
ElasticSearchRestSearchHit elasticSearchRestSearchHit = new ElasticSearchRestSearchHit();
JSONObject currentHitJsonObject = new JSONObject(hitsJsonArray.get(i).toString());
elasticSearchRestSearchHit.setIndexName(currentHitJsonObject.get("_index").toString());
elasticSearchRestSearchHit.setIndexType(currentHitJsonObject.get("_type").toString());
JSONObject currentHitSourceJsonObject = new JSONObject(currentHitJsonObject.get("_source").toString());
elasticSearchRestSearchHit.setRawHit(currentHitJsonObject.get("_source").toString());
List<Pair> sourceList = new ArrayList<>();
Iterator sourceIterator = currentHitSourceJsonObject.keys();
while (sourceIterator.hasNext()) {
String sourceKey = (String) sourceIterator.next();
sourceList.add(new Pair(sourceKey, currentHitSourceJsonObject.get(sourceKey)));
if (sourceKey.equals("hiveColumns")) {
List<HiveColumn> hiveColumns = new ArrayList<>();
String newHiveColumns = "{\"a\":" + currentHitSourceJsonObject.get(sourceKey).toString() + "}";
JSONObject hiveColumnsJsonObject = new JSONObject(newHiveColumns);
JSONArray hiveColumnsJsonArray = hiveColumnsJsonObject.getJSONArray("a");
for (int x = 0; x < hiveColumnsJsonArray.length(); x++) {
JSONObject hiveColumnJsonObject = new JSONObject(hiveColumnsJsonArray.get(x).toString());
Iterator hiveColumnIterator = hiveColumnJsonObject.keys();
String columnName = "";
String columnType = "";
String columnComment = "";
while (hiveColumnIterator.hasNext()) {
String columnKey = (String) hiveColumnIterator.next();
switch(columnKey) {
case "columnName":
columnName = hiveColumnJsonObject.get(columnKey).toString();
break;
case "columnType":
columnType = hiveColumnJsonObject.get(columnKey).toString();
break;
case "columnComment":
columnComment = hiveColumnJsonObject.get(columnKey).toString();
break;
default:
break;
}
}
hiveColumns.add(new HiveColumn(columnName, columnType, columnComment));
}
elasticSearchRestSearchHit.setHiveColumns(hiveColumns);
}
sourceIterator.remove();
}
elasticSearchRestSearchHit.setSource(sourceList);
JSONObject currentHitHighlightJsonObject = new JSONObject(currentHitJsonObject.get("highlight").toString());
List<Pair> highlightsList = new ArrayList<>();
Iterator highlightIterator = currentHitHighlightJsonObject.keys();
while (highlightIterator.hasNext()) {
String highlightKey = (String) highlightIterator.next();
JSONArray highlightArray = currentHitHighlightJsonObject.getJSONArray(highlightKey);
if (highlightArray.length() > 0) {
highlightsList.add(new Pair(highlightKey, highlightArray.get(0)));
}
highlightIterator.remove();
}
elasticSearchRestSearchHit.setHighlights(highlightsList);
elasticSearchRestSearchHits.add(elasticSearchRestSearchHit);
}
elasticSearchRestSearchResponse.setElasticSearchRestSearchHits(elasticSearchRestSearchHits);
return elasticSearchRestSearchResponse;
} catch (IOException | JSONException exception) {
log.warn("An error occurred during decoding search result e=", exception);
}
return null;
}
Aggregations