2

我正在使用以下方法来检查和创建弹性搜索索引。该脚本适用于 Elasticsearch 2.3 和 2.4 版本。我正在尝试使用 Elasticsearch 5.0 版,但它不起作用。我正在尝试的只是使用 groovy 脚本动态创建索引和搜索索引。

static def checkOrCreateESIndex(String baseUrl, String path)
    {
        try
        {
            def res_GET = null
            def res_PUT = null
            def status_message = null
            def http = new HTTPBuilder(baseUrl)
            println "New ES Check Index : "+baseUrl+path
            http.request(Method.GET, ContentType.JSON)
            {
                uri.path = path
                requestContentType = ContentType.XML
                headers.'Accept-Encoding' = 'gzip,deflate'
                headers.Accept = 'application/json';
                response.success = { resp  ->
                                        res_GET =  200
                                        println "SUCCESS! ${resp.status}"
                                   }
                response.failure = { resp ->
                                        res_GET =  400
                                        println "Failure! ${resp.status}"
                                   }
            }
            if (res_GET != 200)
            {
                String params = "{\"settings\":{\"number_of_shards\":2,\"number_of_replicas\":0},\"mappings\":{\"run\":{\"_timestamp\":{\"enabled\":true},\"properties\":{\"70 Percentile\":{\"type\":\"float\"},\"80 Percentile\":{\"type\":\"float\"},\"85 Percentile\":{\"type\":\"float\"},\"95 Percentile\":{\"type\":\"float\"},\"90 Percentile\":{\"type\":\"float\"},\"Average\":{\"type\":\"float\"},\"Fail\":{\"type\":\"string\"},\"Maximum\":{\"type\":\"float\"},\"Minimum\":{\"type\":\"float\"},\"Pass\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"ProjectName\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"RunID\":{\"type\":\"string\"},\"VirtualUsers\":{\"type\":\"string\"},\"Release\":{\"type\":\"string\"},\"BuildNumber\":{\"type\":\"string\"},\"StartTime\":{\"type\":\"string\"},\"EndTime\":{\"type\":\"string\"},\"StdDeviation\":{\"type\":\"string\"},\"TestName\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"TransactionName\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"Baseline\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"SLAviolationcount\":{\"type\":\"float\"}}}}}"
                def bodyMap2 = new JsonSlurper().parseText(params)
                def response_body = null
                def response_header = null
                def http2 = new HTTPBuilder(baseUrl)

                println "New ES Create Index : "+baseUrl+path
                println "New ES Mapping : "+params
                http2.request(Method.PUT)
                {
                    uri.path = path
                    requestContentType = ContentType.JSON
                    headers.'Accept' = 'application/json';
                    body = bodyMap2
                    headers.'Accept-Encoding' = 'gzip,deflate'
                    headers.'Cookie' = 'JSESSIONID=934ED773C47D81C74C63BEAFE1D6CA1B'

                    response.success = { resp ->
                                            res_PUT =  200
                                            println "SUCCESS! ${resp.status}"
                                       }
                    response.failure = { resp ->
                                            res_PUT =  400
                                            println "Failure! ${resp.status}"
                                       }
                }
            }

            if (res_GET == 200)
            {
                status_message = "IDX_EXISTS"
            }
            else if (res_GET != 200 && res_PUT == 200)
            {
                 status_message = "IDX_CREATED"
            }
            else
            {
                 status_message = "IDX_FAIL"
            }

            return status_message

        }
        catch (groovyx.net.http.HttpResponseException ex)
        {
            ex.printStackTrace()
            return null
        }
        catch (java.net.ConnectException ex)
        {
            ex.printStackTrace()
            return null
        }
    }

    static def postElasticSearchMessage(String baseUrl, String path,String params) 
    {
        try 
        {
            def res_ES = null
            def bodyMap = new JsonSlurper().parseText(params)
            def response_body = null
            def response_header = null
            def http = new HTTPBuilder(baseUrl)
            http.request(Method.POST) 
            {
                uri.path = path
                requestContentType = ContentType.JSON
                body = bodyMap
                headers.'Accept-Encoding' = 'gzip,deflate'
                headers.'Cookie' = 'JSESSIONID=934ED773C47D81C74C63BEAFE1D6CA1B'
                response.success = { resp ->
                                        res_ES = 'Y'
                                        println "SUCCESS! ${resp.status}"
                                    }
               response.failure = { resp ->
                                        res_ES = 'N'
                                        println "FAILURE! ${resp.status}"

                                  }
            }
            return res_ES
        }
        catch (groovyx.net.http.HttpResponseException ex) 
        {
            ex.printStackTrace()
            return 'N'
        }
        catch (java.net.ConnectException ex) 
        {
            ex.printStackTrace()
            return 'N'
        }
    }

下面是我的索引结构:

{\"settings\":{\"number_of_shards\":2,\"number_of_replicas\":0},\"mappings\":{\"run\":{\"_timestamp\":{\"enabled\":true},\"properties\":{\"70 Percentile\":{\"type\":\"float\"},\"80 Percentile\":{\"type\":\"float\"},\"85 Percentile\":{\"type\":\"float\"},\"95 Percentile\":{\"type\":\"float\"},\"90 Percentile\":{\"type\":\"float\"},\"Average\":{\"type\":\"float\"},\"Fail\":{\"type\":\"string\"},\"Maximum\":{\"type\":\"float\"},\"Minimum\":{\"type\":\"float\"},\"Pass\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"ProjectName\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"RunID\":{\"type\":\"string\"},\"VirtualUsers\":{\"type\":\"string\"},\"Release\":{\"type\":\"string\"},\"BuildNumber\":{\"type\":\"string\"},\"StartTime\":{\"type\":\"string\"},\"EndTime\":{\"type\":\"string\"},\"StdDeviation\":{\"type\":\"string\"},\"TestName\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"TransactionName\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"Baseline\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"SLAviolationcount\":{\"type\":\"float\"}}}}}

我将如何使这项工作适用于 elasticsearch 5.0 版。请帮助我处理 5.0 的索引结构和搜索查询。那真的很有帮助。在此先感谢。

4

0 回答 0