elasticsearch資料操作02
阿新 • • 發佈:2020-08-11
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號等