Search in sources :

Example 21 with ServiceResponse

use of net.yacy.grid.http.ServiceResponse in project yacy_grid_mcp by yacy.

the class DeleteService method serviceImpl.

@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response) {
    // String indexName, String typeName, final String id, JSONObject object
    String indexName = call.get("index", "");
    // should not be null
    String typeName = call.get("type", "");
    String id = call.get("id", "");
    QueryLanguage language = QueryLanguage.valueOf(call.get("language", "yacy"));
    String query = call.get("query", "");
    JSONObject json = new JSONObject(true);
    if (indexName.length() > 0 && typeName.length() > 0 && id.length() > 0) {
        try {
            Index index = Data.gridIndex.getElasticIndex();
            String url = index.checkConnection().getConnectionURL();
            boolean deleted = index.delete(indexName, typeName, id);
            json.put(ObjectAPIHandler.SUCCESS_KEY, true);
            json.put("deleted", deleted);
            json.put("count", deleted ? 1 : 0);
            if (url != null)
                json.put(ObjectAPIHandler.SERVICE_KEY, url);
        } catch (IOException e) {
            json.put(ObjectAPIHandler.SUCCESS_KEY, false);
            json.put(ObjectAPIHandler.COMMENT_KEY, e.getMessage());
        }
    } else if (indexName.length() > 0 && typeName.length() > 0 && query.length() > 0) {
        try {
            Index index = Data.gridIndex.getElasticIndex();
            String url = index.checkConnection().getConnectionURL();
            long count = index.delete(indexName, typeName, language, query);
            json.put(ObjectAPIHandler.SUCCESS_KEY, true);
            json.put("count", count);
            if (url != null)
                json.put(ObjectAPIHandler.SERVICE_KEY, url);
        } catch (IOException e) {
            json.put(ObjectAPIHandler.SUCCESS_KEY, false);
            json.put(ObjectAPIHandler.COMMENT_KEY, e.getMessage());
        }
    } else {
        json.put(ObjectAPIHandler.SUCCESS_KEY, false);
        json.put(ObjectAPIHandler.COMMENT_KEY, "the request must contain an index, type, and either an id or a query");
    }
    return new ServiceResponse(json);
}
Also used : ServiceResponse(net.yacy.grid.http.ServiceResponse) JSONObject(org.json.JSONObject) QueryLanguage(net.yacy.grid.io.index.Index.QueryLanguage) Index(net.yacy.grid.io.index.Index) IOException(java.io.IOException)

Example 22 with ServiceResponse

use of net.yacy.grid.http.ServiceResponse in project yacy_grid_mcp by yacy.

the class ExistService method serviceImpl.

@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response) {
    // String indexName, String typeName, final String id, JSONObject object
    String indexName = call.get("index", "");
    String typeName = call.get("type", "");
    if (typeName.length() == 0)
        typeName = null;
    String id = call.get("id", "");
    JSONObject json = new JSONObject(true);
    if (indexName.length() > 0 && id.length() > 0) {
        try {
            Index index = Data.gridIndex.getElasticIndex();
            String url = index.checkConnection().getConnectionURL();
            boolean exists = index.exist(indexName, typeName, id);
            json.put(ObjectAPIHandler.SUCCESS_KEY, true);
            json.put("exists", exists);
            if (url != null)
                json.put(ObjectAPIHandler.SERVICE_KEY, url);
        } catch (IOException e) {
            json.put(ObjectAPIHandler.SUCCESS_KEY, false);
            json.put(ObjectAPIHandler.COMMENT_KEY, e.getMessage());
        }
    } else {
        json.put(ObjectAPIHandler.SUCCESS_KEY, false);
        json.put(ObjectAPIHandler.COMMENT_KEY, "the request must contain an index, type, and an id");
    }
    return new ServiceResponse(json);
}
Also used : ServiceResponse(net.yacy.grid.http.ServiceResponse) JSONObject(org.json.JSONObject) Index(net.yacy.grid.io.index.Index) IOException(java.io.IOException)

Example 23 with ServiceResponse

use of net.yacy.grid.http.ServiceResponse in project yacy_grid_mcp by yacy.

the class YaCySearchService method serviceImpl.

@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response) {
    String callback = call.get("callback", "");
    boolean jsonp = callback != null && callback.length() > 0;
    boolean minified = call.get("minified", false);
    boolean explain = call.get("explain", false);
    String q = call.get("query", "");
    Classification.ContentDomain contentdom = Classification.ContentDomain.contentdomParser(call.get("contentdom", "all"));
    // important: call arguments may overrule parsed collection values if not empty. This can be used for authentified indexes!
    String collection = call.get("collection", "");
    // to be compatible with the site-operator of GSA, we use a vertical pipe symbol here to divide collections.
    collection = collection.replace(',', '|');
    String[] collections = collection.length() == 0 ? new String[0] : collection.split("\\|");
    int maximumRecords = call.get("maximumRecords", call.get("rows", call.get("num", 10)));
    int startRecord = call.get("startRecord", call.get("start", 0));
    // int meanCount = call.get("meanCount", 5);
    int timezoneOffset = call.get("timezoneOffset", -1);
    // String nav = call.get("nav", "");
    // String prefermaskfilter = call.get("prefermaskfilter", "");
    // String constraint = call.get("constraint", "");
    int facetLimit = call.get("facetLimit", 10);
    String facetFields = call.get("facetFields", YaCyQuery.FACET_DEFAULT_PARAMETER);
    List<WebMapping> facetFieldMapping = new ArrayList<>();
    for (String s : facetFields.split(",")) facetFieldMapping.add(WebMapping.valueOf(s));
    Sort sort = new Sort(call.get("sort", ""));
    YaCyQuery yq = new YaCyQuery(q, collections, contentdom, timezoneOffset);
    ElasticsearchClient ec = Data.gridIndex.getElasticClient();
    HighlightBuilder hb = new HighlightBuilder().field(WebMapping.text_t.getMapping().name()).preTags("").postTags("").fragmentSize(140);
    ElasticsearchClient.Query query = ec.query("web", null, yq.queryBuilder, null, sort, hb, timezoneOffset, startRecord, maximumRecords, facetLimit, explain, facetFieldMapping.toArray(new WebMapping[facetFieldMapping.size()]));
    JSONObject json = new JSONObject(true);
    JSONArray channels = new JSONArray();
    json.put("channels", channels);
    JSONObject channel = new JSONObject(true);
    channels.put(channel);
    JSONArray items = new JSONArray();
    channel.put("title", "Search for " + q);
    channel.put("description", "Search for " + q);
    channel.put("startIndex", "" + startRecord);
    channel.put("itemsPerPage", "" + items.length());
    channel.put("searchTerms", q);
    channel.put("totalResults", Integer.toString(query.hitCount));
    channel.put("items", items);
    List<Map<String, Object>> result = query.results;
    List<String> explanations = query.explanations;
    for (int hitc = 0; hitc < result.size(); hitc++) {
        WebDocument doc = new WebDocument(result.get(hitc));
        JSONObject hit = new JSONObject(true);
        String titleString = doc.getTitle();
        String link = doc.getLink();
        if (Classification.ContentDomain.IMAGE == contentdom) {
            // the url before we extract the link
            hit.put("url", link);
            link = doc.pickImage((String) link);
            hit.put("icon", link);
            hit.put("image", link);
        }
        String snippet = doc.getSnippet(query.highlights.get(hitc), yq);
        Date last_modified_date = doc.getDate();
        int size = doc.getSize();
        int sizekb = size / 1024;
        int sizemb = sizekb / 1024;
        String size_string = sizemb > 0 ? (Integer.toString(sizemb) + " mbyte") : sizekb > 0 ? (Integer.toString(sizekb) + " kbyte") : (Integer.toString(size) + " byte");
        String host = doc.getHost();
        hit.put("title", titleString);
        hit.put("link", link.toString());
        hit.put("description", snippet);
        hit.put("pubDate", DateParser.formatRFC1123(last_modified_date));
        hit.put("size", Integer.toString(size));
        hit.put("sizename", size_string);
        hit.put("host", host);
        if (explain) {
            hit.put("explanation", explanations.get(hitc));
        }
        items.put(hit);
    }
    ;
    JSONArray navigation = new JSONArray();
    channel.put("navigation", navigation);
    Map<String, List<Map.Entry<String, Long>>> aggregations = query.aggregations;
    for (Map.Entry<String, List<Map.Entry<String, Long>>> fe : aggregations.entrySet()) {
        String facetname = fe.getKey();
        WebMapping mapping = WebMapping.valueOf(facetname);
        JSONObject facetobject = new JSONObject(true);
        facetobject.put("facetname", mapping.getMapping().getFacetname());
        facetobject.put("displayname", mapping.getMapping().getDisplayname());
        facetobject.put("type", mapping.getMapping().getFacettype());
        facetobject.put("min", "0");
        facetobject.put("max", "0");
        facetobject.put("mean", "0");
        facetobject.put("count", fe.getValue().size());
        JSONArray elements = new JSONArray();
        facetobject.put("elements", elements);
        for (Map.Entry<String, Long> element : fe.getValue()) {
            JSONObject elementEntry = new JSONObject(true);
            elementEntry.put("name", element.getKey());
            elementEntry.put("count", element.getValue().toString());
            elementEntry.put("modifier", mapping.getMapping().getFacetmodifier() + ":" + element.getKey());
            elements.put(elementEntry);
        }
        navigation.put(facetobject);
    }
    if (jsonp) {
        StringBuilder sb = new StringBuilder(1024);
        sb.append(callback).append("([").append(json.toString(minified ? 0 : 2)).append("]);");
        return new ServiceResponse(sb.toString());
    } else {
        return new ServiceResponse(json);
    }
}
Also used : ArrayList(java.util.ArrayList) ServiceResponse(net.yacy.grid.http.ServiceResponse) Classification(net.yacy.grid.tools.Classification) Sort(net.yacy.grid.io.index.Sort) ArrayList(java.util.ArrayList) List(java.util.List) ElasticsearchClient(net.yacy.grid.io.index.ElasticsearchClient) JSONArray(org.json.JSONArray) YaCyQuery(net.yacy.grid.io.index.YaCyQuery) Date(java.util.Date) WebDocument(net.yacy.grid.io.index.WebDocument) JSONObject(org.json.JSONObject) WebMapping(net.yacy.grid.io.index.WebMapping) Map(java.util.Map) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

Aggregations

ServiceResponse (net.yacy.grid.http.ServiceResponse)23 JSONObject (org.json.JSONObject)20 IOException (java.io.IOException)18 Index (net.yacy.grid.io.index.Index)6 GridQueue (net.yacy.grid.io.messages.GridQueue)6 JSONArray (org.json.JSONArray)5 Date (java.util.Date)4 SusiThought (ai.susi.mind.SusiThought)3 Map (java.util.Map)3 QueryLanguage (net.yacy.grid.io.index.Index.QueryLanguage)3 JSONList (net.yacy.grid.tools.JSONList)3 SusiAction (ai.susi.mind.SusiAction)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 APIHandler (net.yacy.grid.http.APIHandler)2 ObjectAPIHandler (net.yacy.grid.http.ObjectAPIHandler)2 ElasticsearchClient (net.yacy.grid.io.index.ElasticsearchClient)2 IndexFactory (net.yacy.grid.io.index.IndexFactory)2 Sort (net.yacy.grid.io.index.Sort)2 WebDocument (net.yacy.grid.io.index.WebDocument)2