es-04-mapping和setting的建立


mapping和setting, 使用java客戶端比較難組裝, 可以使用python或者scala

這兒直接在kibana中進行DSL創建

1, mapping

創建索引的時候, 可以事先對數據進行定義, 告訴es如果索引數據並被搜索

實際上, es會猜測原數據並判斷, 但對一些特俗的字段, 需要指定

類型

類型: text, keyword(棄用)

數據: long, integer, short, byte, double, float

日期: date

bool類型: boolean

binary: binary

復雜類型: object (內置對象, dict), nested (把object 放在數組中)

geo類型, geo-point, geo-shape

專業: ip, competion

 

mapping中, 新的數據 類型相比之前的發生了變化, keyword類型被棄用(v5.x)

屬性: 

store:            是否存儲, 適合all    
index,            是否分析, 適合string
null_value:    字段為空, 可設置默認值 NA, 搜索時可以搜搜, 適合all
analyzer:      分詞器, 默認 standard, 一般設置 ik。    適合。all
include_in_all:     默認es對每個文檔設置一個, 讓每個字段被搜索到, 如果不想搜索到,
                    就可以設置false
format:     格式化

 

 

1, 創建index

PUT test
{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "properties" : {
                "field1" : { "type" : "text" }
            }
        }
    }
}

mapping 和 type 必須那樣寫

2, 刪除

DELETE /twitter

3, 查看

GET /twitter

4, exists

HEAD twitter

5, 索引開關

關閉后的索引, 節省資源, 僅僅維持原數據, 不進行讀寫操作, 還可以在mapping更新的時候進行

POST /my_index/_close

POST /my_index/_open

6, 減少索引

注意: 素數只能縮減為素數

1), 創建一個新的包含更少分片的索引

2), 將segement 從 source index 硬連接到 target source

3)  恢復索引

PUT /my_source_index/_settings
{
  "settings": {
    "index.routing.allocation.require._name": "shrink_node_name", 
    "index.blocks.write": true 
  }
}
POST my_source_index/_shrink/my_target_index

7, split, 擴大分片

過程和減少類似, 對於已有的數據, 可修改副本, 不可修改分片, 因為數據位置需要分片數來確定, 一旦修改, 之前的就無效了

a) 准備用於切分的庫

PUT my_source_index
{
    "settings": {
        "index.number_of_shards" : 1,
        "index.number_of_routing_shards" : 2 
    }
}

b), 切分

POST my_source_index/_split/my_target_index
{
  "settings": {
    "index.number_of_shards": 2
  }
}

c), 創建硬連接

POST my_source_index/_split/my_target_index
{
  "settings": {
    "index.number_of_shards": 5 
  },
  "aliases": {
    "my_search_indices": {}
  }
}

8, 滾動索引: 

rollover index 當原索引太舊或者太老的時候, 可以滾動到新的索引上

9, put mapping

put mapping可以對一個已有的index添加字段等

PUT my_index 
{
  "mappings": {
    "_doc": {
      "properties": {
        "name": {
          "properties": {
            "first": {
              "type": "text"
            }
          }
        },
        "user_id": {
          "type": "keyword"
        }
      }
    }
  }
}

PUT my_index/_mapping/_doc
{
  "properties": {
    "name": {
      "properties": {
        "last": { 
          "type": "text"
        }
      }
    },
    "user_id": {
      "type": "keyword",
      "ignore_above": 100 
    }
  }
}

10, get mapping

GET /twitter/_mapping/_doc

11, 查看某一個字段的屬性

准備mapping

PUT publications
{
  
"mappings": { "_doc": { "properties": { "id": { "type": "text" }, "title": { "type": "text"}, "abstract": { "type": "text"}, "author": { "properties": { "id": { "type": "text" }, "name": { "type": "text" } } } } } } }
GET publications/_mapping/_doc/field/title

或者使用通配符的方式

GET publications/_mapping/_doc/field/a*

12 type exists

HEAD twitter/_mapping/tweet

13 為索引設置別名

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } }
    ]
}

刪除

POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } }
    ]
}

為多個索引創建同一個別名(感覺像聯合索引)

POST /_aliases
{
    "actions" : [
        { "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } }
    ]
}

14 更新索引

POST /twitter/_close

PUT /twitter/_settings
{
  "analysis" : {
    "analyzer":{
      "content":{
        "type":"custom",
        "tokenizer":"whitespace"
      }
    }
  }
}

POST /twitter/_open

15, get 索引

GET /twitter,kimchy/_settings

16 analyze, 進行分詞預計

GET _analyze
{
  "analyzer" : "standard",
  "text" : ["this is a test", "the second text"]
}

explain

GET _analyze
{
  "tokenizer" : "standard",
  "filter" : ["snowball"],
  "text" : "detailed output",
  "explain" : true,
  "attributes" : ["keyword"] 
}

 一個完整的mapping的示例

put macsearch_fileds
{
  "settings": {
    "number_of_shards": "3",
    "number_of_replicas": "1"
  },
  "mappings": {
    "mac": {
      "dynamic": "true",
      "properties": {
        "app_name": {
          "type":     "keyword",
          "index_options": "freqs"
        },
        "content": {
          "type": "text",
          "index_options": "offsets"
        },
        "current_time": {
          "type": "long"
        },
        "mac": {
“store”: “false”,
"type": "keyword", "index_options": "freqs" }, "server_time": { "type": "long" }, "time": { "type": "text", "index_options": "freqs" }, "topic": {
      “stroe”: “true”,
"type": "keyword", "index_options": "freqs",
“analyzer”: “ik_max_word”
} } } } }

dynamic取值: 

    true:默認值,動態添加字段;

        false:忽略新字段;

        strict:碰到陌生字段,拋出異常。

 

index_options : 

index_options 參數用於控制增加到倒排索引的信息,為了搜索和高亮。它可以接受如下設置:

  • docs: 只索引文檔號。可以用於回答詞項是否存在於文檔中的這個域。

  • freqs: 文檔號和詞頻都會被存儲. 詞項頻率越高積分越高。

  • positions: 文檔號,詞項,還有詞的位置被索引。位置可以用於模糊或者短語查詢。

  • offsets: 文檔號,詞項,詞的位置,和開始到結束的字符偏移(詞項映射到原來的字符串)被索引。 偏移提供postings highlighter。

分析字符串域默認是會使用positions,其他域默認使用docs。

 如果有的字段只想索引, 不想存儲, 可以使用 _source

put security_2
{
  "settings": {
    "number_of_shards": "5",
    "number_of_replicas": "1"
  },
  "mappings": {
    "push": {
      "dynamic": "true",
      "_source": {
        "excludes": ["AesPhoneNum", "AesEmail"]
      },
      "properties": {
        "AesPhoneNum": {
          "type": "keyword",
          "store": "false", 
          "index_options": "freqs"
        },
        "AesEmail": {
          "type": "keyword",
          "store": "false", 
          "index_options": "freqs"
        }
      }
    }
  }
}

 

對於有數據的需要更改的mapping

close index
post mapping
open index

這樣可以保證原數據不丟, 但執行過程中會丟掉執行過程的1-2s的數據

 2, setting

1), 可以對一個正在運行的集群進行擴容

將原 1 個副本, 擴大為2個副本

PUT /blogs/_settings
{
   "number_of_replicas" : 2
}

但主分片的數量無法更改, 因為分片的位置需要 分片數量來確定, 如果更改, 那么之前存儲的數據將無效

所以不允許修改

 

3, ik分詞器

只需要在需要安裝的位置, 進行添加ik分詞即可

put macsearch_fileds
{
  "settings": {
    "number_of_shards": "3",
    "number_of_replicas": "1"
  },
  "mappings": {
    "mac": {
      "dynamic": "true",
      "properties": {
        "app_name": {
          "type":     "keyword",
          "index_options": "freqs"
        },
        "topic": {
          "type": "keyword", 
          "index_options": "freqs",
          “analyzer”: “ik_max_word”

        }
      }
    }
  }
}

 

所有的分詞器, 如果有index屬性的話, 做分詞的時候會進行大小寫轉換,

而term在查詢的時候, 會原樣查詢, 所以如果有大寫可能會匹配不到

 


免責聲明!

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



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