io.searchbox.params.Parameters Java Examples

The following examples show how to use io.searchbox.params.Parameters. 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: EsStorage.java    From apiman with Apache License 2.0 6 votes vote down vote up
private void initScroll() throws StorageException {
    try {
        Search search = new Search.Builder(query)
                .addIndex(getIndexName())
                .addType(entityType)
                .setParameter(Parameters.SCROLL, "1m")
                .addSort(sort)
                .build();
        SearchResult response = esClient.execute(search);
        if (!response.isSucceeded()) {
            throw new StorageException("Scrolled query failed " + response.getErrorMessage());
        }
        scrollId = response.getJsonObject().get("_scroll_id").getAsString();
        this.hits = (List) response.getHits(Map.class);
    } catch (IOException e) {
        throw new StorageException(e);
    }
}
 
Example #2
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 #3
Source File: ElasticSearchClient.java    From vind with Apache License 2.0 5 votes vote down vote up
private synchronized SearchResult  getScrollQuery(String query, int retry, JestClient client) throws InterruptedException {

        if (client != null) {
            final Search.Builder searchBuilder = new Search.Builder(query)
                    .addIndex(elasticIndex)
                    .setParameter(Parameters.SCROLL, SCROLL_TIME_SESSION);

            final Search search = searchBuilder.build();

            try {
                final SearchResult result = client.execute(search);
                log.debug("Completed query scroll query in {} tries. Succeeded: {}", retry + 1, result.isSucceeded());
                return result;
            } catch (IOException e) {
                log.warn("Try {} - Error in query scroll request query: {}", retry, e.getMessage(), e);
                if(retry > ES_MAX_TRIES) {
                    log.error("Error in query scroll request: reached maximum number of scroll tries [{}].", retry);
                    throw new RuntimeException("Error in query scroll request query: " + e.getMessage(), e);
                } else {
                    Thread.sleep((retry + 1) * ES_WAIT_TIME);
                    return getScrollQuery(query, retry + 1, client);
                }
            }
            //TODO: move to async at some point
            /*client.executeAsync(search,new JestResultHandler<JestResult>() {
                @Override
                public void completed(JestResult result) {
                    log.debug("Completed total requests query. Succeeded: {}", result.isSucceeded());
                }

                @Override
                public void failed(Exception e) {
                    log.error("Error indexing content : {}", e.getMessage(), e);
                }
            });*/
        }
        log.error("Error in scroll request query: ES client has not been initialized, client is null.");
        throw new RuntimeException("Error in scroll request query: ES client has not been initialized, client is null.");
    }
 
Example #4
Source File: IndexFunctionsDaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * The ids in index function will take as arguments the index name and the document type and will return a list of all the ids in the index.
 */
@Override
public final List<String> getIdsInIndex(String indexName, String documentType)
{
    // Create an array list for storing the ids
    List<String> idList = new ArrayList<>();

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchAllQuery());
    // Create a search request and set the scroll time and scroll size
    final SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(new ElasticsearchClientImpl(), SearchAction.INSTANCE);
    searchRequestBuilder.setIndices(indexName).setTypes(documentType).setScroll(new TimeValue(ELASTIC_SEARCH_SCROLL_KEEP_ALIVE_TIME))
        .setSize(ELASTIC_SEARCH_SCROLL_PAGE_SIZE).setSource(searchSourceBuilder);

    // Retrieve the search response
    final Search.Builder searchBuilder = new Search.Builder(searchRequestBuilder.toString()).addIndex(indexName);

    searchBuilder.setParameter(Parameters.SIZE, ELASTIC_SEARCH_SCROLL_PAGE_SIZE);
    searchBuilder.setParameter(Parameters.SCROLL, new TimeValue(ELASTIC_SEARCH_SCROLL_KEEP_ALIVE_TIME).toString());

    JestResult jestResult = jestClientHelper.execute(searchBuilder.build());

    // While there are hits available, page through the results and add them to the id list
    while (jestResult.getSourceAsStringList().size() != 0)
    {
        for (String jsonString : jestResult.getSourceAsStringList())
        {
            JsonElement root = new JsonParser().parse(jsonString);
            idList.add(root.getAsJsonObject().get("id").getAsString());
        }
        String scrollId = jestResult.getJsonObject().get("_scroll_id").getAsString();
        SearchScroll scroll = new SearchScroll.Builder(scrollId, new TimeValue(ELASTIC_SEARCH_SCROLL_KEEP_ALIVE_TIME).toString()).build();
        jestResult = jestClientHelper.execute(scroll);

    }
    return idList;
}
 
Example #5
Source File: EsStorage.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * Updates a single entity.
 * @param type
 * @param id
 * @param source
 * @throws StorageException
 */
private void updateEntity(String type, String id, XContentBuilder source) throws StorageException {
    try {
        String doc = source.string();
        /* JestResult response = */esClient.execute(new Index.Builder(doc)
                .setParameter(Parameters.OP_TYPE, "index").index(getIndexName()).type(type).id(id).build()); //$NON-NLS-1$
    } 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: ESRegistry.java    From apiman with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("nls")
public void listOrgs(IAsyncResultHandler<List<String>> handler) {
    try {
        String query =
                "{\n" +
                "    \"aggs\" : {\n" +
                "        \"all_orgs\" : {\n" +
                "            \"terms\" : { \"field\" : \"organizationId\" }\n" + // i.e. only records containing an orgId field.
                "        }\n" +
                "    }\n" +
                "}";
        Search search = new Search.Builder(query)
                .addIndex(getIndexName())
                .setParameter(Parameters.SIZE, 0)
                .build();
        SearchResult response = getClient().execute(search);
        // Aggregations section
        MetricAggregation aggregation = response.getAggregations();
        // Look at the terms subsection
        TermsAggregation terms = aggregation.getTermsAggregation("all_orgs");
        // Grab only the name of each aggregation (we don't care about count
        List<String> results = terms.getBuckets().stream()
                .map(TermsAggregation.Entry::getKey)
                .collect(Collectors.toList());
        handler.handle(AsyncResultImpl.create(results));
    } catch (IOException e) {
        handler.handle(AsyncResultImpl.create(e));
    }
}
 
Example #8
Source File: HooverElasticsearchReader.java    From newsleak with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
	super.initialize(context);

	logger = context.getLogger();
	// init hoover connection
	client = hooverResource.getClient();
	esIndex = hooverResource.getIndex();

	// query hoover's elasticsearch index
	Search search = new Search.Builder(
			"{\"query\": {\"match_all\" : {}}, \"_source\" : false, \"size\" : " + PARAM_SCROLL_SIZE + "}")
					.addIndex(hooverResource.getIndex()).addType(HooverResource.HOOVER_DOCUMENT_TYPE)
					.setParameter(Parameters.SCROLL, PARAM_SCROLL_TIME).build();

	try {

		// run JEST request
		JestResult result = client.execute(search);

		totalIdList = new ArrayList<String>();

		JsonArray hits = result.getJsonObject().getAsJsonObject("hits").getAsJsonArray("hits");
		Integer total = result.getJsonObject().getAsJsonObject("hits").get("total").getAsInt();

		int nHits = hits.size();

		logger.log(Level.INFO, "Hits first result: " + nHits);
		logger.log(Level.INFO, "Hits total: " + total);

		totalIdList.addAll(hooverResource.getIds(hits));

		String scrollId = result.getJsonObject().get("_scroll_id").getAsString();

		// run scroll request to collect all Ids
		int i = 0;
		while (nHits > 0) {
			SearchScroll scroll = new SearchScroll.Builder(scrollId, PARAM_SCROLL_TIME).build();

			result = client.execute(scroll);

			hits = result.getJsonObject().getAsJsonObject("hits").getAsJsonArray("hits");
			nHits = hits.size();
			logger.log(Level.INFO, "Hits " + ++i + " result: " + nHits);
			totalIdList.addAll(hooverResource.getIds(hits));
			scrollId = result.getJsonObject().getAsJsonPrimitive("_scroll_id").getAsString();
		}

		if (maxRecords > 0 && maxRecords < totalIdList.size()) {
			totalIdList = new ArrayList<String>(totalIdList.subList(0, maxRecords));
		}

		totalRecords = totalIdList.size();
		logger.log(Level.INFO, "Found " + totalRecords + " ids in index " + esIndex);

	} catch (IOException e) {
		throw new ResourceInitializationException(e);
	}

}
 
Example #9
Source File: ESRegistry.java    From apiman with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("nls")
public void listClients(String organizationId, int page, int pageSize, IAsyncResultHandler<List<String>> handler) {
    try {
        String query = "{" +
                "      \"query\": {" +
                "        \"bool\": {" +
                "          \"filter\": [" +
                "            {" +
                "              \"exists\": {" +
                "                \"field\": \"clientId\" " + // clientId
                "              }" +
                "            }," +
                "            {" +
                "              \"term\": {" +
                "                \"organizationId\": ? " +  // organizationId
                "              }" +
                "            }" +
                "          ]" +
                "        }" +
                "    }," +
                "    \"aggs\": {" +
                "        \"clients\": {" +
                "            \"terms\": {" +
                "                \"field\": \"clientId\" " + // only return aggregated clientId field
                "            }" +
                "        }" +
                "    }" +
                "}";
        String escaped = ESUtils.queryWithEscapedArgs(query, organizationId);
        Search search = new Search.Builder(escaped)
                .addIndex(getIndexName())
                .addType("client")
                .setParameter(Parameters.SIZE, 0)
                .build();
        SearchResult response = getClient().execute(search);
        // Aggregations section
        MetricAggregation aggregation = response.getAggregations();
        // Look at the terms subsection
        TermsAggregation terms = aggregation.getTermsAggregation("clients");
        // Grab only the name of each aggregation (we don't care about count [for now]).
        List<String> results = terms.getBuckets().stream()
                .map(TermsAggregation.Entry::getKey)
                .collect(Collectors.toList());
        handler.handle(AsyncResultImpl.create(results));
    } catch (IOException e) {
        handler.handle(AsyncResultImpl.create(e));
    }
}
 
Example #10
Source File: ESRegistry.java    From apiman with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("nls")
@Override
public void listApis(String organizationId, int page, int pageSize, IAsyncResultHandler<List<String>> handler) {
    try {
        String query = "{" +
                "      \"query\": {" +
                "        \"bool\": {" +
                "          \"filter\": [" +
                "            {" +
                "              \"exists\": {" +
                "                \"field\": \"apiId\" " + // must have field apiId
                "              }" +
                "            }," +
                "            {" +
                "              \"term\": {" +
                "                \"organizationId\": ? " +  // organizationId
                "              }" +
                "            }" +
                "          ]" +
                "        }" +
                "    }," +
                "    \"aggs\": {" +
                "        \"apis\": {" +
                "            \"terms\": {" +
                "                \"field\": \"apiId\" " + // only return aggregated apiId field
                "            }" +
                "        }" +
                "    }" +
                "}";
        String escaped = ESUtils.queryWithEscapedArgs(query, organizationId);
        Search search = new Search.Builder(escaped)
                .addIndex(getIndexName())
                .addType("api")
                .setParameter(Parameters.SIZE, 0)
                .build();
        SearchResult response = getClient().execute(search);
        // Aggregations section
        MetricAggregation aggregation = response.getAggregations();
        // Look at the terms subsection
        TermsAggregation terms = aggregation.getTermsAggregation("apis");
        // Grab only the name of each aggregation (we don't care about count [for now]).
        List<String> results = terms.getBuckets().stream()
                .map(TermsAggregation.Entry::getKey)
                .collect(Collectors.toList());
        handler.handle(AsyncResultImpl.create(results));
    } catch (IOException e) {
        handler.handle(AsyncResultImpl.create(e));
    }
}
 
Example #11
Source File: ESRegistry.java    From apiman with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("nls")
public void listClientVersions(String organizationId, String clientId, int page, int pageSize, IAsyncResultHandler<List<String>> handler) {
    try {
        String query = "{" +
                "  \"query\": {" +
                "    \"bool\": {" +
                "      \"filter\": [" +
                "        {" +
                "          \"term\": {" +
                "            \"organizationId\": ?" +  // organizationId
                "          }" +
                "        }," +
                "        {" +
                "          \"term\": {" +
                "            \"clientId\": ?" + // clientId
                "          }" +
                "        }" +
                "      ]" +
                "    }" +
                "  }," +
                "    \"aggs\": {" +
                "      \"client_versions\": {" +
                "        \"terms\": {" +
                "          \"field\": \"version\"" + // only return version fields of clients
                "        }" +
                "      }" +
                "    }" +
                "}";

        String escaped = ESUtils.queryWithEscapedArgs(query, organizationId, clientId);
        Search search = new Search.Builder(escaped)
                .addIndex(getIndexName())
                .addType("client")
                .setParameter(Parameters.SIZE, 0) // size zero to return only aggregate data (not raw query results)
                .build();
        SearchResult response = getClient().execute(search);
        // Aggregations section
        MetricAggregation aggregation = response.getAggregations();
        // Look at the terms subsection
        TermsAggregation terms = aggregation.getTermsAggregation("client_versions");
        // Grab only the name of each aggregation
        List<String> results = terms.getBuckets().stream()
                .map(TermsAggregation.Entry::getKey)
                .collect(Collectors.toList());
        handler.handle(AsyncResultImpl.create(results));
    } catch (IOException e) {
        handler.handle(AsyncResultImpl.create(e));
    }
}
 
Example #12
Source File: ESRegistry.java    From apiman with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("nls")
public void listApiVersions(String organizationId, String apiId, int page, int pageSize,
                            IAsyncResultHandler<List<String>> handler) {
    try {
        String query = "{" +
                "  \"query\": {" +
                "    \"bool\": {" +
                "      \"filter\": [" +
                "        {" +
                "          \"term\": {" +
                "            \"organizationId\": ?" + // organizationId
                "          }" +
                "        }," +
                "        {" +
                "          \"term\": {" +
                "            \"apiId\": ?" + // apiId
                "          }" +
                "        }" +
                "      ]" +
                "    }" +
                "  }," +
                "    \"aggs\": {" +
                "      \"api_versions\": {" +
                "        \"terms\": {" +
                "          \"field\": \"version\"" + // only return version fields of APIs
                "        }" +
                "      }" +
                "    }" +
                "}";
        String escaped = ESUtils.queryWithEscapedArgs(query, organizationId, apiId);
        Search search = new Search.Builder(escaped)
                .addIndex(getIndexName())
                .addType("api")
                .setParameter(Parameters.SIZE, 0)
                .build();
        SearchResult response = getClient().execute(search);
        // Aggregations section
        MetricAggregation aggregation = response.getAggregations();
        // Look at the terms subsection
        TermsAggregation terms = aggregation.getTermsAggregation("api_versions");
        // Grab only the name of each aggregation
        List<String> results = terms.getBuckets().stream()
                .map(TermsAggregation.Entry::getKey)
                .collect(Collectors.toList());
        handler.handle(AsyncResultImpl.create(results));
    } catch (IOException e) {
        handler.handle(AsyncResultImpl.create(e));
    }
}