1 2 3 4 5 | POST /es_info/_doc/1800 { "create_time" : "2022-10-09T10:00:00", "type" : "KM", } |
分类: ElasticSearch
Docker: 设置ElasticSearch密码报错
ElasticSearch: 批量更新指定字段
腾讯云CTSDB: 使用Java API查询数据
最近在使用腾讯云CTSDB,发现查询数据时使用ElasticSearch原生Java API更方便一些.
具体操作如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must(QueryBuilders.termQuery("phone", terminalId)); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); // 判断时间范围. boolQueryBuilder.must( QueryBuilders.rangeQuery("createDateTime") .gte(LocalDateTime.of(2022, 9, 14, 19, 0, 0).format(dateTimeFormatter)) .lte(LocalDateTime.now().format(dateTimeFormatter))); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(boolQueryBuilder); searchSourceBuilder.size(100); searchSourceBuilder .docValueField("_id") .docValueField("phone") .docValueField("createDateTime") .docValueField("userName"); searchSourceBuilder.sort(SortBuilders.fieldSort("createDateTime").order(SortOrder.DESC)); SearchRequest searchRequest = new SearchRequest("user_info"); searchRequest.source(searchSourceBuilder); try { var searchResponse = ctsdbElasticsearchClient.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = searchResponse.getHits(); SearchHit[] searchHits = hits.getHits(); log.info( objectMapper.writeValueAsString( Arrays.stream(searchHits).map(SearchHit::getFields).toList())); } catch (IOException e) { log.error("读取数据异常!"); } |
ElasticSearch: 搜索结果根据指定字段去重
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | /* 语句: GET /es_info/_search { "from":0, "size":10, "query":{ "bool":{ "must":[ { "range":{ "createDateTime":{ "from":"2021-09-01T10:39:25", "to":"2022-09-02T10:39:25", "include_lower":true, "include_upper":true, "boost":1 } } } ], "adjust_pure_negative":true, "boost":1 } }, "version":true, "explain":false, "_source":{ "includes":[ "id", "userId", "phone", "totalScore" ], "excludes":[ ] }, "sort":[ { "totalScore":{ "order":"desc" } } ], "collapse":{ "field":"userId" } }*/ NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder().withFields(new String[]{"id", "userId", "phone", "totalScore"}); BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery(); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); // 判断时间范围. queryBuilder.must( QueryBuilders.rangeQuery("createDateTime") .gte(entity.startDateTime().format(dateTimeFormatter)) .lte(entity.endDateTime().format(dateTimeFormatter))); FieldSortBuilder sortBuilders = SortBuilders.fieldSort("totalScore").order(SortOrder.DESC); Query query = nativeSearchQueryBuilder .withMaxResults(10) .withQuery(queryBuilder) .withSort(sortBuilders) .withCollapseField("userId") .build(); SearchHits<ESInfo> searchHits = elasticsearchOperations.search(query, ESInfo.class); searchHits .get() .map(SearchHit::getContent) .map( item -> { // 处理... return item; }) .toList(); |