org.elasticsearch.search.builder.SearchSourceBuilder Java Examples

The following examples show how to use org.elasticsearch.search.builder.SearchSourceBuilder. 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: ESClient.java    From dht-spider with MIT License 10 votes vote down vote up
public List<MetaData> search(String searchValue) throws Exception{
    List<MetaData> list=new ArrayList<>();
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.from(0);
    searchSourceBuilder.size(100);
    searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
    searchSourceBuilder.query(QueryBuilders.matchQuery("name", searchValue));
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("torrent");
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    SearchHits hits = searchResponse.getHits();
    SearchHit[] searchHits = hits.getHits();
    for (SearchHit hit : searchHits) {
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        list.add(new MetaData((String)sourceAsMap.get("infoHash"),
                Long.parseLong(String.valueOf(sourceAsMap.get("length"))),
                (String)sourceAsMap.get("name"),(String)sourceAsMap.get("nameInfo")));
    }
    return list;
}
 
Example #2
Source File: CaseController.java    From skywalking with Apache License 2.0 7 votes vote down vote up
private void search(String indexName) throws IOException {
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(QueryBuilders.termQuery("author", "Marker"));
    sourceBuilder.from(0);
    sourceBuilder.size(10);

    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices(indexName);
    searchRequest.source(sourceBuilder);
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

    int length = searchResponse.getHits().getHits().length;
    if (!(length > 0)) {
        String message = "elasticsearch search data fail.";
        logger.error(message);
        throw new RuntimeException(message);
    }
}
 
Example #3
Source File: EsHelper.java    From occurrence with Apache License 2.0 6 votes vote down vote up
/**
 * Finds all the indexes of the alias where the given datasetKey is indexed.
 *
 * @param esClient client to connect to ES
 * @param datasetKey datasetKey to look for
 * @param aliases index or alias where we are looking for indexes
 * @return indexes found
 */
public static Set<String> findExistingIndexesInAliases(
    final RestHighLevelClient esClient, String datasetKey, String[] aliases) {
  Objects.requireNonNull(esClient);
  Preconditions.checkArgument(!Strings.isNullOrEmpty(datasetKey), "datasetKey is required");
  Preconditions.checkArgument(aliases != null && aliases.length > 0, "aliases are required");

  SearchRequest esRequest = new SearchRequest();
  esRequest.indices(aliases);

  SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  searchSourceBuilder.size(0);
  esRequest.source(searchSourceBuilder);

  // add match query to filter by datasetKey
  searchSourceBuilder.query(QueryBuilders.matchQuery(DATASET_KEY_FIELD, datasetKey));
  // add aggs by index
  searchSourceBuilder.aggregation(AggregationBuilders.terms(AGG_BY_INDEX).field("_index"));

  try {
    return parseFindExistingIndexesInAliasResponse(esClient.search(esRequest, HEADERS.get()));
  } catch (IOException e) {
    throw new SearchException("Could not find indexes that contain the dataset " + datasetKey, e);
  }
}
 
Example #4
Source File: LogstashIT.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private static long queryDocumentCount(
        final RestHighLevelClient client)
        throws IOException {
    final SearchSourceBuilder searchSourceBuilder =
            new SearchSourceBuilder()
                    .size(0)
                    .fetchSource(false);
    final SearchRequest searchRequest =
            new SearchRequest(MavenHardcodedConstants.ES_INDEX_NAME)
                    .source(searchSourceBuilder);
    try {
        final SearchResponse searchResponse =
                client.search(searchRequest, RequestOptions.DEFAULT);
        return searchResponse.getHits().getTotalHits().value;
    } catch (ElasticsearchStatusException error) {
        if (RestStatus.NOT_FOUND.equals(error.status())) {
            return 0L;
        }
        throw new IOException(error);
    }
}
 
Example #5
Source File: ProfileTaskQueryEsDAO.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public ProfileTask getById(String id) throws IOException {
    if (StringUtil.isEmpty(id)) {
        return null;
    }

    SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();
    sourceBuilder.query(QueryBuilders.idsQuery().addIds(id));
    sourceBuilder.size(1);

    final SearchResponse response = getClient().search(ProfileTaskRecord.INDEX_NAME, sourceBuilder);

    if (response.getHits().getHits().length > 0) {
        return parseTask(response.getHits().getHits()[0]);
    }

    return null;
}
 
Example #6
Source File: DetectorMappingRepositoryImpl.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
private MatchingDetectorsResponse getDetectorMappings(SearchSourceBuilder searchSourceBuilder,
                                                      List<Map<String, String>> tagsList) throws IOException {
    val searchRequest = new SearchRequest()
            .source(searchSourceBuilder)
            .indices(elasticSearchProperties.getIndexName());

    val searchResponse = legacyElasticSearchClient.search(searchRequest, RequestOptions.DEFAULT);
    delayTimer.update(searchResponse.getTook().getMillis(), TimeUnit.MILLISECONDS);
    val hits = searchResponse.getHits().getHits();

    List<DetectorMapping> detectorMappings = Arrays.asList(hits).stream()
            .map(hit -> getDetectorMapping(hit.getSourceAsString(), hit.getId(), Optional.of(hit.getFields())))
            // .filter(detectorMapping -> detectorMapping.isEnabled()) //FIXME - move this condition into search query
            .collect(Collectors.toList());
    return convertToMatchingDetectorsResponse(new DetectorMatchResponse(detectorMappings,
            searchResponse.getTook().getMillis()));
}
 
Example #7
Source File: ElasticsearchAnySearchDAO.java    From syncope with Apache License 2.0 6 votes vote down vote up
private SearchRequest searchRequest(
        final Set<String> adminRealms,
        final SearchCond cond,
        final AnyTypeKind kind,
        final int from,
        final int size,
        final List<SortBuilder<?>> sortBuilders) {

    Pair<DisMaxQueryBuilder, Set<String>> filter = adminRealmsFilter(adminRealms);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().
            query(SyncopeConstants.FULL_ADMIN_REALMS.equals(adminRealms)
                    ? getQueryBuilder(cond, kind)
                    : QueryBuilders.boolQuery().
                            must(filter.getLeft()).
                            must(getQueryBuilder(buildEffectiveCond(cond, filter.getRight()), kind))).
            from(from).
            size(size);
    sortBuilders.forEach(sourceBuilder::sort);

    return new SearchRequest(ElasticsearchUtils.getContextDomainName(AuthContextUtils.getDomain(), kind)).
            searchType(SearchType.QUERY_THEN_FETCH).
            source(sourceBuilder);
}
 
Example #8
Source File: MetadataBackendKV.java    From heroic with Apache License 2.0 6 votes vote down vote up
@Override
public AsyncFuture<CountSeries> countSeries(final CountSeries.Request filter) {
    return doto(c -> {
        final OptionalLimit limit = filter.getLimit();

        if (limit.isZero()) {
            return async.resolved(new CountSeries());
        }

        final QueryBuilder f = filter(filter.getFilter());

        SearchRequest request = c.getIndex().count(METADATA_TYPE);
        SearchSourceBuilder sourceBuilder = request.source();
        limit.asInteger().ifPresent(sourceBuilder::terminateAfter);
        sourceBuilder.query(new BoolQueryBuilder().must(f));

        final ResolvableFuture<SearchResponse> future = async.future();
        c.execute(request, bind(future));

        return future.directTransform(
            r -> new CountSeries(r.getHits().getTotalHits().value, false));

    });
}
 
Example #9
Source File: ShellScope.java    From elasticshell with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new <code>ShellScope</code> given the actual scope object
 * @param scope the actual scope object that depends on the engine in use
 */
ShellScope(Scope scope, ResourceRegistry resourceRegistry) {
    this.scope = scope;
    this.resourceRegistry = resourceRegistry;
    registerJavaClass(Requests.class);
    registerJavaClass(SearchSourceBuilder.class);
    registerJavaClass(QueryBuilders.class);
    registerJavaClass(FilterBuilders.class);
    registerJavaClass(SortBuilders.class);
    registerJavaClass(FacetBuilders.class);
    registerJavaClass(RescoreBuilder.class);
    registerJavaClass(SuggestBuilder.class);
    registerJavaClass(AliasAction.class);
    registerJavaClass(HttpParameters.class);
    registerJavaClass(AllocateAllocationCommand.class);
    registerJavaClass(CancelAllocationCommand.class);
    registerJavaClass(MoveAllocationCommand.class);
    registerJavaClass(ShardId.class);
}
 
Example #10
Source File: DetectorMappingRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    val mom = ObjectMother.instance();
    val config = new ElasticSearchProperties.Config().setConnectionTimeout(100);

    percolatorDetectorMapping = buildPercolatorDetectorMapping();
    when(metricRegistry.timer(any())).thenReturn(mock(Timer.class));
    when(metricRegistry.counter(any())).thenReturn(mock(Counter.class));
    when(elasticSearchProperties.getIndexName()).thenReturn("detector-mappings");
    when(elasticSearchProperties.getDocType()).thenReturn("details");
    when(elasticSearchProperties.getConfig()).thenReturn(config);

    this.repoUnderTest = new DetectorMappingRepositoryImpl(metricRegistry);
    ReflectionTestUtils.setField(repoUnderTest, "legacyElasticSearchClient", legacyElasticSearchClient);
    ReflectionTestUtils.setField(repoUnderTest, "elasticSearchProperties", elasticSearchProperties);
    ReflectionTestUtils.setField(repoUnderTest, "elasticsearchUtil", elasticsearchUtil);
    ReflectionTestUtils.setField(repoUnderTest, "objectMapperUtil", objectMapperUtil);

    when(elasticsearchUtil.getSourceBuilder(any(QueryBuilder.class))).thenReturn(new SearchSourceBuilder());
    when(elasticsearchUtil.getSearchRequest(any(SearchSourceBuilder.class), anyString(), anyString())).thenReturn(new SearchRequest());
    when(elasticsearchUtil.index(any(IndexRequest.class), anyString())).thenReturn(new IndexResponse());

    when(objectMapperUtil.convertToString(any())).thenReturn("");
    when(objectMapperUtil.convertToObject(anyString(), any())).thenReturn(percolatorDetectorMapping);
}
 
Example #11
Source File: ElasticSearchQueryManualTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() throws Exception {
    final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags")
        .field("title");

    final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
    final SearchRequest searchRequest = new SearchRequest("blog").source(builder);

    final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

    final Map<String, Aggregation> results = response.getAggregations()
        .asMap();
    final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");

    final List<String> keys = topTags.getBuckets()
        .stream()
        .map(MultiBucketsAggregation.Bucket::getKeyAsString)
        .sorted()
        .collect(toList());
    assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys);
}
 
Example #12
Source File: MetricsQueryEsDAO.java    From skywalking with Apache License 2.0 6 votes vote down vote up
protected final void buildQuery(SearchSourceBuilder sourceBuilder, MetricsCondition condition, Duration duration) {
    final List<PointOfTime> pointOfTimes = duration.assembleDurationPoints();
    List<String> ids = new ArrayList<>(pointOfTimes.size());
    pointOfTimes.forEach(pointOfTime -> {
        ids.add(pointOfTime.id(condition.getEntity().buildId()));
    });

    RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(Metrics.TIME_BUCKET)
                                                       .gte(duration.getStartTimeBucket())
                                                       .lte(duration.getEndTimeBucket());

    final String entityId = condition.getEntity().buildId();

    if (entityId == null) {
        sourceBuilder.query(rangeQueryBuilder);
    } else {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.must().add(rangeQueryBuilder);
        boolQuery.must().add(QueryBuilders.termsQuery(Metrics.ENTITY_ID, entityId));

        sourceBuilder.query(boolQuery);
    }
    sourceBuilder.size(0);
}
 
Example #13
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 #14
Source File: SearchableImpl.java    From spring-content with Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<Serializable> findKeyword(String query) {
	SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
	searchRequest.types(domainClass.getName());

	SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
	sourceBuilder.query(QueryBuilders.queryStringQuery(query));
	searchRequest.source(sourceBuilder);

	SearchResponse res = null;
	try {
		res = client.search(searchRequest, RequestOptions.DEFAULT);
	}
	catch (IOException ioe) {
		throw new StoreAccessException(format("Error searching indexed content for '%s'", query), ioe);
	}

	return getIDs(res.getHits());
}
 
Example #15
Source File: ElasticSearchClient.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
public SearchHits queryBoostedQueryIndex(String boostedQuery, String field, int resultsSize) throws IOException
{
	if(client==null)
		return null;
	
	QueryBuilder query = QueryBuilders.queryStringQuery(boostedQuery).defaultField(field);
	SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
	sourceBuilder.query(query);
	sourceBuilder.size(resultsSize);
	
	SearchRequest searchRequest = new SearchRequest();
	searchRequest.source(sourceBuilder);
	
	SearchResponse searchResponse = client.search(searchRequest);
	return searchResponse.getHits();
}
 
Example #16
Source File: AnomalyDetectorProfileRunner.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
/**
 * Create search request to check if we have at least 1 anomaly score larger than 0 after AD job enabled time
 * @param detectorId detector id
 * @param enabledTime the time when AD job is enabled in milliseconds
 * @return the search request
 */
private SearchRequest createInittedEverRequest(String detectorId, long enabledTime) {
    BoolQueryBuilder filterQuery = new BoolQueryBuilder();
    filterQuery.filter(QueryBuilders.termQuery(AnomalyResult.DETECTOR_ID_FIELD, detectorId));
    filterQuery.filter(QueryBuilders.rangeQuery(AnomalyResult.EXECUTION_END_TIME_FIELD).gte(enabledTime));
    filterQuery.filter(QueryBuilders.rangeQuery(AnomalyResult.ANOMALY_SCORE_FIELD).gt(0));

    // I am only looking for last 1 occurrence and have no interest in the total number of documents that match the query.
    // ES will not try to count the number of documents and will be able to terminate the query as soon as 1 document
    // have been collected per segment.
    SearchSourceBuilder source = new SearchSourceBuilder().query(filterQuery).size(1).trackTotalHits(false);

    SearchRequest request = new SearchRequest(AnomalyResult.ANOMALY_RESULT_INDEX);
    request.source(source);
    return request;
}
 
Example #17
Source File: EsSearchRequestBuilder.java    From occurrence with Apache License 2.0 6 votes vote down vote up
static SearchRequest buildSuggestQuery(
    String prefix, OccurrenceSearchParameter parameter, Integer limit, String index) {
  SearchRequest request = new SearchRequest();
  request.indices(index);

  SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  request.source(searchSourceBuilder);

  OccurrenceEsField esField = SEARCH_TO_ES_MAPPING.get(parameter);

  // create suggest query
  searchSourceBuilder.suggest(
      new SuggestBuilder()
          .addSuggestion(
              esField.getFieldName(),
              SuggestBuilders.completionSuggestion(esField.getFieldName() + ".suggest")
                  .prefix(prefix)
                  .size(limit != null ? limit : SearchConstants.DEFAULT_SUGGEST_LIMIT)
                  .skipDuplicates(true)));

  // add source field
  searchSourceBuilder.fetchSource(esField.getFieldName(), null);

  return request;
}
 
Example #18
Source File: EsHighLevelRestSearchTest.java    From java-study with Apache License 2.0 6 votes vote down vote up
/**
 * @return void
 * @Author pancm
 * @Description exist查询
 * @Date 2019/9/17
 * @Param []
 **/
private static void existSearch() throws IOException {
    String type = "_doc";
    String index = "test1";
    // 查询指定的索引库
    SearchRequest searchRequest = new SearchRequest(index);
    searchRequest.types(type);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    // 设置查询条件
    sourceBuilder.query(QueryBuilders.existsQuery("msgcode"));
    searchRequest.source(sourceBuilder);
    System.out.println("存在查询的DSL语句:"+sourceBuilder.toString());
    // 同步查询
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    // 结果
    searchResponse.getHits().forEach(hit -> {
        Map<String, Object> map = hit.getSourceAsMap();
        String string = hit.getSourceAsString();
        System.out.println("存在查询的Map结果:" + map);
        System.out.println("存在查询的String结果:" + string);
    });
    System.out.println("\n=================\n");
}
 
Example #19
Source File: DefaultMigrationClient.java    From elasticsearch-migration with Apache License 2.0 6 votes vote down vote up
private List<MigrationEntry> getAllMigrations() {
    try {
        final QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.termQuery(MigrationEntryMeta.IDENTIFIER_FIELD, identifier));
        final SearchRequest searchRequest = new SearchRequest()
                .indices(MigrationEntryMeta.INDEX)
                .searchType(SearchType.DEFAULT)
                .source(SearchSourceBuilder.searchSource().query(queryBuilder).fetchSource(true).size(1000).sort(MigrationEntryMeta.VERSION_FIELD, SortOrder.ASC));

        final SearchResponse searchResponse = restHighLevelClient.search(searchRequest);
        if (searchResponse.status() == RestStatus.OK) {
            return transformHitsFromEs(searchResponse.getHits(), MigrationEntry.class);
        } else {
            throw new MigrationFailedException("Could not access '" + MigrationEntryMeta.INDEX + "' index. Failures: " + Arrays.asList(searchResponse.getShardFailures()));
        }
    } catch (IOException e) {
        throw new MigrationFailedException("IO Exception during migration", e);
    }
}
 
Example #20
Source File: SearchableImpl.java    From spring-content with Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<Serializable> search(String queryStr) {
	SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
	searchRequest.types(domainClass.getName());

	SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
	sourceBuilder.query(QueryBuilders.simpleQueryStringQuery(queryStr));
	searchRequest.source(sourceBuilder);

	SearchResponse res = null;
	try {
		res = client.search(searchRequest, RequestOptions.DEFAULT);
	}
	catch (IOException ioe) {
		throw new StoreAccessException(format("Error searching indexed content for '%s'", queryStr), ioe);
	}

	return getIDs(res.getHits());
}
 
Example #21
Source File: StatsAggregationMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("cmd", "weather_hourforcast");
        StatsAggregationBuilder aggregationBuilder = AggregationBuilders.stats("utm_stats").field("utm").missing(0);

        SearchRequest searchRequest = new SearchRequest("serverlog_20180710");//限定index
        searchRequest.types("log");//限定type

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(matchQueryBuilder);
        searchSourceBuilder.aggregation(aggregationBuilder);
        searchSourceBuilder.size(0);
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);

    }finally{
        HighLevelClient.close();
    }
}
 
Example #22
Source File: ElasticSearchRestDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public List<TaskExecLog> getTaskExecutionLogs(String taskId) {
    try {
        BoolQueryBuilder query = boolQueryBuilder("taskId='" + taskId + "'", "*");

        // Create the searchObjectIdsViaExpression source
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(query);
        searchSourceBuilder.sort(new FieldSortBuilder("createdTime").order(SortOrder.ASC));
        searchSourceBuilder.size(config.getElasticSearchTasklogLimit());

        // Generate the actual request to send to ES.
        String docType = StringUtils.isBlank(docTypeOverride) ? LOG_DOC_TYPE : docTypeOverride;
        SearchRequest searchRequest = new SearchRequest(logIndexPrefix + "*");
        searchRequest.types(docType);
        searchRequest.source(searchSourceBuilder);

        SearchResponse response = elasticSearchClient.search(searchRequest);

        return mapTaskExecLogsResponse(response);
    } catch (Exception e) {
        logger.error("Failed to get task execution logs for task: {}", taskId, e);
    }
    return null;
}
 
Example #23
Source File: TopologyQueryEsDAO.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private List<Call.CallDetail> loadEndpoint(SearchSourceBuilder sourceBuilder, String indexName,
                                           DetectPoint detectPoint) throws IOException {
    sourceBuilder.aggregation(AggregationBuilders.terms(Metrics.ENTITY_ID).field(Metrics.ENTITY_ID).size(1000));

    SearchResponse response = getClient().search(indexName, sourceBuilder);

    List<Call.CallDetail> calls = new ArrayList<>();
    Terms entityTerms = response.getAggregations().get(Metrics.ENTITY_ID);
    for (Terms.Bucket entityBucket : entityTerms.getBuckets()) {
        String entityId = entityBucket.getKeyAsString();

        Call.CallDetail call = new Call.CallDetail();
        call.buildFromEndpointRelation(entityId, detectPoint);
        calls.add(call);
    }
    return calls;
}
 
Example #24
Source File: MultiMatchQueryApidMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        QueryBuilder matchQueryBuilder = QueryBuilders.multiMatchQuery("token","retdesc", "reqpkg");//查询有问题??

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(matchQueryBuilder);
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(5);

        SearchRequest searchRequest = new SearchRequest("serverlog_20180701");//限定index
        searchRequest.types("log");//限定type
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);


    }finally{
        HighLevelClient.close();
    }
}
 
Example #25
Source File: SalesTrendsQueryAdapter.java    From micronaut-microservices-poc with Apache License 2.0 6 votes vote down vote up
@Override
SearchRequest buildQuery() {
    SearchRequest searchRequest = new SearchRequest("policy_stats")
            .types("policy_type");

    BoolQueryBuilder filterBuilder = QueryBuilders.boolQuery();
    if (query.getFilterByProductCode()!=null) {
        filterBuilder.must(QueryBuilders.termQuery("productCode.keyword", query.getFilterByProductCode()));
    }
    if (query.getFilterBySalesDate()!=null){
        RangeQueryBuilder datesRange = QueryBuilders
                .rangeQuery("from")
                .gte(query.getFilterBySalesDate().getFrom().toString())
                .lt(query.getFilterBySalesDate().getTo().toString());
        filterBuilder.must(datesRange);
    }
    AggregationBuilder aggBuilder = AggregationBuilders.filter("agg_filter",filterBuilder);

    DateHistogramAggregationBuilder histBuilder = AggregationBuilders
            .dateHistogram("sales")
            .field("from")
            .dateHistogramInterval(query.getAggregationUnit().toDateHistogramInterval())
            .subAggregation(AggregationBuilders.sum("total_premium").field("totalPremium"));
    aggBuilder.subAggregation(histBuilder);

    SearchSourceBuilder srcBuilder = new SearchSourceBuilder()
            .aggregation(aggBuilder)
            .size(0);
    searchRequest.source(srcBuilder);

    return searchRequest;
}
 
Example #26
Source File: RestHighLevelClientCase.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private void search(RestHighLevelClient client, String indexName) throws IOException {

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.termQuery("author", "Marker"));
        sourceBuilder.from(0);
        sourceBuilder.size(10);

        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(indexName);
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        if (!(searchResponse.getHits().totalHits > 0)) {
            String message = "elasticsearch search data fail.";
            logger.error(message);
            throw new RuntimeException(message);
        }
    }
 
Example #27
Source File: SearchFeatureDaoTests.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
@Test
public void test_getLatestDataTime_returnEmpty_givenNoData() {
    // pre-conditions
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
        .aggregation(AggregationBuilders.max(SearchFeatureDao.AGG_NAME_MAX).field(detector.getTimeField()))
        .size(0);
    searchRequest.source(searchSourceBuilder);

    when(searchResponse.getAggregations()).thenReturn(null);

    // test
    Optional<Long> result = searchFeatureDao.getLatestDataTime(detector);

    // verify
    assertFalse(result.isPresent());
}
 
Example #28
Source File: BookingDaoESImp.java    From blue-marlin with Apache License 2.0 6 votes vote down vote up
@Override
public Booking getBooking(String bookingId) throws IOException
{
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    SearchRequest searchRequest;
    append(boolQueryBuilder, "bk_id", bookingId);
    append(boolQueryBuilder, "del", false);

    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(boolQueryBuilder).size(MAX_BOOKING_READ_PER_DAY);
    searchRequest = new SearchRequest(this.bookingsIndex).source(sourceBuilder);
    ESResponse esResponse = esclient.search(searchRequest);

    List docs = (List) ((Map) (esResponse.getSourceMap().get("hits"))).get("hits");
    for (Object doc : docs)
    {
        Map mapDoc = (Map) doc;
        Booking booking = buildBooking(mapDoc);
        if (booking != null)
        {
            return booking;
        }
    }
    return null;
}
 
Example #29
Source File: TopologyQueryEsDAO.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public List<Call.CallDetail> loadEndpointRelation(long startTB,
                                                  long endTB,
                                                  String destEndpointId) throws IOException {
    SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();
    sourceBuilder.size(0);

    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    boolQuery.must()
             .add(QueryBuilders.rangeQuery(EndpointRelationServerSideMetrics.TIME_BUCKET).gte(startTB).lte(endTB));

    BoolQueryBuilder serviceIdBoolQuery = QueryBuilders.boolQuery();
    boolQuery.must().add(serviceIdBoolQuery);
    serviceIdBoolQuery.should()
                      .add(QueryBuilders.termQuery(
                          EndpointRelationServerSideMetrics.SOURCE_ENDPOINT, destEndpointId
                      ));
    serviceIdBoolQuery.should()
                      .add(QueryBuilders.termQuery(
                          EndpointRelationServerSideMetrics.DEST_ENDPOINT, destEndpointId
                      ));

    sourceBuilder.query(boolQuery);

    return loadEndpoint(sourceBuilder, EndpointRelationServerSideMetrics.INDEX_NAME, DetectPoint.SERVER);
}
 
Example #30
Source File: CountAggregationMain.java    From elasticsearch-pool with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("cmd", "get_fee_transfer_content_data");
        ValueCountAggregationBuilder aggregationBuilder = AggregationBuilders.count("cmd_count").field("cmd");

        SearchRequest searchRequest = new SearchRequest("serverlog_20180715");//限定index
        searchRequest.types("log");//限定type

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(matchQueryBuilder);
        searchSourceBuilder.aggregation(aggregationBuilder);
        searchSourceBuilder.size(0);//不返回具体业务数据,只需要聚合结果
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);

        //统计结果
        Aggregations aggregations = searchResponse.getAggregations();
        Map<String, Aggregation> aggregationMap = aggregations.asMap();
        for(Map.Entry<String,Aggregation> each: aggregationMap.entrySet()){
            System.out.println(((ParsedValueCount)(each.getValue())).getValue());
        }

    }finally{
        HighLevelClient.close();
    }
}