无法使用groovy创建或搜索索引

时间:2017-08-16 12:24:39

标签: groovy elasticsearch-5 groovy-console

我使用以下方法检查并创建弹性搜索索引。此脚本适用于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的索引结构和搜索查询。那将是非常有帮助的。谢谢。

0 个答案:

没有答案