io.searchbox.core.Search Java Examples

The following examples show how to use io.searchbox.core.Search. 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: LocationRepositoryImpl.java    From bearchoke with Apache License 2.0 6 votes vote down vote up
/**
 * What we want this complex elasticsearch query to do is:
 * 1. Match user search input to the fields code, name and description
 * 2. If any of the documents are of type REGION --> boost document 4
 * 2. If any of the documents are of type CITY --> boost document 3
 * 3. If any of the documents are of type AIRPORT --> boost document 2
 * 4. If any of the documents are of type COUNTRY --> boost document 1
 * 4. If any of the documents are of type HOTEL --> no boost necessary
 *
 * @param userInput user input from search
 * @return
 */
public List<Location> locationSearch(String userInput) {
    List<Location> locations = null;

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.queryStringQuery(userInput));

    Search search = new Search.Builder(searchSourceBuilder.toString())
            .addIndex(LOCATION_INDEX_NAME)
            .addType(LOCATION_INDEX_TYPE).build();

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

        locations = result.getSourceAsObjectList(Location.class);

    } catch (Exception e) {
        log.error(e.getMessage());
    }

    return locations;
}
 
Example #3
Source File: IndexFunctionsDaoTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testIdsInIndexFunction()
{
    JestResult jestResult = mock(JestResult.class);
    SearchResult searchResult = mock(SearchResult.class);
    List<String> idList = Arrays.asList("{id:1}");
    List<String> emptyList = new ArrayList<>();
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("_scroll_id", "100");
    when(jestClientHelper.execute(any(Search.class))).thenReturn(searchResult);
    when(searchResult.getSourceAsStringList()).thenReturn(idList);
    when(searchResult.getJsonObject()).thenReturn(jsonObject);
    when(jestClientHelper.execute(any(SearchScroll.class))).thenReturn(jestResult);
    when(jestResult.getSourceAsStringList()).thenReturn(emptyList);
    indexFunctionsDao.getIdsInIndex("INDEX_NAME", "DOCUMENT_TYPE");
    verify(jestClientHelper).execute(any(Search.class));
    verify(searchResult, times(2)).getSourceAsStringList();
    verify(searchResult).getJsonObject();
    verify(jestClientHelper).execute(any(SearchScroll.class));
    verify(jestResult).getSourceAsStringList();
}
 
Example #4
Source File: JestClientHelperTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearchExecuteWithException() throws Exception
{
    // Mock
    Search search = mock(Search.class);
    JestClient jestClient = mock(JestClient.class);
    when(jestClientFactory.getJestClient()).thenReturn(jestClient);
    when(jestClient.execute(search)).thenThrow(new IOException());

    try
    {
        // Test
        jestClientHelper.execute(search);
    }
    catch (RuntimeException runtimeException)
    {
        // Validate
        assertThat(runtimeException, is(instanceOf(RuntimeException.class)));
    }

    // Verify
    verify(jestClientFactory).getJestClient();
    verify(jestClient).execute(search);
    verifyNoMoreInteractions(createdMocks.toArray());
}
 
Example #5
Source File: JestClientHelperTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearchExecute() throws Exception
{
    // Mock
    Search search = mock(Search.class);
    SearchResult searchResult = mock(SearchResult.class);
    JestClient jestClient = mock(JestClient.class);
    when(jestClientFactory.getJestClient()).thenReturn(jestClient);
    when(jestClient.execute(search)).thenReturn(searchResult);

    // Test
    SearchResult result = jestClientHelper.execute(search);

    // Validate
    assertThat(result, is(not(nullValue())));

    // Verify
    verify(jestClientFactory).getJestClient();
    verify(jestClient).execute(search);
    verifyNoMoreInteractions(createdMocks.toArray());
}
 
Example #6
Source File: SearchServiceImpl.java    From light-reading-cloud with MIT License 5 votes vote down vote up
/**
 * ES 执行查询结果
 * @param query
 * @return
 */
private SearchBookResult getSearchResult(String query){
    SearchBookResult result = new SearchBookResult();
    // 封装查询对象
    Search search = new Search.Builder(query)
            .addIndex(aliasName)
            .addType(indexType).build();

    // 执行查询
    try {
        SearchResult searchResult = this.jestClient.execute(search);
        List<SearchBookItem> bookList;
        if (searchResult.isSucceeded()) {
            // 查询成功,处理结果项
            List<SearchResult.Hit<SearchBookItem, Void>> hitList = searchResult.getHits(SearchBookItem.class);
            bookList = new ArrayList<>(hitList.size());
            for (SearchResult.Hit<SearchBookItem, Void> hit : hitList) {
                bookList.add(hit.source);
            }
        } else {
            bookList = new ArrayList<>();
        }

        // 赋值
        result.setTotal(searchResult.getTotal());
        result.setBookList(bookList);
    } catch (IOException e) {
        LOGGER.error("查询图书异常,查询语句:{}", query, e);
    }
    return result;
}
 
Example #7
Source File: EsStorage.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a list of entities.
 * @param type
 * @param searchSourceBuilder
 * @throws StorageException
 */
private List<Hit<Map<String, Object>, Void>> listEntities(String type,
        SearchSourceBuilder searchSourceBuilder) throws StorageException {
    try {
        String query = searchSourceBuilder.string();
        Search search = new Search.Builder(query).addIndex(getIndexName()).addType(type).build();
        SearchResult response = esClient.execute(search);
        @SuppressWarnings({ "rawtypes", "unchecked" })
        List<Hit<Map<String, Object>, Void>> thehits = (List) response.getHits(Map.class);
        return thehits;
    } catch (Exception e) {
        throw new StorageException(e);
    }
}
 
Example #8
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 #9
Source File: ElasticsearchService.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
private List<Object[]> getRawHistory(Long id, Long min, Long max) {
  String query = String.format("{\n" +
      "  \"size\" : " + maxResults + ",\n" +
      "  \"query\" : {\n" +
      "    \"bool\" : {\n" +
      "      \"must\" : [ {\n" +
      "        \"term\" : {\n" +
      "          \"id\" : %d\n" +
      "        }\n" +
      "      }, {\n" +
      "        \"range\" : {\n" +
      "          \"timestamp\" : {\n" +
      "            \"from\" : %d,\n" +
      "            \"to\" : %d,\n" +
      "            \"include_lower\" : true,\n" +
      "            \"include_upper\" : true\n" +
      "          }\n" +
      "        }\n" +
      "      } ]\n" +
      "    }\n" +
      "  },\n" +
      "  \"sort\" : [ {\n" +
      "    \"timestamp\" : {\n" +
      "      \"order\" : \"asc\"\n" +
      "    }\n" +
      "  } ]\n" +
      "}", id, min, max);
  Search search = new Search.Builder(query).addIndex(timeSeriesIndex).build();
  try {
    SearchResult result = client.execute(search);
    List<Object[]> results = new ArrayList<>();
    for (SearchResult.Hit<Map, Void> hit : result.getHits(Map.class)) {
      results.add(new Object[]{hit.source.get("timestamp"), hit.source.get("value")});
    }
    return results;
  } catch (IOException e) {
    throw new RuntimeException("Error querying raw tag history", e);
  }

}
 
Example #10
Source File: JestClient.java    From wES with MIT License 5 votes vote down vote up
@Override
public <T> WSearchResult<T> searchObj(String index, String type, SearchQuery query, Class<T> cls) throws
		Exception {
	Search.Builder builder = new Search.Builder(convert.toText(query));
	if (index != null) {
		builder.addIndex(index);
	}
	if (type != null) {
		builder.addType(type);
	}
	SearchResult result = _exec(builder.build());
	if (result != null) {
		WSearchResult<T> wresult = new WSearchResult<>();
		WSearchResultHits<T> hits = new WSearchResultHits<>();
		hits.setTotal(result.getTotal());
		List<SearchResult.Hit<T, Void>> allHist = result.getHits(cls);
		List<WEsDoc<T>> data = new ArrayList<>();
		for (SearchResult.Hit<T, Void> hit : allHist) {
			WEsDoc<T> doc = new WEsDoc<>();
			doc.setIndex(hit.index);
			doc.setType(hit.type);
			doc.setIndex(hit.index);
			doc.setSource(hit.source);
			doc.setScore(hit.score);
			data.add(doc);
		}
		hits.setHits(data);
		wresult.setHits(hits);
		return wresult;
	}
	return null;
}
 
Example #11
Source File: ElasticsearchHelperService.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 按天Level统计  各个日志级别的数量 查询条件
 *
 * @param EsModel es
 *                keywords
 *                highlights
 * @return
 */
public Search statisticsLevelCondition(EsModel es) {
    Search search = null;
    if (es == null) {
        return search;
    }
    String indexName = es.getIndex();
    String type = es.getType();
    String startTime = es.getStartTime();
    String endTime = es.getEndTime();
    JSONObject keywords = es.getKeyWord();
    if (StringUtil.isBlank(indexName)) {
        return search;
    }
    if (StringUtil.isBlank(type)) {
        return search;
    }
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    if (StringUtil.isNotBlank(startTime) && StringUtil.isNotBlank(endTime)) {
        RangeQueryBuilder rangequerybuilder = QueryBuilders
                .rangeQuery("createTime")
                .from(startTime).to(endTime);
        searchSourceBuilder.query(rangequerybuilder);
    }
    AddKeyWords(keywords, searchSourceBuilder);
    ExtendedBounds extendedBounds = new ExtendedBounds(startTime, endTime);
    AggregationBuilder levelAgg = AggregationBuilders.terms("level_count").field("level").minDocCount(0);
    searchSourceBuilder.aggregation(levelAgg).size(0);
    search = new Search.Builder(searchSourceBuilder.toString())
            .addIndex(indexName).addType(type).build();
    return search;
}
 
Example #12
Source File: ElasticsearchHelperService.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 按天统计  各个日志级别的数量 查询条件
 *
 * @param EsModel es
 *                keywords
 *                highlights
 * @return
 */
public Search statisticsDayCondition(EsModel es) {
    Search search = null;
    if (es == null) {
        return search;
    }
    String indexName = es.getIndex();
    String type = es.getType();
    String startTime = es.getStartTime();
    String endTime = es.getEndTime();
    JSONObject keywords = es.getKeyWord();
    if (StringUtil.isBlank(indexName)) {
        return search;
    }
    if (StringUtil.isBlank(type)) {
        return search;
    }
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    if (StringUtil.isNotBlank(startTime) && StringUtil.isNotBlank(endTime)) {
        RangeQueryBuilder rangequerybuilder = QueryBuilders
                .rangeQuery("createTime")
                .from(startTime).to(endTime);
        searchSourceBuilder.query(rangequerybuilder);
    }
    AddKeyWords(keywords, searchSourceBuilder);
    ExtendedBounds extendedBounds = new ExtendedBounds(startTime, endTime);
    AggregationBuilder levelAgg = AggregationBuilders.terms("level_count").field("level").minDocCount(0);
    AggregationBuilder dateAgg = AggregationBuilders.dateHistogram("day_count")
            .field("createTime")
            .dateHistogramInterval(DateHistogramInterval.DAY)
            .format("yyyy-MM-dd")
            .extendedBounds(extendedBounds)
            .minDocCount(0L)//为空0补充
            .timeZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT+8")));
    AggregationBuilder builder = dateAgg.subAggregation(levelAgg);
    searchSourceBuilder.aggregation(builder).size(0);
    search = new Search.Builder(searchSourceBuilder.toString())
            .addIndex(indexName).addType(type).build();
    return search;
}
 
Example #13
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 #14
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 #15
Source File: ESRegistry.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * Searches for a client by its orgid:clientId:version and returns it.
 * @param orgId
 * @param clientId
 * @param version
 */
@SuppressWarnings("nls") // Do beans need escaping or will that be done 'automatically'. Test it. Strings do, but probably only quotes?
private Client lookupClient(String orgId, String clientId, String version) {
    String query = "{" +
            "  \"query\": {" +
            "        \"bool\": {" +
            "            \"filter\": [{" +
            "                    \"term\": {" +
            "                        \"organizationId\": ? " + // orgId
            "                    }" +
            "          }," +
            "          {" +
            "                    \"term\": {" +
            "                        \"clientId\": ? " + // clientId
            "                    }" +
            "          }," +
            "          {" +
            "                    \"term\": {" +
            "                        \"version\": ? " + // version
            "          }" +
            "      }" +
            "            ]" +
            "    }" +
            "  }" +
            "}";

    String escaped = ESUtils.queryWithEscapedArgs(query, orgId, clientId, version);
    try {
        Search search = new Search.Builder(escaped)
                .addIndex(getIndexName())
                .addType("client")
                .build();
        SearchResult response = getClient().execute(search);
        if (!response.isSucceeded()) {
            throw new RuntimeException(response.getErrorMessage());
        }
        if (response.getTotal() < 1) {
            throw new IOException();
        }
        Hit<Client,Void> hit = response.getFirstHit(Client.class);
        return hit.source;
    } catch (IOException e) {
        throw new ClientNotFoundException(Messages.i18n.format("ESRegistry.ClientNotFound"), e);  //$NON-NLS-1$
    }
}
 
Example #16
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 #17
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 #18
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));
    }
}
 
Example #19
Source File: ESMetricsAccessor.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getClientUsagePerApi(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@Override
@SuppressWarnings("nls")
public ClientUsagePerApiBean getClientUsagePerApi(String organizationId, String clientId,
        String version, DateTime from, DateTime to) {
    ClientUsagePerApiBean rval = new ClientUsagePerApiBean();

    try {
        String query =
                "{" +
                "    \"query\": {" +
                "        \"bool\": {" +
                "            \"filter\": [{" +
                "                    \"term\": {" +
                "                        \"clientOrgId\": \"${clientOrgId}\"" +
                "                    }" +
                "                }," +
                "                {" +
                "                    \"term\": {" +
                "                        \"clientId\": \"${clientId}\"" +
                "                    }" +
                "                }," +
                "                {" +
                "                    \"term\": {" +
                "                        \"clientVersion\": \"${clientVersion}\"" +
                "                    }" +
                "                }," +
                "                {" +
                "                    \"range\": {" +
                "                        \"requestStart\": {" +
                "                            \"gte\": \"${from}\"," +
                "                            \"lte\": \"${to}\"" +
                "                        }" +
                "                    }" +
                "                }" +
                "            ]" +
                "        }" +
                "    }," +
                "    \"size\": 0," +
                "    \"aggs\": {" +
                "        \"usage_by_api\": {" +
                "            \"terms\": {" +
                "                \"field\": \"apiId\"" +
                "            }" +
                "        }" +
                "    }" +
                "}";

        Map<String, String> params = new HashMap<>();
        params.put("from", formatDate(from));
        params.put("to", formatDate(to));
        params.put("clientOrgId", organizationId.replace('"', '_'));
        params.put("clientId", clientId.replace('"', '_'));
        params.put("clientVersion", version.replace('"', '_'));
        StrSubstitutor ss = new StrSubstitutor(params);
        query = ss.replace(query);

        Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
        SearchResult response = getEsClient().execute(search);
        MetricAggregation aggregations = response.getAggregations();
        ApimanTermsAggregation aggregation = aggregations.getAggregation("usage_by_api", ApimanTermsAggregation.class); //$NON-NLS-1$
        if (aggregation != null) {
            List<ApimanTermsAggregation.Entry> buckets = aggregation.getBuckets();
            for (ApimanTermsAggregation.Entry entry : buckets) {
                rval.getData().put(entry.getKey(), entry.getCount());
            }
        }
    } catch (IOException e) {
        log.error(e);
    }

    return rval;

}
 
Example #20
Source File: ESMetricsAccessor.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getResponseStatsPerPlan(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@Override
@SuppressWarnings("nls")
public ResponseStatsPerPlanBean getResponseStatsPerPlan(String organizationId, String apiId,
        String version, DateTime from, DateTime to) {
    ResponseStatsPerPlanBean rval = new ResponseStatsPerPlanBean();

    try {
        String query =
                "{" +
                "    \"query\": {" +
                "        \"bool\": {" +
                "            \"filter\": [{" +
                "                \"term\": {" +
                "                    \"apiOrgId\": \"${apiOrgId}\"" +
                "                }" +
                "            }, {" +
                "                \"term\": {" +
                "                    \"apiId\": \"${apiId}\"" +
                "                }" +
                "            }, {" +
                "                \"term\": {" +
                "                    \"apiVersion\": \"${apiVersion}\"" +
                "                }" +
                "            }, {" +
                "                \"range\": {" +
                "                    \"requestStart\": {" +
                "                        \"gte\": \"${from}\"," +
                "                        \"lte\": \"${to}\"" +
                "                    }" +
                "                }" +
                "            }]" +
                "        }" +
                "    }," +
                "    \"size\": 0," +
                "    \"aggs\": {" +
                "        \"by_plan\": {" +
                "            \"terms\": {" +
                "                \"field\": \"planId\"" +
                "            }," +
                "            \"aggs\": {" +
                "                \"total_failures\": {" +
                "                    \"filter\": {" +
                "                        \"term\": {" +
                "                            \"failure\": true" +
                "                        }" +
                "                    }" +
                "                }," +
                "                \"total_errors\": {" +
                "                    \"filter\": {" +
                "                        \"term\": {" +
                "                            \"error\": true" +
                "                        }" +
                "                    }" +
                "                }" +
                "            }" +
                "        }" +
                "    }" +
                "}";

        Map<String, String> params = new HashMap<>();
        params.put("from", formatDate(from));
        params.put("to", formatDate(to));
        params.put("apiOrgId", organizationId.replace('"', '_'));
        params.put("apiId", apiId.replace('"', '_'));
        params.put("apiVersion", version.replace('"', '_'));
        StrSubstitutor ss = new StrSubstitutor(params);
        query = ss.replace(query);

        Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
        SearchResult response = getEsClient().execute(search);
        MetricAggregation aggregations = response.getAggregations();
        ApimanTermsAggregation aggregation = aggregations.getAggregation("by_plan", ApimanTermsAggregation.class); //$NON-NLS-1$
        if (aggregation != null) {
            List<ApimanTermsAggregation.Entry> buckets = aggregation.getBuckets();
            int counter = 0;
            for (ApimanTermsAggregation.Entry entry : buckets) {
                rval.addDataPoint(entry.getKey(), entry.getCount(), entry.getFilterAggregation("total_failures").getCount(),
                        entry.getFilterAggregation("total_errors").getCount());
                counter++;
                if (counter > 10) {
                    break;
                }
            }
        }
    } catch (IOException e) {
        log.error(e);
    }

    return rval;
}
 
Example #21
Source File: ESMetricsAccessor.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getResponseStatsPerClient(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@Override
@SuppressWarnings("nls")
public ResponseStatsPerClientBean getResponseStatsPerClient(String organizationId, String apiId,
        String version, DateTime from, DateTime to) {
    ResponseStatsPerClientBean rval = new ResponseStatsPerClientBean();

    try {
        String query =
                "{" +
                "    \"query\": {" +
                "        \"bool\": {" +
                "            \"filter\": [{" +
                "                    \"term\": {" +
                "                        \"apiOrgId\": \"${apiOrgId}\"" +
                "                    }" +
                "                }, {" +
                "                    \"term\": {" +
                "                        \"apiId\": \"${apiId}\"" +
                "                    }" +
                "                }, {" +
                "                    \"term\": {" +
                "                        \"apiVersion\": \"${apiVersion}\"" +
                "                    }" +
                "                }, {" +
                "                    \"range\": {" +
                "                        \"requestStart\": {" +
                "                            \"gte\": \"${from}\"," +
                "                            \"lte\": \"${to}\"" +
                "                        }" +
                "                    }" +
                "                }" +
                "            ]" +
                "        }" +
                "    }," +
                "    \"aggs\": {" +
                "        \"by_client\": {" +
                "            \"terms\": {" +
                "                \"field\": \"clientId\"" +
                "            }," +
                "            \"aggs\": {" +
                "                \"total_failures\": {" +
                "                    \"filter\": {" +
                "                        \"term\": {" +
                "                            \"failure\": true" +
                "                        }" +
                "                    }" +
                "                }," +
                "                \"total_errors\": {" +
                "                    \"filter\": {" +
                "                        \"term\": {" +
                "                            \"error\": true" +
                "                        }" +
                "                    }" +
                "                }" +
                "            }" +
                "        }" +
                "    }," +
                "    \"size\": 0" +
                "}";

        Map<String, String> params = new HashMap<>();
        params.put("from", formatDate(from));
        params.put("to", formatDate(to));
        params.put("apiOrgId", organizationId.replace('"', '_'));
        params.put("apiId", apiId.replace('"', '_'));
        params.put("apiVersion", version.replace('"', '_'));
        StrSubstitutor ss = new StrSubstitutor(params);
        query = ss.replace(query);

        Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
        SearchResult response = getEsClient().execute(search);
        MetricAggregation aggregations = response.getAggregations();
        ApimanTermsAggregation aggregation = aggregations.getAggregation("by_client", ApimanTermsAggregation.class); //$NON-NLS-1$
        if (aggregation != null) {
            List<ApimanTermsAggregation.Entry> buckets = aggregation.getBuckets();
            int counter = 0;
            for (ApimanTermsAggregation.Entry entry : buckets) {
                rval.addDataPoint(entry.getKey(), entry.getCount(), entry.getFilterAggregation("total_failures").getCount(),
                        entry.getFilterAggregation("total_errors").getCount());
                counter++;
                if (counter > 10) {
                    break;
                }
            }
        }
    } catch (IOException e) {
        log.error(e);
    }

    return rval;
}
 
Example #22
Source File: ESMetricsAccessor.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getResponseStatsSummary(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@Override
@SuppressWarnings("nls")
public ResponseStatsSummaryBean getResponseStatsSummary(String organizationId, String apiId,
        String version, DateTime from, DateTime to) {
    ResponseStatsSummaryBean rval = new ResponseStatsSummaryBean();

    try {
        String query =
                "{" +
                "    \"query\": {" +
                "        \"bool\": {" +
                "            \"filter\": [{" +
                "                \"term\": {" +
                "                    \"apiOrgId\": \"${apiOrgId}\"" +
                "                }" +
                "            }, {" +
                "                \"term\": {" +
                "                    \"apiId\": \"${apiId}\"" +
                "                }" +
                "            }, {" +
                "                \"term\": {" +
                "                    \"apiVersion\": \"${apiVersion}\"" +
                "                }" +
                "            }, {" +
                "                \"range\": {" +
                "                    \"requestStart\": {" +
                "                        \"gte\": \"${from}\"," +
                "                        \"lte\": \"${to}\"" +
                "                    }" +
                "                }" +
                "            }]" +
                "        }" +
                "    }," +
                "    \"size\": 0," +
                "    \"aggs\": {" +
                "        \"total_failures\": {" +
                "            \"filter\": {" +
                "                \"term\": {" +
                "                    \"failure\": true" +
                "                }" +
                "            }" +
                "        }," +
                "        \"total_errors\": {" +
                "            \"filter\": {" +
                "                \"term\": {" +
                "                    \"error\": true" +
                "                }" +
                "            }" +
                "        }" +
                "    }" +
                "}";

        Map<String, String> params = new HashMap<>();
        params.put("from", formatDate(from));
        params.put("to", formatDate(to));
        params.put("apiOrgId", organizationId.replace('"', '_'));
        params.put("apiId", apiId.replace('"', '_'));
        params.put("apiVersion", version.replace('"', '_'));
        StrSubstitutor ss = new StrSubstitutor(params);
        query = ss.replace(query);

        Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
        SearchResult response = getEsClient().execute(search);

        rval.setTotal(response.getTotal());
        rval.setFailures(response.getAggregations().getFilterAggregation("total_failures").getCount());
        rval.setErrors(response.getAggregations().getFilterAggregation("total_errors").getCount());
    } catch (IOException e) {
        log.error(e);
    }

    return rval;
}
 
Example #23
Source File: ESMetricsAccessor.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getResponseStats(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.metrics.HistogramIntervalType, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@SuppressWarnings("nls")
@Override
public ResponseStatsHistogramBean getResponseStats(String organizationId, String apiId,
        String version, HistogramIntervalType interval, DateTime from, DateTime to) {
    ResponseStatsHistogramBean rval = new ResponseStatsHistogramBean();
    Map<String, ResponseStatsDataPoint> index = generateHistogramSkeleton(rval, from, to, interval, ResponseStatsDataPoint.class);

    try {
        String query =
                "{" +
                "    \"query\": {" +
                "        \"bool\": {" +
                "            \"filter\": [{" +
                "                \"term\": {" +
                "                    \"apiOrgId\": \"${apiOrgId}\"" +
                "                }" +
                "            }, {" +
                "                \"term\": {" +
                "                    \"apiId\": \"${apiId}\"" +
                "                }" +
                "            }, {" +
                "                \"term\": {" +
                "                    \"apiVersion\": \"${apiVersion}\"" +
                "                }" +
                "            }, {" +
                "                \"range\": {" +
                "                    \"requestStart\": {" +
                "                        \"gte\": \"${from}\"," +
                "                        \"lte\": \"${to}\"" +
                "                    }" +
                "                }" +
                "            }]" +
                "        }" +
                "    }," +
                "    \"size\": 0," +
                "    \"aggs\": {" +
                "        \"histogram\": {" +
                "            \"date_histogram\": {" +
                "                \"field\": \"requestStart\"," +
                "                \"interval\": \"${interval}\"" +
                "            }," +
                "            \"aggs\": {" +
                "                \"total_failures\": {" +
                "                    \"filter\": {" +
                "                        \"term\": {" +
                "                            \"failure\": true" +
                "                        }" +
                "                    }" +
                "                }," +
                "                \"total_errors\": {" +
                "                    \"filter\": {" +
                "                        \"term\": {" +
                "                            \"error\": true" +
                "                        }" +
                "                    }" +
                "                }" +
                "            }" +
                "        }" +
                "    }" +
                "}";

        Map<String, String> params = new HashMap<>();
        params.put("from", formatDate(from));
        params.put("to", formatDate(to));
        params.put("apiOrgId", organizationId.replace('"', '_'));
        params.put("apiId", apiId.replace('"', '_'));
        params.put("apiVersion", version.replace('"', '_'));
        params.put("interval", interval.name());
        StrSubstitutor ss = new StrSubstitutor(params);
        query = ss.replace(query);

        Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
        SearchResult response = getEsClient().execute(search);
        MetricAggregation aggregations = response.getAggregations();
        DateHistogramAggregation aggregation = aggregations.getDateHistogramAggregation("histogram");
        if (aggregation != null) {
            List<DateHistogram> buckets = aggregation.getBuckets();
            for (DateHistogram entry : buckets) {
                String keyAsString = entry.getTimeAsString();
                if (index.containsKey(keyAsString)) {
                    FilterAggregation totalFailuresAgg = entry.getFilterAggregation("total_failures");
                    FilterAggregation totalErrorsAgg = entry.getFilterAggregation("total_errors");
                    long failures = totalFailuresAgg.getCount();
                    long errors = totalErrorsAgg.getCount();
                    ResponseStatsDataPoint point = index.get(keyAsString);
                    point.setTotal(entry.getCount());
                    point.setFailures(failures);
                    point.setErrors(errors);
                }
            }
        }
    } catch (IOException e) {
        log.error(e);
    }

    return rval;
}
 
Example #24
Source File: ESMetricsAccessor.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getUsagePerPlan(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@SuppressWarnings("nls")
@Override
public UsagePerPlanBean getUsagePerPlan(String organizationId, String apiId, String version,
        DateTime from, DateTime to) {
    UsagePerPlanBean rval = new UsagePerPlanBean();

    try {
        String query =
                "{" +
                "    \"query\": {" +
                "        \"bool\": {" +
                "            \"filter\": [{" +
                "                    \"term\": {" +
                "                        \"apiOrgId\": \"${apiOrgId}\"" +
                "                    }" +
                "                }, {" +
                "                    \"term\": {" +
                "                        \"apiId\": \"${apiId}\"" +
                "                    }" +
                "                }, {" +
                "                    \"term\": {" +
                "                        \"apiVersion\": \"${apiVersion}\"" +
                "                    }" +
                "                }," +
                "                {" +
                "                    \"range\": {" +
                "                        \"requestStart\": {" +
                "                            \"gte\": \"${from}\"," +
                "                            \"lte\": \"${to}\"" +
                "                        }" +
                "                    }" +
                "                }" +
                "            ]" +
                "        }" +
                "    }," +
                "    \"size\": 0," +
                "    \"aggs\": {" +
                "        \"usage_by_plan\": {" +
                "            \"terms\": {" +
                "                \"field\": \"planId\"" +
                "            }" +
                "        }" +
                "    }" +
                "}";

        Map<String, String> params = new HashMap<>();
        params.put("from", formatDate(from));
        params.put("to", formatDate(to));
        params.put("apiOrgId", organizationId.replace('"', '_'));
        params.put("apiId", apiId.replace('"', '_'));
        params.put("apiVersion", version.replace('"', '_'));
        StrSubstitutor ss = new StrSubstitutor(params);
        query = ss.replace(query);

        Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
        SearchResult response = getEsClient().execute(search);
        MetricAggregation aggregations = response.getAggregations();
        ApimanTermsAggregation aggregation = aggregations.getAggregation("usage_by_plan", ApimanTermsAggregation.class); //$NON-NLS-1$
        if (aggregation != null) {
            List<ApimanTermsAggregation.Entry> buckets = aggregation.getBuckets();
            for (ApimanTermsAggregation.Entry entry : buckets) {
                rval.getData().put(entry.getKey(), entry.getCount());
            }
        }
    } catch (IOException e) {
        log.error(e);
    }

    return rval;
}
 
Example #25
Source File: ESMetricsAccessor.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getUsagePerClient(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@SuppressWarnings("nls")
@Override
public UsagePerClientBean getUsagePerClient(String organizationId, String apiId, String version,
        DateTime from, DateTime to) {
    UsagePerClientBean rval = new UsagePerClientBean();

    try {
        String query =
                "{" +
                "    \"query\": {" +
                "        \"bool\": {" +
                "            \"filter\": [{" +
                "                \"term\": {" +
                "                    \"apiOrgId\": \"${apiOrgId}\"" +
                "                }" +
                "            }, {" +
                "                \"term\": {" +
                "                    \"apiId\": \"${apiId}\"" +
                "                }" +
                "            }, {" +
                "                \"term\": {" +
                "                    \"apiVersion\": \"${apiVersion}\"" +
                "                }" +
                "            }, {" +
                "                \"range\": {" +
                "                    \"requestStart\": {" +
                "                        \"gte\": \"${from}\"," +
                "                        \"lte\": \"${to}\"" +
                "                    }" +
                "                }" +
                "            }]" +
                "        }" +
                "    }," +
                "    \"size\": 0," +
                "    \"aggs\": {" +
                "        \"usage_by_client\": {" +
                "            \"terms\": {" +
                "                \"field\": \"clientId\"" +
                "            }" +
                "        }" +
                "    }" +
                "}";

        Map<String, String> params = new HashMap<>();
        params.put("from", formatDate(from));
        params.put("to", formatDate(to));
        params.put("apiOrgId", organizationId.replace('"', '_'));
        params.put("apiId", apiId.replace('"', '_'));
        params.put("apiVersion", version.replace('"', '_'));
        StrSubstitutor ss = new StrSubstitutor(params);
        query = ss.replace(query);

        Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
        SearchResult response = getEsClient().execute(search);
        MetricAggregation aggregations = response.getAggregations();
        ApimanTermsAggregation aggregation = aggregations.getAggregation("usage_by_client", ApimanTermsAggregation.class); //$NON-NLS-1$
        if (aggregation != null) {
            List<ApimanTermsAggregation.Entry> buckets = aggregation.getBuckets();
            int counter = 0;
            for (ApimanTermsAggregation.Entry entry : buckets) {
                rval.getData().put(entry.getKey(), entry.getCount());
                counter++;
                if (counter > 5) {
                    break;
                }
            }
        }
    } catch (IOException e) {
        log.error(e);
    }

    return rval;
}
 
Example #26
Source File: ESMetricsAccessor.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getUsage(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.metrics.HistogramIntervalType, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@SuppressWarnings("nls")
@Override
public UsageHistogramBean getUsage(String organizationId, String apiId, String version,
        HistogramIntervalType interval, DateTime from, DateTime to) {
    UsageHistogramBean rval = new UsageHistogramBean();
    Map<String, UsageDataPoint> index = generateHistogramSkeleton(rval, from, to, interval, UsageDataPoint.class);

    try {
        String query =
                "{" +
                "    \"query\": {" +
                "        \"bool\": {" +
                "            \"filter\": [{" +
                "                    \"term\": {" +
                "                        \"apiOrgId\": \"${apiOrgId}\"" +
                "                    }" +
                "                }, {" +
                "                    \"term\": {" +
                "                        \"apiId\": \"${apiId}\"" +
                "                    }" +
                "                }, {" +
                "                    \"term\": {" +
                "                        \"apiVersion\": \"${apiVersion}\"" +
                "                    }" +
                "                }," +
                "                {" +
                "                    \"range\": {" +
                "                        \"requestStart\": {" +
                "                            \"gte\": \"${from}\"," +
                "                            \"lte\": \"${to}\"" +
                "                        }" +
                "                    }" +
                "                }" +
                "            ]" +
                "        }" +
                "    }," +
                "    \"size\": 0," +
                "    \"aggs\": {" +
                "        \"histogram\": {" +
                "            \"date_histogram\": {" +
                "                \"field\": \"requestStart\"," +
                "                \"interval\": \"${interval}\"" +
                "            }" +
                "        }" +
                "    }" +
                "}";

        Map<String, String> params = new HashMap<>();
        params.put("from", formatDate(from));
        params.put("to", formatDate(to));
        params.put("apiOrgId", organizationId.replace('"', '_'));
        params.put("apiId", apiId.replace('"', '_'));
        params.put("apiVersion", version.replace('"', '_'));
        params.put("interval", interval.name());
        StrSubstitutor ss = new StrSubstitutor(params);
        query = ss.replace(query);

        Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
        SearchResult response = getEsClient().execute(search);
        MetricAggregation aggregations = response.getAggregations();
        DateHistogramAggregation aggregation = aggregations.getDateHistogramAggregation("histogram");
        if (aggregation != null) {
            List<DateHistogram> buckets = aggregation.getBuckets();
            for (DateHistogram entry : buckets) {
                String keyAsString = entry.getTimeAsString();
                if (index.containsKey(keyAsString)) {
                    index.get(keyAsString).setCount(entry.getCount());
                }
            }
        }
    } catch (IOException e) {
        log.error(e);
    }

    return rval;
}
 
Example #27
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 #28
Source File: ElasticsearchService.java    From c2mon with GNU Lesser General Public License v3.0 4 votes vote down vote up
private List<Object[]> getAggregatedHistory(Long id, Long min, Long max, String aggregate) {
  // Figure out the right interval
  String interval = aggregate.equals("auto") ? getInterval(min, max) : aggregate;
  log.info("Using interval: " + interval);
  String query = String.format("{\n" +
      "  \"size\" : " + maxResults + ",\n" +
      "  \"query\" : {\n" +
      "    \"term\" : {\n" +
      "      \"id\" : %d\n" +
      "    }\n" +
      "  },\n" +
      "  \"aggregations\" : {\n" +
      "    \"time-range\" : {\n" +
      "      \"filter\" : {\n" +
      "        \"range\" : {\n" +
      "          \"timestamp\" : {\n" +
      "            \"from\" : %d,\n" +
      "            \"to\" : %d,\n" +
      "            \"include_lower\" : true,\n" +
      "            \"include_upper\" : true\n" +
      "          }\n" +
      "        }\n" +
      "      },\n" +
      "      \"aggregations\" : {\n" +
      "        \"events-per-interval\" : {\n" +
      "          \"date_histogram\" : {\n" +
      "            \"field\" : \"timestamp\",\n" +
      "            \"interval\" : \"%s\"\n" +
      "          },\n" +
      "          \"aggregations\" : {\n" +
      "            \"avg-value\" : {\n" +
      "              \"avg\" : {\n" +
      "                \"field\" : \"value\"\n" +
      "              }\n" +
      "            }\n" +
      "          }\n" +
      "        }\n" +
      "      }\n" +
      "    }\n" +
      "  }\n" +
      "}", id, min, max, aggregate);
  Search search = new Search.Builder(query).addIndex(timeSeriesIndex).build();
  long start = System.currentTimeMillis();
  try {
    List<Object[]> results = new ArrayList<>();
    SearchResult result = client.execute(search);
    DateHistogramAggregation aggregation = result.getAggregations().getFilterAggregation("time-range").getDateHistogramAggregation("events-per-interval");
    for (DateHistogram bucket : aggregation.getBuckets()) {
      AvgAggregation avg = bucket.getAvgAggregation("avg-value");
      results.add(new Object[]{Long.parseLong(bucket.getTimeAsString()), avg.getAvg()});
    }
    log.info("Loaded {} values in {}ms", results.size(), System.currentTimeMillis() - start);
    return results;
  } catch (IOException e) {
    throw new RuntimeException("Error querying history for tag #" + id, e);
  }
}
 
Example #29
Source File: ElasticsearchHelperService.java    From xmfcn-spring-cloud with Apache License 2.0 4 votes vote down vote up
/**
 * 组装搜索条件
 *
 * @param EsModel es
 *                keywords
 *                highlights
 * @return
 */
public Search searchCondition(EsModel es) {
    Search search = null;
    if (es == null) {
        return search;
    }
    JSONObject keywords = es.getKeyWord();
    JSONObject hightWord = es.getHightWord();
    EsPage esPage = es.getEsPage();
    String indexName = es.getIndex();
    String type = es.getType();
    boolean isVague = es.isVague();//是否模糊搜索
    boolean isAccuracySort = es.isAccuracySort();//是否根据精确度排序
    boolean isAndSearch = es.isAndSearch();//是否And搜索
    if (StringUtil.isBlank(indexName)) {
        return search;
    }
    if (StringUtil.isBlank(type)) {
        return search;
    }
    int pageNo = 1;
    int pageSize = 10;
    int startIndex = 0;
    if (esPage != null) {
        pageNo = esPage.getPageNo();
        pageSize = esPage.getPageSize();
        startIndex = esPage.getStartIndex();
    }
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    AddKeyWords(keywords, searchSourceBuilder);
    HighlightBuilder highlightBuilder = new HighlightBuilder();
    AddHighLigh(keywords, highlightBuilder);
    searchSourceBuilder.explain(isAccuracySort);
    searchSourceBuilder.from(startIndex);
    searchSourceBuilder.size(pageSize);
    Sort sort = new Sort("time", Sort.Sorting.DESC);
    searchSourceBuilder.highlighter(highlightBuilder);
    search = new Search.Builder(searchSourceBuilder.toString())
            .addIndex(indexName).addType(type).addSort(sort).build();

    return search;
}
 
Example #30
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);
    }
}