Java Code Examples for org.elasticsearch.action.search.SearchRequest#source()
The following examples show how to use
org.elasticsearch.action.search.SearchRequest#source() .
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 |
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: RestHighLevelClientCase.java From skywalking with Apache License 2.0 | 6 votes |
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 3
Source File: PercentilesAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("cmd", "weather_hourforcast"); PercentilesAggregationBuilder aggregationBuilder = AggregationBuilders.percentiles("utm").field("utm").keyed(false); // PercentilesAggregationBuilder aggregationBuilder = AggregationBuilders.percentiles("utm").field("utm").percentiles(95,99,99.9).keyed(false);//自定义百分区间 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 4
Source File: InternalEsClient.java From io with Apache License 2.0 | 6 votes |
/** * 非同期でドキュメントを検索. * @param index インデックス名 * @param type タイプ名 * @param routingId routingId * @param query クエリ情報 * @return 非同期応答 */ public ActionFuture<SearchResponse> asyncSearch( String index, String type, String routingId, Map<String, Object> query) { SearchRequest req = new SearchRequest(index).types(type).searchType(SearchType.DEFAULT); if (query != null) { req.source(query); } if (routingFlag) { req = req.routing(routingId); } ActionFuture<SearchResponse> ret = esTransportClient.search(req); this.fireEvent(Event.afterRequest, index, type, null, JSONObject.toJSONString(query), "Search"); return ret; }
Example 5
Source File: ElasticSearchDataReader.java From pacbot with Apache License 2.0 | 6 votes |
/** * @param indexNameFromRuleParam * @param autoFixPlanType * @param resourceId * @return * @throws IOException */ public String searchDocument(String indexNameFromRuleParam, String autoFixPlanType, String resourceId) throws IOException { SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.termQuery(ESUtils.convertAttributetoKeyword("resourceId"), resourceId)); SearchRequest searchRequest = new SearchRequest(indexNameFromRuleParam); searchRequest.types(autoFixPlanType); searchRequest.source(sourceBuilder); logger.debug("searching auto fix plan with query " ,searchRequest.toString()); SearchResponse response = client.search(searchRequest); SearchHits hits = response.getHits(); if(RestStatus.OK==response.status() && hits.totalHits>0){ return hits.getAt(0).getSourceAsString(); }else{ throw new IOException(String.format("no plan found for resource %s",resourceId)); } }
Example 6
Source File: EsUtil.java From bookmark with MIT License | 6 votes |
/** * Description: 搜索 * * @param index index * @param builder 查询参数 * @param c 结果类对象 * @return java.util.ArrayList * @author fanxb * @date 2019/7/25 13:46 */ public <T> List<T> search(String index, SearchSourceBuilder builder, Class<T> c) { if (!status) { return null; } SearchRequest request = new SearchRequest(index); request.source(builder); try { SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHit[] hits = response.getHits().getHits(); List<T> res = new ArrayList<>(hits.length); for (SearchHit hit : hits) { res.add(JSON.parseObject(hit.getSourceAsString(), c)); } return res; } catch (Exception e) { throw new EsException(e); } }
Example 7
Source File: OccurrenceSearchEsImpl.java From occurrence with Apache License 2.0 | 6 votes |
private <T> T searchByKey(Long key, Function<SearchHit, T> mapper) { //This should be changed to use GetRequest once ElasticSearch stores id correctly SearchRequest searchRequest = new SearchRequest(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.size(1); searchRequest.indices(esIndex); searchSourceBuilder.query(QueryBuilders.termQuery(OccurrenceEsField.GBIF_ID.getFieldName(), key)); searchRequest.source(searchSourceBuilder); try { SearchHits hits = esClient.search(searchRequest, HEADERS.get()).getHits(); if (hits != null && hits.totalHits > 0) { return mapper.apply(hits.getAt(0)); } return null; } catch (IOException ex) { throw new SearchException(ex); } }
Example 8
Source File: ElasticSearchUtil.java From ranger with Apache License 2.0 | 6 votes |
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 9
Source File: MaxMinAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ // MaxAggregationBuilder aggregationBuilder = AggregationBuilders.max("utm").field("utm"); MinAggregationBuilder aggregationBuilder = AggregationBuilders.min("utm").field("utm"); SearchRequest searchRequest = new SearchRequest("serverlog_20180715");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 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(((ParsedMax)(each.getValue())).getValue()); System.out.println(((ParsedMin)(each.getValue())).getValue()); } }finally{ HighLevelClient.close(); } }
Example 10
Source File: ElasticSearcher.java From jpress with GNU Lesser General Public License v3.0 | 5 votes |
@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 11
Source File: ElasticSearchRestDAOV5.java From conductor with Apache License 2.0 | 5 votes |
/** * 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 12
Source File: AbstractEs6_4ClientInstrumentationTest.java From apm-agent-java with Apache License 2.0 | 5 votes |
@Test public void testMultiSearchRequest_validateSpanContentAndDbContext() throws InterruptedException, ExecutionException, IOException { createDocument(); reporter.reset(); MultiSearchRequest multiSearchRequest = new MultiSearchRequest(); SearchRequest firstSearchRequest = new SearchRequest(INDEX); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchQuery(FOO, BAR)); firstSearchRequest.source(searchSourceBuilder); multiSearchRequest.add(firstSearchRequest); MultiSearchResponse response = doMultiSearch(multiSearchRequest); List<Span> spans = reporter.getSpans(); assertThat(spans).hasSize(1); Span span = spans.get(0); validateSpanContent(span, "Elasticsearch: POST /_msearch", 200, "POST"); verifyMultiSearchSpanContent(span); deleteDocument(); }
Example 13
Source File: AgentSalesQueryAdapter.java From micronaut-microservices-poc with Apache License 2.0 | 5 votes |
@Override SearchRequest buildQuery() { SearchRequest searchRequest = new SearchRequest("policy_stats") .types("policy_type"); BoolQueryBuilder filterBuilder = QueryBuilders.boolQuery(); if (query.getFilterByAgentLogin() != null) { filterBuilder.must(QueryBuilders.termQuery("agentLogin.keyword", query.getFilterByAgentLogin())); } 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); TermsAggregationBuilder sumAggBuilder = AggregationBuilders .terms("count_by_agent") .field("agentLogin.keyword") .subAggregation(AggregationBuilders.sum("total_premium").field("totalPremium")); aggBuilder.subAggregation(sumAggBuilder); SearchSourceBuilder srcBuilder = new SearchSourceBuilder() .aggregation(aggBuilder) .size(0); searchRequest.source(srcBuilder); return searchRequest; }
Example 14
Source File: ElasticSearcher.java From jpress with GNU Lesser General Public License v3.0 | 5 votes |
@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 15
Source File: IndexShard.java From Elasticsearch with Apache License 2.0 | 5 votes |
public long getShardDocNumByType(String type) { String query = String.format(DOCNUM_QUERY_PATTERN, 1); SearchRequest searchRequest = generateLocalSearchRequest(query, shardId.getIndex(), type); ShardSearchLocalRequest request = new ShardSearchLocalRequest(shardId, 1, SearchType.QUERY_AND_FETCH, searchRequest.source(), searchRequest.types(), true); request.putHeader("search_source", "reindex"); QueryFetchSearchResult result = shardSearchService.executeFetchPhase(request); return result.queryResult().topDocs().totalHits; }
Example 16
Source File: HistogramAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ HistogramAggregationBuilder histogramAggregationBuilder = AggregationBuilders.histogram("utm_histogram"); histogramAggregationBuilder.field("utm");//设置直方图针对的字段 histogramAggregationBuilder.interval(1000);//直方图每个分组对应的范围 histogramAggregationBuilder.order(BucketOrder.aggregation("_key",true));//分组key的排序 // histogramAggregationBuilder.minDocCount(0);//对于每个分组最少具有多少条数据,少于这个设置,则该分组不显示 // histogramAggregationBuilder.extendedBounds(0,8000);//设置分组区间的下线和上线,只有当min_doc_count为0时有效 TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("top_cmd"); aggregationBuilder.field("cmd"); aggregationBuilder.size(3); aggregationBuilder.subAggregation(histogramAggregationBuilder); SearchRequest searchRequest = new SearchRequest("serverlog_20180710");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.aggregation(aggregationBuilder); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); }finally{ HighLevelClient.close(); } }
Example 17
Source File: BoolApiMain.java From elasticsearch-pool with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ BoolQueryBuilder matchQueryBuilder = QueryBuilders.boolQuery(); matchQueryBuilder.must(QueryBuilders.termQuery("cpname","llbxinterface")); matchQueryBuilder.mustNot(QueryBuilders.termQuery("status","fail")); matchQueryBuilder.should(QueryBuilders.rangeQuery("utm").from(0,true).to(100,true)); matchQueryBuilder.minimumShouldMatch(1); // matchQueryBuilder.filter(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(matchQueryBuilder); searchSourceBuilder.from(0); searchSourceBuilder.size(3); 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 18
Source File: SearchApiMain.java From elasticsearch-pool with Apache License 2.0 | 4 votes |
public static void searchApi() throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try { SearchRequest searchRequest = new SearchRequest("jingma2_test");//限定index searchRequest.types("testlog");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); /*查询所有记录*/ // searchSourceBuilder.query(QueryBuilders.matchAllQuery()); /*根据匹配查询*/ QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "风雷"); /*设置中文分词器*/ // ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik"); // ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik_max_word"); // ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik_smart"); // ((MatchQueryBuilder) matchQueryBuilder).analyzer("standard"); searchSourceBuilder.query(matchQueryBuilder); /*限定查询条件和查询条数*/ // searchSourceBuilder.query(QueryBuilders.termQuery("name", "风雷")); searchSourceBuilder.from(0); searchSourceBuilder.size(5); // searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); /*限定查询结果排序*/ // searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC)); // searchSourceBuilder.sort(new FieldSortBuilder("age").order(SortOrder.ASC)); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); SearchHits hits = searchResponse.getHits(); long totalHits = hits.getTotalHits(); float maxScore = hits.getMaxScore(); SearchHit[] searchHits = hits.getHits(); for (SearchHit hit : searchHits) { String index = hit.getIndex(); String type = hit.getType(); String id = hit.getId(); float score = hit.getScore(); String sourceAsString = hit.getSourceAsString(); System.out.println(sourceAsString); // Map<String, Object> sourceAsMap = hit.getSourceAsMap(); } }finally { HighLevelClient.close(); } }
Example 19
Source File: AbstractEs6_4ClientInstrumentationTest.java From apm-agent-java with Apache License 2.0 | 4 votes |
@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 20
Source File: ElasticSearch7Client.java From skywalking with Apache License 2.0 | 4 votes |
public SearchResponse search(String indexName, SearchSourceBuilder searchSourceBuilder) throws IOException { indexName = formatIndexName(indexName); SearchRequest searchRequest = new SearchRequest(indexName); searchRequest.source(searchSourceBuilder); return client.search(searchRequest, RequestOptions.DEFAULT); }