Search in sources :

Example 1 with ElasticSearchRestSearchResponse

use of com.thinkbiganalytics.search.rest.model.es.ElasticSearchRestSearchResponse in project kylo by Teradata.

the class ElasticSearchRestService method search.

@Override
public SearchResult search(String query, int size, int start) {
    buildRestClient();
    String queryForExecution = rewriteQuery(query);
    ElasticSearchRestSearchResponse restSearchResponse = executeRestSearch(queryForExecution, size, start);
    if (restSearchResponse != null) {
        return transformRestResult(query, size, start, restSearchResponse);
    } else {
        log.warn("Search execution produced null response");
        return null;
    }
}
Also used : ElasticSearchRestSearchResponse(com.thinkbiganalytics.search.rest.model.es.ElasticSearchRestSearchResponse)

Example 2 with ElasticSearchRestSearchResponse

use of com.thinkbiganalytics.search.rest.model.es.ElasticSearchRestSearchResponse 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

ElasticSearchRestSearchResponse (com.thinkbiganalytics.search.rest.model.es.ElasticSearchRestSearchResponse)2 HiveColumn (com.thinkbiganalytics.search.rest.model.HiveColumn)1 Pair (com.thinkbiganalytics.search.rest.model.Pair)1 ElasticSearchRestSearchHit (com.thinkbiganalytics.search.rest.model.es.ElasticSearchRestSearchHit)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)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