Search in sources :

Example 1 with QueryLanguage

use of net.yacy.grid.io.index.Index.QueryLanguage in project yacy_grid_mcp by yacy.

the class CountService 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;
    QueryLanguage language = QueryLanguage.valueOf(call.get("language", "yacy"));
    String query = call.get("query", "");
    JSONObject json = new JSONObject(true);
    if (indexName.length() > 0 && query.length() > 0) {
        try {
            Index index = Data.gridIndex.getElasticIndex();
            String url = index.checkConnection().getConnectionURL();
            long count = index.count(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 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 2 with QueryLanguage

use of net.yacy.grid.io.index.Index.QueryLanguage in project yacy_grid_mcp by yacy.

the class QueryService 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", "");
    QueryLanguage language = QueryLanguage.valueOf(call.get("language", "yacy"));
    String query = call.get("query", "");
    int maximumRecords = call.get("maximumRecords", call.get("rows", call.get("num", 10)));
    int startRecord = call.get("startRecord", call.get("start", 0));
    JSONObject json = new JSONObject(true);
    if (indexName.length() > 0 && id.length() > 0) {
        try {
            Index index = Data.gridIndex.getElasticIndex();
            String url = index.checkConnection().getConnectionURL();
            JSONObject object = index.query(indexName, typeName, id);
            json.put(ObjectAPIHandler.SUCCESS_KEY, true);
            JSONList list = new JSONList();
            if (object != null)
                list.add(object);
            json.put("count", list.length());
            json.put("list", list.toArray());
            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 && query.length() > 0) {
        try {
            Index index = Data.gridIndex.getElasticIndex();
            String url = index.checkConnection().getConnectionURL();
            JSONList list = index.query(indexName, typeName, language, query, startRecord, maximumRecords);
            json.put(ObjectAPIHandler.SUCCESS_KEY, true);
            json.put("count", list.length());
            json.put("list", list.toArray());
            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) JSONList(net.yacy.grid.tools.JSONList)

Example 3 with QueryLanguage

use of net.yacy.grid.io.index.Index.QueryLanguage 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)

Aggregations

IOException (java.io.IOException)3 ServiceResponse (net.yacy.grid.http.ServiceResponse)3 Index (net.yacy.grid.io.index.Index)3 QueryLanguage (net.yacy.grid.io.index.Index.QueryLanguage)3 JSONObject (org.json.JSONObject)3 JSONList (net.yacy.grid.tools.JSONList)1