elasticsearch数据操作02

kibana

数据的存储

检索

1.全文检索,模糊查询
	GET name/doc/_search?q==xx
2.聚合,group by,分组

#添加,修改
POST aa/doc/1
{
	"name":"aa"
}
Get aa/doc/_search

#格式化
POST aa/doc/1?pretty
{
	"name":"aa"
}

#删除
DEL aa/doc/1

#条件
Get aa/doc/_search=first_name

#批量插入
POST aa/doc/_bulk
{
	"index":{
    "_id":1        
    }
}
...
{
    ...
}
...
    
#多文档检索
POST /name/blog/_mget
{
    "ids":"["2","1"]"
}

#GET请求,浏览器请求数据
#POST请求,添加数据
#json数据格式化网站

GET _search
{
  "query": {
    "match_all": {}
  }
}

POST /bbb
GET /bbb

POST bbb/doc/11
{
	"name":"aa"
}
GET bbb/doc/_search?q==name

PUT /student/user/1?pretty
{
  "name": "lhd",
  "sex": "man",
  "age": "18",
  "about": "good good study",
  "interests": [
    "chinese",
    "english"
  ]
}
GET student

#导入数据集
curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary @accounts.json
curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/_bulk?pretty' --data-binary @logs.jsonl

红黑树

docld:文档id,文档的原始信息
TF:单词频率,记录该单词在文档中的出现次数,用于后续的相关计算
position:位置,记录field分词后,单词所在的位置,从0开始
offset:偏移量,记录单词在文档中的开始和结束的位置,用于高亮显示



一、ES数据操作

1.创建索引

#语法:
PUT /<index>

#示例:
PUT /qiudao
PUT zengdao

2.创建数据

1)数据结构

ES存储数据三个必要'构成条件'

#下划线开头的字段不能修改,删除
构成条件 说明
_index 索引(数据存储的地方)
_type 类型(数据对应的类)
_id 数据唯一标识符

2)语法

#创建数据

PUT /<index>/_doc/<_id>
POST /<index>/_doc/
PUT /<index>/_create/<_id>
POST /<index>/_create/<_id>

index:索引名称,如果索引不存在,会'自动创建'
_doc:类型,不存在自动创建,'不能修改'
<_id>:唯一识别符,创建一个数据时,可以'自定义ID',也可以让他'自动生成'

3)使用自定义ID插入数据

PUT /student/user/4
{
  "name":"congtianqi",
  "sex":"male"
}

#该方式可以修改数据
#程序要判断这个id值在不在,所以大数据量的情况下速度相对较慢

4)使用随机ID插入数据

POST /student/user/
{
  "name":"liuxinyu",
  "sex":"fmale"
}

#每次插入的id不一样,确保插入数据的时候不会被覆盖
#该方式可以修改数据
#POST也可以指定id插入数据

5)添加或删除指定字段

POST /student/user/
{
  "id":"1",
  "name":"liuxinyu",
  "sex":"fmale"
}

#注意逗号

{
  "_index" : "student",				#索引
  "_type" : "user",					#数据类型,不能修改
  "_id" : "WOoW3XMBI2H-LxU4o1RE",	 #自动生成的id
  "_version" : 1,					#更新的次数
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

#修改既是覆盖
#elasticseach数据的插入,不用管字段

3.查询数据

1)简单查询

#查看所有索引信息,字段
GET _all
GET /_all

#查看所有索引的数据
GET _all/_search

#查看指定索引信息,字段
GET student

#查看指定索引 的所有数据
GET student/_search

#查看指定 /索引/类型/id 的数据
GET student/user/1

2)条件查询

1>方法一:
GET /student/_search
{
  "query": {
    "term": {
      "age": {
        "value": "18"
      }
    }
  }
}

2>方法二:
GET /student/_search
{
  "query": {
    "term": {
      "age":"18"
    }
  }
}

#term 拆分成一个个词条
3>方法三:
GET /student/_search
{
  "query": {
    "match": {
      "age": "18"
    }
  }
}

#match 作为词条,注意value格式

3)多条件查询

1>must查询 (且)
#bool 多条件查询必须加bool
#must 查询条件必须全部满足
GET /student/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "age": {
              "value": "18"
            }
          }
        },
        {
          "term": {
            "name": {
              "value": "lhd"
            }
          }
        }
      ]
    }
  }
}

2>filter查询
#filter 跟must一样,只不过在数据量很大时,比must查询快一点
GET /student/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "age": {
              "value": "18"
            }
          }
        },
        {
          "term": {
            "name": {
              "value": "lhd"
            }
          }
        }
      ]
    }
  }
}
3>should查询
#should 多条件查询时,查询条件只要有一个满足就可以(或)
GET /student/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "age": {
              "value": "18"
            }
          }
        },
        {
          "term": {
            "name": {
              "value": "lhd"
            }
          }
        }
      ]
    }
  }
}
4>must_not查询(非)
GET /student/user/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "age": {
              "value": "18"
            }
          }
        },
        {
          "term": {
            "name": {
              "value": "syy"
            }
          }
        }
      ]
    }
  }
}
5>must和should结合 (且或非一起使用的情况)
#查询年龄是21岁',或者'年龄是18岁'并且'名字是lhd的数据
#bool多条件查询
GET /student/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "age": {
              "value": "21"
            }
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "age": {
                    "value": "18"
                  }
                }
              },
              {
                "term": {
                  "name": {
                    "value": "lhd"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

#有几个多条件查询,就用几个bool
6>条件范围查询
GET /student/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "age": {
              "gte": 20,
              "lte": 25
            }
          }
        }
      ]
    }
  }
}

#head插件中,对多条件 范围的查询比较方便

4.修改数据

#修改数据时一定要指定id修改
PUT /student/user/1
{
  "name":"song",
  "sex":"fmale",
  "age":"18"
}

#注意,修改数据时,除了要修改的值。其他的值也要带上,因为修改既是覆盖
PUT /student/user/2
{
  "name":"lhd",
  "sex":"man",
  "age":"19"
}

5.删除数据

#删除指定ID数据
DELETE /student/user/4

#删除索引
DELETE /student

#优先在head插件中的动作,选择关闭,过一段时间再删除

三、集群

1.集群

1)集群状态

1.红色:'数据'都不完整
2.黄色:'数据'完整,但是'副本'有问题
3.绿色:'数据'和'副本'全都没有问题

2)节点类型

1.主节点:负责'调度分配数据'
2.数据节点:处理分配到自己的数据

3)分片

1.主分片:'存储'数据,负责'读写'数据
2.副本分片:主分片的'备份',只用于备份

#head插件中,边框粗的是主分片,细的是副分片

2.搭建集群

1)同步时间

2)安装Java环境

3)安装ES

4)配置文件

[root@db01 ~]# grep "^[a-z]" /etc/elasticsearch/elasticsearch.yml
cluster.name: es-cluster
node.name: node-1
path.data: /service/es/data
path.logs: /service/es/logs
bootstrap.memory_lock: true
network.host: 10.0.0.51,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.51", "10.0.0.52"]
discovery.zen.minimum_master_nodes: 2

[root@db02 ~]# grep "^[a-z]" /etc/elasticsearch/elasticsearch.yml
cluster.name: es-cluster
node.name: node-1
path.data: /service/es/data
path.logs: /service/es/logs
bootstrap.memory_lock: true
network.host: 10.0.0.51,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.51", "10.0.0.52"]
discovery.zen.minimum_master_nodes: 2

5)根据配置文件创建目录

6)启动ES

7) 配置别的节点

作业:

1.搭建ES集群
2.建索引,保存组员信息
3.信息包括,姓名,性别,QQ号等

原文地址:https://www.cnblogs.com/syy1757528181/p/13479781.html