天天看點

ElasticSearch相關基礎彙總

ElasticSearch相關基礎彙總:
    1、版本演變:  1.x----->2.x----->5.x----->6.x 
    2、mac電腦上安裝elasticsearch指令: brew  install  elasticsearch 或者直接下載下傳壓縮包解壓
    3、mac上檢視已經安裝好的services清單: brew  services list 
    4、實用插件head的安裝:
          (1)、下載下傳:git clone git://github.com/mobz/elasticsearch-head.git
          (2)、cd elasticsearch-head
          (3)、npm install  或者  cnpm install
          (4)、npm run start 或者 cnpm run start
    5、配置elasticsaerch 和 elasticsearch-head 插件
          (1)、停掉elasticsearch-head 
          (2)、停掉elasticsearch:    brew services stop elasticsearch
          (3)、查找elasticsearch.yml檔案位置:     sudo find / -name elasticsearch.yml
          (4)、在elasticsearch.yml檔案末尾添加:    
                    http.cors.enabled: true
                    http.cors.allow-origin:  "*"
                    
          (5)、 啟動es:  brew services start  elasticsearch  啟動elasticsearch-head:  cnpm run start 
          
          
    6、分布式安裝es ,一個master節點,兩個slave節點
          (1)、修改主節點elasticsearch.yml配置檔案,在檔案末尾添加:
                   cluster.name:  biges         #指定es叢集的名字
                   node.name:   master          #指定節點的名字
                   node.master:  true           #指定目前節點就是主節點
                   network.host:  127.0.0.1     #指定為本機位址,端口還是9200
                  
          (2)、修改slave節點的  elasticsearch.yml 檔案:
                   cluster.name:  biges         #指定es叢集的名字    
                   node.name:   slave1/slave2   #指定節點的名字
                   network.host:  127.0.0.1     #指定為本機位址
                   http.port: 9201/9202         #指定slave節點的端口   
                   discovery.zen.ping.unicast.hosts: ["127.0.0.1"]     #發現master位置          
                  
     7、es基礎概念
          (1)、叢集與節點:   master + slave1 + slave2  總體是一個叢集,master、slave1、slave2分别是一個子節點
          (2)、索引:含有相同屬性的文檔的集合
          (3)、類型:索引可以定義一個或者多個類型,文檔必須屬于一個類型
          (4)、文檔:文檔是可以被索引的基本資料機關
          (5)、分片:每個索引都有多個分片,每個分片是一個lucene索引
          (6)、備份:拷貝一份分片就完成了分片的備份
          
    8.es中索引相關:
          (1)、API基本格式: http://<ip>:<port>/<索引>/<類型>/<文檔id>
          (2)、restful  api 的 http 動詞: get/put/post/delete
          (3)、實用postman建立索引很友善。
    9.es的查詢文法
          (1)、簡單查詢:
               127.0.0.1:9200/book/novel/1
          
          (2)、條件查詢
               127.0.0.1:9200/book/_search
               {
                  "query":{
                      #"match_all": {}
                      "match":{
                          "title": "elastcisearch"
                      }
                  },
                  "from": 1,
                  "size": 10,
                  "sort": [
                        {"publish_date": {"order": "desc"}}
                  ]
               }
          
          (3)、聚合查詢
                127.0.0.1:9200/book/_search
                
                {
                    "aggs":{
                         "group_by_word_count": {
                              "terms": {
                                  "field": "word_count"
                              }
                        },
                        
                        "group_by_publish_date": {
                              "terms": {
                                  "field": "publish_date"
                              }
                        }
                    }
                }
                
                或者 
                
                {
                    "aggs":{
                         "group_word_count": {
                              "stats": {
                                  "field": "word_count"
                              }
                        }
                    }
                }
                
                
  10、進階查詢
       子條件查詢:  特定字段查詢所指特定值
       複合條件查詢:  以一定的邏輯組合子條件查詢
      
      (1)、Query
            全文本查詢:  針對文本類型資料
            字段級别查詢:   針對結構化資料,如數字、日期等
            模糊比對:
                127.0.0.1:9200/book/_search
                {
                    "query": {
                        "match": {
                             "author"; "test"
                        }
                    }
                }
                或者
                {
                    "query": {
                        "match_phrase": {
                             "title"; "elasticsearch入門"
                        }
                    }
                }
                多字段比對查詢:
                {
                    "query":{
                        "multi_match": {
                            "query": "瓦利",
                            "fields":  ["author" ,"title"]
                        }
                    }
                }
                文法查詢:
                {
                    "query": {
                        "query_string": {
                            #"query": "(elasticsearch AND  大法) OR python"
                            "query":  "test  OR elasticsearch",
                            "fields":  ["title","author"]
                        }
                    }
                }
                結構化查詢:
                {
                    "query":{
                        "term":{
                            #"word_count": 1000
                            "author":  "test"
                        }
                    }
                }
                範圍查詢:
                {
                    "query": {
                        "range": {
                            "word_count": {
                                "gte": 1000,
                                "lte": 10000
                            }
                        }
                    }
                }
                
              
            
            
      (2)、filter
            在查詢的過程中,隻判斷該文檔是否滿足條件,隻有yes或者no
            127.0.0.1:9200/book/_search
            {
                "query": {
                    "bool": {
                        "filter": {
                            "term":  {
                                "word_count":  1000
                            }
                        }
                    }
                }
            }
      (3)、複合查詢
            固定分數查詢 , 隻支援filter
            127.0.0.1:9200/_search
            {
                "query": {
                    "constant_score": {
                         "filter": {
                            "match": {
                                "title": "elasticsearch"
                            }
                        },
                        "boost": 2
                    }
                }
            }
            
            布爾查詢
            {
                "query": {
                    "bool": {
                        "should": [
                            {
                                "match": {
                                    "author": "test"
                                }
                            },
                            {
                                "match":  {
                                    "title": "elasticsearch"
                                }
                            }
                        ]
                    }
                }
            }
            
            或者:
            
            {
                "query": {
                    "bool": {
                        "must": [
                            {
                                "match": {
                                    "author": "test"
                                }
                            },
                            {
                                "match": {
                                    "title": "elasticsearch"
                                }
                            }
                        ],
                        "filter":[
                            {
                                "term": {
                                    "word_count": 1000
                                }
                            }
                        ]
                    }
                }
            }
            或
            {
                "query": {
                    "bool": {
                        "must_not": {
                            "term": {
                                "author": "test"
                            }
                        }
                    }
                }
            }
            
          
          
    備注:本文是從個人的印象筆記之中整理到部落格文章的,印象筆記寫部落格,整理文章還是欠缺,還得是部落格才行,後期會保持每天一篇的節奏将個人印象筆記中記錄的東西整理到阿裡雲部落格上,阿裡雲部落格上傳圖檔有時候會顯示不出來,尴尬。                  

繼續閱讀