Search in sources :

Example 56 with TimeValue

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.unit.TimeValue in project pancm_project by xuwujing.

the class EsHighLevelRestSearchTest method genSearch.

/**
 * @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");
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) RestStatus(org.elasticsearch.rest.RestStatus) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 57 with TimeValue

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.unit.TimeValue in project pancm_project by xuwujing.

the class EsHighLevelRestTest1 method updateByQuery.

/**
 * 根据查询条件更新
 *
 * @throws IOException
 */
private static void updateByQuery() throws IOException {
    String type = "_doc";
    String index = "test1";
    // 
    UpdateByQueryRequest request = new UpdateByQueryRequest(index, type);
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    boolQueryBuilder.must(QueryBuilders.termQuery("user", "pancm"));
    // Script script = new Script(ScriptType.INLINE, SCRIPT_NAME, id, map);
    // boolQueryBuilder.must(QueryBuilders.scriptQuery(script));
    // boolQueryBuilder.must(QueryBuilders.scriptQuery(script));
    // // 设置查询条件
    // request.setQuery(new TermQueryBuilder("user", "pancm"));
    // 设置复制文档的数量
    request.setSize(10);
    // 设置一次批量处理的条数,默认是1000
    request.setBatchSize(100);
    // 设置超时时间
    request.setTimeout(TimeValue.timeValueMinutes(2));
    // 索引选项
    request.setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN);
    // 同步执行
    BulkByScrollResponse bulkResponse = client.updateByQuery(request, RequestOptions.DEFAULT);
    // 异步执行
    // client.updateByQueryAsync(request, RequestOptions.DEFAULT, listener);
    // 返回结果
    TimeValue timeTaken = bulkResponse.getTook();
    boolean timedOut = bulkResponse.isTimedOut();
    long totalDocs = bulkResponse.getTotal();
    long updatedDocs = bulkResponse.getUpdated();
    long deletedDocs = bulkResponse.getDeleted();
    long batches = bulkResponse.getBatches();
    long noops = bulkResponse.getNoops();
    long versionConflicts = bulkResponse.getVersionConflicts();
    long bulkRetries = bulkResponse.getBulkRetries();
    long searchRetries = bulkResponse.getSearchRetries();
    TimeValue throttledMillis = bulkResponse.getStatus().getThrottled();
    TimeValue throttledUntilMillis = bulkResponse.getStatus().getThrottledUntil();
    List<ScrollableHitSource.SearchFailure> searchFailures = bulkResponse.getSearchFailures();
    List<BulkItemResponse.Failure> bulkFailures = bulkResponse.getBulkFailures();
    System.out.println("查询更新总共花费了:" + timeTaken.getMillis() + " 毫秒,总条数:" + totalDocs + ",更新数:" + updatedDocs);
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) TimeValue(org.elasticsearch.common.unit.TimeValue) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse)

Example 58 with TimeValue

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.unit.TimeValue in project pancm_project by xuwujing.

the class EsHighLevelRestTest2 method reindex.

/**
 * 索引复制
 *
 * @throws IOException
 */
private static void reindex() throws IOException {
    // 创建索引复制请求并进行索引复制
    ReindexRequest request = new ReindexRequest();
    // 需要复制的索引
    request.setSourceIndices("user");
    // 复制的目标索引
    request.setDestIndex("dest_test");
    // 表示如果在复制索引的时候有缺失的文档的话会进行创建,默认是index
    request.setDestOpType("create");
    // 如果在复制的过程中发现版本冲突,那么会继续进行复制
    request.setConflicts("proceed");
    // 只复制文档类型为 userindex 的数据
    request.setSourceDocTypes("userindex");
    // 只复制 pancm 用户的数据
    request.setSourceQuery(new TermQueryBuilder("user", "pancm"));
    // 设置复制文档的数量
    request.setSize(10);
    // 设置一次批量处理的条数,默认是1000
    request.setSourceBatchSize(100);
    // 进行排序
    // request.addSortField("postDate", SortOrder.DESC);
    // 指定切片大小
    request.setSlices(2);
    // 设置超时时间
    request.setTimeout(TimeValue.timeValueMinutes(2));
    // 允许刷新
    request.setRefresh(true);
    // 同步执行
    BulkByScrollResponse bulkResponse = client.reindex(request, RequestOptions.DEFAULT);
    // 异步执行
    // client.reindexAsync(request, RequestOptions.DEFAULT, listener);
    // 响应结果处理
    TimeValue timeTaken = bulkResponse.getTook();
    boolean timedOut = bulkResponse.isTimedOut();
    long totalDocs = bulkResponse.getTotal();
    long updatedDocs = bulkResponse.getUpdated();
    long createdDocs = bulkResponse.getCreated();
    long deletedDocs = bulkResponse.getDeleted();
    long batches = bulkResponse.getBatches();
    long noops = bulkResponse.getNoops();
    long versionConflicts = bulkResponse.getVersionConflicts();
    long bulkRetries = bulkResponse.getBulkRetries();
    long searchRetries = bulkResponse.getSearchRetries();
    TimeValue throttledMillis = bulkResponse.getStatus().getThrottled();
    TimeValue throttledUntilMillis = bulkResponse.getStatus().getThrottledUntil();
    List<ScrollableHitSource.SearchFailure> searchFailures = bulkResponse.getSearchFailures();
    List<BulkItemResponse.Failure> bulkFailures = bulkResponse.getBulkFailures();
    System.out.println("索引复制总共花费了:" + timeTaken.getMillis() + " 毫秒,总条数:" + totalDocs + ",创建数:" + createdDocs + ",更新数:" + updatedDocs);
}
Also used : ReindexRequest(org.elasticsearch.index.reindex.ReindexRequest) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) TimeValue(org.elasticsearch.common.unit.TimeValue) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse)

Example 59 with TimeValue

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.unit.TimeValue in project pancm_project by xuwujing.

the class EsHighLevelRestTest2 method deleteByQuery.

/**
 * 根据查询条件删除
 * @throws IOException
 */
private static void deleteByQuery() throws IOException {
    // 
    DeleteByQueryRequest request = new DeleteByQueryRequest("user");
    // 设置查询条件
    request.setQuery(new TermQueryBuilder("user", "pancm"));
    // 设置复制文档的数量
    request.setSize(10);
    // 设置一次批量处理的条数,默认是1000
    request.setBatchSize(100);
    // 设置路由
    request.setRouting("=cat");
    // 设置超时时间
    request.setTimeout(TimeValue.timeValueMinutes(2));
    // 允许刷新
    request.setRefresh(true);
    // 索引选项
    request.setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN);
    // 同步执行
    BulkByScrollResponse bulkResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);
    // 异步执行
    // client.updateByQueryAsync(request, RequestOptions.DEFAULT, listener);
    // 返回结果
    TimeValue timeTaken = bulkResponse.getTook();
    boolean timedOut = bulkResponse.isTimedOut();
    long totalDocs = bulkResponse.getTotal();
    long deletedDocs = bulkResponse.getDeleted();
    long batches = bulkResponse.getBatches();
    long noops = bulkResponse.getNoops();
    long versionConflicts = bulkResponse.getVersionConflicts();
    long bulkRetries = bulkResponse.getBulkRetries();
    long searchRetries = bulkResponse.getSearchRetries();
    TimeValue throttledMillis = bulkResponse.getStatus().getThrottled();
    TimeValue throttledUntilMillis = bulkResponse.getStatus().getThrottledUntil();
    List<ScrollableHitSource.SearchFailure> searchFailures = bulkResponse.getSearchFailures();
    List<BulkItemResponse.Failure> bulkFailures = bulkResponse.getBulkFailures();
    System.out.println("查询更新总共花费了:" + timeTaken.getMillis() + " 毫秒,总条数:" + totalDocs + ",删除数:" + deletedDocs);
}
Also used : DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) TimeValue(org.elasticsearch.common.unit.TimeValue) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse)

Example 60 with TimeValue

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.unit.TimeValue in project herd by FINRAOS.

the class BusinessObjectDefinitionIndexSearchDaoImpl method scrollSearchResultsIntoBusinessObjectDefinitionDto.

/**
 * Private method to handle scrolling through the results from the search request and adding them to a business object definition entity list.
 *
 * @param searchRequestBuilder the the search request to scroll through
 * @param indexName the index name
 * @return list of business object definition entities
 */
private List<BusinessObjectDefinitionIndexSearchResponseDto> scrollSearchResultsIntoBusinessObjectDefinitionDto(final SearchRequestBuilder searchRequestBuilder, String indexName) {
    // Retrieve the search response
    final Search.Builder searchBuilder = new Search.Builder(searchRequestBuilder.toString()).addIndex(indexName);
    searchBuilder.setParameter(Parameters.SIZE, ELASTIC_SEARCH_SCROLL_PAGE_SIZE);
    searchBuilder.setParameter(Parameters.SCROLL, new TimeValue(ELASTIC_SEARCH_SCROLL_KEEP_ALIVE_TIME).toString());
    JestResult jestResult = jestClientHelper.searchExecute(searchBuilder.build());
    List<BusinessObjectDefinitionIndexSearchResponseDto> businessObjectDefinitionIndexSearchResponseDtoList = new ArrayList<>();
    List<BusinessObjectDefinitionIndexSearchResponseDto> resultList = jestResult.getSourceAsObjectList(BusinessObjectDefinitionIndexSearchResponseDto.class);
    while (resultList.size() != 0) {
        businessObjectDefinitionIndexSearchResponseDtoList.addAll(resultList);
        String scrollId = jestResult.getJsonObject().get(SCROLL_ID).getAsString();
        SearchScroll scroll = new SearchScroll.Builder(scrollId, new TimeValue(ELASTIC_SEARCH_SCROLL_KEEP_ALIVE_TIME).toString()).build();
        jestResult = jestClientHelper.searchScrollExecute(scroll);
        resultList = jestResult.getSourceAsObjectList(BusinessObjectDefinitionIndexSearchResponseDto.class);
    }
    return businessObjectDefinitionIndexSearchResponseDtoList;
}
Also used : SearchScroll(io.searchbox.core.SearchScroll) Search(io.searchbox.core.Search) BusinessObjectDefinitionIndexSearchResponseDto(org.finra.herd.model.dto.BusinessObjectDefinitionIndexSearchResponseDto) ArrayList(java.util.ArrayList) TimeValue(org.elasticsearch.common.unit.TimeValue) JestResult(io.searchbox.client.JestResult)

Aggregations

TimeValue (org.elasticsearch.common.unit.TimeValue)169 SearchResponse (org.elasticsearch.action.search.SearchResponse)37 ArrayList (java.util.ArrayList)28 IOException (java.io.IOException)27 ClusterState (org.elasticsearch.cluster.ClusterState)26 SearchHit (org.elasticsearch.search.SearchHit)26 CountDownLatch (java.util.concurrent.CountDownLatch)18 Settings (org.elasticsearch.common.settings.Settings)18 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)17 Map (java.util.Map)16 Supplier (org.apache.logging.log4j.util.Supplier)16 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)16 List (java.util.List)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 AbstractRunnable (org.elasticsearch.common.util.concurrent.AbstractRunnable)13 Matchers.containsString (org.hamcrest.Matchers.containsString)13 TimeUnit (java.util.concurrent.TimeUnit)11 ThreadPool (org.elasticsearch.threadpool.ThreadPool)11 HashMap (java.util.HashMap)10 ByteSizeValue (org.elasticsearch.common.unit.ByteSizeValue)10