Elasticsearch基本概念 

Elasticsearch基本概念 
Index  --- 数据库 名字必须是小写
Document  Index 里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index。

 

文档索引到Elasticsearch的时候,默认情况下是对所有字段创建倒排索引的(动态mapping解析出来为数字类型、布尔类型的字段除外),某个字段是否生成倒排索引是由字段的index属性控制的,在Elasticsearch 5之前,index属性的取值有三个:

  analyzed:字段被索引,会做分词,可搜索。反过来,如果需要根据某个字段进搜索,index属性就应该设置为analyzed。
  not_analyzed:字段值不分词,会被原样写入索引。反过来,如果某些字段需要完全匹配,比如人名、地名,index属性设置为not_analyzed为佳。
  no:字段不写入索引,当然也就不能搜索。反过来,有些业务要求某些字段不能被搜索,那么index属性设置为no即可。

常用命令

#查看索引
curl -X GET 'http://localhost:9200/_cat/indices?v'

#创建索引
curl -XPUT 'localhost:9200/customer?pretty'

#插入数据
curl -XPUT 'localhost:9200/customer/external/1?pretty' -H 'Content-Type: application/json' -d '{ "name": "John Doe" }'

#修改数据
curl -XPUT 'localhost:9200/customer/external/1?pretty' -H 'Content-Type: application/json' -d '{ "name": "Jane Doe" }'

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

#将id为1数据的name字段更新为Jane Doe同时增加字段age为20
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -H 'Content-Type: application/json' -d '{ "doc": { "name": "Jane Doe", "age": 20 } }'

#通过一些简单的scripts来执行更新。一下语句通过使用script将年龄增加5:
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -H 'Content-Type: application/json' -d '{ "script" : "ctx._source.age += 5" }'


#执行删除Customer中ID为2的数据
curl -XDELETE 'localhost:9200/customer/external/2?pretty'

#删除索引
curl -XDELETE 'localhost:9200/customer?pretty'

#批处理
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -H 'Content-Type: application/json' -d '
  {"index":{"_id":"1"}}
  {"name": "John Doe" }
  {"index":{"_id":"2"}}
  {"name": "Jane Doe" }
  '
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -H 'Content-Type: application/json' -d '
  {"update":{"_id":"1"}}
  {"doc": { "name": "John Doe becomes Jane Doe" } }
  {"delete":{"_id":"2"}}
  '



#健康
curl -X GET 'http://localhost:9200/_cat/health?v'

#集群的节点列表
curl -X GET 'http://localhost:9200/_cat/nodes?v'


#############进阶
#查找 
curl -XGET "http://localhost:9200/game_log-2020-12-13/_search" -H 'Content-Type: application/json' -d'{  "query": {    "match_all": {}  }}'
curl -XGET "http://localhost:9200/game_log-2020-12-13/_search" -H 'Content-Type: application/json' -d'{  "query": {    "match_all": {}  },  "size": 10}'


#查找 
curl -XGET "http://localhost:9200/game_log-2020-12-13/_search" -H 'Content-Type: application/json' -d'{  "query": {    "match_all": {}  }}'
curl -XGET "http://localhost:9200/game_log-2020-12-13/_search" -H 'Content-Type: application/json' -d'{  "query": {    "match_all": {}  },  "size": 10}'
#短语匹配 包含"AAA BBB"的所有数据
curl -XGET "http://localhost:9200/game_log-2020-12-13/_search" -H 'Content-Type: application/json' -d'{  "query": {    "match_phrase": { "type":"AAA BBB" },  },  "size": 100}'

#包含 AAA 或者 BBB的所有数据
curl -XGET "http://localhost:9200/game_log-2020-12-13/_search" -H 'Content-Type: application/json' -d'{  "query": {    "match": {      "type":"AAA BBB"    }  },  "size": 100}'
#参数内容
{
     "query": {
          "match": {
               "type": "AAA BBB"
          }
     },
     "size": 100
}

#bool表示查询
#must表示所有查询必须都为真才被认为匹配
#should表示查询列表中只要有任何一个为真则认为匹配
#must_not表示查询列表中没有为真的(也就是全为假)时则认为匹配。
curl -XGET "http://localhost:9200/game_log-2020-12-13/_search" -H 'Content-Type: application/json' -d'{  "query": {    "bool": {      "must": [        { "match": {            "type":"AAA"          }        },        {          "match": {            "type":"BBB"          }        }      ]    }  },  "size": 100}'

curl -XGET "http://localhost:9200/game_log-2020-12-13/_search" -H 'Content-Type: application/json' -d'{  "query": {    "bool": {      "should": [        { "match": {            "type":"AAA"          }        },        {          "match": {            "type":"BBB"          }        }      ]    }  },  "size": 100}'

curl -XGET "http://localhost:9200/game_log-2020-12-13/_search" -H 'Content-Type: application/json' -d'{  "query": {    "bool": {      "must_not": [        { "match": {            "type":"AAA"          }        },        {          "match": {            "type":"BBB"          }        }      ]    }  },  "size": 100}'
#参数内容
{
     "query": {
          "bool": {
               "must": [
                    {
                         "match": {
                              "type": "AAA"
                         }
                    },
                    {
                         "match": {
                              "type": "BBB"
                         }
                    }
               ]
          }
     },
     "size": 100
}

#指定字段查询
curl -XPOST "http://localhost:9200/game_log-2020-12-13/_search" -H 'Content-Type: application/json' -d'{  "query": {    "match_phrase": {      "type":"AAA BBB"    }  },  "_source": {    "includes": ["account_id", "account_name"]  },   "size": 100}'
curl -XPOST "http://localhost:9200/game_log-2020-12-13/_search" -H 'Content-Type: application/json' -d'{  "query": {    "match_phrase": {      "type":"AAA BBB"    }  },  "_source": {    "includes": ["account_id", "account*"],    "excludes": ["*name"]  },   "size": 100}'


#导入数据集
curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' -H 'Content-Type: application/json' --data-binary "@accounts.json"
curl 'localhost:9200/_cat/indices?v'
#accounts.json内容如下
{ "index": {"_id":1} }
{ "name": "Test1", "data": "This is my test data", "pos" : 1 }
{ "index": {"_id":2} }
{ "name": "Test2", "data": "This is my test data2", "pos" : 2 }
{ "index": {} }
{ "name": "Test3", "data": "This is my test data3" }
{ "index": {} }
{ "name": "Test4", "data": "This is my test data4" }

#返回所有bank中的索引数据。其中 q=*  表示匹配索引中所有的数据
curl 'localhost:9200/bank/_search?q=*&pretty'

#返回字段
curl -XPOST 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d '
	{

    "query": { "match_all": {} },
    "_source": ["data"]
  }'

#返回从3到4的数据。(索引下标从0开始)
curl -XPOST 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d '{ "query": { "match_all": {} }, "from": 2, "size": 2 }'

#按照pos字段降序排序(只能为数字),并且返回前10条(如果不指定size,默认最多返回10条)
curl -XPOST 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d '{ "query": { "match_all": {} }, "sort": { "pos": { "order": "desc" } }}'

#过滤filter(查询条件设置)
curl -XPOST 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d '{ "query": { "bool": { "must": { "match_all": {} }, "filter": { "range":{ "pos" :{"gte":1, "lte":1} } } } } }'

#聚合 Aggregations   (size=0,不显示查询hits)
curl -XPOST 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 0, "aggs":{"group_by_pos":{ "terms":{"field":"pos"} } } }'
SELECT pos, COUNT(*) FROM bank GROUP BY pos ORDER BY COUNT(*) DESC        #类似mysql
curl -XPOST 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 0, "aggs":{"group_by_pos":{ "terms":{"field":"pos"} }, "aggs":{"average_pos": {"avg" : {"field":"pos" } } } } }'

总结如下:

  curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>
  <REST Verb>:REST风格的语法谓词

  <Node>:节点ip

  <port>:节点端口号,默认9200

  <Index>:索引名

  <Type>:索引类型

  <ID>:操作对象的ID号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值