Search in sources :

Example 6 with HitWrapper

use of org.apache.zeppelin.elasticsearch.action.HitWrapper in project zeppelin by apache.

the class TransportBasedClient method search.

@Override
public ActionResponse search(String[] indices, String[] types, String query, int size) {
    final SearchRequestBuilder reqBuilder = new SearchRequestBuilder(client, SearchAction.INSTANCE);
    reqBuilder.setIndices();
    if (indices != null) {
        reqBuilder.setIndices(indices);
    }
    if (types != null) {
        reqBuilder.setTypes(types);
    }
    if (!StringUtils.isEmpty(query)) {
        // So, try to parse as a JSON => if there is an error, consider the query a Lucene one
        try {
            @SuppressWarnings("rawtypes") final Map source = gson.fromJson(query, Map.class);
            reqBuilder.setExtraSource(source);
        } catch (final JsonSyntaxException e) {
            // This is not a JSON (or maybe not well formatted...)
            reqBuilder.setQuery(QueryBuilders.queryStringQuery(query).analyzeWildcard(true));
        }
    }
    reqBuilder.setSize(size);
    final SearchResponse searchResp = reqBuilder.get();
    final ActionResponse actionResp = new ActionResponse().succeeded(true).totalHits(searchResp.getHits().getTotalHits());
    if (searchResp.getAggregations() != null) {
        setAggregations(searchResp.getAggregations(), actionResp);
    } else {
        for (final SearchHit hit : searchResp.getHits()) {
            // Fields can be found either in _source, or in fields (it depends on the query)
            // => specific for elasticsearch's version < 5
            //
            String src = hit.getSourceAsString();
            if (src == null) {
                final Map<String, Object> hitFields = new HashMap<>();
                for (final SearchHitField hitField : hit.getFields().values()) {
                    hitFields.put(hitField.getName(), hitField.getValues());
                }
                src = gson.toJson(hitFields);
            }
            actionResp.addHit(new HitWrapper(hit.getIndex(), hit.getType(), hit.getId(), src));
        }
    }
    return actionResp;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) JsonSyntaxException(com.google.gson.JsonSyntaxException) HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) SearchHitField(org.elasticsearch.search.SearchHitField) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HitWrapper (org.apache.zeppelin.elasticsearch.action.HitWrapper)6 ActionResponse (org.apache.zeppelin.elasticsearch.action.ActionResponse)5 JsonNode (com.mashape.unirest.http.JsonNode)4 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)4 ActionException (org.apache.zeppelin.elasticsearch.action.ActionException)4 HttpRequest (com.mashape.unirest.request.HttpRequest)2 HttpRequestWithBody (com.mashape.unirest.request.HttpRequestWithBody)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 TreeSet (java.util.TreeSet)1 Matcher (java.util.regex.Matcher)1 AggWrapper (org.apache.zeppelin.elasticsearch.action.AggWrapper)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1