Java Code Examples for org.elasticsearch.action.search.SearchRequest

The following examples show how to use org.elasticsearch.action.search.SearchRequest. These examples are extracted from open source projects. 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 Project: dht-spider   Author: mistletoe9527   File: ESClient.java    License: MIT License 7 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 Project: deprecated-security-advanced-modules   Author: opendistro-for-elasticsearch   File: AuditlogTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSearchRequest() {

    SearchRequest sr = new SearchRequest();
    sr.indices("index1","logstash*");
    sr.types("mytype","logs");

    Settings settings = Settings.builder()
            .put("opendistro_security.audit.type", TestAuditlogImpl.class.getName())
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_TRANSPORT_CATEGORIES, "NONE")
            .put("opendistro_security.audit.threadpool.size", 0)
            .build();
    AbstractAuditLog al = new AuditLogImpl(settings, null,  null, AbstractSecurityUnitTest.MOCK_POOL, null, cs);
    TestAuditlogImpl.clear();
    al.logGrantedPrivileges("indices:data/read/search", sr, null);
    Assert.assertEquals(1, TestAuditlogImpl.messages.size());
}
 
Example #3
Source Project: elasticsearch-pool   Author: andamajing   File: PercentilesAggregationMain.java    License: 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");
            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 Project: james-project   Author: apache   File: ElasticSearchIndexerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void deleteMessage() {
    DocumentId documentId = DocumentId.fromString("1:2");
    String content = "{\"message\": \"trying out Elasticsearch\"}";

    testee.index(documentId, content, useDocumentId(documentId)).block();
    elasticSearch.awaitForElasticSearch();

    testee.delete(ImmutableList.of(documentId), useDocumentId(documentId)).block();
    elasticSearch.awaitForElasticSearch();
    
    SearchResponse searchResponse = client.search(
        new SearchRequest(INDEX_NAME.getValue())
            .source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())),
        RequestOptions.DEFAULT)
        .block();
    assertThat(searchResponse.getHits().getTotalHits()).isEqualTo(0);
}
 
Example #5
Source Project: adaptive-alerting   Author: ExpediaDotCom   File: MetricProfileRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Boolean profilingExists(Map<String, String> tags) {
    try {
        List<BytesReference> refList = new ArrayList<>();
        XContentBuilder xContent = XContentFactory.jsonBuilder();
        xContent.map(tags);
        refList.add(BytesReference.bytes(xContent));

        PercolateQueryBuilder percolateQuery = new PercolateQueryBuilder(PercolatorMetricProfiling.QUERY_KEYWORD,
                refList, XContentType.JSON);

        val boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.filter(percolateQuery);
        val searchSourceBuilder = elasticsearchUtil.getSourceBuilder(boolQueryBuilder);
        searchSourceBuilder.timeout(new TimeValue(elasticSearchProperties.getConfig().getConnectionTimeout()));
        searchSourceBuilder.size(500);

        val searchRequest = new SearchRequest().source(searchSourceBuilder).indices(METRIC_PROFILING_INDEX);
        val searchResponse = legacyElasticSearchClient.search(searchRequest, RequestOptions.DEFAULT);

        return searchResponse.getHits().getHits().length > 0;
    } catch (IOException e) {
        log.error("Error ES lookup", e);
        throw new RuntimeException(e);
    }
}
 
Example #6
Source Project: adaptive-alerting   Author: ExpediaDotCom   File: LegacyDetectorRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
private List<DetectorDocument> getDetectorsFromElasticSearch(SearchRequest searchRequest) {
    SearchResponse response;
    try {
        response = legacyElasticSearchClient.search(searchRequest, RequestOptions.DEFAULT);
    } catch (IOException e) {
        log.error("Error ES lookup", e);
        throw new RuntimeException(e);
    }

    SearchHit[] hits = response.getHits().getHits();
    List<DetectorDocument> detectors = new ArrayList<>();
    for (val hit : hits) {

        val detector = (DetectorDocument) objectMapperUtil.convertToObject(hit.getSourceAsString(), new TypeReference<DetectorDocument>() {
        });
        val newElasticsearchDetector = getElasticSearchDetector(detector);
        detectors.add(newElasticsearchDetector);
    }
    return detectors;
}
 
Example #7
Source Project: siren-join   Author: sirensolutions   File: CardinalityEstimationTask.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
protected void executeCardinalityRequest(final NodeTaskContext context, final NodeTaskReporter reporter) {
  logger.debug("Executing async cardinality action");
  final SearchRequest cardinalityRequest = this.getCardinalityRequest(context.getNode(), context.getVisitor().getParentRequest());
  context.getClient().execute(SearchAction.INSTANCE, cardinalityRequest, new ActionListener<SearchResponse>() {

    @Override
    public void onResponse(SearchResponse searchResponse) {
      Cardinality c = searchResponse.getAggregations().get(context.getNode().getLookupPath());
      context.getNode().setCardinality(c.getValue());
      reporter.success(context);
    }

    @Override
    public void onFailure(Throwable e) {
      reporter.failure(e);
    }

  });
}
 
Example #8
Source Project: james-project   Author: apache   File: ESReporterTest.java    License: Apache License 2.0 6 votes vote down vote up
private boolean checkMetricRecordedInElasticSearch() {
    try (ReactorElasticSearchClient client = elasticSearchExtension.getDockerES().clientProvider().get()) {
        SearchRequest searchRequest = new SearchRequest()
            .source(new SearchSourceBuilder()
                .query(QueryBuilders.matchAllQuery()));
        return !Arrays.stream(client
                .search(searchRequest)
                .block()
                .getHits()
                .getHits())
            .filter(searchHit -> searchHit.getIndex().startsWith(TestDockerESMetricReporterModule.METRICS_INDEX))
            .collect(Collectors.toList())
            .isEmpty();
    } catch (Exception e) {
        return false;
    }
}
 
Example #9
Source Project: occurrence   Author: gbif   File: EsSearchRequestBuilder.java    License: 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 #10
Source Project: spring-content   Author: paulcwarren   File: SearchableImpl.java    License: 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 #11
Source Project: anomaly-detection   Author: opendistro-for-elasticsearch   File: AnomalyDetectorProfileRunner.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * We expect three kinds of states:
 *  -Disabled: if get ad job api says the job is disabled;
 *  -Init: if anomaly score after the last update time of the detector is larger than 0
 *  -Running: if neither of the above applies and no exceptions.
 * @param detectorId detector id
 * @param enabledTime the time when AD job is enabled in milliseconds
 * @param listener listener to process the returned state or exception
 * @param enabled whether the detector job is enabled or not
 */
private void profileState(
    String detectorId,
    long enabledTime,
    MultiResponsesDelegateActionListener<DetectorProfile> listener,
    boolean enabled
) {
    if (enabled) {
        SearchRequest searchLatestResult = createInittedEverRequest(detectorId, enabledTime);
        client.search(searchLatestResult, onInittedEver(listener, detectorId, enabledTime));
    } else {
        DetectorProfile profile = new DetectorProfile();
        profile.setState(DetectorState.DISABLED);
        listener.onResponse(profile);
    }
}
 
Example #12
Source Project: anomaly-detection   Author: opendistro-for-elasticsearch   File: AnomalyDetectorProfileRunner.java    License: 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 #13
Source Project: elasticsearch-carrot2   Author: carrot2   File: ClusteringAction.java    License: Apache License 2.0 6 votes vote down vote up
public ClusteringActionRequest(StreamInput in) throws IOException {
   SearchRequest searchRequest = new SearchRequest(in);

   this.searchRequest = searchRequest;
   this.queryHint = in.readOptionalString();
   this.algorithm = in.readOptionalString();
   this.maxHits = in.readInt();
   this.createUngroupedDocumentsCluster = in.readBoolean();
   this.defaultLanguage = in.readString();

   int count = in.readVInt();
   while (count-- > 0) {
      FieldMappingSpec spec = new FieldMappingSpec(in);
      fieldMapping.add(spec);
   }

   boolean hasAttributes = in.readBoolean();
   if (hasAttributes) {
      attributes = in.readMap();
   }
}
 
Example #14
Source Project: conductor   Author: Netflix   File: ElasticSearchRestDAOV6.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<EventExecution> getEventExecutions(String event) {
    try {
        BoolQueryBuilder query = boolQueryBuilder("event='" + event + "'", "*");

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

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

        SearchResponse response = elasticSearchClient.search(searchRequest);

        return mapEventExecutionsResponse(response);
    } catch (Exception e) {
        logger.error("Failed to get executions for event: {}", event, e);
    }
    return null;
}
 
Example #15
Source Project: heroic   Author: spotify   File: MetadataBackendKV.java    License: 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 #16
Source Project: Elasticsearch   Author: baidu   File: SearchServiceTransportAction.java    License: Apache License 2.0 6 votes vote down vote up
public void sendFreeContext(DiscoveryNode node, final long contextId, SearchRequest request) {
    transportService.sendRequest(node, FREE_CONTEXT_ACTION_NAME, new SearchFreeContextRequest(request, contextId), new ActionListenerResponseHandler<SearchFreeContextResponse>(new ActionListener<SearchFreeContextResponse>() {
        @Override
        public void onResponse(SearchFreeContextResponse response) {
            // no need to respond if it was freed or not
        }

        @Override
        public void onFailure(Throwable e) {

        }
    }) {
        @Override
        public SearchFreeContextResponse newInstance() {
            return new SearchFreeContextResponse();
        }
    });
}
 
Example #17
Source Project: anomaly-detection   Author: opendistro-for-elasticsearch   File: ADStateManagerTests.java    License: Apache License 2.0 6 votes vote down vote up
public void testHasRunningQuery() throws IOException {
    stateManager = new ADStateManager(
        client,
        xContentRegistry(),
        modelManager,
        settings,
        new ClientUtil(settings, client, throttler, context),
        clock,
        duration
    );

    AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableMap.of(), null);
    SearchRequest dummySearchRequest = new SearchRequest();
    assertFalse(stateManager.hasRunningQuery(detector));
    throttler.insertFilteredQuery(detector.getDetectorId(), dummySearchRequest);
    assertTrue(stateManager.hasRunningQuery(detector));
}
 
Example #18
Source Project: Elasticsearch   Author: baidu   File: CountRequest.java    License: Apache License 2.0 6 votes vote down vote up
public SearchRequest toSearchRequest() {
    SearchRequest searchRequest = new SearchRequest(indices());
    searchRequest.indicesOptions(indicesOptions());
    searchRequest.types(types());
    searchRequest.routing(routing());
    searchRequest.preference(preference());
    searchRequest.source(source());
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.size(0);
    if (minScore() != DEFAULT_MIN_SCORE) {
        searchSourceBuilder.minScore(minScore());
    }
    if (terminateAfter() != DEFAULT_TERMINATE_AFTER) {
        searchSourceBuilder.terminateAfter(terminateAfter());
    }
    searchRequest.extraSource(searchSourceBuilder);
    return searchRequest;
}
 
Example #19
Source Project: staccato   Author: planetlabs   File: ElasticsearchCatalogService.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Searches for a collection of items given a api query built from path variables that were traversed in a
 * subcatalog.
 * @param collectionId The id of the collection
 * @param pathVariables A map of fieldnames and their corresponding values that should be used to build the api
 *                      query
 * @return The collection of item results wrapped in a Mono
 */
@Override
public Mono<ItemCollection> getItems(String collectionId, Map<String, String> pathVariables) {
    StringBuilder sb = new StringBuilder();
    Iterator<Map.Entry<String, String>> it = pathVariables.entrySet().iterator();

    while (it.hasNext()) {
        Map.Entry entry = it.next();
        sb.append("properties." + entry.getKey() + "=" + entry.getValue());
        if (it.hasNext()) {
            sb.append(" AND ");
        }
    }
    com.planet.staccato.dto.api.SearchRequest searchRequest = new com.planet.staccato.dto.api.SearchRequest();
    String query = sb.toString().isBlank() ? null : sb.toString();
    searchRequest.setQuery(query);
    searchRequest.setLimit(10);
    searchRequest.setCollections(new String[]{collectionId});
    return searchService.getItemCollection(searchRequest);
}
 
Example #20
Source Project: ranger   Author: apache   File: ElasticSearchUtil.java    License: 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 #21
Source Project: tutorials   Author: eugenp   File: ElasticSearchQueryManualTest.java    License: 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 #22
Source Project: datashare   Author: ICIJ   File: ElasticsearchSpewerTest.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void test_embedded_document() throws Exception {
    Path path = get(getClass().getResource("/docs/embedded_doc.eml").getPath());
    final TikaDocument document = new Extractor().extract(path);

    spewer.write(document);

    GetResponse documentFields = es.client.get(new GetRequest(TEST_INDEX, "doc", document.getId()));
    assertTrue(documentFields.isExists());

    SearchRequest searchRequest = new SearchRequest();
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.multiMatchQuery("simple.tiff", "content"));
    searchRequest.source(searchSourceBuilder);
    SearchResponse response = es.client.search(searchRequest);
    assertThat(response.getHits().totalHits).isGreaterThan(0);
    //assertThat(response.getHits().getAt(0).getId()).endsWith("embedded.pdf");

    verify(publisher, times(2)).publish(eq(Channel.NLP), any(Message.class));
}
 
Example #23
Source Project: demo-project   Author: FleyX   File: EsUtil.java    License: MIT License 6 votes vote down vote up
/**
 * 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) {
    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 RuntimeException(e);
    }
}
 
Example #24
Source Project: elasticsearch-pool   Author: andamajing   File: MatchParseApiMain.java    License: 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 #25
Source Project: elasticsearch-pool   Author: andamajing   File: ExistApiMain.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        ExistsQueryBuilder matchQueryBuilder = QueryBuilders.existsQuery("retcode");//查询某个字段存在的记录

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

        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 #26
Source Project: io   Author: fujitsu-pio   File: InternalEsClient.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 非同期でドキュメントを検索.
 * @param index インデックス名
 * @param routingId routingId
 * @param query クエリ情報
 * @return 非同期応答
 */
public ActionFuture<SearchResponse> asyncSearch(
        String index,
        String routingId,
        QueryBuilder query) {
    SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT);

    String queryString = "null";
    if (query != null) {
        req.source(new SearchSourceBuilder().query(query));
        queryString = query.buildAsBytes().toUtf8();
    }
    if (routingFlag) {
        req = req.routing(routingId);
    }
    ActionFuture<SearchResponse> ret = esTransportClient.search(req);
    this.fireEvent(Event.afterRequest, index, null, null, queryString, "Search");
    return ret;
}
 
Example #27
Source Project: kafka-connect-elasticsearch-source   Author: DarioBalinzo   File: TestSchema.java    License: Apache License 2.0 6 votes vote down vote up
public void testSearch() throws Exception {
    SearchRequest searchRequest = new SearchRequest();
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchAllQuery());
    searchRequest.source(searchSourceBuilder);
    searchRequest.indices("metricbeat-6.2.4-2018.05.20");
    SearchResponse searchResponse = es.getClient().search(searchRequest);
    SearchHits hits = searchResponse.getHits();
    SearchHit[] searchHits = hits.getHits();
    for (SearchHit hit : searchHits) {
        // do something with the SearchHit
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        System.out.println(sourceAsMap);
        Schema schema = SchemaConverter.convertElasticMapping2AvroSchema(sourceAsMap, "test");
        schema.toString();
        Struct struct = StructConverter.convertElasticDocument2AvroStruct(sourceAsMap,schema);
        struct.toString();
    }

}
 
Example #28
Source Project: conductor   Author: Netflix   File: ElasticSearchRestDAOV5.java    License: 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 #29
Source Project: spring-boot-demo   Author: xkcoding   File: BaseElasticsearchService.java    License: MIT License 5 votes vote down vote up
/**
 * search all
 *
 * @param index elasticsearch index name
 * @return {@link SearchResponse}
 * @author fxbin
 */
protected SearchResponse search(String index) {
    SearchRequest searchRequest = new SearchRequest(index);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchAllQuery());
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse = null;
    try {
        searchResponse = client.search(searchRequest, COMMON_OPTIONS);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return searchResponse;
}
 
Example #30
Source Project: deprecated-security-advanced-modules   Author: opendistro-for-elasticsearch   File: BasicAuditlogTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTaskId() throws Exception {

    Settings additionalSettings = Settings.builder()
            .put("opendistro_security.audit.type", TestAuditlogImpl.class.getName())
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_TRANSPORT_CATEGORIES, "NONE")
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_REST_CATEGORIES, "NONE")
            .put("opendistro_security.audit.threadpool.size", 0)
            .build();

    setup(additionalSettings);
    setupStarfleetIndex();
    TestAuditlogImpl.clear();

    try (TransportClient tc = getUserTransportClient(clusterInfo, "spock-keystore.jks", Settings.EMPTY)) {
        StoredContext ctx = tc.threadPool().getThreadContext().stashContext();
        try {
            Header header = encodeBasicHeader("admin", "admin");
            tc.threadPool().getThreadContext().putHeader(header.getName(), header.getValue());
            SearchResponse res = tc.search(new SearchRequest()).actionGet();
            System.out.println(res);
        } finally {
            ctx.close();
        }
    }

    Thread.sleep(1500);
    System.out.println(TestAuditlogImpl.sb.toString());
    Assert.assertTrue(String.valueOf(TestAuditlogImpl.messages.size()), TestAuditlogImpl.messages.size() >= 2);
    Assert.assertTrue(TestAuditlogImpl.sb.toString().contains("GRANTED_PRIVILEGES"));
    Assert.assertTrue(TestAuditlogImpl.sb.toString().contains("AUTHENTICATED"));
    Assert.assertTrue(TestAuditlogImpl.sb.toString().contains("indices:data/read/search"));
    Assert.assertTrue(TestAuditlogImpl.sb.toString().contains("TRANSPORT"));
    Assert.assertTrue(TestAuditlogImpl.sb.toString().contains("\"audit_request_effective_user\" : \"admin\""));
    Assert.assertFalse(TestAuditlogImpl.sb.toString().contains("REST"));
    Assert.assertFalse(TestAuditlogImpl.sb.toString().toLowerCase().contains("authorization"));
    Assert.assertEquals(TestAuditlogImpl.messages.get(0).getAsMap().get(AuditMessage.TASK_ID),
    TestAuditlogImpl.messages.get(1).getAsMap().get(AuditMessage.TASK_ID));
    Assert.assertTrue(validateMsgs(TestAuditlogImpl.messages));
}