在上文centos7安裝elk之,安裝logstash-6.6.0,同步mysql數據,詳細安裝步驟中,我安裝了,logstash-6.6.0,併成功同步了mysql中的數據。
有了數據,接下來就是玩轉數據,作爲一個新手,各種語句我全都不懂,於是搜着查詢方法在kibana中查詢同步的mysql中的數據。
如圖:
get /details/_search?_source=id,title,readsum
{
"query":{
"match_all": {}
}
}
我想用這個語句查詢出這個索引下的所有數據。(老手不要笑我哦!)
結果看到右側,這裏顯示是137條記錄,但滾動條卻說了實話,明顯沒有這麼多條嘛
於是還懷疑是不是logstash同步數據不對。
於是我數了一下,輸出的數據正好10條,那我就覺得肯定有地方可以做限制,他默認就是輸出10條。
百度了一下,想要多少條得在查詢中制定size,真尷尬
我更改查詢語句
get /details/_search?_source=id,title,readsum
{
"query":{
"match_all": {}
},
"size":100
}
這樣就可以了
爲了方便以後操作elasticsearch方便,我把一些常用的語句記錄在這裏
#添加索引
put /lib
{
"settings":{
"index":{
"number_of_shards":5,
"number_of_replicas":1
}
}
}
#可以簡寫成
put /lib
#查詢所有數據
GET _search
{
"query": {
"match_all": {}
}
}
#查看索引設置
get /xinxiera/_settings
#查看所有索引的配置
get /_all/_settings
#添加文檔,指定id
put /lib/user/1
{
"name":"張三",
"age":18,
"hobbit":"打球"
}
添加文檔,不指定id
post /lib/user
{
"name":"李四",
"age":16,
"hobbit":"睡覺"
}
#修改文檔
post /lib/user/1/_update
{
"doc":{
"age":19
}
}
#刪除索引
delete /lib
#刪除文檔
delete /lib/user/(id)
#查看索引的mapping
get /xinxiera/_mappings
#查詢索引下的文檔,指定查看的字段和需要的數據量(注意size)
get /xinxiera/details/_search?_source=id,title
{
"query":{
"match_all": {}
},
"size":500
}
#在瀏覽器中可以簡寫成
/xinxiera/_search?_source=id,title,readsum&size=500
multi get 批量獲取文檔
get /_mget
{
"docs":[
{
"_index":"lib",
"_type":"user",
"_id":1
},
{
"_index":"lib",
"_type":"user",
"_id":2
},
{
"_index":"lib",
"_type":"user",
"_id":3
}
]
}
#簡化,並且指定字段
get /lib/user/_mget
{
"docs":[
{
"_id":1,
"_source":["name","age"]
},
{
"_id":2
},
{
"_id":3
}
]
}
#在簡化
get /lib/user/_mget
{
"ids":[1,2,3]
}
bulk實現批量操作
#批量添加
post /lib2/books/_bulk
{"index":{"_id":1}}
{"title":"java","price":55}
{"index":{"_id":2}}
{"title":"Html5","price":45}
{"index":{"_id":3}}
{"title":"php","price":55}
#批量操作(增刪改)
post /lib2/books/_bulk
{"delete":{"_index":"lib2","_type":"books","_id":4}}
{"create":{"_index":"tt","_type":"ttt","_id":"100"}}
{"name":"lisi"}
{"index":{"_index":"tt","_type":"ttt"}}
{"name":"zhaosi"}
{"update":{"_index":"lib2","_type":"books","_id":4}}
{"doc":{"price":58}}
手動創建mapping
#手動創建mapping
put /lib6
{
"settings":{
"number_of_shards":3,
"number_of_replicas":0
},
"mappings":{
"books":{
"properties":{
"title":{"type":"text"},
"name":{"type":"text","analyzer":"standard"},
"publish_date":{"type":"date","index":false},
"price":{"type":"double"},
"number":{"type":"integer"}
}
}
}
}
搜索數據
#查找name是lisi的
get /lib3/user/_search?q=name:lisi
獲取名字是zhaoliu的(完全匹配)
get /lib3/user/_search
{
"query":{
"term": {"name":"zhaoliu"}
}
}
#獲取興趣中含有唱歌的,並按照年齡降序排序
get /lib3/user/_search?q=interests:changge&sort=age:desc
term忽略分詞器查找
#只要含有一個關鍵詞則會被匹配
get /lib3/user/_search
{
"query":{
"terms": {
"interests": ["hejiu","changge"]
}
}
}
#from從哪開始,size取幾個
get /lib3/user/_search
{
"from":0,
"size":2,
"query":{
"terms": {
"interests": ["hejiu","changge"]
}
}
}
#查看版本號
get /lib3/user/_search
{
"version":true,
"from":0,
"size":2,
"query":{
"terms": {
"interests": ["hejiu","changge"]
}
}
}
#match 分詞查找
get /lib3/user/_search
{
"query":{
"match": {
"name": "zhaoliu zhaoming"
}
}
}
#multi_match指定多個字段查找
get /lib3/user/_search
{
"query":{
"multi_match": {
"query": "changge",
"fields": ["interests","name"]
}
}
}
#短語匹配,匹配含有此短語的
get /lib3/user/_search
{
"query":{
"match_phrase": {
"interests":"duanlian,changge"
}
}
}
#控制返回的字段(可以使用通配符)
get /lib3/user/_search
{
"_source":["address","name"],
"query":{
"match_phrase": {
"interests":"duanlian,changge"
}
}
}
#包含什麼字段,排除什麼字段
get /lib3/user/_search
{
"_source":{
"includes":["name"],
"excludes":["age"]
},
"query":{
"match_phrase": {
"interests":"duanlian,changge"
}
}
}
#按年齡升序
get /lib3/user/_search
{
"_source":{
"includes":["name"],
"excludes":["age"]
},
"query":{
"match_phrase": {
"interests":"duanlian,changge"
}
},
"sort":[{
"age":{
"order":"asc"
}
}]
}
#前綴匹配
get /lib3/user/_search
{
"query":{
"match_phrase_prefix": {
"name": {
"query":"zhao"
}
}
}
}
#範圍匹配
get /lib3/user/_search
{
"query":{
"range":{
"birthday": {
"from": "1990-10-10",
"to": "2018-05-01"
}
}
}
}
#from包含,to不包含
get /lib3/user/_search
{
"query":{
"range":{
"birthday": {
"from": "1990-10-10",
"to": "2018-05-01",
"include_lower":true,
"include_upper":false
}
}
}
}
#通配符查詢
get /lib3/user/_search
{
"query":{
"wildcard": {
"name":"zhao*"
}
}
}
get /lib3/user/_search
{
"query":{
"wildcard": {
"name":"li?i"
}
}
}
#模糊查詢
get /lib3/user/_search
{
"query":{
"fuzzy": {
"name": "zholiu"
}
}
}
#搜索內容高亮顯示
get /lib3/user/_search
{
"query":{
"match": {
"interests": "changge"
}
},
"highlight":{
"fields":{
"interests":{}
}
}
}
中文分詞相關
前提是需要在elasticsearch中安裝中文分詞器,覺得有必要的朋友可以評論一下,然後我在加上文章
#創建索引,配置mapping,注意選擇中文分詞器
put /lib4
{
"settings":{
"number_of_shards":3,
"number_of_replicas":0
},
"mappings":{
"user":{
"properties":{
"name":{"type":"text","analyzer":"ik_max_word"},
"address":{"type":"text","analyzer":"ik_max_word"},
"age":{"type":"integer"},
"interests":{"type":"text","analyzer":"ik_max_word"},
"birthday":{"type":"date"}
}
}
}
}
其他的都和上面英文的一樣了,暫且寫這些吧,聚合查詢,符合查詢,等用上了在總結