Java Code Examples for org.elasticsearch.search.builder.SearchSourceBuilder#from()

The following examples show how to use org.elasticsearch.search.builder.SearchSourceBuilder#from() . 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: 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 2
Source File: ElasticSearchUtil.java    From ranger with Apache License 2.0 6 votes vote down vote up
public SearchResponse searchResources(SearchCriteria searchCriteria, List<SearchField> searchFields, List<SortField> sortFields, RestHighLevelClient client, String index) throws IOException {
    // See Also: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-query-builders.html
    QueryAccumulator queryAccumulator = new QueryAccumulator(searchCriteria);
    if (searchCriteria.getParamList() != null) {
        searchFields.stream().forEach(queryAccumulator::addQuery);
        // For now assuming there is only date field where range query will
        // be done. If we there are more than one, then we should create a
        // hashmap for each field name
        if (queryAccumulator.fromDate != null || queryAccumulator.toDate != null) {
            queryAccumulator.queries.add(setDateRange(queryAccumulator.dateFieldName, queryAccumulator.fromDate, queryAccumulator.toDate));
        }
    }
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    queryAccumulator.queries.stream().filter(x -> x != null).forEach(boolQueryBuilder::must);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    setSortClause(searchCriteria, sortFields, searchSourceBuilder);
    searchSourceBuilder.from(searchCriteria.getStartIndex());
    searchSourceBuilder.size(searchCriteria.getMaxRows());
    searchSourceBuilder.fetchSource(true);
    SearchRequest query = new SearchRequest();
    query.indices(index);
    query.source(searchSourceBuilder.query(boolQueryBuilder));
    return client.search(query, RequestOptions.DEFAULT);
}
 
Example 3
Source File: QueryAllMain.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.matchAllQuery();
        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 4
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 5
Source File: NumberRangeApiMain.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{
        RangeQueryBuilder matchQueryBuilder = QueryBuilders.rangeQuery("utm").from(9,false).to(11,false);

        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 6
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 7
Source File: MatchParseApiMain.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.matchPhraseQuery("content","校车");

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

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

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


    }finally{
        HighLevelClient.close();
    }
}
 
Example 8
Source File: DateRangeApiMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void dateRange1() throws IOException {
        RestHighLevelClient client = HighLevelClient.getInstance();
        try{
            RangeQueryBuilder matchQueryBuilder = QueryBuilders.rangeQuery("ctm")
                    .from("2018-07-01 07:27:59.733",true)
                    .to("2018-07-01 07:30:00.000",false);
//            matchQueryBuilder.format("yyyy-MM-dd HH:mm:ss.SSS");//设置日期格式
//            matchQueryBuilder.timeZone("+08:00");//设置时区

            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 9
Source File: ElasticSearcher.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Page<Product> search(String keyword, int pageNum, int pageSize) {

    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    boolQueryBuilder.should(new MatchQueryBuilder("title", keyword));
    boolQueryBuilder.should(new MatchQueryBuilder("content", keyword));


    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.from(pageNum * pageSize - pageSize);
    sourceBuilder.size(pageSize);
    sourceBuilder.query(boolQueryBuilder);

    SearchRequest searchRequest = new SearchRequest();
    searchRequest.source(sourceBuilder);
    searchRequest.indices(index);
    searchRequest.types(type);

    try {
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        if (response == null || response.getHits() == null || response.getHits().getTotalHits().value <= 0) {
            return null;
        }

        int total = (int) response.getHits().getTotalHits().value;

        List<Product> products = new ArrayList<>();
        response.getHits().forEach(hit -> {
            Product product = new Product();
            product.put(hit.getSourceAsMap());
            products.add(product);
        });

        return new Page<>(products, pageNum, pageSize, total / pageSize, total);

    } catch (Exception e) {
        LOG.error(e.toString(), e);
    }
    return null;
}
 
Example 10
Source File: ElasticSearchRestDAOV5.java    From conductor with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to find object ids for a given query in an index.
 *
 * @param indexName The name of the index.
 * @param queryBuilder The query to use for searching.
 * @param start The start to use.
 * @param size The total return size.
 * @param sortOptions A list of string options to sort in the form VALUE:ORDER; where ORDER is optional and can be either ASC OR DESC.
 * @param docType The document type to searchObjectIdsViaExpression for.
 *
 * @return The SearchResults which includes the count and IDs that were found.
 * @throws IOException If we cannot communicate with ES.
 */
private SearchResult<String> searchObjectIds(String indexName, QueryBuilder queryBuilder, int start, int size, List<String> sortOptions, String docType) throws IOException {

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(queryBuilder);
    searchSourceBuilder.from(start);
    searchSourceBuilder.size(size);

    if (sortOptions != null && !sortOptions.isEmpty()) {

        for (String sortOption : sortOptions) {
            SortOrder order = SortOrder.ASC;
            String field = sortOption;
            int index = sortOption.indexOf(":");
            if (index > 0) {
                field = sortOption.substring(0, index);
                order = SortOrder.valueOf(sortOption.substring(index + 1));
            }
            searchSourceBuilder.sort(new FieldSortBuilder(field).order(order));
        }
    }

    // Generate the actual request to send to ES.
    SearchRequest searchRequest = new SearchRequest(indexName);
    searchRequest.types(docType);
    searchRequest.source(searchSourceBuilder);

    SearchResponse response = elasticSearchClient.search(searchRequest);

    List<String> result = new LinkedList<>();
    response.getHits().forEach(hit -> result.add(hit.getId()));
    long count = response.getHits().getTotalHits();
    return new SearchResult<>(count, result);
}
 
Example 11
Source File: EsSearchRequestBuilder.java    From occurrence with Apache License 2.0 5 votes vote down vote up
public static SearchRequest buildSearchRequest(
    OccurrenceSearchRequest searchRequest, boolean facetsEnabled, String index) {

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

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

  // size and offset
  searchSourceBuilder.size(searchRequest.getLimit());
  searchSourceBuilder.from((int) searchRequest.getOffset());

  // sort
  if (Strings.isNullOrEmpty(searchRequest.getQ())) {
    searchSourceBuilder.sort(SortBuilders.fieldSort("year").order(SortOrder.DESC));
    searchSourceBuilder.sort(SortBuilders.fieldSort("month").order(SortOrder.ASC));
    searchSourceBuilder.sort(SortBuilders.fieldSort("_id").order(SortOrder.ASC));
  } else {
    searchSourceBuilder.sort(SortBuilders.scoreSort());
  }

  // group params
  GroupedParams groupedParams = groupParameters(searchRequest);

  // add query
  buildQuery(groupedParams.queryParams, searchRequest.getQ())
      .ifPresent(searchSourceBuilder::query);

  // add aggs
  buildAggs(searchRequest, groupedParams.postFilterParams, facetsEnabled)
      .ifPresent(aggsList -> aggsList.forEach(searchSourceBuilder::aggregation));

  // post-filter
  buildPostFilter(groupedParams.postFilterParams).ifPresent(searchSourceBuilder::postFilter);

  return esRequest;
}
 
Example 12
Source File: ElasticSearcher.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Page<Article> search(String keyword, int pageNum, int pageSize) {

    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    boolQueryBuilder.should(new MatchQueryBuilder("title", keyword));
    boolQueryBuilder.should(new MatchQueryBuilder("content", keyword));


    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.from(pageNum * pageSize - pageSize);
    sourceBuilder.size(pageSize);
    sourceBuilder.query(boolQueryBuilder);

    SearchRequest searchRequest = new SearchRequest();
    searchRequest.source(sourceBuilder);
    searchRequest.indices(index);
    searchRequest.types(type);

    try {
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        if (response ==null || response.getHits() == null || response.getHits().getTotalHits().value <= 0){
            return null;
        }

        int total = (int) response.getHits().getTotalHits().value;

        List<Article> articles = new ArrayList<>();
        response.getHits().forEach(hit -> {
            Article article = new Article();
            article.put(hit.getSourceAsMap());
            articles.add(article);
        });

        return new Page<>(articles, pageNum, pageSize, total / pageSize, total);

    } catch (Exception e) {
        LOG.error(e.toString(), e);
    }
    return null;
}
 
Example 13
Source File: SearchQueryProcessor.java    From occurrence with Apache License 2.0 5 votes vote down vote up
/**
 * Executes a query and applies the predicate to each result.
 *
 * @param downloadFileWork it's used to determine how to page through the results and the search query to be used
 * @param downloadFileWork it's used to determine how to page through the results and the search query to be used
 * @param resultHandler    predicate that process each result, receives as parameter the occurrence key
 */
public static void processQuery(DownloadFileWork downloadFileWork, Consumer<Occurrence> resultHandler) {

  // Calculates the amount of output records
  int nrOfOutputRecords = downloadFileWork.getTo() - downloadFileWork.getFrom();

  try {

    int recordCount = 0;
    // Creates a search request instance using the search request that comes in the fileJob
    SearchSourceBuilder searchSourceBuilder = createSearchQuery(downloadFileWork.getQuery());


    while (recordCount < nrOfOutputRecords) {

      searchSourceBuilder.size(recordCount + LIMIT > nrOfOutputRecords ? nrOfOutputRecords - recordCount : LIMIT);
      searchSourceBuilder.from(downloadFileWork.getFrom() + recordCount);
      searchSourceBuilder.fetchSource(null, "all"); //All field is not needed in the response
      SearchRequest searchRequest = new SearchRequest().indices(downloadFileWork.getEsIndex()).source(searchSourceBuilder);

      SearchResponse searchResponse = downloadFileWork.getEsClient().search(searchRequest, RequestOptions.DEFAULT);
      consume(searchResponse, resultHandler);

      SearchHit[] searchHits = searchResponse.getHits().getHits();
      recordCount += searchHits.length;

    }
  } catch (IOException ex) {
    throw Throwables.propagate(ex);
  }
}
 
Example 14
Source File: ElasticsearchRestClientInstrumentationIT_RealReporter.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDocumentScenario() throws IOException {
    // Index a document
    IndexResponse ir = client.index(new IndexRequest(INDEX, DOC_TYPE, DOC_ID).source(
        jsonBuilder()
            .startObject()
            .field(FOO, BAR)
            .endObject()
    ).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
    assertThat(ir.status().getStatus()).isEqualTo(201);

    SearchRequest searchRequest = new SearchRequest(INDEX);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(QueryBuilders.termQuery(FOO, BAR));
    sourceBuilder.from(0);
    sourceBuilder.size(5);
    searchRequest.source(sourceBuilder);
    SearchResponse sr = client.search(searchRequest, RequestOptions.DEFAULT);
    assertThat(sr.getHits().totalHits).isEqualTo(1L);
    assertThat(sr.getHits().getAt(0).getSourceAsMap().get(FOO)).isEqualTo(BAR);

    // Now update and re-search
    Map<String, Object> jsonMap = new HashMap<>();
    jsonMap.put(FOO, BAZ);
    UpdateResponse ur = client.update(new UpdateRequest(INDEX, DOC_TYPE, DOC_ID).doc(jsonMap)
        .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
    assertThat(ur.status().getStatus()).isEqualTo(200);
    sr = client.search(new SearchRequest(INDEX), RequestOptions.DEFAULT);
    assertThat(sr.getHits().getAt(0).getSourceAsMap().get(FOO)).isEqualTo(BAZ);

    // Finally - delete the document
    DeleteResponse dr = client.delete(new DeleteRequest(INDEX, DOC_TYPE, DOC_ID), RequestOptions.DEFAULT);
    assertThat(dr.status().getStatus()).isEqualTo(200);
}
 
Example 15
Source File: AlarmQueryEsDAO.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public Alarms getAlarm(final Integer scopeId, final String keyword, final int limit, final int from,
    final long startTB, final long endTB) throws IOException {
    SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();

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

    if (Objects.nonNull(scopeId)) {
        boolQueryBuilder.must().add(QueryBuilders.termQuery(AlarmRecord.SCOPE, scopeId.intValue()));
    }

    if (!Strings.isNullOrEmpty(keyword)) {
        String matchCName = MatchCNameBuilder.INSTANCE.build(AlarmRecord.ALARM_MESSAGE);
        boolQueryBuilder.must().add(QueryBuilders.matchPhraseQuery(matchCName, keyword));
    }

    sourceBuilder.query(boolQueryBuilder).sort(AlarmRecord.START_TIME, SortOrder.DESC);
    sourceBuilder.size(limit);
    sourceBuilder.from(from);

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

    Alarms alarms = new Alarms();
    alarms.setTotal((int) response.getHits().totalHits);

    for (SearchHit searchHit : response.getHits().getHits()) {
        AlarmRecord.Builder builder = new AlarmRecord.Builder();
        AlarmRecord alarmRecord = builder.map2Data(searchHit.getSourceAsMap());

        AlarmMessage message = new AlarmMessage();
        message.setId(String.valueOf(alarmRecord.getId0()));
        message.setMessage(alarmRecord.getAlarmMessage());
        message.setStartTime(alarmRecord.getStartTime());
        message.setScope(Scope.Finder.valueOf(alarmRecord.getScope()));
        message.setScopeId(alarmRecord.getScope());
        alarms.getMsgs().add(message);
    }
    return alarms;
}
 
Example 16
Source File: TestElasticSearchRestDAOV5.java    From conductor with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to find object ids for a given query in an index.
 *
 * @param indexName The name of the index.
 * @param queryBuilder The query to use for searching.
 * @param start The start to use.
 * @param size The total return size.
 * @param sortOptions A list of string options to sort in the form VALUE:ORDER; where ORDER is optional and can be either ASC OR DESC.
 * @param docType The document type to searchObjectIdsViaExpression for.
 *
 * @return The SearchResults which includes the count and IDs that were found.
 * @throws IOException If we cannot communicate with ES.
 */
private SearchResponse searchObjectIds(String indexName, QueryBuilder queryBuilder, int start, int size, List<String> sortOptions, String docType) throws IOException {

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(queryBuilder);
    searchSourceBuilder.from(start);
    searchSourceBuilder.size(size);

    if (sortOptions != null && !sortOptions.isEmpty()) {

        for (String sortOption : sortOptions) {
            SortOrder order = SortOrder.ASC;
            String field = sortOption;
            int index = sortOption.indexOf(":");
            if (index > 0) {
                field = sortOption.substring(0, index);
                order = SortOrder.valueOf(sortOption.substring(index + 1));
            }
            searchSourceBuilder.sort(new FieldSortBuilder(field).order(order));
        }
    }

    // Generate the actual request to send to ES.
    SearchRequest searchRequest = new SearchRequest(indexName);
    searchRequest.types(docType);
    searchRequest.source(searchSourceBuilder);

    return elasticSearchClient.search(searchRequest);
}
 
Example 17
Source File: ElasticsearchRestClientInstrumentationIT.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testDocumentScenario() throws IOException, ExecutionException, InterruptedException {
    // Index a document
    IndexResponse ir = doIndex(new IndexRequest(INDEX, DOC_TYPE, DOC_ID).source(
        jsonBuilder()
            .startObject()
            .field(FOO, BAR)
            .endObject()
    ).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE));
    assertThat(ir.status().getStatus()).isEqualTo(201);


    List<Span> spans = reporter.getSpans();
    assertThat(spans).hasSize(1);
    validateSpanContent(spans.get(0), String.format("Elasticsearch: PUT /%s/%s/%s", INDEX, DOC_TYPE, DOC_ID), 201, "PUT");

    // Search the index
    reporter.reset();

    SearchRequest searchRequest = new SearchRequest(INDEX);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(QueryBuilders.termQuery(FOO, BAR));
    sourceBuilder.from(0);
    sourceBuilder.size(5);
    searchRequest.source(sourceBuilder);
    SearchResponse sr = doSearch(searchRequest);
    assertThat(sr.getHits().totalHits).isEqualTo(1L);
    assertThat(sr.getHits().getAt(0).getSourceAsMap().get(FOO)).isEqualTo(BAR);


    spans = reporter.getSpans();
    assertThat(spans).hasSize(1);
    Span searchSpan = spans.get(0);
    validateSpanContent(searchSpan, String.format("Elasticsearch: GET /%s/_search", INDEX), 200, "GET");
    validateDbContextContent(searchSpan, "{\"from\":0,\"size\":5,\"query\":{\"term\":{\"foo\":{\"value\":\"bar\",\"boost\":1.0}}}}");

    // Now update and re-search
    reporter.reset();

    Map<String, Object> jsonMap = new HashMap<>();
    jsonMap.put(FOO, BAZ);
    UpdateResponse ur = doUpdate(new UpdateRequest(INDEX, DOC_TYPE, DOC_ID).doc(jsonMap)
        .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE));
    assertThat(ur.status().getStatus()).isEqualTo(200);
    sr = doSearch(new SearchRequest(INDEX));
    assertThat(sr.getHits().getAt(0).getSourceAsMap().get(FOO)).isEqualTo(BAZ);


    spans = reporter.getSpans();
    assertThat(spans).hasSize(2);
    boolean updateSpanFound = false;
    for(Span span: spans) {
        if(span.getNameAsString().contains("_update")) {
            updateSpanFound = true;
            break;
        }
    }
    assertThat(updateSpanFound).isTrue();

    // Finally - delete the document
    reporter.reset();
    DeleteResponse dr = doDelete(new DeleteRequest(INDEX, DOC_TYPE, DOC_ID));
    validateSpanContent(spans.get(0), String.format("Elasticsearch: DELETE /%s/%s/%s", INDEX, DOC_TYPE, DOC_ID), 200, "DELETE");
}
 
Example 18
Source File: AbstractEs6_4ClientInstrumentationTest.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testDocumentScenario() throws Exception {
    // Index a document
    createDocument();

    List<Span> spans = reporter.getSpans();
    assertThat(spans).hasSize(1);
    validateSpanContent(spans.get(0), String.format("Elasticsearch: PUT /%s/%s/%s", INDEX, DOC_TYPE, DOC_ID), 201, "PUT");
    reporter.reset();

    // do search request
    SearchRequest searchRequest = new SearchRequest(INDEX);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(QueryBuilders.termQuery(FOO, BAR));
    sourceBuilder.from(0);
    sourceBuilder.size(5);
    searchRequest.source(sourceBuilder);

    SearchResponse response = doSearch(searchRequest);

    verifyTotalHits(response.getHits());
    spans = reporter.getSpans();
    assertThat(spans).hasSize(1);
    Span searchSpan = spans.get(0);
    validateSpanContent(searchSpan, String.format("Elasticsearch: POST /%s/_search", INDEX), 200, "POST");
    validateDbContextContent(searchSpan, "{\"from\":0,\"size\":5,\"query\":{\"term\":{\"foo\":{\"value\":\"bar\",\"boost\":1.0}}}}");
    reporter.reset();

    Map<String, Object> jsonMap = new HashMap<>();
    jsonMap.put(FOO, BAZ);
    UpdateRequest updateRequest = new UpdateRequest(INDEX, DOC_TYPE, DOC_ID).doc(jsonMap).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    UpdateResponse ur = doUpdate(updateRequest);
    assertThat(ur.status().getStatus()).isEqualTo(200);
    SearchResponse sr = doSearch(new SearchRequest(INDEX));
    assertThat(sr.getHits().getAt(0).getSourceAsMap().get(FOO)).isEqualTo(BAZ);

    spans = reporter.getSpans();
    assertThat(spans).hasSize(2);
    boolean updateSpanFound = false;
    for (Span span : spans) {
        if (span.getNameAsString().contains("_update")) {
            updateSpanFound = true;
            break;
        }
    }
    assertThat(updateSpanFound).isTrue();

    // Finally - delete the document
    reporter.reset();
    DeleteResponse dr = deleteDocument();
    assertThat(dr.status().getStatus()).isEqualTo(200);
    validateSpanContent(spans.get(0), String.format("Elasticsearch: DELETE /%s/%s/%s", INDEX, DOC_TYPE, DOC_ID), 200, "DELETE");
}
 
Example 19
Source File: EsUtil.java    From java-study with Apache License 2.0 4 votes vote down vote up
/**
 * @return boolean
 * @Author pancm
 * @Description 根据条件查询
 * @Date 2019/3/21
 * @Param []
 **/
public static List<Map<String, Object>> query(String index, String type, EsQueryCondition esQueryCondition, QueryBuilder... queryBuilders) throws IOException {
    if (index == null || type == null) {
        return null;
    }
    List<Map<String, Object>> list = new ArrayList<>();
    try {
        // 查询指定的索引库
        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.types(type);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        if (esQueryCondition != null) {
            Integer form = esQueryCondition.getIndex();
            Integer pagesize = esQueryCondition.getPagesize();
            if (form != null && form > 0 && pagesize != null && pagesize > 0) {
                form = (form - 1) * pagesize;
                pagesize = form + pagesize;
                // 设置起止和结束
                sourceBuilder.from(form);
                sourceBuilder.size(pagesize);
            }
            String routing = esQueryCondition.getRouting();
            if (routing != null && routing.length() > 0) {
                // 设置路由
                searchRequest.routing(routing);
            }

            // 设置索引库表达式
            searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());

            //设置排序
            String order = esQueryCondition.getOrder();
            if (order != null) {
                String[] orderField = esQueryCondition.getOrderField();
                SortOrder order2 = order.equals(SortOrder.DESC) ? SortOrder.DESC : SortOrder.ASC;
                //如果设置了排序字段则用排序的字段进行排序,否则就默认排序
                if (orderField != null) {
                    for (String field : orderField) {
                        sourceBuilder.sort(new FieldSortBuilder(field).order(order2));
                    }
                } else {
                    sourceBuilder.sort(new ScoreSortBuilder().order(order2));
                }
            }
            String[] includeFields = esQueryCondition.getIncludeFields();
            String[] excludeFields = esQueryCondition.getExcludeFields();
            if (includeFields != null && includeFields.length > 0 && excludeFields != null && excludeFields.length > 0) {
                sourceBuilder.fetchSource(includeFields, excludeFields);
            }
            sourceBuilder.fetchSource(esQueryCondition.isCloseSource());
        }
        //设置条件
        if (queryBuilders != null) {
            for (QueryBuilder queryBuilder : queryBuilders) {
                sourceBuilder.query(queryBuilder);
            }
        }

        searchRequest.source(sourceBuilder);
        // 同步查询
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        if(queryBuilders != null|| (esQueryCondition != null && esQueryCondition.isQueryData())){
            // 结果
            searchResponse.getHits().forEach(hit -> {
                Map<String, Object> map = hit.getSourceAsMap();
                list.add(map);
            });
        }

          if(esQueryCondition != null && esQueryCondition.isNeedTotal()){
              Map<String, Object> mapTotal = new HashMap<>();
              mapTotal.put("total", searchResponse.getHits().getTotalHits());
              list.add(mapTotal);
          }

    } finally {
        if (isAutoClose) {
            close();
        }
    }
    return list;
}
 
Example 20
Source File: EsHighLevelRestSearchTest.java    From java-study with Apache License 2.0 4 votes vote down vote up
/**
     * @return void
     * @Author pancm
     * @Description 普通查询
     * @Date 2019/9/12
     * @Param []
     **/
    private static void genSearch() throws IOException {
        String type = "_doc";
        String index = "test1";
        // 查询指定的索引库
        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.types(type);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 设置查询条件
        sourceBuilder.query(QueryBuilders.termQuery("uid", "1234"));
        // 设置起止和结束
        sourceBuilder.from(0);
        sourceBuilder.size(5);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        // 设置路由
//		searchRequest.routing("routing");
        // 设置索引库表达式
        searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());
        // 查询选择本地分片,默认是集群分片
        searchRequest.preference("_local");

        // 排序
        // 根据默认值进行降序排序
//		sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
        // 根据字段进行升序排序
//		sourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.ASC));

        // 关闭suorce查询
//		sourceBuilder.fetchSource(false);

        String[] includeFields = new String[]{"title", "user", "innerObject.*"};
        String[] excludeFields = new String[]{"_type"};
        // 包含或排除字段
//		sourceBuilder.fetchSource(includeFields, excludeFields);

        searchRequest.source(sourceBuilder);
        System.out.println("普通查询的DSL语句:"+sourceBuilder.toString());
        // 同步查询
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        // HTTP状态代码、执行时间或请求是否提前终止或超时
        RestStatus status = searchResponse.status();
        TimeValue took = searchResponse.getTook();
        Boolean terminatedEarly = searchResponse.isTerminatedEarly();
        boolean timedOut = searchResponse.isTimedOut();

        // 供关于受搜索影响的切分总数的统计信息,以及成功和失败的切分
        int totalShards = searchResponse.getTotalShards();
        int successfulShards = searchResponse.getSuccessfulShards();
        int failedShards = searchResponse.getFailedShards();
        // 失败的原因
        for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
            // failures should be handled here
        }
        // 结果
        searchResponse.getHits().forEach(hit -> {
            Map<String, Object> map = hit.getSourceAsMap();
            System.out.println("普通查询的结果:" + map);
        });
        System.out.println("\n=================\n");
    }