ElasticSearch 2 (1) - Getting Start

Richaaaard發表於2016-01-28

ElasticSearch 2.1.1 (1) - Getting Start

Install & Up

cd elasticsearch-2.1.1/bin

./elasticsearch

./elasticsearch --cluster.name my_cluster_name --node.name my_node_name

Cluster Health

curl 'localhost:9200/_cat/health?v'
curl 'localhost:9200/_cat/nodes?v'

List All Indices

curl 'localhost:9200/_cat/indices?v'

Create an Index

curl -XPUT 'localhost:9200/customer?pretty'

{ 
    "acknowledged" : true 
} 

curl 'localhost:9200/_cat/indices?v'

health | index      | pri   | rep   | docs.count | docs.deleted | store.size | pri.store.size 
yellow | customer   | 5     | 1     | 0          |0             | 495b       | 495b

Index and Query

Index:

curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{ 
    "name": "John Doe” 
}'

Response:

{ 
    "_index" : "customer”, 
    "_type" : "external”, 
    "_id" : "1”, 
    "_version" : 1, 
    "created" : true 
}

Query:

curl -XGET 'localhost:9200/customer/external/1?pretty'

{ 
    "_index" : "customer", 
    "_type" : "external", 
    "_id" : "1", 
    "_version" : 1, 
    "found" : true, 
    "_source" : 
    { 
        "name": "John Doe" 
    } 
}

Delete an Index

curl -XDELETE 'localhost:9200/customer?pretty'
{ 
    "acknowledged" : true 
} 

curl 'localhost:9200/_cat/indices?v'
health | index | pri | rep | docs.count | docs.deleted  | store.size | pri.store.size

curl -X :///

Updating Document

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d
' { "doc": { "name": "Jane Doe" } }'

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d 
' { "doc": { "name": "Jane Doe", "age": 20 } }'

Script:

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d 
' { "script" : "ctx._source.age += 5" }'

Error:

{
  "error" : {
    "root_cause" : [ {
      "type" : "remote_transport_exception",
      "reason" : "[Angelica Jones][127.0.0.1:9300][indices:data/write/update[s]]"
    } ],
    "type" : "illegal_argument_exception",
    "reason" : "failed to execute script",
    "caused_by" : {
      "type" : "script_exception",
      "reason" : "scripts of type [inline], operation [update] and lang [groovy] are disabled"
    }
  },
  "status" : 400
}

Solution:elasticsearch.yml

script.inline: on 
script.indexed: on

Deleting Documents

curl -XDELETE 'localhost:9200/customer/external/2?pretty’

The delete-by-query plugin can delete all documents matching a specific query.

Batch Processing

curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d 
'{"index":{"_id":"1”}}
 {"name": "John Doe” }
 {"index":{"_id":"2”}}
 {"name": "Jane Doe" } ‘

Delete:

curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d 
' {"update":{"_id":"1”}}
 {
    "doc": { "name": "John Doe becomes Jane Doe" } 
 }
 {"delete":{"_id":"2"}} ‘

The Search API

curl 'localhost:9200/bank/_search?q=*&pretty’
  • took –

    time in milliseconds for Elasticsearch to execute the search

  • timed_out –

    tells us if the search timed out or not

  • _shards –

    tells us how many shards were searched, as well as a count of the successful/failed searched shards

  • hits –

    search results

  • hits.total –

    total number of documents matching our search criteria

  • hits.hits –

    actual array of search results (defaults to first 10 documents)

  • _score and max_score -

    ignore these fields for now

XPOST:

curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} } }'

NO CURSOR DON’T LIKE SQL

Introducing the Query Language

curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "size": 1 }'

curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "from": 10, "size": 10 }'

curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "sort": { "balance": { "order": "desc" } } }’

Executing Searches

Basic Query

  • Fields:

      curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "_source": ["account_number", "balance"] }'
  • Returns the account numbered 20:

      curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match": { "account_number": 20 } } }'
  • Containing the term "mill" in the address:

      curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match": { "address": "mill" } } }'
  • Containing the term "mill" or "lane" in the address:

      curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match": { "address": "mill lane" } } }'
  • Containing the phrase "mill lane" in the address:

      curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_phrase": { "address": "mill lane" } } }'

Boolean Query

  • AND

      curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }'
  • OR

      curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "should": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }'
  • NOR

      curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must_not": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }'
  • Anybody who is 40 years old but don’t live in ID(aho):

      curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must": [ { "match": { "age": "40" } } ], "must_not": [ { "match": { "state": "ID" } } ] } } }'

Range Query:

curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must": { "match_all": {} }, "filter": { "range": { "balance": { "gte": 20000, "lte": 30000 } } } } } }'

Executing Aggregations

Groups all the accounts by state, and then returns the top 10 (default) states sorted by count descending (also default):

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state"
      }
    }
  }
}'

SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC
  • Calculates the average account balance by state:

      curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "size": 0, "aggs": { "group_by_state": { "terms": { "field": "state" }, "aggs": { "average_balance": { "avg": { "field": "balance" } } } } } }'

You can nest aggregations inside aggregations arbitrarily to extract pivoted summarizations that you require from your data.

  • Sort on the average balance in descending order:

      curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
      {
        "size": 0,
        "aggs": {
          "group_by_state": {
            "terms": {
              "field": "state",
              "order": {
                "average_balance": "desc"
              }
            },
            "aggs": {
              "average_balance": {
                "avg": {
                  "field": "balance"
                }
              }
            }
          }
        }
      }'
  • Group by age brackets (ages 20-29, 30-39, and 40-49), then by gender, and then finally get the average account balance, per age bracket, per gender:

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_age": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"group_by_gender": {
"terms": {
"field": "gender"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
}
}'

Reference

https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html

相關文章