ElasticSearch-basic commands

Original author: Cattle McConaughey

Original address: ElasticSearch command-(Basic article)

Curl is an open source file transfer tool that uses URL syntax to work in the command line mode. Common get/post requests can be easily realized by using curl . Simply think of it as a tool that can access URL under the command line . Common curl parameters: 

  • -X specifies that the http request method has HEAD GET POST PUT DELETE 
  • -d specifies the data to be transferred 
  • -H specifies http request header information 

ElasticSearch's command calls are based on http, and it provides a rich RESTFul API, and uses curl to operate ElasticSearch common commands. From the functional point of view, the commands of ElasticSearch can be divided into 4 categories:

  1. Check the status information of the cluster, node, index, etc.;
  2. Manage clusters, nodes, index data and metadata;
  3. Perform CRUD operations and search operations;
  4. Perform advanced search operations, such as paging, filtering, scripting, faceting, aggregations and other operations;

One, API commands

1. View commands for cluster, node, index and other status information:

1. Check your health

curl -XGET 'localhost:9200/_cat/health?v'

2. View Index

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

3. View nodes

curl -XGET 'localhost:9200/_cat/nodes?v'

Summary: These commands are all GET requests

curl -XGET 'ip:port/_cat/XXX?v'

2. Add, delete, modify and check commands:

The format followed by the command:

curl -X<REST Verb> '<Node>:<Port>/<Index>/<Type>/<ID>'

New modification is a PUT request, deletion is a DELETE request, and query is GET

1. Create Index

pretty Not necessary, in order to make the returned information more beautiful

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

2. Insert document

curl -XPUT 'localhost:9200/customer/car/1?pretty' -d 
        '{
          "name": "YeJingtao"
         }'
  • The type cannot be omitted when inserting the document, it must be specified.
  • The creation of the index can be implicit, if not specified, Elasticsearch will generate a unique ID to index this document. From the perspective of ES design, we are more concerned about the index of the document content, rather than the ID, so ID can be handed over to ES for maintenance in practical applications.

3. View records

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

4. Modification records

curl -XPUT 'localhost:9200/customer/car/1?pretty' -d 
       '{
          "name": "YeJingtao-V2"
        }'

(PUT and POST have the same effect, PUT is idempotent)

5. Delete records

curl -XDELETE 'localhost:9200/customer/car/1?pretty'

6. Delete Index

curl -XDELETE 'localhost:9200/customer'

When the index is deleted, all types and records in the index are cleared.

Two, Bulk batch operation command

In addition to the single operations described above, ES also provides an API called bulk that can perform batch operations , update or delete large quantities of documents and indexes, which greatly improves operation efficiency. The API command format is:

ip:port/index/type/_bulk–data-binary @jsonfile

Jsonfile also has format requirements, which must be one line of description information and one line of document information. Description:

{"index":
    {
        "_index":"stuff_orders",
        "_type":"order_list",
        "_id":903713
    }
}

Among them, index and type are not necessary. They correspond to the index and type in the above API command. Choose one of them. If both exist, the json file shall prevail.

As long as the document information is in standard json format, there is no content requirement, for example:

{
    "account_number":1,
    "balance":39225,
    "firstname":"Amber",
    "lastname":"Duke",
    "age":32,
    "gender":"M",
    "address":"880HolmesLane",
    "employer":"Pyrami",
    "email":"[email protected]",
    "city":"Brogan",
    "state":"IL"
}

Document sample: https://github.com/yejingtao/forblog/tree/master/elasticsearch

command:

curl -XPOST 192.168.226.133:9200/my_index/customer/_bulk?pretty --data-binary @accounts.json

Look back at the status of the index:

With these 1,000 records, we can start the core "search journey" of ES.

3. Query classification:

There are two ways to divide the command format:

  • Pass query parameters via RESTfulrequest API, also called "query-string";
  • By sending REST request body, also known as JSON format.

First, intuitively feel the difference between the two styles. The following two commands are equivalent.

Style 1:

curl -XGET '192.168.226.133:9200/my_index/_search?pretty'

Style 2:

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

The index name and type name can be specified in the url when searching to reduce the scope of the search :

  • / _search : Search all types of all indexes;
  • /my_index/_search: Search all types of my_index index;
  • /students, my_index /_search: search all types of students and my_index indexes;
  • /my _* /_search: Search all types of all indexes whose names start with my_;
  • / my_index/customer/_search: search the customer type of my_index index;
  • / _all /customer/_search: Search all indexed customer types

PS: Once you retrieve your search results, Elasticsearch has completed its mission. It will not maintain any server-side resources or open cursors in your results. This is also a feature of the RESTFul style, and it is destined for ES to return every time The value is limited by default.

Since the JSON format of the REST request body brings too much beauty to developers in terms of readability and flexibility, the JSON format is mainly used in practical applications. This query statement is also called DSL . The DSL is divided into query DSL (query DSL) and filter DSL (filter DSL) due to different principles .

If you use linux curl command to write in DSL format, it is really tiring. Here we use Kibana in ELK combination to help us query ES.

Download link: https://www.elastic.co/downloads/kibana

Follow the official website tutorial:

  1. Download and unzip,
  2. Placement config / kibana.yml,
  3. 执 执 bin / kibana,
  4. Browser visit http://localhost:5601

 

 

Two, Kibana command line

Cluster related queries

# 查询集群健康状态
GET _cluster/health

# 查询所有节点
GET _cat/nodes

# 查询索引及分片的分布
GET _cat/shards

# 查询指定索引分片的分布
GET _cat/shards/order_stpprdinf_2019-12?v

# 查询所有插件
GET _cat/plugins

Index related queries

# 查询所有索引及容量
GET _cat/indices

# 查询索引映射结构
GET my_index/_mapping

# 查询所有索引映射结构    
GET _all

# 查询所有的相同前缀索引
GET my-*/_search

# 查询所有索引模板   
GET _template

# 查询具体索引模板
GET _template/my_template

Index related operations

1. Create an index template

# 创建模板
PUT _template/test_hot_cold_template
{
    "index_patterns": "test_*",
    "settings": {
        "number_of_shards" : 3,
        "index.number_of_replicas": 1
     },
    "mappings": {
      "order": {
          "dynamic": false, 
          "properties": {
              "id": {"type": "long"},
              "name": {"type": "keyword"}
          }
      }    
    },
    "aliases": {
      "test": {}
    }      
}

# 根据模板创建索引并写入数据
POST test_hot_cold-2019-12-01/order
{
  "id":1,
  "name":"cwx"
}

# 删除模板
DELETE _template/order_stpprdinf_template

2. Create index directly

PUT my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "name": {
          "type": "text"
        },
        "blob": {
          "type": "binary"
        }
      }
    }
  }
}

# 增加字段
put my_index/_mapping/doc
{
    "properties": {
      "cwxtest": {"type": "keyword"}
    }
}

# 数据冷备份
PUT _snapshot/my_backup  # my_backup 备份的名称
{
    "type": "order", 
    "settings": {
        "location": "/mount/backups/my_backup" 
    }
}

3. Write index

PUT my_index/doc/1
{
  "name": "Some binary blob",
  "blob": "U29tZSBiaW5hcnkgYmxvYg==" 
}

4. Delete the index

DELETE my-index

5. Modify the index setting

PUT /test_hot_cold-2019-12-01/_settings 
{ 
  "settings": { 
    "index.routing.allocation.require.hotwarm_type": "cold"
  } 
}

DSL

1. Query query

# 1.查询所有
GET _search
{
  "query": {
    "match_all": {}
  }
}

# 2.查询单个索引 的 固定属性
# 精确匹配
GET _search
{
  "query": {
    "term": { "name" : "you" }
  }
}

# 模糊匹配
GET _search
{
  "query": {
    "match": { "name" : "you" }
  }
}
# 范围查找
GET _search
{
  "query": {
    "range": {
        "age":{ "gte" : 15 , "lte" : 25 }
    }
  }
}
 GET indexName/_search {     "query": {           "wildcard":{"relateId":"*672499460503*"}     } }

# 3.功能性查询
# 过滤
GET my_index/_search
{
  "query": {
    "bool": {
      "filter": {
        "term":{"age":1095}
      }
    }
  }
}

# 或 or
GET my - test / _search {
  "query": {
    "bool": {
      "should": [{
        "term": {
          "name": "you"
        }
        }, {
        "match": {
          "age": 20
        }
      }]
    }
  }
}

# 与 AND
GET my-test/_search
{
  "query": {
    "bool": {
      "must" : [{
        "match" : {
          "name" : "you"
        }
      },{
        "range":{
        "age":{
          "from" : 10 , "to" : 20
        } 
        }
      }]
    }
  }
}

# 必须 =
GET my_index/_search
{
  "query": {
    "bool": {
      "must" : {
        "range" : {
          "age" : { "from" : 10, "to" : 20 }
        }
      }
    }
  }
}

# 必须不 not
GET my_index/_search
{
  "query": {
    "bool": {
      "must_not" : {
        "term" : {
          "name" : "you"
        }
      }
    }
  }
}

# 复合查找
GET my_index/_search 
{
"query": {
"bool": {
"should": [{
"match": {
"age": 40
}
}, 
{
"match": {
"age": 20
}
}],
"filter": {
  "match":{
    "name":"you"
  }
}
}
}
}

# 4.索引迁移
# 场景 从A索引 复制到B索引
POST _reindex
{
  "source": {
    "index": "my_index"
  },
  "dest": {
    "index": "new_my_index"
  }
}


# 5.基于查询的删除
POST test-index/_delete_by_query
{
  "query":{
        "term": {
         "cameraId":"00000000002"
        }
  }

}

# 查询
GET test-index/_search
{
  "query":{
        "term": {
         "cameraId":"00000000002"
        }
  }
}

Query by time range:

GET order_stpprdinf_2019-12/_search
{
  "size":10,
  "query":{
    "range":{
      "order_time":{
        "gte":"2019-12-11T00:00:00+08:00",
        "lte":"2019-12-11T23:59:59+08:00"
      } 
    }
  }
}

Delete by condition:

GET order_stpprdinf_2019-12/_delete_by_query?conflicts=proceed
{
  "query":{
    "range":{
      "order_time":{
        "gte":"2019-12-11T00:00:00+08:00",
        "lte":"2019-12-11T23:59:59+08:00"
      } 
    }
  }
}

The above commands can be executed on the Dev Tools control panel of kibana:

Reference link: https://blog.csdn.net/ailice001/article/details/79541816

Guess you like

Origin blog.csdn.net/sanmi8276/article/details/112941215