Saturday, May 6, 2017

Elastic Search Sample Query

http://localhost:5601/app/kibana#/dev_tools/console?_g=()

user:pwd elastic/changeme
PUT /customer?pretty
GET /_cat/indices?v

PUT /customer/external/1?pretty
{
  "name": "John Doe"
}

GET /customer/external/1?pretty

PUT /customer
PUT /customer/external/1
{
  "name": "John Doe"
}
GET /customer/external/1
DELETE /customer

PUT /customer/external/1?pretty
{
  "name": "John Doe"
}
PUT /customer/external/2?pretty
{
  "name": "Sam Stint"
}

CURL Hit
 curl -X POST "http://<$ES_HOST>/INDEX_NAME/OPERATION?conflicts=proceed&wait_for_completion=false" -H 'Content-Type: application/json' -d'
{
 ES_QUERY
 }'

POST /customer/external/1/_update?pretty
{
  "doc": { "name": "Jane Doe" }
}
POST /customer/external/1/_update?pretty
{
  "doc": { "name": "Jane Doe", "age": 20 }
}

POST /customer/external/2/_update?pretty
{
  "doc": { "name": "Jane Doe2", "age": 20 }
}

POST /customer/external/1/_update?pretty
{
  "script" : "ctx._source.age += 5"
}

DELETE /customer/external/2?pretty

#batch operation
POST /customer/external/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }

POST /customer/external/_bulk?pretty
{"update":{"_id":"1"}}
{"doc":{"name":"John Doe becomes Jane Doe"}}
{"delete":{"_id":"2"}}

#Bank sample
accounts.json
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}

curl --user elastic:changeme -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"

curl 'localhost:9200/_cat/indices?v' --user elastic:changeme

GET /bank/_search?q=*&sort=account_number:asc&pretty

GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ]
}

GET /bank/_search
{
  "query": { "match_all": {} },
  "size": 2
}

GET /bank/_search
{
  "query": { "match_all": {} },
  "from": 10,
  "size": 10
}

GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": { "balance": { "order": "desc" } }
}

GET /bank/_search
{
  "query": { "match_all": {} },
  "_source": ["account_number", "balance"]
}

GET /bank/_search
{
  "query": { "match": { "address": "mill lane" } }
}

GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}

GET /bank/_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}

GET /bank/_search
{
  "query": {
    "bool": {
      "must_not": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}

GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "age": "40" } }
      ],
      "must_not": [
        { "match": { "state": "ID" } }
      ]
    }
  }
}

Term query based search:
GET employee/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "name": {
              "value": "John"
            }
          }
        },
        {
          "term": {
            "department": {
              "value": "Sales"
            }
          }
        }
        
      ]
    }
  }
}
GET /bank/_search
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}

Find distinct values
GET /cars/transactions/_search?search_type=count
{
  "aggs": {
    "distinct_colors": {
      "terms": {
        "field": "color",
        "size": 1000
      }
    }
  }
}

More: https://www.elastic.co/guide/en/elasticsearch/reference/current/_executing_filters.html

Elastic Search : quick start search based application

Elastic Search Installation:

Install Elastic search
curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.0.tar.gz
tar -xvf elasticsearch-5.4.0.tar.gz

Posting data:curl --user elastic:changeme -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"
elasticsearch-5.4.0/bin
https://www.elastic.co/guide/en/elasticsearch/reference/current/_installation.html
Install kibana:
cd user_home/kibana-5.4.0-darwin-x86_64/bin
./kibana.sh
Install marvel:
https://www.elastic.co/downloads/x-pack
elasticsearch-plugin install x-pack
kibana-plugin install x-pack


Marvel url:http://localhost:5601/app/kibana#/dev_tools/console?_g=()
<REST Verb> /<Index>/<Type>/<ID>
curl 'localhost:9200/_cat/indices?v' --user elastic:changeme