Search in sources :

Example 1 with ElasticSearchRestSearchHit

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;
}
Also used : TableSearchResultData(com.thinkbiganalytics.search.rest.model.TableSearchResultData) SchemaSearchResultData(com.thinkbiganalytics.search.rest.model.SchemaSearchResultData) SearchResultData(com.thinkbiganalytics.search.rest.model.SearchResultData) FeedMetadataSearchResultData(com.thinkbiganalytics.search.rest.model.FeedMetadataSearchResultData) UnknownTypeSearchResultData(com.thinkbiganalytics.search.rest.model.UnknownTypeSearchResultData) CategoryMetadataSearchResultData(com.thinkbiganalytics.search.rest.model.CategoryMetadataSearchResultData) ArrayList(java.util.ArrayList) ElasticSearchRestSearchHit(com.thinkbiganalytics.search.rest.model.es.ElasticSearchRestSearchHit) SearchResult(com.thinkbiganalytics.search.rest.model.SearchResult)

Example 2 with ElasticSearchRestSearchHit

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;
}
Also used : HttpEntity(org.apache.http.HttpEntity) JSONArray(org.codehaus.jettison.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.codehaus.jettison.json.JSONException) ElasticSearchRestSearchResponse(com.thinkbiganalytics.search.rest.model.es.ElasticSearchRestSearchResponse) ElasticSearchRestSearchHit(com.thinkbiganalytics.search.rest.model.es.ElasticSearchRestSearchHit) IOException(java.io.IOException) JSONObject(org.codehaus.jettison.json.JSONObject) Iterator(java.util.Iterator) HiveColumn(com.thinkbiganalytics.search.rest.model.HiveColumn) Pair(com.thinkbiganalytics.search.rest.model.Pair)

Aggregations

ElasticSearchRestSearchHit (com.thinkbiganalytics.search.rest.model.es.ElasticSearchRestSearchHit)2 ArrayList (java.util.ArrayList)2 CategoryMetadataSearchResultData (com.thinkbiganalytics.search.rest.model.CategoryMetadataSearchResultData)1 FeedMetadataSearchResultData (com.thinkbiganalytics.search.rest.model.FeedMetadataSearchResultData)1 HiveColumn (com.thinkbiganalytics.search.rest.model.HiveColumn)1 Pair (com.thinkbiganalytics.search.rest.model.Pair)1 SchemaSearchResultData (com.thinkbiganalytics.search.rest.model.SchemaSearchResultData)1 SearchResult (com.thinkbiganalytics.search.rest.model.SearchResult)1 SearchResultData (com.thinkbiganalytics.search.rest.model.SearchResultData)1 TableSearchResultData (com.thinkbiganalytics.search.rest.model.TableSearchResultData)1 UnknownTypeSearchResultData (com.thinkbiganalytics.search.rest.model.UnknownTypeSearchResultData)1 ElasticSearchRestSearchResponse (com.thinkbiganalytics.search.rest.model.es.ElasticSearchRestSearchResponse)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 HttpEntity (org.apache.http.HttpEntity)1 JSONArray (org.codehaus.jettison.json.JSONArray)1 JSONException (org.codehaus.jettison.json.JSONException)1 JSONObject (org.codehaus.jettison.json.JSONObject)1