Java Code Examples for org.elasticsearch.action.search.SearchRequest#indices()

The following examples show how to use org.elasticsearch.action.search.SearchRequest#indices() . 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: ElasticsearchHighRestFactory.java    From database-transform-tool with Apache License 2.0 6 votes vote down vote up
public String selectMatchAll(String indexs,String types,String field,String value){
	try {
		if(StringUtil.isEmpty(indexs))indexs="_all";
		if(xclient==null){
			init();
		}
		SearchSourceBuilder search = new SearchSourceBuilder();
		if(!StringUtil.isEmpty(field)&&!StringUtil.isEmpty(value)&&!(field.matches(regex)||field.matches(value))){
			search.query(QueryBuilders.matchQuery(field, value));
		}
		search.aggregation(AggregationBuilders.terms("data").field(field+".keyword"));
		search.explain(false);
		SearchRequest request = new SearchRequest();
		request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
		request.source(search);
		request.indices(indexs.split(","));
		request.types(types.split(","));
		SearchResponse response = xclient.search(request);
		return response.toString();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example 4
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 5
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 6
Source File: EsHighLevelRestSearchTest.java    From java-study with Apache License 2.0 6 votes vote down vote up
/**
 * @return void
 * @Author pancm
 * @Description 或查询
 * @Date 2019/9/6
 * @Param []
 **/
private static void orSearch() throws IOException {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("test1");
    searchRequest.types("_doc");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    BoolQueryBuilder boolQueryBuilder2 = new BoolQueryBuilder();
    /**
     *  SELECT * FROM test1 where (uid = 1234 or uid =12345)  and phone = 12345678909
     * */
    boolQueryBuilder2.should(QueryBuilders.termQuery("uid", 1234));
    boolQueryBuilder2.should(QueryBuilders.termQuery("uid", 12345));
    boolQueryBuilder.must(boolQueryBuilder2);
    boolQueryBuilder.must(QueryBuilders.termQuery("phone", "12345678909"));
    searchSourceBuilder.query(boolQueryBuilder);
    System.out.println("或查询语句:" + searchSourceBuilder.toString());
    searchRequest.source(searchSourceBuilder);
    // 同步查询
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    searchResponse.getHits().forEach(documentFields -> {
        System.out.println("或查询结果:" + documentFields.getSourceAsMap());
    });
    System.out.println("\n=================\n");
}
 
Example 7
Source File: EsHighLevelRestSearchTest.java    From java-study with Apache License 2.0 6 votes vote down vote up
/**
 * @return void
 * @Author pancm
 * @Description 模糊查询
 * @Date 2019/9/12
 * @Param []
 **/
private static void likeSearch() throws IOException {
    String type = "_doc";
    String index = "test1";
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices(index);
    searchRequest.types(type);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    /**
     *  SELECT * FROM p_test where  message like '%xu%';
     * */
    boolQueryBuilder.must(QueryBuilders.wildcardQuery("message", "*xu*"));
    searchSourceBuilder.query(boolQueryBuilder);
    System.out.println("模糊查询语句:" + searchSourceBuilder.toString());
    searchRequest.source(searchSourceBuilder);
    // 同步查询
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    searchResponse.getHits().forEach(documentFields -> {
        System.out.println("模糊查询结果:" + documentFields.getSourceAsMap());
    });
    System.out.println("\n=================\n");
}
 
Example 8
Source File: ElasticsearchExtendHighRestFactory.java    From database-transform-tool with Apache License 2.0 6 votes vote down vote up
public String selectTermAll(String indexs,String types,String field,String value){
	try {
		if(StringUtil.isEmpty(indexs))indexs="_all";
		if(xclient==null){
			init();
		}
		SearchSourceBuilder search = new SearchSourceBuilder();
		if(!StringUtil.isEmpty(field)&&!StringUtil.isEmpty(value)&&!(field.matches(regex)||field.matches(value))){
			search.query(QueryBuilders.termQuery(field, value));
		}
		search.aggregation(AggregationBuilders.terms("data").field(field+".keyword"));
		search.explain(false);
		SearchRequest request = new SearchRequest();
		request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
		request.source(search);
		request.indices(indexs.split(","));
		request.types(types.split(","));
		SearchResponse response = xclient.search(request);
		return response.toString();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example 9
Source File: ElasticsearchHighRestFactory.java    From database-transform-tool with Apache License 2.0 6 votes vote down vote up
public String selectAll(String indexs,String types,String condition){
	try {
		if(StringUtil.isEmpty(indexs))indexs="_all";
		if(xclient==null){
			init();
		}
		SearchSourceBuilder search = new SearchSourceBuilder();
		search.query(QueryBuilders.queryStringQuery(condition)); 
		search.explain(false);
		SearchRequest request = new SearchRequest();
		request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
		request.source(search);
		request.indices(indexs.split(","));
		request.types(types.split(","));
		SearchResponse response = xclient.search(request);
		return response.toString();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example 10
Source File: TestSchema.java    From kafka-connect-elasticsearch-source with 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 11
Source File: ElasticSqlParseResult.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
private SearchRequest toRequest() {
    SearchRequest searchRequest = new SearchRequest();
    List<String> indexList = indices.parallelStream().map(StringManager::removeStringSymbol).collect(Collectors.toList());
    if (CollectionUtils.isNotEmpty(indexList)) {
        searchRequest.indices(indexList.toArray(new String[0]));
    } else {
        throw new ElasticSql2DslException("[syntax error] indices name must be set");
    }
    //这里不会修改from的值
    searchSourceBuilder.from(Math.max(from, 0));
    searchSourceBuilder.size(Math.max(size, 0));
    searchSourceBuilder.trackTotalHits(this.trackTotalHits);
    if (CollectionUtils.isNotEmpty(highlighter)) {
        HighlightBuilder highlightBuilder = HighlightBuilders.highlighter(highlighter);
        searchSourceBuilder.highlighter(highlightBuilder);
    }
    searchSourceBuilder.query(whereCondition);
    if (StringUtils.isNotBlank(distinctName)) {
        searchSourceBuilder.collapse(new CollapseBuilder(distinctName));
    }
    if (CollectionUtils.isNotEmpty(orderBy)) {
        for (SortBuilder sortBuilder : orderBy) {
            searchSourceBuilder.sort(sortBuilder);
        }
    }
    searchSourceBuilder.fetchSource(includeFields.toArray(new String[0]), excludeFields.toArray(new String[0]));
    if (CollectionUtils.isNotEmpty(routingBy)) {
        searchRequest.routing(routingBy.toArray(new String[0]));
    }
    if (CollectionUtils.isNotEmpty(groupBy)) {
        for (AggregationBuilder aggItem : groupBy) {
            searchSourceBuilder.aggregation(aggItem);
        }
    }
    return searchRequest.source(searchSourceBuilder);
}
 
Example 12
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 13
Source File: AuditlogTest.java    From deprecated-security-advanced-modules with 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 14
Source File: PermissionAwareSearchService.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Perform a search operation for the given site
 * @param siteId the site id
 * @param allowedPaths the paths that should be included in the results
 * @param request the search request
 * @param options the request options
 * @return the search response
 * @throws IOException if there is an error connecting to Elasticsearch
 */
public SearchResponse search(String siteId, List<String> allowedPaths, SearchRequest request,
                             RequestOptions options) throws IOException {

    request.indices(siteId + indexSuffix);

    //TODO: Prevent running the search without allowedPaths
    if(CollectionUtils.isNotEmpty(allowedPaths)) {
        updateFilters(request, allowedPaths);
    }

    return client.search(request, options);
}
 
Example 15
Source File: EsHeatmapRequestBuilder.java    From occurrence with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static SearchRequest buildRequest(OccurrenceHeatmapRequest request, String index) {
  // build request body
  SearchRequest esRequest = new SearchRequest();
  esRequest.indices(index);

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

  // size 0
  searchSourceBuilder.size(0);

  // add the geometry filter
  String[] coords = Iterables.toArray(Splitter.on(",").split(request.getGeometry()), String.class);

  double top = Double.valueOf(coords[3]);
  double left = Double.valueOf(coords[0]);
  double bottom = Double.valueOf(coords[1]);
  double right = Double.valueOf(coords[2]);

  BoolQueryBuilder bool = QueryBuilders.boolQuery();
  bool.filter().add(QueryBuilders.geoBoundingBoxQuery(OccurrenceEsField.COORDINATE_POINT.getFieldName())
    .setCorners(top, left, bottom, right));

  // add hasCoordinate to the filter and create query
  request.addHasCoordinateFilter(true);
  EsSearchRequestBuilder.buildQueryNode(request).ifPresent(bool.filter()::add);

  searchSourceBuilder.query(bool);

  // add aggs
  searchSourceBuilder.aggregation(buildAggs(request));

  return esRequest;
}
 
Example 16
Source File: IndexShard.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static SearchRequest generateLocalSearchRequest(String source, String indexName, String type) {
    LocalRestRequest localRestRequest = new LocalRestRequest("/_search", Method.POST);
    localRestRequest.setContent(source, null);
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices(Strings.splitStringByCommaToArray(indexName));
    // get the content, and put it in the body
    // add content/source as template if template flag is set
    searchRequest.source(localRestRequest.content());
    searchRequest.searchType(SearchType.QUERY_AND_FETCH);
    searchRequest.extraSource(RestSearchAction.parseSearchSource(localRestRequest));
    searchRequest.types(Strings.splitStringByCommaToArray(type));
    searchRequest.putHeader("search_source", "reindex");
    return searchRequest;
}
 
Example 17
Source File: SiteAwareElasticsearchService.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void updateIndex(final SearchRequest request) {
    SiteContext siteContext = SiteContext.getCurrent();
    if (siteContext != null) {
        request.indices(String.format(indexIdFormat, siteContext.getSiteName()));
    } else {
        throw new IllegalStateException("Current site context not found");
    }
}
 
Example 18
Source File: Test.java    From dht-spider with MIT License 5 votes vote down vote up
public static void search(Map<String, Object> m) throws Exception{

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(5);
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        searchSourceBuilder.query(QueryBuilders.termQuery("message", "视频"));

        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("torrent");
        searchRequest.source(searchSourceBuilder);


        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        RestStatus status = searchResponse.status();
        TimeValue took = searchResponse.getTook();
        Boolean terminatedEarly = searchResponse.isTerminatedEarly();
        boolean timedOut = searchResponse.isTimedOut();
        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.print(sourceAsMap+"===");
        }


    }
 
Example 19
Source File: ElasticSearchService.java    From SpringMVC-Project with MIT License 4 votes vote down vote up
/**
     * 查询文章数据
     */
    public List<Article> queryArticle(String keyword, String category) throws IOException {
        BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //数据偏移量,下标从0开始,取前5个
        sourceBuilder.from(0);
        sourceBuilder.size(5);
        //指定返回结果的字段,第一个参数为包含的字段,第二个参数为排除的字段
        sourceBuilder.fetchSource(new String[]{"title"}, new String[]{});

        //分词匹配查询

        //且条件组合,必须满足每一项,计算相关性得分
        boolBuilder.must(QueryBuilders.matchQuery("title", keyword));
//        boolBuilder.must(QueryBuilders.matchQuery("content", keyword));

        //且条件组合,必须满足每一项,不计算相关性得分
//        boolBuilder.filter(QueryBuilders.matchQuery("title", keyword));
//        boolBuilder.filter(QueryBuilders.matchQuery("content", keyword));

        //或条件组合,满足任意一项即可
//        boolBuilder.should(QueryBuilders.matchQuery("title", keyword));
//        boolBuilder.should(QueryBuilders.matchQuery("content", keyword));

        //非条件组合,不满足每一项
//        boolBuilder.mustNot(QueryBuilders.matchQuery("title", keyword));
//        boolBuilder.mustNot(QueryBuilders.matchQuery("content", keyword));

        if (StringUtils.isNotBlank(category)) {
            //不分词匹配查询
            boolBuilder.must(QueryBuilders.matchPhraseQuery("category", category));

            //只匹配分词,如字段值为"Quick Foxes",则只有"Quick"和"Foxes"可以匹配到值,"Quick Foxes"则不行
//            boolBuilder.must(QueryBuilders.termQuery("category", category));
        }

        //范围条件
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("publishTime");
        rangeQueryBuilder.gte("2018-12-31T00:00:00Z");
        rangeQueryBuilder.lte("2018-12-31T23:59:59Z");
        boolBuilder.must(rangeQueryBuilder);

        sourceBuilder.query(boolBuilder);
        SearchRequest searchRequest = new SearchRequest();
        //指定索引名称,如果未指定,则会查询所有
        searchRequest.indices(BLOG_INDEX);
        searchRequest.types(ARTICLE_TYPE);
        searchRequest.source(sourceBuilder);

        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        logger.info("查询数据 | {} | {} | {}", keyword, category, JSON.toJSONString(searchResponse));

        List<Article> articleList = Lists.newArrayList();
        SearchHit[] searchHits = searchResponse.getHits().getHits();
        for (SearchHit searchHit : searchHits) {
            Article article = new Article();
            article.setId(Long.valueOf(searchHit.getId()));
            article.setTitle((String) searchHit.getSourceAsMap().get("title"));

            articleList.add(article);
        }

        return articleList;
    }
 
Example 20
Source File: IgnoreAuditUsersTest.java    From deprecated-security-advanced-modules with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void initSearchRequest() {
    sr = new SearchRequest();
    sr.indices("index1", "logstash*");
    sr.types("mytype", "logs");
}