/elasticsearch

vagrant elasticsearch cluster

verbes

GET PUT DELETE

Creating an index

curl -XPUT "http://localhost:9200/ecommerce" -d' { }'
{"acknowledged":true}

deleting an index

curl -XDELETE "http://localhost:9200/ecommerce"
{"acknowledged":true}

cat command

curl -XGET "http://localhost:9200/_cat/indices?v"

Data Types

Core

  1. String
  2. Numeric
  3. Date
  4. Boolean
  5. Binary (Base64) it is not searchable

Complex Types

  1. Object (flat list of key/value pairs)
  2. Arrays (not supported )
  3. Nested data types
  4. Geo Data Types (rectangles and polygons)
  5. Attachment

Meta fields

each document has metadata e.g. _index, _type, _id

document source meta fields

_source

_size

indexing meta fields

_ALL

_FIELDS_NAMES

routing meta fields

_PARENT (foreign key)

_ROUTING (advanced)

_META

configuration for master

root@es0:~# cat /etc/elasticsearch/elasticsearch.yml
cluster.name: mycluster
node.name: "es0"
node.master: true
node.data: false
http.port : 9200
tcp.port : 9300
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping.unicast.hosts: ["data0", "data1", "data2"]
network.bind_host: 0.0.0.0
network.publish_host: 172.20.20.50

configuration for data

node1

root@es1:~# cat /etc/elasticsearch/elasticsearch.yml
cluster.name: mycluster
node.name: "es1"
node.master: false
node.data: true
http.port : 9200
tcp.port : 9300
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping.unicast.hosts: ["data0", "data1", "data2"]
network.bind_host: 0.0.0.0
network.publish_host: 172.20.20.51

node2

root@es2:~# cat /etc/elasticsearch/elasticsearch.yml
cluster.name: mycluster
node.name: "es2"
node.master: false
node.data: true
http.port : 9200
tcp.port : 9300
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping.unicast.hosts: ["data0", "data1", "data2"]
network.bind_host: 0.0.0.0
network.publish_host: 172.20.20.52

check health

root@es0:~# curl -XGET 'http://localhost:9200/_cluster/health'?pretty
{
  "cluster_name" : "mycluster",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 7,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 23857,
  "active_shards_percent_as_number" : 100.0
}

Create index and mappings

SENSE format

PUT /ecommerce
{
    "mappings": {
        "product": {
            "properties": {
                "name": {
                    "type": "string"
                },
                "price": {
                    "type": "double"
                },
                "description": {
                    "type": "string"
                },
                "status": {
                    "type": "string"
                },
                "quantity": {
                    "type": "integer"
                },
                "categories": {
                    "type": "nested",
                    "properties": {
                        "name": {
                            "type": "string"
                        }
                    }
                },
                "tags": {
                    "type": "string"
                }
            }
        }
    }
}

curl format

curl -XPUT "http://localhost:9200/ecommerce" -d'
{
    "mappings": {
        "product": {
            "properties": {
                "name": {
                    "type": "string"
                },
                "price": {
                    "type": "double"
                },
                "description": {
                    "type": "string"
                },
                "status": {
                    "type": "string"
                },
                "quantity": {
                    "type": "integer"
                },
                "categories": {
                    "type": "nested",
                    "properties": {
                        "name": {
                            "type": "string"
                        }
                    }
                },
                "tags": {
                    "type": "string"
                }
            }
        }
    }
}'

Addming Test Data

bulk insert

curl -XPOST http://localhost:9200/ecommerce/product/_bulk --data-binary "@test-data.json"
{"took":47,"errors":false,"items":[{"index":{"_index":"ecommerce","_type":"product","_id":"1","_version":1,"status":201}},{"index":{"_index":"ecommerce","_type":"product","_id":"2","_version":1,"status":201}},{"index":{"_index":"ecommerce","_type":"product","_id":"3","_version":1,"status":201}},{"index":{"_index":"ecommerce","_type":"product","_id":"4","_version":1,"status":201}},{"index":{"_index":"ecommerce","_type":"product","_id":"5","_version":1,"status":201}},
....

If the request succeeds in creating a new document, Elasticsearch will return the usual metadata and an HTTP response code of 201 Created.

Retrieving a document

sense query

GET /ecommerce/product/1002

curl statement

curl -XGET "http://localhost:9200/ecommerce/product/1002"

inserting a doc

SENSE

PUT /ecommerce/product/1001
{
    "name": "Zend Framework 2: From Beginner to Professional",
    "price": 30.00,
    "description": "Learn Zend from Framework 2 in just a few hours!",
    "status": "active",
    "quantity": 1,
    "categories": [
        {"name": "Software" }
    ],
    "tags": ["zend framework", "zf", "php", "programming"]
}

curl

curl -XPUT "http://localhost:9200/ecommerce/product/1001" -d'
{
    "name": "Zend Framework 2: From Beginner to Professional",
    "price": 30.00,
    "description": "Learn Zend from Framework 2 in just a few hours!",
    "status": "active",
    "quantity": 1,
    "categories": [
        {"name": "Software" }
    ],
    "tags": ["zend framework", "zf", "php", "programming"]
}'

updating a doc

deleting a doc

SENSE

DELETE /ecommerce/product/1001

curl

curl -XDELETE "http://localhost:9200/ecommerce/product/1001"

plugins

head

http://localhost:9200/_plugin/head/