Java Code Examples for io.searchbox.client.JestResult#getErrorMessage()

The following examples show how to use io.searchbox.client.JestResult#getErrorMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ESRegistry.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * @see io.apiman.gateway.engine.IRegistry#publishApi(io.apiman.gateway.engine.beans.Api, io.apiman.gateway.engine.async.IAsyncResultHandler)
 */
@Override
public void publishApi(final Api api, final IAsyncResultHandler<Void> handler) {
    try {
        String id = getApiId(api);
        Index index = new Index.Builder(api).refresh(false)
                .index(getIndexName()).setParameter(Parameters.OP_TYPE, "index") //$NON-NLS-1$
                .type("api").id(id).build(); //$NON-NLS-1$
        JestResult result = getClient().execute(index);
        if (!result.isSucceeded()) {
            throw new IOException(result.getErrorMessage());
        } else {
            handler.handle(AsyncResultImpl.create((Void) null));
        }
    } catch (Exception e) {
        handler.handle(AsyncResultImpl.create(
                new PublishingException(Messages.i18n.format("ESRegistry.ErrorPublishingApi"), e),  //$NON-NLS-1$
                Void.class));
    }
}
 
Example 2
Source File: AbstractClientFactory.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an index.
 * @param indexName
 * @throws Exception
 */
@SuppressWarnings("nls")
protected void createIndex(JestClient client, String indexName, String settingsName) throws Exception {
    URL settings = AbstractClientFactory.class.getResource(settingsName);
    String source = IOUtils.toString(settings);
    JestResult response = client.execute(new CreateIndex.Builder(indexName).settings(source).build());
    if (!response.isSucceeded()) {
        // When running in e.g. Wildfly, the Gateway exists as two separate WARs - the API and the
        // runtime Gateway itself.  They both create a registry and thus they both try to initialize
        // the ES index if it doesn't exist.  A race condition could result in both WARs trying to
        // create the index.  So a result of "IndexAlreadyExistsException" should be ignored.
        if (!indexAlreadyExistsException(response)) {
            throw new Exception("Failed to create index: '" + indexName + "' Reason: " + response.getErrorMessage());
        }
    }
}
 
Example 3
Source File: IndexManagerBean.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
private void createIndex(String indexName) throws IndexerNotAvailableException, IndexerRequestException {
    Settings settings = Settings.builder().build();
    JestResult result;

    try {
        result = esClient.execute(new CreateIndex.Builder(indexName).settings(settings.toString()).build());
    } catch (IOException e) {
        throw new IndexerNotAvailableException();
    }

    if (!result.isSucceeded()) {
        LOGGER.log(Level.WARNING, "Cannot create index" + indexName + ": " + result.getErrorMessage());
        throw new IndexerRequestException(result.getErrorMessage());
    }
}
 
Example 4
Source File: EsStorage.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * @param indexName
 * @throws Exception
 */
private void createIndex(String indexName) throws Exception {
    URL settings = getClass().getResource("index-settings.json"); //$NON-NLS-1$
    String source = IOUtils.toString(settings);
    JestResult response = esClient.execute(new CreateIndex.Builder(indexName).settings(source).build());
    if (!response.isSucceeded()) {
        throw new StorageException("Failed to create index " + indexName + ": " + response.getErrorMessage()); //$NON-NLS-1$ //$NON-NLS-2$
    } else {
        logger.info("Created index:" + indexName);
    }
}
 
Example 5
Source File: EsStorage.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IStorage#deleteMemberships(java.lang.String, java.lang.String)
 */
@Override
@SuppressWarnings("nls")
public void deleteMemberships(String userId, String organizationId) throws StorageException {
    QueryBuilder query =
        FilterBuilders.boolFilter(
                FilterBuilders.filter(
                        FilterBuilders.termFilter("organizationId", organizationId),
                        FilterBuilders.termFilter("userId", userId)
                )
        );
    try {
        String string = query.string();
        // Workaround for bug in FilteredQueryBuilder which does not (yet) wrap
        // the JSON in a query element
        if (string.indexOf("query") < 0 || string.indexOf("query") > 7) {
            string = "{ \"query\" : " + string + "}";
        }
        DeleteByQuery deleteByQuery = new DeleteByQuery.Builder(string).addIndex(getIndexName())
                .addType("roleMembership").build();
        JestResult response = esClient.execute(deleteByQuery);
        if (!response.isSucceeded()) {
            throw new StorageException(response.getErrorMessage());
        }
    } catch (Exception e) {
        throw new StorageException(e);
    }
}
 
Example 6
Source File: ESRegistry.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * @see io.apiman.gateway.engine.IRegistry#registerClient(io.apiman.gateway.engine.beans.Client, io.apiman.gateway.engine.async.IAsyncResultHandler)
 */
@Override
public void registerClient(final Client client, final IAsyncResultHandler<Void> handler) {
    try {
        // Validate the client and populate the api map with apis found during validation.
        validateClient(client);

        String id = getClientId(client);
        Index index = new Index.Builder(client)
                .refresh(false)
                .index(getIndexName())
                .setParameter(Parameters.OP_TYPE, "index") //$NON-NLS-1$
                .type("client") //$NON-NLS-1$
                .id(id)
                .build();
        JestResult result = getClient().execute(index);
        if (!result.isSucceeded()) {
            throw new IOException(result.getErrorMessage());
        } else {
            handler.handle(AsyncResultImpl.create((Void) null));
        }
    } catch (IOException e) {
        handler.handle(AsyncResultImpl.create(
                new RegistrationException(Messages.i18n.format("ESRegistry.ErrorRegisteringClient"), e),  //$NON-NLS-1$
                Void.class));
    } catch (RuntimeException re) {
        handler.handle(AsyncResultImpl.create(re, Void.class));
    }
}
 
Example 7
Source File: ElasticSearchLabelValuesStmt.java    From sofa-lookout with Apache License 2.0 4 votes vote down vote up
@Override
public List<String> executeQuery() {
    //合理大小
    size = size <= 0 || size > MAX_LABEL_SIZE ? MAX_LABEL_SIZE : size;

    QueryBuilder queryBuilder = new QueryBuilder();
    if (label != null) {
        //辅助筛选的label是?
        if (MetricName.equals(label.getName())) {
            queryBuilder.addMustQueries(new QueryBuilder.StringQuery().addMetricName(label.getValue()).toString());
        } else {
            queryBuilder.addMustQueries(
                    new QueryBuilder.StringQuery().addTagCond(label.getName(), label.getValue()).toString());
        }
    }

    Search search = null;
    String query = null;
    //query for metric names;
    if (MetricName.equals(labelName)) {
        if (!StringUtils.isEmpty(q)) {
            //metricName的模糊条件过滤
            queryBuilder.addMustQueries(new QueryBuilder.StringQuery(true).addMetricName(q + "*").toString());
        }
        query = queryBuilder.buildMetricNameQuery(startTime, endTime, size);
    } else {
        //普通tag的模糊条件过滤:q
        query = queryBuilder.buildLabelQuery(labelName, q, startTime, endTime, size);
    }
    search = new Search.Builder(query).addIndex(index).build();

    try {
        JestResult jestResult = jestClient.execute(search);

        if (!jestResult.isSucceeded()) {
            logger.error("execute query err:{}, query body:{}!", jestResult.getErrorMessage(), query);
            throw new RuntimeException(jestResult.getErrorMessage());
        }
        List<String> tags = new ArrayList<>();
        //pop to series set;
        JsonArray elements = jestResult.getJsonObject().getAsJsonObject("aggregations").getAsJsonObject("tags").getAsJsonArray(
                "buckets");
        if (elements != null) {
            elements.forEach(jsonElement -> {
                String key = jsonElement.getAsJsonObject().getAsJsonPrimitive("key").getAsString();
                tags.add(key.substring(key.indexOf('=') + 1));
            });
        }
        return tags;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: ElasticSearchQueryStmt.java    From sofa-lookout with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Series> executeQuery() {
    QueryBuilder queryBuilder = new QueryBuilder();
    for (Matcher m : matchers) {
        if (StringUtils.equals(MetricName, m.getName())) {
            Preconditions.checkState(m.getType() == MatchEqual, "metric name match should be equal type!");
            queryBuilder.addMustQueries(new QueryBuilder.StringQuery().addMetricName(m.getValue()).toString());
            continue;
        }

        switch (m.getType()) {
            case MatchEqual: {
                queryBuilder.addMustQueries(new QueryBuilder.StringQuery().addTagCond(
                        m.getName(), m.getValue()
                ).toString());
                break;
            }
            case MatchRegexp: {
                queryBuilder.addMustQueries(new QueryBuilder.RegexQuery(
                        m.getName(), m.getValue()
                ).toString());
                break;
            }
            case MatchNotEqual: {
                queryBuilder.addMustNotQueries(new QueryBuilder.StringQuery().addTagCond(
                        m.getName(), m.getValue()
                ).toString());
                break;
            }
            case MatchNotRegexp: {
                queryBuilder.addMustNotQueries(new QueryBuilder.RegexQuery(
                        m.getName(), m.getValue()
                ).toString());
                break;
            }
        }
    }
    String query = queryBuilder.build(startTime, endTime);

    Search search = new Search.Builder(query).addIndex(index).build();

    try {
        JestResult jestResult = jestClient.execute(search);

        if (!jestResult.isSucceeded()) {
            logger.error("execute query err:{}, query body:{}!", jestResult.getErrorMessage(), query);
            throw new RuntimeException(jestResult.getErrorMessage());
        }
        HashMap<Labels, Series> seriesMap = new HashMap<>();
        //pop to series set;
        long totalSize = jestResult.getJsonObject().getAsJsonObject("hits").getAsJsonPrimitive("total").getAsLong();
        if (totalSize > MAX_DATA_POINTS) {
            throw new TooManyPointsException(totalSize);
        }

        JsonArray elements = jestResult.getJsonObject().getAsJsonObject("hits").getAsJsonArray("hits");
        if (elements != null) {
            elements.forEach(jsonElement -> {
                JsonObject obj = jsonElement.getAsJsonObject().getAsJsonObject("_source");
                Labels labels = new Labels();
                labels.add(new Label(MetricName, obj.get("id").getAsString()));
                //add tags
                JsonArray tagArr = obj.getAsJsonArray("tags");
                tagArr.forEach(je -> {
                    List<String> kv = Splitter.on('=').splitToList(je.getAsString());
                    labels.add(new Label(kv.get(0), kv.get(1)));
                });

                String timeStr = obj.get("time").getAsString();
                float val = obj.get("value").getAsFloat();
                Series.Point point = new Series.Point(str2Time(timeStr), val);

                Series s = seriesMap.get(labels);
                if (s != null) {
                    s.add(point);
                } else {
                    s = new Series();
                    s.setMetric(labels);
                    s.add(point);
                    seriesMap.put(labels, s);
                }
            });
        }
        return seriesMap.values();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 9
Source File: DefaultTimeTaskController.java    From easy-sync with Apache License 2.0 4 votes vote down vote up
@ResponseBody
@RequestMapping("esRebuildIndex.htm")
public String esRebuildIndex(long id,HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
    TimeTask timeTask=timeTaskService.get(id);
    SyncConfig syncConfig= syncConfigDao.queryOneByProps (null,"timeTaskId",id);
    EsConfig esConfig = Global.toObject(timeTask.getTaskConfig(), EsConfig.class);
    String newIndexName=esConfig.getEs().getIndexAlias()+"_"+ DateFormatUtils.format(new Date(),"yyMMdd_HHmmss");
    Jest jest =new Jest(esConfig.getEs());

    boolean safe= esConfig.getRebuild().isSwitchAfterFullSync();

    //-- first build
    if(StringUtils.isBlank( syncConfig.getTaskConfig1()) || !safe){


        if(esConfig.getRebuild().isDeleteOldIndex() && StringUtils.isNotBlank(syncConfig.getWaitIndexName1())){
            if(!jest.deleteIndex(syncConfig.getWaitIndexName1())){
                logger.error("delete old index "+syncConfig.getWaitIndexName1()+" fail.");
            }

        }

        syncConfig.setWaitIndexName1(newIndexName);
        syncConfig.setFullStatus1(Const.REBUILD_NEED);



    }else{
        //todo:delete old index , judge status not run first
        syncConfig.setWaitIndexName2(newIndexName);
        syncConfig.setFullStatus2(Const.REBUILD_NEED);
    }



  /*  String settings = "\"settings\" : {\n" +
            "        \"number_of_shards\" : 5,\n" +
            "        \"number_of_replicas\" : 1\n" +
            "    }\n"*/;

    logger.info("begin to create index {}",newIndexName);

   EsMapping esMapping= esConfig.getEsMapping();
    EsField[] esFields=esMapping.getEsFields();
    JSONObject  fields=new JSONObject();

    for(EsField esField:esFields){

        JSONObject  field=new JSONObject();
        field.put("type",esField.getType());
        if("text".equalsIgnoreCase(esField.getType())){
            field.put("analyzer", esConfig.getEs().getAnalyzer());
        }

        logger.debug("add custom config");
        if(StringUtils.isNotBlank(esField.getCustom())){
            field.putAll( Global.toObject( esField.getCustom()));
        }

        fields.put(esField.getTarget(),field);

    }

    JSONObject  mapping=new JSONObject();
    JSONObject  properties=new JSONObject();
    properties.put("properties",fields);



    final String type="doc";
    mapping.put(type,properties);

    PutMapping putMapping = new PutMapping.Builder(
            newIndexName,
            type,
            mapping.toJSONString()
    ).build();

    CreateIndex.Builder builder=new CreateIndex.Builder(newIndexName);
    if(StringUtils.isNotBlank(esConfig.getEs().getIndexSettings())){
        builder.settings(esConfig.getEs().getIndexSettings());
    }

    JestResult jestResult= jest.getJestClient().execute(builder.build());

    if(! jestResult.isSucceeded()){
        throw new Exception(jestResult.getErrorMessage());
    }

    jestResult= jest.getJestClient().execute(putMapping);

    if(! jestResult.isSucceeded()){
        throw new Exception(jestResult.getErrorMessage());
    }
    syncConfigDao.save(syncConfig);

    return newIndexName;


}
 
Example 10
Source File: EsStorage.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IStorage#deleteOrganization(io.apiman.manager.api.beans.orgs.OrganizationBean)
 */
@Override
@SuppressWarnings("nls")
public void deleteOrganization(OrganizationBean organization) throws StorageException {
    try {
        String orgId = organization.getId().replace('"', '_');

        QueryBuilder qb =
            FilterBuilders.boolFilter(
                FilterBuilders.shouldFilter(
                    FilterBuilders.termFilter("organizationId", orgId),
                    FilterBuilders.termFilter("clientOrganizationId", orgId),
                    FilterBuilders.termFilter("apiOrganizationId", orgId)
                )
            );

        SearchSourceBuilder query = new SearchSourceBuilder().query(qb);
        DeleteByQuery deleteByQuery = new DeleteByQuery.Builder(query.string()).addIndex(getIndexName())
                .addType("api")
                .addType("apiPolicies")
                .addType("apiVersion")
                .addType("auditEntry")
                .addType("client")
                .addType("clientPolicies")
                .addType("clientVersion")
                .addType("contract")
                .addType("plan")
                .addType("planPolicies")
                .addType("planVersion")
                .addType("roleMembership")
                .build();

        JestResult response = esClient.execute(deleteByQuery);
        if (!response.isSucceeded()) {
            throw new StorageException(response.getErrorMessage());
        }
        deleteEntity("organization", orgId); //$NON-NLS-1$
    } catch (Exception e) {
        throw new StorageException(e);
    }
}
 
Example 11
Source File: EsStorage.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IStorage#deleteClient(io.apiman.manager.api.beans.clients.ClientBean)
 */
@Override
@SuppressWarnings("nls")
public void deleteClient(ClientBean client) throws StorageException {
    String clientId = client.getId().replace('"', '_');
    String orgId = client.getOrganization().getId().replace('"', '_');

    QueryBuilder qb =
        QueryBuilders.query(
            FilterBuilders.boolFilter(
                FilterBuilders.filter(
                    FilterBuilders.boolFilter(
                        FilterBuilders.shouldFilter(
                            FilterBuilders.termFilter("clientOrganizationId", orgId),
                            FilterBuilders.termFilter("organizationId", orgId)
                        )
                    ),
                    FilterBuilders.boolFilter(
                        FilterBuilders.shouldFilter(
                            FilterBuilders.boolFilter(
                                FilterBuilders.filter(
                                    FilterBuilders.termFilter("entityId", clientId),
                                    FilterBuilders.termFilter("entityType", AuditEntityType.Client.name())
                                )
                            ),
                            FilterBuilders.boolFilter(
                                FilterBuilders.filter(
                                    FilterBuilders.termFilter("entityId", clientId),
                                    FilterBuilders.termFilter("type", AuditEntityType.Client.name())
                                )
                            ),
                            FilterBuilders.termFilter("clientId", clientId)
                        )
                    )
                )
            )
        );

    try {
        DeleteByQuery deleteByQuery = new DeleteByQuery.Builder(qb.string()).addIndex(getIndexName())
                .addType("auditEntry")
                .addType("client")
                .addType("clientVersion")
                .addType("clientPolicies")
                .addType("contract")
                .build();

        JestResult response = esClient.execute(deleteByQuery);
        if (!response.isSucceeded()) {
            throw new StorageException(response.getErrorMessage());
        }
    } catch (Exception e) {
        throw new StorageException(e);
    }
    deleteEntity("client", id(orgId, clientId)); //$NON-NLS-1$
}
 
Example 12
Source File: EsStorage.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IStorage#deleteApi(io.apiman.manager.api.beans.apis.ApiBean)
 */
@Override
@SuppressWarnings("nls")
public void deleteApi(ApiBean api) throws StorageException {
    String apiId = api.getId().replace('"', '_');
    String orgId = api.getOrganization().getId().replace('"', '_');

    QueryBuilder qb =
            QueryBuilders.query(
                FilterBuilders.boolFilter(
                    FilterBuilders.filter(
                        FilterBuilders.boolFilter(
                            FilterBuilders.shouldFilter(
                                FilterBuilders.termFilter("apiOrganizationId", orgId),
                                FilterBuilders.termFilter("organizationId", orgId)
                            )
                        ),
                        FilterBuilders.boolFilter(
                            FilterBuilders.shouldFilter(
                                FilterBuilders.boolFilter(
                                    FilterBuilders.filter(
                                        FilterBuilders.termFilter("entityId", apiId),
                                        FilterBuilders.termFilter("entityType", AuditEntityType.Api.name())
                                    )
                                ),
                                FilterBuilders.boolFilter(
                                    FilterBuilders.filter(
                                        FilterBuilders.termFilter("entityId", apiId),
                                        FilterBuilders.termFilter("type", AuditEntityType.Api.name())
                                    )
                                ),
                                FilterBuilders.termFilter("apiId", apiId)
                            )
                        )
                    )
                )
            );

    try {
        DeleteByQuery deleteByQuery = new DeleteByQuery.Builder(qb.string()).addIndex(getIndexName())
                .addType("auditEntry")
                .addType("api")
                .addType("apiVersion")
                .addType("apiPolicies")
                .addType("contract")
                .build();

        JestResult response = esClient.execute(deleteByQuery);
        if (!response.isSucceeded()) {
            throw new StorageException(response.getErrorMessage());
        }
    } catch (Exception e) {
        throw new StorageException(e);
    }
    deleteEntity("api", id(orgId, apiId)); //$NON-NLS-1$
}
 
Example 13
Source File: EsStorage.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IStorage#deletePlan(io.apiman.manager.api.beans.plans.PlanBean)
 */
@Override
@SuppressWarnings("nls")
public void deletePlan(PlanBean plan) throws StorageException {
    String planId = plan.getId().replace('"', '_');
    String orgId = plan.getOrganization().getId().replace('"', '_');

    QueryBuilder qb =
        FilterBuilders.boolFilter(
            FilterBuilders.shouldFilter(
                FilterBuilders.boolFilter(
                    FilterBuilders.filter(
                       FilterBuilders.termFilter("entityId", planId),
                       FilterBuilders.termFilter("entityType", AuditEntityType.Plan.name()),
                       FilterBuilders.termFilter("organizationId", orgId)
                    )
                ),
                FilterBuilders.boolFilter(
                    FilterBuilders.filter(
                       FilterBuilders.termFilter("planId", planId),
                       FilterBuilders.termFilter("organizationId", orgId)
                    )
                ),
                FilterBuilders.boolFilter(
                    FilterBuilders.filter(
                       FilterBuilders.termFilter("entityId", planId),
                       FilterBuilders.termFilter("type", AuditEntityType.Plan.name())
                    )
                )
            )
        );

    try {
        DeleteByQuery deleteByQuery = new DeleteByQuery.Builder("{\"query\":"+qb.string()+"}") // TODO
                .addIndex(getIndexName())
                .addType("auditEntry")
                .addType("planVersion")
                .addType("planPolicies")
                .build();

        JestResult response = esClient.execute(deleteByQuery);
        if (!response.isSucceeded()) {
            throw new StorageException(response.getErrorMessage());
        }
    } catch (Exception e) {
        throw new StorageException(e);
    }
    deleteEntity("plan", id(plan.getOrganization().getId(), plan.getId())); //$NON-NLS-1$
}
 
Example 14
Source File: Jest.java    From easy-sync with Apache License 2.0 3 votes vote down vote up
public boolean indexExists(String indexName) throws IOException {

        AliasIndices aliasIndices = new AliasIndices(indexName);
        JestResult jestResult = jestClient.execute(aliasIndices);

        //because index and alias can not be the same,    alias found , means index not exists
        if (jestResult.isSucceeded()) {
            return  false;
        } else if (jestResult.getResponseCode() == 404) {

        } else {

           throw new IOException(jestResult.getResponseCode()+","+jestResult.getErrorMessage());

        }



        jestResult = jestClient.execute(new IndicesExists.Builder(indexName).build());
            if (jestResult.isSucceeded()) {
                return true;
            }else  if(jestResult.getResponseCode()==404){
                    return false;

            }else{
                throw new IOException(jestResult.getResponseCode()+","+jestResult.getErrorMessage());
            }




    }