elasticsearch 之curl增删改查语句

1、检查集群状态

curl 'localhost:9200/_cat/health?v'  -u admin:xxxx

2、获取节点信息

curl 'localhost:9200/_cat/nodes?v'  -u admin:xxxx

3、列出所有索引

curl 'localhost:9200/_cat/indices?v'   -u admin:xxxx

4、创建索引

现在我们创建一个名为“kzf”的索引,然后再查看所有的索引
curl -XPUT 'localhost:9200/kzf?pretty'  -u admin:xxxx

5、插入数据到索引

插入一些数据到集群索引。我们必须给ES指定所以的类型。
必须制定数据type:external,ID:1 ,数据主体{"name": "John Doe"}

curl -XPUT 'localhost:9200/kzf/external/1?pretty' -d   '{"name": "John Doe"}'  -u admin:xxxx

6、查询索引数据

1、指定索引类型和索引id
curl -XGET 'localhost:9200/kzf/external/1?pretty'   -u admin:xxxx

2、查询索引下面的所有(注意:如果siez不指定,则默认返回10条数据。)
curl 'localhost:9200/bank/_search?q=*&pretty'
上面示例返回所有bank中的索引数据。其中 q=*  表示匹配索引中所有的数据。
等价于:
  curl -H "Content-Type: application/json"  -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
    "query": { "match_all": {} }
  }'

3、查询指定范围的数据(从第10行开始,往后查询100条数据)
curl -H "Content-Type: application/json" -XPOST  'localhost:9200/fy_smart_news_sentiment_tag/_search?pretty'  -u admin:xxxx -d  
'{ "query": { "match_all": {}} , 
   "from": 10,  
   "size": 100 
}'

4、匹配所有的索引中的数据,按照balance字段降序排序,并且返回前10条(如果不指定size,默认最多返回10条)。
curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -u admin:xxxx -d '
  {
    "query": { "match_all": {} },
    "sort": { "balance": { "order": "desc" } }
  }'


5、下面例子展示如何返回两个字段(account_number balance)
curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -u admin:xxxx -d '
  {
    "query": { "match_all": {} },
    "_source": ["account_number", "balance"]
  }'
View Code

根据字段值查询

1、返回account_number 为20 的数据
curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -u admin:xxxx -d '
  {
    "query": { "match": { "account_number": 20 } }
  }'

2、返回address中包含mill的所有数据::

  curl -H "Content-Type: application/json"  -XPOST 'localhost:9200/bank/_search?pretty' -u admin:xxxx -d '
  {
    "query": { "match": { "address": "mill" } }
  }'

3、返回地址中包含mill或者lane的所有数据:

  curl -H "Content-Type: application/json"  -XPOST 'localhost:9200/bank/_search?pretty' -u admin:xxxx -d '
  {
    "query": { "match": { "address": "mill lane" } }
  }'

4、和上面匹配单个词语不同,下面这个例子是多匹配(match_phrase短语匹配),返回地址中包含短语 “mill lane”的所有数据:

  curl -H "Content-Type: application/json"  -XPOST 'localhost:9200/bank/_search?pretty' -u admin:xxxx -d '
  {
    "query": { "match_phrase": { "address": "mill lane" } }
  }'
View Code

以下是布尔查询,布尔查询允许我们将多个简单的查询组合成一个更复杂的布尔逻辑查询。

这个例子将两个查询组合,返回地址中含有mill和lane的所有记录数据:

  curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
    "query": {
      "bool": {
        "must": [
          { "match": { "address": "mill" } },
          { "match": { "address": "lane" } }
        ]
      }
    }
  }'
  上述例子中,must表示所有查询必须都为真才被认为匹配。

 
相反, 这个例子组合两个查询,返回地址中含有mill或者lane的所有记录数据:

  curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
    "query": {
      "bool": {
        "should": [
          { "match": { "address": "mill" } },
          { "match": { "address": "lane" } }
        ]
      }
    }
  }'
 

  上述例子中,bool表示查询列表中只要有任何一个为真则认为匹配。

 

下面例子组合两个查询,返回地址中既没有mill也没有lane的所有数据:

  curl -H "Content-Type: application/json"  -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
    "query": {
      "bool": {
        "must_not": [
          { "match": { "address": "mill" } },
          { "match": { "address": "lane" } }
        ]
      }
    }
  }'
  上述例子中,must_not表示查询列表中没有为真的(也就是全为假)时则认为匹配。

 

我们可以组合must、should、must_not来实现更加复杂的多级逻辑查询。下面这个例子返回年龄大于40岁、不居住在ID的所有数据:

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

过滤filter(查询条件设置)

下面这个例子使用了布尔查询返回balance在20000到30000之间的所有数据。
  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
      "query": {
        "bool": {
          "must": { "match_all": {} },
          "filter": {
            "range": {
            "balance": {
              "gte": 20000,
              "lte": 30000
            }
          }
        }
      }
    }
  }'
View Code

聚合 Aggregations

下面这个例子: 将所有的数据按照state分组(group),然后按照分组记录数从大到小排序,返回前十条(默认):
  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
    "size": 0,
    "aggs": {
      "group_by_state": {
        "terms": {
           "field": "state"
        }
      }
    }
  }'

下面这个实例按照state分组,降序排序,返回balance的平均值:
  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
    "size": 0,
    "aggs": {
      "group_by_state": {
        "terms": {
          "field": "state"
        },
        "aggs": {
          "average_balance": {
            "avg": {
              "field": "balance"
            }
          }
        }
      }
    }
  }'
View Code

7、删除索引 

curl -XDELETE 'localhost:9200/kzf?pretty'     -u admin:xxxx

8、更新数据

将id为1文档的name字段更新为Jane Doe
curl -XPOST 'localhost:9200/customer/kzf/1/_update?pretty' -u admin:xxxx  -d '
  {
    "doc": { "name": "Jane Doe" }
  }'

9、下面语句将在一个批量操作中执行创建索引:

curl -XPOST 'localhost:9200/kzf/external/_bulk?pretty' -u admin:xxxx   -d '
  {"index":{"_id":"1"}}
  {"name": "John Doe" }
  {"index":{"_id":"2"}}
  {"name": "Jane Doe" }
  '

10、通过文件倒入大量数据

首先把数据写入json文件命名为asdf.json

curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@asdf.json"
原文地址:https://www.cnblogs.com/fanggege/p/14459507.html