ndevroy
2/7/2019 - 5:27 PM

C360-Dev-Elastic-Queries


#Start by extracting the dev jks cert to pem to use over SSL
$ keytool -importkeystore -srckeystore es-c360-dev.jks -destkeystore es-dev.p12 -srcstoretype jks -deststoretype pkcs12
$ openssl pkcs12 -in es-dev.p12 -out es-dev.pem

#Next, let's create an environment variable to invoke the password without exposing it in your terminal
export keystorePass=$(head -1 es-c360-dev-keystore-pass)

#Now we can run queries against dev instance
#--~--~~-~~~--~~---~--~~--~~~~-----~-~-~-~-~~--~~---~~--~~---~~~-~~~-~--~~--~~~-
#Create a test index in the dev instance
curl --cert es-dev.p12:$keystorePass -XPUT "https://elasticsearch-dev.optum.com:443/c360-test-index" -H 'Content-Type: application/json' -d'
 {
     "settings" : {
         "index" : {
             "number_of_shards" : 1,
             "number_of_replicas" : 1
         }
     },
     "mappings" : {
         "_doc" : {
           "dynamic": "strict",
             "properties" : {
                 "name" : { "type" : "text" },
                 "hobby" : { "type" : "text" },
                 "age" : {"type" : "text"}
             }
         }
     }
 }'

#Delete our sample index
curl --cert es-dev.p12:$keystorePass -XDELETE "https://elasticsearch-dev.optum.com:443/c360-test-index"

#--~--~~-~~~--~~---~--~~--~~~~-----~-~-~-~-~~--~~---~~--~~---~~~-~~~-~--~~--~~~-

#Add records to our sample index
curl --cert es-dev.p12:$keystorePass -XPUT "https://elasticsearch-dev.optum.com:443/c360-test-index/_doc/1" -H 'Content-Type: application/json' -d'
{
  "name":"Francis",
  "hobby":["bowling","cycling","bare knuckle boxing","graft"],
  "age":"70"
}'

curl --cert es-dev.p12:$keystorePass -XPUT "https://elasticsearch-dev.optum.com:443/c360-test-index/_doc/3" -H 'Content-Type: application/json' -d'
{
  "name":"Aflie",
  "hobby":["lawn darts","malingering","debate"],
  "age":"22"
}'

curl --cert es-dev.p12:$keystorePass -XPUT "https://elasticsearch-dev.optum.com:443/c360-test-index/_doc/2" -H 'Content-Type: application/json' -d'
{
  "name":"Lucky",
  "hobby":"bowling",
  "age":"22"
}'

#--~--~~-~~~--~~---~--~~--~~~~-----~-~-~-~-~~--~~---~~--~~---~~~-~~~-~--~~--~~~-

#GET all the records in the index
curl --cert es-dev.p12:$keystorePass -XGET "https://elasticsearch-dev.optum.com:443/c360-test-index/_doc/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query" : {
    "match_all" : {  }
  }
}'

#GET using a filtered query -- get all persons under 30, for example
curl --cert es-dev.p12:$keystorePass -XGET "https://elasticsearch-dev.optum.com:443/c360-test-index/_doc/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query" : {
    "bool" : {
      "filter" : {
        "range" : {
          "age" : {"lt" : 30}
        }
      }
    }
  }
}'

#Full-text Search -- by field
curl --cert es-dev.p12:$keystorePass -XGET "https://elasticsearch-dev.optum.com:443/c360-test-index/_doc/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query" : {
    "match" : {
      "hobby" : "graft"
    }
  }
}'

#Multi-matching queries
curl --cert es-dev.p12:$keystorePass -XGET "https://elasticsearch-dev.optum.com:443/c360-test-index/_doc/_search"?pretty -H 'Content-Type: application/json' -d'
{
  "query" : {
    "bool" : {
      "must" : {
        "match" : {
          "hobby" : "bowling"
        }
      },
      "must_not" : {
        "match" : {
          "name" : "Francis"
        }
      }
    }
  }
}'