Elastic Search(一)

时间:2023-03-08 22:07:27

一. 安装插件

  1. Marvel集群管理

    root@lj-ThinkPad-L460:~# sudo bin/plugin install license
    root@lj-ThinkPad-L460:~# sudo bin/plugin install marvel-agent 访问 http://localhost:9200/_plugin/marvel/
  2. Kibana 4.5.1可视化

    root@lj-ThinkPad-L460:~# wget https://download.elastic.co/kibana/kibana/kibana-4.5.1-linux-x64.tar.gz
    root@lj-ThinkPad-L460:~# vim config/kibana.yml
    Set the elasticsearch.url
    root@lj-ThinkPad-L460:~# ./bin/kibana 运行 访问 http://yourhost.com:5601
  3. 启动es

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

二.快速入门

  1. 管理

    #1.cluster healthy
    curl 'localhost:9200/_cat/health?v'
    #2. nodes in our cluster
    curl 'localhost:9200/_cat/nodes?v'
    #3.list all indices
    curl 'localhost:9200/_cat/indices?v'
  2. customer例子

    #1. create an index named "customer" and then list all the indexes again:
    ➜ ~ curl 'localhost:9200/_cat/indices?v'
    ➜ ~ curl 'localhost:9200/_cat/indices?v' #2. Let’s index a simple customer document into the customer index, "external" type, with an ID of 1
    ➜ ~ curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
    {
    "name": "John Doe"
    }' # ip:port/index name/type/id
    ➜ ~ curl -XGET 'localhost:9200/customer/external/1?pretty'
    {
    "_index" : "customer",
    "_type" : "external",
    "_id" : "1",
    "_version" : 1,
    "found" : true,
    "_source" : {
    "name" : "John Doe" # full json
    }
    } #3. delete the index that we just created
    # curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>
    ➜ ~ curl -XGET 'localhost:9200/customer/external/1?pretty' #4. batch processing
    ➜ ~ curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
    {"index":{"_id":"1"}}
    {"name": "John ss" }
    {"index":{"_id":"2"}}
    {"name": "Jane Doe" }
    '
  3. bank例子

    #1. download json file : https://github.com/bly2k/files/blob/mas
    #2. load file
    ➜ ~ curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"
    ➜ ~ curl 'localhost:9200/_cat/indices?v'
    health status index pri rep docs.count docs.deleted store.size pri.store.size
    yellow open bank 5 1 1000 0 442.1kb 442.1kb
    yellow open .marvel-es-1-2016.06.14 1 1 4816 26 1.9mb 1.9mb
    yellow open .marvel-es-data-1 1 1 4 2 12.2kb 12.2kb
    yellow open .kibana 1 1 1 0 3.1kb 3.1kb
    yellow open customer 5 1 2 0 6.5kb 6.5kb #3. search api
    ➜ ~ curl 'localhost:9200/bank/_search?q=*&pretty'
    {
    "took" : 89, #time in milliseconds for Elasticsearch to execute the search
    "timed_out" : false, #if the search timed out or not
    "_shards" : { #how many shards were searched, as well as a count of the successful/failed searched shards
    "total" : 5,
    "successful" : 5,
    "failed" : 0
    },
    "hits" : { #search results
    "total" : 1000, # total number of documents matching our search criteria
    "max_score" : 1.0,
    "hits" : [ {
    "_index" : "bank",
    "_type" : "account",
    "_id" : "25",
    "_score" : 1.0,
    "_source" : {
    "account_number" : 25,
    "balance" : 40540,
    "firstname" : "Virginia",
    "lastname" : "Ayala",
    "age" : 39,
    "gender" : "F",
    "address" : "171 Putnam Avenue",
    "employer" : "Filodyne",
    "email" : "virginiaayala@filodyne.com",
    "city" : "Nicholson",
    "state" : "PA"
    }
    }, { # instead of passing q=* in the URI, we POST a JSON-style query : Json Query DSL
    ➜ ~ curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
    {
    "query": { "match_all": {} }
    }'
    ➜ ~ curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
    {
    "query": { "match_all": {} },
    "from": 10,
    "size": 10,
    "sort": { "balance": { "order": "desc" } },
    "_source": ["account_number", "balance"] #显示的字段
    }'
    #where条件
    ➜ ~ curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
    {
    "query": { "match": { "account_number": 20 } }
    }' # and or条件查询语法
    ➜ ~ curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
    {
    "query": {
    "bool": {
    "must": [
    { "match": { "address": "mill" } },
    { "match": { "address": "lane" } }
    ]
    }
    }
    }'#"bool":bool型查询 #"must":and "should":or "must_not":neither..nor #范围查询
    ➜ ~ curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
    {
    "query": {
    "bool": {
    "must": { "match_all": {} },
    "filter": {
    "range": {
    "balance": {
    "gte": 20000,
    "lte": 30000
    }
    }
    }
    }
    }
    }' # "gte":大于 "lte":小于 #group by
    #SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC
    ➜ ~ curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
    {
    "size": 0,
    "aggs": {
    "group_by_state": {
    "terms": {
    "field": "state"
    }
    }
    }
    }'