Search in sources :

Example 1 with Index

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

the class AddService 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", "");
    String id = call.get("id", "");
    // this contains the JSON object
    byte[] object = call.get("object", EMPTY_OBJECT);
    JSONObject json = new JSONObject(true);
    if (indexName.length() > 0 && typeName.length() > 0 && id.length() > 0 && object.length > 0) {
        try {
            Index index = Data.gridIndex.getElasticIndex();
            JSONObject payload = new JSONObject(new JSONTokener(new String(object, StandardCharsets.UTF_8)));
            IndexFactory factory = index.add(indexName, typeName, id, payload);
            String url = factory.getConnectionURL();
            json.put(ObjectAPIHandler.SUCCESS_KEY, true);
            if (url != null)
                json.put(ObjectAPIHandler.SERVICE_KEY, url);
        } catch (IOException | JSONException 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, id and json object");
    }
    return new ServiceResponse(json);
}
Also used : JSONTokener(org.json.JSONTokener) ServiceResponse(net.yacy.grid.http.ServiceResponse) JSONObject(org.json.JSONObject) IndexFactory(net.yacy.grid.io.index.IndexFactory) JSONException(org.json.JSONException) Index(net.yacy.grid.io.index.Index) IOException(java.io.IOException)

Example 2 with Index

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

the class CheckService method serviceImpl.

@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response) {
    JSONObject json = new JSONObject(true);
    try {
        Index index = Data.gridIndex.getElasticIndex();
        IndexFactory factory = index.checkConnection();
        String url = factory.getConnectionURL();
        json.put(ObjectAPIHandler.SUCCESS_KEY, true);
        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());
    }
    return new ServiceResponse(json);
}
Also used : ServiceResponse(net.yacy.grid.http.ServiceResponse) JSONObject(org.json.JSONObject) IndexFactory(net.yacy.grid.io.index.IndexFactory) Index(net.yacy.grid.io.index.Index) IOException(java.io.IOException)

Example 3 with Index

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

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

use of net.yacy.grid.io.index.Index 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)6 ServiceResponse (net.yacy.grid.http.ServiceResponse)6 Index (net.yacy.grid.io.index.Index)6 JSONObject (org.json.JSONObject)6 QueryLanguage (net.yacy.grid.io.index.Index.QueryLanguage)3 IndexFactory (net.yacy.grid.io.index.IndexFactory)2 JSONList (net.yacy.grid.tools.JSONList)1 JSONException (org.json.JSONException)1 JSONTokener (org.json.JSONTokener)1