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

The following examples show how to use io.searchbox.client.JestResult#isSucceeded() . 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: AbstractClientFactory.java    From apiman with Apache License 2.0 7 votes vote down vote up
/**
 * Called to initialize the storage.
 * @param client the jest client
 * @param indexName the name of the ES index to initialize
 * @param defaultIndexName the default ES index - used to determine which -settings.json file to use
 */
protected void initializeClient(JestClient client, String indexName, String defaultIndexName) {
    try {
        client.execute(new Health.Builder().build());
        Action<JestResult> action = new IndicesExists.Builder(indexName).build();
        // There was occasions where a race occurred here when multiple threads try to
        // create the index simultaneously. This caused a non-fatal, but annoying, exception.
        synchronized(AbstractClientFactory.class) {
            JestResult result = client.execute(action);
            if (!result.isSucceeded()) {
                createIndex(client, indexName, defaultIndexName + "-settings.json"); //$NON-NLS-1$
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: JestHttpObjectFactory.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
protected JestResultHandler<JestResult> createResultHandler(Bulk bulk, Function<Bulk, Boolean> failureHandler) {
    return new JestResultHandler<JestResult>() {
        @Override
        public void completed(JestResult result) {

            backoffPolicy.deregister(bulk);

            if (!result.isSucceeded()) {
                LOG.warn(result.getErrorMessage());
                failureHandler.apply(bulk);
            }
        }
        @Override
        public void failed(Exception ex) {
            LOG.warn(ex.getMessage(), ex);
            backoffPolicy.deregister(bulk);
            failureHandler.apply(bulk);
        }
    };
}
 
Example 3
Source File: EsStorage.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * This method will check if a migration of the index is needed
 * @return true or false
 * @throws IOException
 */
private boolean isMigrationNeeded() throws IOException {
    // Check if migration is needed for too large swagger files - "keyword" to "text"
    // See https://github.com/apiman/apiman/issues/736
    JestResult result = esClient.execute(new GetMapping.Builder().addIndex(getIndexName()).build());
    if (result.isSucceeded()) {
        boolean migrationNeeded = result.getJsonObject()
                .getAsJsonObject(getIndexName())
                .getAsJsonObject("mappings")
                .getAsJsonObject("auditEntry")
                .getAsJsonObject("properties")
                .getAsJsonObject("data")
                .getAsJsonPrimitive("type")
                .getAsString()
                .equals("keyword");
        if (migrationNeeded) {
            logger.info("Migration of Elasticsearch index is needed.");
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
 
Example 4
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 5
Source File: EsStorage.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * Gets an entity.  Callers must unmarshal the resulting map.
 * @param type
 * @param id
 * @throws StorageException
 */
private Map<String, Object> getEntity(String type, String id) throws StorageException {
    try {
        JestResult response = esClient.execute(new Get.Builder(getIndexName(), id).type(type).build());
        if (!response.isSucceeded()) {
            return null;
        }
        return response.getSourceAsObject(Map.class);
    } catch (Exception e) {
        throw new StorageException(e);
    }
}
 
Example 6
Source File: BufferedJestHttpObjectFactory.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
protected JestResultHandler<JestResult> createResultHandler(Bulk bulk, Function<Bulk, Boolean> failureHandler) {
    return new JestResultHandler<JestResult>() {

        @Override
        public void completed(JestResult result) {

            backoffPolicy.deregister(bulk);

            if (!result.isSucceeded()) {
                LOG.warn(result.getErrorMessage());
                // TODO: filter only failed items when retry is ready.
                // failing whole bulk for now
                failureHandler.apply(bulk);
            }
            ((BufferedBulk)bulk).completed();
        }

        @Override
        public void failed(Exception ex) {
            LOG.warn(ex.getMessage(), ex);
            backoffPolicy.deregister(bulk);
            failureHandler.apply(bulk);
            ((BufferedBulk)bulk).completed();
        }

    };
}
 
Example 7
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 8
Source File: ESRateLimiterComponent.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * @see io.apiman.gateway.engine.components.IRateLimiterComponent#accept(java.lang.String, io.apiman.gateway.engine.rates.RateBucketPeriod, long, long, io.apiman.gateway.engine.async.IAsyncResultHandler)
 */
@Override
public void accept(final String bucketId, final RateBucketPeriod period, final long limit,
        final long increment, final IAsyncResultHandler<RateLimitResponse> handler) {
    final String id = id(bucketId);

    try {
        Get get = new Get.Builder(getIndexName(), id).type("rateBucket").build(); //$NON-NLS-1$
        JestResult result = getClient().execute(get);
        RateLimiterBucket bucket;
        long version;
        if (result.isSucceeded()) {
            // use the existing bucket
            version = result.getJsonObject().get("_version").getAsLong(); //$NON-NLS-1$
            bucket = result.getSourceAsObject(RateLimiterBucket.class);
        } else {
            // make a new bucket
            version = 0;
            bucket = new RateLimiterBucket();
        }
        bucket.resetIfNecessary(period);

        final RateLimitResponse rlr = new RateLimitResponse();
        if (bucket.getCount() > limit) {
            rlr.setAccepted(false);
        } else {
            rlr.setAccepted(bucket.getCount() < limit);
            bucket.setCount(bucket.getCount() + increment);
            bucket.setLast(System.currentTimeMillis());
        }
        int reset = (int) (bucket.getResetMillis(period) / 1000L);
        rlr.setReset(reset);
        rlr.setRemaining(limit - bucket.getCount());
        updateBucketAndReturn(id, bucket, rlr, version, bucketId, period, limit, increment, handler);
    } catch (Throwable e) {
        handler.handle(AsyncResultImpl.create(e, RateLimitResponse.class));
    }
}
 
Example 9
Source File: SearchIndexingServiceImpl.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() throws Exception {
    log.info("Instantiating jestSearchIndexingService...");
    log.info(String.format("Checking if %s needs to be created", LOCATION_INDEX_NAME));
    IndicesExists indicesExists = new IndicesExists.Builder(LOCATION_INDEX_NAME).build();
    JestResult r = jestClient.execute(indicesExists);

    if (!r.isSucceeded()) {
        log.info("Index does not exist. Creating index...");
        // create new index (if u have this in elasticsearch.yml and prefer
        // those defaults, then leave this out
        ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();
        settings.put("number_of_shards", 1);
        settings.put("number_of_replicas", 0);

        CreateIndex.Builder builder = new CreateIndex.Builder(LOCATION_INDEX_NAME);
        builder.settings(settings.build().getAsMap());
        CreateIndex createIndex = builder.build();

        log.info(createIndex.toString());

        jestClient.execute(createIndex);

        log.info("Index created");
    } else {
        log.info("Index already exist!");
    }
}
 
Example 10
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 11
Source File: ESRegistry.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the client synchronously.
 * @param id
 * @throws IOException
 */
protected Client getClient(String id) throws IOException {
    Get get = new Get.Builder(getIndexName(), id).type("client").build(); //$NON-NLS-1$
    JestResult result = getClient().execute(get);
    if (result.isSucceeded()) {
        Client client = result.getSourceAsObject(Client.class);
        return client;
    } else {
        return null;
    }
}
 
Example 12
Source File: ESRegistry.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the api synchronously.
 * @param id
 * @throws IOException
 */
protected Api getApi(String id) throws IOException {
    Get get = new Get.Builder(getIndexName(), id).type("api").build(); //$NON-NLS-1$
    JestResult result = getClient().execute(get);
    if (result.isSucceeded()) {
        Api api = result.getSourceAsObject(Api.class);
        return api;
    } else {
        return null;
    }
}
 
Example 13
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 14
Source File: AliasIndices.java    From easy-sync with Apache License 2.0 4 votes vote down vote up
public String[] parse(JestResult jestResult){
    if(!jestResult.isSucceeded()) return new String[0];
    JsonObject jsonObject= jestResult.getJsonObject();
    return  jsonObject.keySet().toArray(new String[0]);
}
 
Example 15
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 16
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 17
Source File: JestTest.java    From java-study with Apache License 2.0 2 votes vote down vote up
/**
 * 新增数据
 * @param indexName
 * @param typeName
 * @param source
 * @return
 * @throws Exception
 */
public boolean insert(JestClient jestClient,String indexName, String typeName, String source) throws Exception {  
    PutMapping putMapping = new PutMapping.Builder(indexName, typeName, source).build();  
    JestResult jr = jestClient.execute(putMapping);  
    return jr.isSucceeded();  
}
 
Example 18
Source File: JestTest.java    From java-study with Apache License 2.0 2 votes vote down vote up
/**
 * 创建索引
 * @param indexName
 * @return
 * @throws Exception
 */
public boolean createIndex(JestClient jestClient,String indexName) throws Exception {  
    JestResult jr = jestClient.execute(new CreateIndex.Builder(indexName).build());  
    return jr.isSucceeded();  
}
 
Example 19
Source File: JestTest.java    From springBoot-study with Apache License 2.0 2 votes vote down vote up
/**
 * 新增数据
 * @param indexName
 * @param typeName
 * @param source
 * @return
 * @throws Exception
 */
public boolean insert(JestClient jestClient,String indexName, String typeName, String source) throws Exception {  
    PutMapping putMapping = new PutMapping.Builder(indexName, typeName, source).build();  
    JestResult jr = jestClient.execute(putMapping);  
    return jr.isSucceeded();  
}
 
Example 20
Source File: JestTest.java    From springBoot-study with Apache License 2.0 2 votes vote down vote up
/**
 * 创建索引
 * @param indexName
 * @return
 * @throws Exception
 */
public boolean createIndex(JestClient jestClient,String indexName) throws Exception {  
    JestResult jr = jestClient.execute(new CreateIndex.Builder(indexName).build());  
    return jr.isSucceeded();  
}