Elasticsearch 入门认知
https://blog.csdn.net/cnweike/article/details/33736429
优点: 接近实时(NRT), 集群(cluster)分布式 , 节点(node) 节点方式存储数据,单机容量有限,横向扩展 , 主从 分片复制 默认 5分片:5复制 可靠 , nosql 非关系型
Elasticsearch结构认知,可比对MySQL来认知
索引(index) ---> database 库
类型(type) ---> table 表
文档(document) ---> row 行记录
直接操作Elasticsearch的模式
curl -<REST Verb> <Node>:<Port>/<Index>/<Type><ID>
demo:
curl 'localhost:9200/_cat/health?v' // 状态
curl 'localhost:9200/_cat/nodes?v' // 节点
curl 'localhost:9200/_cat/indices?v' // 查看索引
curl -XPUT 'localhost:9200/customer' // 添加索引
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "wpf"
}' // 添加文档 指定id
curl -XPOST 'localhost:9200/customer/external?pretty' -d '
{
"name": "Jane Doe"
}' // 添加文档 未指定id,随机生成
curl -XGET 'localhost:9200/tradedb/ac_busi_trade/3622636?pretty' // 取出文档
curl -XDELETE 'localhost:9200/projectname?pretty' // 删除索引
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
"doc": { "name": "Jane11 Doe" }
}' // 修改文档
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
"script" : "ctx._source.age += 5"
}' // 脚本 修改文档 ctx._source指向当前要被更新的文档。
curl -XDELETE 'localhost:9200/customer/external/2?pretty' // 删除ID为2的文档
curl -XDELETE 'localhost:9200/customer/external/_query?pretty' -d '
{
"query": { "match": { "name": "John" } }
}' // 一次删除符合某个查询条件的多个文档
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
' // 一次bulk操作中索引了两个文档(ID 1 - John Doe and ID 2 - Jane Doe)
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
' // 一个bulk操作中,首先更新第一个文档(ID为1),然后删除第二个文档(ID为2)
curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary @accounts.json // 批量导入记录
格式如: {"index":{"_id":"1"}}
{"name": "John Doe","balance":39225 }
{"index":{"_id":"2"}}
{"name": "Jane Doe","balance":39225 }
curl 'localhost:9200/projectname/_search?q=*&pretty' // 返回bank索引中的所有的文档
等价于
curl -XPOST 'localhost:9200/my_index/_search?pretty' -d '
{
"query": { "match_all": {} }
}'
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
}' // 返回第11到第20个文档
curl -XPOST 'localhost:9200/bank _search?pretty' -d '
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } },
"size": 2
}' // 以账户余额降序排序,最后返前2个文档
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"],
"size": 2
}' // _source中只返回 account_number, balance字段的信息
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "account_number": 20 } }
}' // 返回账户编号为20的文档
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "address": "mill" } }
}' // 返回地址中包含“mill”的所有账户 此时需要完成匹配一个单词 如715 Mill Avenue中,当address中写入值为1,无法匹配到,
它匹配的是整个单词,而不是单个字 不区分大小写
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "address": "mill lane" } }
}' // 返回地址中包含“mill”或者包含“lane”的账户 或查询
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_phrase": { "address": "mill lane" } }
}' // 匹配短语“mill lane”
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}' // 组合查询返回包含“mill”和“lane”的所有的账户 所有的查询都必须为真,这个文档才能够匹配成功 与
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}' // 组合了两个match查询,它返回的是地址中包含“mill”或者“lane”的所有的账户 或
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}' // 返回地址中既不包含“mill”,同时也不包含“lane”的所有的账户信息 not查询
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}' // 返回40岁以上并且不生活在ID(daho)的人的账户 联合
curl -XPOST 'localhost:9200/test_index_1/external/_search?pretty' -d '
{
"query": {
"term": {
"age": "40"
}
}
}
' // 精确查询 年龄为40的
ids 查询
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"query": {
"ids": {
"type": "student",
"values": [
"1",
"2"
]
}
}
}
' // 查询数据id值为1和2 type属性为student的
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"query": {
"prefix": {
"name": "赵"
}
}
}
' // Prefix Query 前缀查询。查找姓【赵】的同学
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"query": {
"range": {
"age": {
"gte": "18", // 表示>=
"lte": "20" // 表示<=
}
}
}
}
' // Range Query 针对date和number类型的数据。查找年龄到18~20岁
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"query": {
"terms": {
"studentNo": [
"1",
"3"
]
}
}
}
' // Terms Query 多词语查询,查找符合词语列表的数据 类似于关系型数据库中的in查询。查找学号为1,3的
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"query": {
"wildcard": {
"name": "*亮"
}
}
}
' // * 代表任意(包括0个)多个字符 Wildcard Query 通配符查询,是简化的正则表达式查询
? 代表任意一个字符
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"query": {
"regexp": {
"address": ".*长沙市.*" // 这里的.号表示任意一个字符
}
}
}
' // Regexp Query查询 正则表达式查询
Filter查询
下面的情况下适合使用filter查询:
yes/no的二元查询
针对精确值进行查询
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"filter": {
"term": {
"name": "诸葛亮",
"_cache" : true // 与query主要是这里的区别,可以设置数据缓存
}
}
}
' // 词语查询 Term Filter
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"filter": {
"bool": {
"must": [
{
"term": {
"classNo": "2"
}
},
{
"term": {
"isLeader": "true"
}
}
]
}
}
}
' // Bool Filter
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"filter": {
"and": [
{
"term": {
"classNo": "2"
}
},
{
"term": {
"isLeader": "true"
}
}
]
}
}
' // And逻辑连接查询
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"filter": {
"or": [
{
"term": {
"classNo": "2"
}
},
{
"term": {
"isLeader": "true"
}
}
]
}
}
' // or 查询
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"filter": {
"exists": {
"field": "address"
}
}
}
' // Exists Filter 存在查询
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"filter": {
"missing": {
"field": "address"
}
}
}
' // 缺失值查询 查询地址不存在
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"filter": {
"prefix": {
"name": "赵"
}
}
} // 前缀查询。
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"filter": {
"range": {
"age": {
"gte": "18",
"lte": "20"
}
}
}
}
' // Range Filter
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"filter": {
"terms": {
"studentNo": [
"1",
"3"
]
}
}
}
'// Terms Filter 多词语查询
curl -XPOST "192.168.1.101:9200/student/student/_search" -d
'
{
"filter": {
"regexp": {
"address": ".*长沙市.*"
}
}
}
' // Regexp Filter 正则表达式查询
搜索结果中的_score字段,这个得分是与我们指定的搜索查询匹配程度的一个相对度量。得分越高,文档越相关,得分越低文档的相关度越低。
过滤器 非常快的执行速度
- 过滤器不会计算相关度的得分,所以它们在计算上更快一些
- 过滤器可以被缓存到内存中,这使得在重复的搜索查询上,其要比相应的查询快出许多。
// TODO 过滤查询
聚合API https://blog.csdn.net/xialei199023/article/details/48298635
聚合API的调用格式如下:
"aggregations" : { // 表示聚合操作,可以使用aggs替代
"<aggregation_name>" : { // 聚合名,可以是任意的字符串。用做响应的key,便于快速取得正确的响应数据。
"<aggregation_type>" : { // 聚合类别,就是各种类型的聚合,如min等
<aggregation_body> // 聚合体,不同的聚合有不同的body
}
[,"aggregations" : { [<sub_aggregation>]+ } ]? // 嵌套的子聚合,可以有0或多个
}
[,"<aggregation_name_2>" : { ... } ]* // 另外的聚合,可以有0或多个
}
度量类型(metric)聚合
桶类型(bucketing)聚合
ela 5.0版本后,text类型字段是被禁止group by
·PUT http://123.123.123.123:9200/index/type/
{
"settings": {
//设置10个分片,理解为类似数据库中的表分区中一个个分区的概念,不知道是否妥当
"number_of_shards": 10
},
"mappings": {
"trades": {
"_id": {
"path": "id"
},
"properties": {
"id": {
"type": "integer",
//id:自增数字
//要求:查询
"store" : true
},
"name": { //名称
"type": "string"
},
"brand": { //品牌: PG,P&G,宝洁集团,宝洁股份,联想集团,联想电脑等
"type": "string"
},
"orderNo": { //订单号 :如ATTS000928732
"type": "string",
"index": "not_analyzed"
},
"description": {
//描述: 2015款玫瑰香型强生婴儿沐浴露,550ml,包邮
"type": "string",
"sort": true
},
"date": {
"type": "date"
},
"city": {
"type": "string"
},
"qty": {// index分词无效
"type": "float"
},
"price": {
//价格: float index无效
"type": "float"
}
}
}
}
}
npm config set registry "https://registry.npm.taobao.org"
npm install -g cnpm --registry=https://registry.npm.taobao.org
https://www.cnblogs.com/hts-technology/p/8477258.html