elasticsearch-dsl筆記


一、elasticsearch安裝

  1. 安裝java1.8以上

  2. 安裝elasticsearch-rtf(https://github.com/medcl/elasticsearch-rtf)

  3. head插件和kibana的安裝

    • headA web front end for an elastic search cluster
    • KibanaKibana is a window into the Elastic Stack. It enables visual exploration and real-time analysis of your data in Elasticsearch

二、Elasticsearch學習

1. elasticsearch概念

    • 集群
    • 節點
    • 分片:將索引划分為多份的能力,允許水平分割和擴展容量,多個分片響應請求,提高性能和吞吐量
    • 副本

2. elasticsearchmysql概念對比

Elasticsearch

Mysql

Index(索引)

數據庫

Type(類型)

Documents(文檔)

Fields

3. 倒排索引

倒排索引源於實際應用中需要根據屬性的值來查找記錄。這種索引表中的每一項都包括一個屬性值和具有該屬性值的各記錄的地址。由於不是由記錄來確定屬性值,而是由屬性值來確定記錄的位置,因而稱為倒排索引(inverted index)。帶有倒排索引的文件我們稱為倒排索引文件,簡稱倒排文件(inverted file)

4. es的文檔、索引的CRUD操作

    • 索引初始化操作
    • 指定分片和副本的數量
    • shards一旦設置不能修改
      1 PUT  lagou
      2 {
      3      “settings”: {
      4              “index”: {
      5                      “number_of_shards”: 5,
      6                      “number_of_replicas”:1
      7          }
      8      }
      9 } 
    •  獲取settings信息
      1 GET  lagou/_settings
      2 GET  _all/_settings
      3 GET  _settings
    • 更新settings信息
      1 PUT  lagou/_settings
      2 {
      3      “number_of_replicas”:2
      4 }
    • 獲取索引信息
      1 GET  _all
      2 GET  lagou
    • 保存一個文檔
       1 PUT  lagou/job/1   --可以不指明id(不指明id,需用POST提交)
       2 {
       3     "title":"python分布式爬蟲開發",
       4     "salary_min":15000,
       5     "city":"北京",
       6     "company":{
       7         "name":"百度",
       8         "company_addr":"北京市軟件園"
       9     },
      10     "publish_date":"2017-4-16",
      11     "comments":15
      12 }
    • 獲取文檔
      1 GET  lagou/job/1
      2 GET  lagou/job/1?_source=title
      3 GET  lagou/job/1?_source=title,city
    • 修改文檔---覆蓋方式
      1 PUT  lagou/job/1
      2 {
      3     Xxx:yyy
      4 }
    •  修改文檔---增量修改方式
      1 POST  lagou/job/1/_update
      2 {
      3     "doc":{
      4         "comments":100
      5     }
      6 }
    • 刪除文檔
      1 DELETE  lagou/job/1
    • 刪除type
      1 POST lagou/job/_delete_by_query?conflicts=proceed
      2 {
      3     "query": {
      4     "match_all": {}
      5   }
      6 }
    • 刪除索引
      1 DELETE  lagou

 5. es的批量操作

    • 原始數據

    • 批量查詢
       1 GET _mget
       2 {
       3   "docs":[
       4     {
       5       "_index":"testdb",
       6       "_type":"job1",
       7       "_id":1
       8     },
       9     {
      10       "_index":"testdb",
      11       "_type":"job2",
      12       "_id":2
      13     }
      14     ]
      15 }

      或者

       1 GET testdb/_mget
       2 {
       3   "docs":[
       4     {
       5       "_type":"job1",
       6       "_id":1
       7     },
       8     {
       9       "_type":"job2",
      10       "_id":2
      11     }
      12     ]
      13 }

      或者

       1 GET testdb/job1/_mget
       2 {
       3   "docs":[
       4     {
       5       "_id":1
       6     },
       7     {
       8       "_id":2
       9     }
      10     ]
      11 }

      或者

      1 GET testdb/job1/_mget
      2 {
      3   "ids":[1,2]
      4 }
    • bulk批量操作
  • 批量導入可以合並多個操作,比如indexdeleteupdatecreate等等。也可以幫助我們從一個索引導入到另一個索引。
  • 命令格式:
    1 action_and_meta_data\n
    2 optional_source\n
    3 action_and_meta_data\n
    4 optional_source\n
    5 ...
    6 action_and_meta_data\n
    7 optional_source\n
  •  例如:
     1 {"index":{"_index":"test1", "_type":"type1", "_id":1}}
     2 {"field1":"value1", ...}
     3 
     4 {"delete":{"_index":"test1", "_type":"type1", "_id":2}}
     5 
     6 {"create":{"_index":"test1", "_type":"type1", "_id":1}}
     7 {"field1":"value1", ...}
     8 
     9 {"update":{"_index":"test1", "_type":"type1", "_id":1}}
    10 {"doc":{"field1":"value1"}}
  • 需要注意的是,每一條數據都由兩行構成(delete除外),其他的命令比如indexcreate都是由元信息行和數據行組成,update比較特殊它的數據行可能是doc也可能是upsert或者script
  • 批量導入
    1 POST  _bulk
    2 {"index":{"_index":"lagou", "_type":"job", "_id":1}}
    3 {"title":"python分布式爬蟲開發", "salary_min":15000, "city":"北京", "company":{"name":"百度", "company_addr":"北京市軟件園"}, "publish_date":"2017-4-16", "comments":15}
    4 {"index":{"_index":"lagou", "_type":"job", "_id":2}}
    5 {"title":"python django開發", "salary_min":30000, "city":"上海", "company":{"name":"騰訊", "company_addr":"北京市軟件園4-1"}, "publish_date":"2017-4-17", "comments":30}

 6. 映射(mapping

創建索引的時候,可以預先定義字段的類型以及相關屬性,mapping是類似於數據庫中的表結構定義,主要作用如下:

  • 定義index下的字段名
  • 定義字段類型,比如數值型、浮點型、布爾型等
  • 定義倒排索引相關的設置,比如是否索引、記錄position

作用:會讓索引建立得更加細致和完善

類型:靜態映射和動態映射

內置類型:

  • String類型:textkeyword
  • 數字類型:long, integer, short, byte, double, float
  • 日期類型:date
  • Bool類型:boolean
  • Binary類型:binary
  • 復雜類型:objectnested
  • Geo類型:geo-pointgeo-shape
  • 專業類型:ipcompetion

 常用屬性:

屬性 描述 適合類型
store

Yes:存儲,no:不存儲,默認no

 all
index  

yes:分析,no:不分析,默認值為true

 string
null_value  

如果字段為空,可以設置一個默認值,比如NA

 all
analyzer  

可以設置索引和搜索時用的分析器,默認使用的是standard分析器,還可以使用whitespacesimpleenglish

 all
 

include_in_all

默認es為每個文檔定義一個特殊域_all,它的作用是讓每個字段被搜索到,,如果不想某個字段被搜索到,可以設置為false

 
 all
format  

時間格式字符串的模式

 date
    • 創建映射
       1 PUT zhilian
       2 {
       3   "mappings": {
       4     "job":{
       5       "properties": {
       6         "title":{
       7           "type":"text"
       8         },
       9         "salary_min":{
      10           "type":"integer"
      11         },
      12         "city":{
      13           "type":"keyword"
      14         },
      15         "company":{
      16           "properties": {
      17             "name":{
      18               "type":"text"
      19             },
      20             "company_addr":{
      21               "type":"text"
      22             },
      23             "employee_count":{
      24               "type":"integer"
      25             }
      26           }
      27         },
      28         "publish_date":{
      29           "type":"date",
      30           "format": "yyyy-MM-dd"
      31         },
      32         "comments":{
      33           "type": "integer"
      34         }
      35       }
      36     }
      37   }
      38 }
    •  獲取mapping
      1 GET zhilian/job/_mapping

 7.查詢

    • 未完待續...


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM