2

I'm using below method to check and create elasticsearch index. This script works perfectly for Elasticsearch 2.3 and 2.4 versions. I'm trying things with Elasticsearch version 5.0 but it isn't working. All i'm trying is to create an index and search index dynamically using groovy script.

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'
        }
    }

Below is my index structure:

{\"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\"}}}}}

how would i make this work for elasticsearch version 5.0. Kindly help me with the index structure and search query for 5.0. That would be really helpful.Thanks in advance.

aristotll
  • 8,694
  • 6
  • 33
  • 53
syndy1989
  • 403
  • 10
  • 25
  • 1
    What is the error message? – aristotll Aug 16 '17 at 12:48
  • [Thread-63] WARN groovyx.net.http.HTTPBuilder - Error parsing 'application/json; charset=UTF-8' response groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object The current character read is '?' with an int value of 65533 Unable to determine the current character, it is not a string, number, array, or object line number 1 index number 1 ? at groovy.json.internal.JsonParserCharArray.decodeValueInternal(JsonParserCharArray.java:206) at groovy.json.internal.JsonParserCharArray.decodeValue – syndy1989 Aug 19 '17 at 18:00
  • Kindly refer this link for more info: https://stackoverflow.com/questions/45756598/search-query-isnt-working-for-elasticsearch-5-0 – syndy1989 Aug 19 '17 at 18:01

0 Answers0