es初學

es關鍵詞 
    索引:含有相同屬性的文檔集合
        必須是英文字母小寫,且不含中劃線
    類型: 索引可以定義一個或多個類型,文檔必須屬於一個類型
    文檔:文檔是可以被索引的基本數據單位,是ES中的最小存儲單位
    分片:每個索引都有多個分片,每個分片是一個Lucene索引
        ES索引默認5個分片,分片指定後不可以修改,備份數可以修改。
    備份:拷貝一份分片就完成了分片的備份
    
    問題:爲什麼要有分片和備份
        1、假設索引數據量大,造成硬盤壓力大,搜索速度出現瓶頸,將索引分爲多個分片,分攤壓力,分片也允許用戶進行水平擴展和拆分,以及分佈式的操作,可以提高搜索的效率
        2、主分片失敗或者出現問題時,備份的分片可以代替工作,提高了es的可用性,備份的分片還可以執行搜索操作,分攤搜索的壓力
    
es與mysql對比
        es                         mysql
        索引                    數據庫
        類型                    表
        文檔                    一行數據記錄
        
es 基本用法
    API基本格式 http://<ip>:<port>/<索引>/<類型>/<文檔id>
    常用HTTP動詞 GET/PUT/POST/DELETE
    
創建索引:
 

PUT http://localhost:9200/people
Content-Type:application/json; charset=utf-8
    {
    "settings": {                                        ## 關鍵詞,指定索引配置
        "number_of_shards": 3,                            ## 設置索引分片數
        "number_of_replicas": 1                            ## 設置索引備份數
    },
    "mappings": {                                        ##    索引映射定義
        "man": {                                        ##    類型,mysql表
            "properties": {                                ##    屬性,表字段集合
                "name": {                                ##    名字,相當於mysql字段名稱
                    "type": "text"                        ##  名字類型,mysql字段類型
                },
                "country": {                            ##
                    "type": "keyword"                    ##
                },
                "age": {                                ##
                    "type": "integer"                    ##
                },
                "date": {                                ##
                    "type": "date",                        ##
                    "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"            ##時間格式化
                }
            }
        },
        "woman": {                                        ##新的類型,另一個表
            
        }
    }
}

put http://localhost:9200/people
    {
    "settings": {                                        
        "number_of_shards": 3,                            
        "number_of_replicas": 1                            
    },
    "mappings": {                                        
        "data": {                                        
            "properties": {                                
                "name": {                                
                    "type": "text"                        
                },
                "country": {                            
                    "type": "keyword"                    
                },
                "age": {                                
                    "type": "integer"                    
                },
                "date": {                                
                    "type": "date",                        
                    "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"            
                }
            }
        }
    }
}

數據插入:
    

#指定文檔id插入

 #       put請求:  ip:port/索引/類型/id
        put  http://127.0.0.1:9200/peoper/man/1
        {
           "name":"tom",
           "country":"china",
           "age":30,
           "date":"1987-03-07"
        }

  #  自動產生文檔id插入:插入數據,不指定id,需要用post請求
        #post請求: ip:port/索引/類型    
        post  http://127.0.0.1:9200/people/man
        {
          "name":"超人tom",
          "country":"china",
          "age":40,
          "date":"1977-03-07"
        }

修改數據:
    

#直接修改文檔
 #       post請求:ip:端口/索引/類型/id/_update
        post  http://127.0.0.1:9200/people/man/1/_update
        {
            "doc":{
                "name":"我是誰"
            }
        }
  #  腳本修改文檔
        post  http://127.0.0.1:9200/people/man/1/_update
        {
            "script": {
                "lang": "painless",
                "inline": "ctx._source.age = params.age",
                "params": {
                    "age": 100
                }
            }
        }


刪除數據:
    刪除文檔
        DELETE 請求 ip:端口/索引/類型/id
        
        DELETE http://127.0.0.1:9200/peoper/man/1
        
    刪除索引
        DELETE 請求 ip:端口/索引
        DELETE http://127.0.0.1:9200/peoper
查詢

查詢數據:query,返回查詢數據
    簡單查詢
        get請求: ip:port/index/type/id 
         
        get http://127.0.0.1:9200/peoper/man/1
         
    條件查詢
        post請求:  ip:port/index/_search
        post請求:  ip:port/index/type/_search
        
        查詢全部數據
        post http://127.0.0.1:9200/peoper/_search
         {
            "query":{         ##查詢關鍵詞
                    "match_all":{}    ##    查詢所有數據
                }
         }
         按分頁條件查詢,相當於limit
        post http://127.0.0.1:9200/peoper/_search
         {
            "query":{         ##查詢關鍵詞
                    "match_all":{}    
                },
                "from":1,        ##從哪裏返回
                "size":1        ##返回的數據量,
         }
         
        字段內容模糊查詢,將模糊內容分割,查詢不精確
        post http://127.0.0.1:9200/peoper/_search
         {
            "query":{         ##查詢關鍵詞
                    "match":{    ## 按字段查詢
                        "title":"elasticsearch入門"        ## 查詢title字段中包含elasticsearch和入門的所以數據,相當於模糊查詢,不是整個內容模糊
                    }
                }
            "sort":[    ##排序。默認升序
                {"date":{"order":"desc"}} ##按日期降序
            ]
         }


    聚合查詢
    
        按字數和時間聚合
        post  http://127.0.0.1:9200/peoper/_search
        {
          "aggs":{         ##聚合查詢關鍵詞
             "group_by_word_count":{  ##按字數聚合,名稱自定義
                 "terms":{
                   "field":"word_count"
                 }
             },
             "group_by_publish_date":{      ## 按日期進行聚合
                "terms":{
                   "field":"publish_date"
                }
             }
          }
        }
        
        統計聚合查詢
        post  http://127.0.0.1:9200/peoper/_search
        {
          "aggs":{         ##聚合查詢關鍵詞
             "grades_word_count":{  ##按字數聚合,名稱自定義
                 "stats":{
                   "field":"word_count"
                 }
             }
          }
        }
        返回結果包含:count,min,max,avg,sum
        
        聚合查詢最小值
        post  http://127.0.0.1:9200/peoper/_search
        {
          "aggs":{         ##聚合查詢關鍵詞
             "grades_word_count":{  ##按字數聚合,名稱自定義
                 "min":{
                   "field":"word_count"
                 }
             }
          }
        }
        返回結果包含只有min
    
高級查詢:
    條件查詢
        文本查詢:
        短語匹配(習語匹配) match_phrase:字段內容模糊查詢,按整個內容模糊匹配 
        post http://127.0.0.1:9200/peoper/_search
         {
            "query":{         ##查詢關鍵詞
                    "match_phrase":{    ## 按短語查詢
                        "title":"elasticsearch入門"        ## 查詢title字段中包含elasticsearch和入門的所以數據,相當於模糊查詢
                    }
                }            
         }
         
         多個字段匹配查詢
         post http://127.0.0.1:9200/peoper/_search
         {
            "query":{                 ##查詢關鍵詞
                    "multi_match":{    ## 多字段查詢
                        "query":"elasticsearch"
                        "fields":["outhor","title"]        ## 查詢outhor,title字段中包含elasticsearch的所有數據
                    }
                }            
         }
         
         語法查詢,應用於kibana,數據搜索
         post http://127.0.0.1:9200/peoper/_search
         {
            "query":{                 
                    "query_string":{    ## 語法查詢
                        "query":"(elasticsearch AND 入門) OR Python" ##查詢包含elasticsearch和入門的數據,或查詢
                    }
                }            
         }
         語法查詢,應用於kibana,數據搜索
         post http://127.0.0.1:9200/peoper/_search
         {
            "query":{                 
                    "query_string":{    ## 語法查詢
                        "query":"elasticsearch OR mao" ##在字段title和author中,查詢包含elasticsearch或mao的數據
                        "fields":["title","author"]
                    }
                }            
         }
         
    字段查詢:
        結構化數據查詢
        
        查詢作者爲mao的數據
        post http://127.0.0.1:9200/peoper/_search
         {
            "query":{                 
                    "term":{
                    "author":"mao"   ##按author字段查詢
                }
                }            
         }
         
         查詢字數在1000~2000的數據
         post http://127.0.0.1:9200/peoper/_search
         {
            "query":{                 
                    "range":{
                    "word_count":{        ## 字段名
                        "gte":1000,        ##get greater equals 大於等於
                        "lte":2000        ##less than equals 小於等於
                    }
                }
            }            
         }
         
         查詢時間段數據
         post http://127.0.0.1:9200/peoper/_search
         {
            "query":{                 
                    "range":{
                    "date":{        ## 字段名
                        "gt":"2019-07-01",        ##get greater equals 大於等於
                        "lte":"now"        ##less than equals 小於等於
                    }
                }
            }            
         }
filter 查詢:在查詢過程中,只判斷該文檔是否滿足條件結果只有 yes或者no
        與bool關鍵字結合使用,且可以緩存

        查詢只有1000字的書籍
        post http://127.0.0.1:9200/book/_search
         {
            "query":{                 
                "bool":{
                    "filter":{
                        "term":{
                            "word_count":1000                            
                        }
                    }
                }
            }            
         }
         
    query和fifter的區別

        用作過濾不用做模糊查詢

        對fifter es會用緩存,相對query來說會更快
        
常用查詢

        固定分數查詢,不支持match,只支持filter
        post http://127.0.0.1:9200/book/_search
        {
            "query": {
                "constant_score": {        ##固定分數查詢關鍵字
                    "filter": {
                        "match": {
                            "title": "ElasticSearch"
                        }
                    },
                    "boost":2            ##指定分數
                }
            }
        }
        布爾查詢
        post http://127.0.0.1:9200/book/_search
        {
            "query": {
                "bool": {
                    "should": [     ##should 相當於或查詢,滿足一個即可
                        {
                            "match": {
                                "author":"cc"
                            }
                        },
                        {
                            "match": {
                                "title": "ElasticSearch"
                            }    
                        }
                    ]
                }
            }
        }
        
        多條件查詢
        post http://127.0.0.1:9200/book/_search
        {
            "query": {
                "bool": {
                    "must": [     ##should 相當於與查詢,必須滿足,must_not:與must相反
                        {
                            "match": {
                                "author":"cc"
                            }
                        },
                        {
                            "match": {
                                "title": "ElasticSearch"
                            }    
                        }
                    ],
                    "filter":[
                        {
                            "term":{
                                "word_count":1000
                            }
                        }
                    ]
                }
            }
        }
    
    ...more
    
    
    ## select date,sum(trans_at),sum(trans_count) from trans group by trans_dt
    ##按時間分組,求每組內的筆數和金額
GET trans/data/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "trans_tp": "01"
          }
        },
          {
            "match_phrase": {
              "adjust_id": "44444"
            }
          }
      ]
    }
  }, 
  "from": 0,
  "size": 2000,
    "aggregations": {
        "trans_dt": {
            "terms": {
                "field": "trans_dt" 
            },
            "aggregations": {
                "SUM(trans_count)": {
                    "sum": {
                        "field": "trans_count"
                    }
                },
                "SUM(trans_at)": {
                    "sum": {
                        "field": "trans_at"
                    }
                }
            }
        }
    }
}

GET trans/data/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "trans_tp": "01"
          }
        },
          {
            "match_phrase": {
              "adjust_id": "44444"
            }
          },
          {
            "match_phrase": {
              "activity_id": "00004"
            }
          },
          {
            "match_phrase": {
              "mchnt_cd": "ddddd"
            }
          }
      ]
    }
  }, 
  "from": 0,
  "size": 2000,
    "aggregations": {
        "trans_dt": {
            "terms": {
                "field": "trans_dt" 
            },
            "aggregations": {
                "SUM(trans_count)": {
                    "sum": {
                        "field": "trans_count"
                    }
                },
                "SUM(trans_at)": {
                    "sum": {
                        "field": "trans_at"
                    }
                }
            }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章