From a6ec66ceea0ec0fed77f228211fe3841bff45cf2 Mon Sep 17 00:00:00 2001 From: gaojuan1 Date: Tue, 8 Apr 2025 14:43:15 +0800 Subject: [PATCH] =?UTF-8?q?es=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fangjia/script/util/ElasticUtils.groovy | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/projects/fangjia-script/src/main/groovy/com/fangjia/script/util/ElasticUtils.groovy b/projects/fangjia-script/src/main/groovy/com/fangjia/script/util/ElasticUtils.groovy index 6aba12207..a611fedc8 100644 --- a/projects/fangjia-script/src/main/groovy/com/fangjia/script/util/ElasticUtils.groovy +++ b/projects/fangjia-script/src/main/groovy/com/fangjia/script/util/ElasticUtils.groovy @@ -1,5 +1,8 @@ package com.fangjia.script.util +import com.fangjia.core.util.HttpRequest +import com.fangjia.script.gj.xiamen.ElasticUtils89 +import net.sf.json.JSONObject import org.apache.http.HttpHost import org.apache.http.auth.AuthScope import org.apache.http.auth.UsernamePasswordCredentials @@ -17,11 +20,13 @@ import org.elasticsearch.action.search.ClearScrollRequest import org.elasticsearch.action.search.SearchRequest import org.elasticsearch.action.search.SearchResponse import org.elasticsearch.action.search.SearchScrollRequest +import org.elasticsearch.action.support.WriteRequest import org.elasticsearch.client.RequestOptions import org.elasticsearch.client.RestClient import org.elasticsearch.client.RestClientBuilder import org.elasticsearch.client.RestHighLevelClient import org.elasticsearch.common.unit.TimeValue +import org.elasticsearch.common.xcontent.XContentType import org.elasticsearch.index.query.QueryBuilder import org.elasticsearch.search.Scroll import org.elasticsearch.search.SearchHit @@ -262,4 +267,68 @@ class ElasticUtils { client.close() return results; } + + static def modify(id, map, host, indexName){ + BulkRequest request = new BulkRequest(); + int processedCount = 0; + def client = null + if(host && host == "192.168.0.89"){ + client = ElasticUtils89.getClient(host) + }else if(host && host == "192.168.0.52"){ + client = ElasticUtils.getClient(host) + } + + request.add(new IndexRequest(indexName, "_doc", id).source(map, XContentType.JSON)); + if (request.numberOfActions() > 100) { + request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT); + if (bulkResponse.hasFailures()) { + // ´¦Àí´íÎó + println "Bulk request has failures: " + bulkResponse.buildFailureMessage() + } else { + processedCount += request.numberOfActions(); + println "Processed " + processedCount + " documents" + } + } + if (request.numberOfActions() > 0) { + request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT); + if (bulkResponse.hasFailures()) { + // ´¦Àí´íÎó + println "Bulk request has failures: " + bulkResponse.buildFailureMessage() + } else { + processedCount += request.numberOfActions(); + println "Processed " + processedCount + " documents" + } + } + } + + static def delete(id, host, indexName){ + def header = [:] + if(host && host == "192.168.0.89"){ + header["Authorization"] = "Basic ${Base64.getEncoder().encodeToString("elastic:elasticsearch".getBytes("UTF-8"))}".toString() + }else if(host && host == "192.168.0.52"){ + header["Authorization"] = "Basic ${Base64.getEncoder().encodeToString("elastic:goojia123456".getBytes("UTF-8"))}".toString() + } + String seawaterUrl = "http://${host}:9200/${indexName}/_delete_by_query" + def body = """ + { + "query": { + "bool": { + "must": [ + { + "match": { + "id": "${id}" + } + } + ] + } + } + } + """ + HttpRequest request = new HttpRequest() + String res = request.postWithBody(seawaterUrl, JSONObject.fromObject(body.toString()), header, "UTF-8") + println(res) + } + }