Elasticsearch-mapping

Mapping
  1. 概念:mapping就是ES數(shù)據(jù)字段field的type類型元數(shù)據(jù),ES在創(chuàng)建索引的時候,動態(tài)映射(dynamic mapping) 會自動為不同的啥數(shù)據(jù)指定響應(yīng)的mapping,mapping中包含了字段類型淮韭、搜索方式(精準(zhǔn)匹配和全文檢索)、分詞器等个束。

  2. 查看mapping

    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n8" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 0px; width: inherit; background-position: initial initial; background-repeat: initial initial;"> GET /product/_mapping
    {
    "product" : {
    "mappings" : {
    "properties" : {
    "desc" : {
    "type" : "text",
    "fields" : {
    "keyword" : {
    "type" : "keyword",
    "ignore_above" : 256
    }
    }
    },
    "name" : {
    "type" : "text",
    "fields" : {
    "keyword" : {
    "type" : "keyword",
    "ignore_above" : 256
    }
    }
    },
    "price" : {
    "type" : "long"
    },
    "tags" : {
    "type" : "text",
    "fields" : {
    "keyword" : {
    "type" : "keyword",
    "ignore_above" : 256
    }
    }
    }
    }
    }
    }
    }</pre>

  3. 搜索方式:

    • 精確匹配(exact value):在倒排索引過程中,分詞器會將field作為一個整體創(chuàng)建到索引中聊疲。

    • 全文匹配(full text):分詞播急、近義詞、同義詞售睹、混淆詞桩警、大小寫、詞性昌妹、過濾捶枢、事態(tài)轉(zhuǎn)換等

  4. 動態(tài)映射(dynamic mapping)

    • 常見類型轉(zhuǎn)換:

      數(shù)據(jù) 類型
      "Elasticsearch" text/keyword
      123456 long
      123.123 double
      true/false boolean
      2020-04-12 date
    • 數(shù)字 123456 為何轉(zhuǎn)為long?

      因為es的mapping_type是由JSON分析器檢測數(shù)據(jù)類型飞崖,而JSON沒有隱式類型轉(zhuǎn)換(integer->long float->double)烂叔,故dynamic mapping 會選擇比較寬的數(shù)據(jù)類型。

    • 123.123 為何轉(zhuǎn)為官網(wǎng)為double而實際為float固歪?

      也許為es潛在bug蒜鸡。

ES數(shù)據(jù)類型
  1. 核心數(shù)據(jù)類型

    • 數(shù)字類型:

      1. long、integer牢裳、short逢防、byte、double蒲讯、float忘朝、half_float、scaled_float

      2. 在滿足需求的情況下判帮,盡可能選擇方位小的數(shù)據(jù)類型局嘁。

      3. 浮點(diǎn)類型

        類型 取值范圍
        double 64位雙精度
        float 32位單精度
        half_float 16位半精度
        scaled_float 縮放類型的浮點(diǎn)數(shù)
      4. 對于float、half_float晦墙、scaled_float悦昵,-0.0和+0.0是不同的值,使用term查詢查找-0.0不會匹配+0.0晌畅,同樣range查詢中上邊界是-0.0不會匹配+0.0但指,下邊界是+0.0不會匹配-0.0。

      5. 對于scaled_float,比如價格只需要精確到分枚赡,price為67.34的字段縮放因子為100,存起來就是5734.

      6. 有限考慮使用帶縮放因子的scaled_float浮點(diǎn)類型。

    • 字符串

      1. keyword:適用于索引結(jié)構(gòu)化的字段谓谦,可以用于過濾贫橙、聚合。keyword類型字段只能通過精確度(exact value)搜索到反粥。Id應(yīng)該用keyword卢肃。

      2. text:當(dāng)一個字段是要被全文搜索,比如Email才顿、內(nèi)容莫湘、產(chǎn)品描述,這些字段應(yīng)該使用text類型郑气,設(shè)置text類型后幅垮,字段內(nèi)容會被分析,在生成倒排索引以前尾组,字符串會被分析器分成一個一個詞項忙芒,text類型的字段不用于排序,很少用于聚合讳侨。

      3. 問何text不會創(chuàng)建索引呵萨? 字段數(shù)據(jù)會占用大量空間,尤其是在加載高基數(shù)text字段時跨跨,字段數(shù)據(jù)一旦加載到堆中潮峦,就在該字段生命周期內(nèi)保存在哪里,同樣勇婴,加載字段數(shù)據(jù)是一個昂貴的過程忱嘹,可能導(dǎo)致用戶遇到延遲問題。

      4. 在同一字段中同事具有全文本(text)和 關(guān)鍵字(keyword)版本會很有用耕渴,一個用于全文本搜索德谅,另一個用于聚合和排序。

    • date(時間類型):精確查找(exact value)

    • boolean(布爾類型)

    • 二級制類型(binary)

    • 區(qū)間類型(range):integer_range萨螺、float_range窄做、long_range、double_range慰技、date_range椭盏。

  2. 復(fù)雜類型

    • Object:用于單個JSON對象

    • Nested:用于JSON對象數(shù)組

  3. 地理位置

    • Geo-point:緯度/經(jīng)度積分

    • Geo-shape:用于多邊形等復(fù)雜形狀

  4. 特有類型:

    • IP地址:ip 用于IPv4和IPv6地址

    • Completion:提供自動完成建議

    • Tocken_count:計算字符串中令牌的數(shù)量

    • Murmur3:在索引時計算值的哈希并將其存儲在索引中

    • Annotated-text:索引包含特殊標(biāo)記的文本(通常用于標(biāo)識命名實體)

    • Percolator:接受來自query-dsl的查詢

    • Join:為同一索引內(nèi)的文檔定義父/子關(guān)系

    • Rank features:記錄數(shù)字功能以提高查詢時的點(diǎn)擊率。

    • Dense vector:記錄浮點(diǎn)值的密集向量吻商。

    • Sparse vector:記錄浮點(diǎn)值的稀疏向量掏颊。

    • Search-as-you-type:針對查詢優(yōu)化的文本字段,以實現(xiàn)按需輸入的完成

    • Alias:為現(xiàn)有字段定義別名。

    • Flattened:允許將整個JSON對象索引為單個字段乌叶。

    • Shapeshape 對于任意笛卡爾幾何盆偿。

    • Histogramhistogram 用于百分位數(shù)聚合的預(yù)聚合數(shù)值。

    • keyword當(dāng)所有文檔都具有相同值時的情況的 專業(yè)化准浴。

  5. Array(數(shù)組):在Elasticsearch中事扭,數(shù)組不需要專用的字段數(shù)據(jù)類型。默認(rèn)情況下乐横,任何字段都可以包含零個或多個值求橄,但是,數(shù)組中的所有值都必須具有相同的數(shù)據(jù)類型葡公。

  6. ES 7新增:

    • Date_nanos:date plus 納秒
手工創(chuàng)建mapping fields的mapping只能創(chuàng)建罐农,無法修改。
  1. 分詞

    • 語法

      <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n164" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 0px; width: inherit; background-position: initial initial; background-repeat: initial initial;"> GET /_analyze
      {
      "analyzer": "standard"
      , "text": ["2020-05-20"]
      }</pre>

    • 動態(tài)映射(dynamic mapping)

      • 語法

        <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n170" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 0px; width: inherit; background-position: initial initial; background-repeat: initial initial;"> PUT /dm/_doc/1
        {
        "name": "xiaomi phone",
        "desc": "shouji zhong de zhandouji",
        "count": 123456,
        "price": 123.123,
        "date": "2020-05-20",
        "isdel": false,
        "tags": [
        "xingjiabi",
        "fashao",
        "buka"
        ]
        }</pre>

      • 查看映射

        <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n173" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 0px; width: inherit; background-position: initial initial; background-repeat: initial initial;"> {
        "dm" : {
        "mappings" : {
        "properties" : {
        "count" : {
        "type" : "long"
        },
        "date" : {
        "type" : "date"
        },
        "desc" : {
        "type" : "text",
        "fields" : {
        "keyword" : {
        "type" : "keyword",
        "ignore_above" : 256
        }
        }
        },
        "isdel" : {
        "type" : "boolean"
        },
        "name" : {
        "type" : "text",
        "fields" : {
        "keyword" : {
        "type" : "keyword",
        "ignore_above" : 256
        }
        }
        },
        "price" : {
        "type" : "float"
        },
        "tags" : {
        "type" : "text",
        "fields" : {
        "keyword" : {
        "type" : "keyword",
        "ignore_above" : 256
        }
        }
        }
        }
        }
        }
        }
        ?</pre>

      • *****正排索引(doc_values)催什、倒排索引(index)和fielddata*****

        • ****內(nèi)存:doc_value 和 index 使用的是系統(tǒng)內(nèi)存涵亏,fielddata使用的是jvm內(nèi)存。****

        • ****設(shè)置index=false 則 無法進(jìn)行搜索蒲凶,_source值存在溯乒;設(shè)置doc_values=false 則 無法進(jìn)行聚合;doc_values和index一旦設(shè)置豹爹,則無法修改裆悄,除非重建索引。如果不行重建索引而想聚合的話臂聋,只能設(shè)置field_data=true光稼,以達(dá)到字段聚合目的。fielddata為聚合而生孩等。****

        • ****優(yōu)化:es官方建議艾君,es是基于大量os cache來進(jìn)行緩存以提高性能。不建議用jvm內(nèi)存來進(jìn)行緩存肄方,jvm緩存會導(dǎo)致一定的gc開銷和oom問題冰垄,給jvm更少內(nèi)存,給os cache更大的內(nèi)存权她。 比如64G服務(wù)器虹茶,給jvm最多4-16G內(nèi)存(1/16~~1/4), os cache可以提高doc value和倒排索引的緩存、****

        • ****查詢效率隅要。****

      • mapping parameters

        • index:是否對創(chuàng)建當(dāng)前字段創(chuàng)建索引蝴罪,默認(rèn)為true,如果不創(chuàng)建索引步清,該字段不會通過索引被搜索到要门,但是仍然會在_source元數(shù)據(jù)中展示虏肾。

        • analyzer:指定分析器。

        • boost:對當(dāng)前字段相關(guān)度評分權(quán)重 欢搜,默認(rèn)為1封豪。

        • coerce:是否允許強(qiáng)制類型轉(zhuǎn)換。true:"1" 可以轉(zhuǎn)為為 1炒瘟;fasle:"1" 轉(zhuǎn)為 1 是報錯吹埠。

          <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n185" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 0px; width: inherit; background-position: initial initial; background-repeat: initial initial;"> DELETE coerce
          ?
          PUT coerce
          {
          "mappings": {
          "properties": {
          "number_one": {
          "type": "integer"
          },
          "number_two": {
          "type": "integer",
          "coerce": false
          }
          }
          }
          }
          ?
          PUT coerce/_doc/1
          {
          "number_one": "10"
          }
          ?

          拒絕,因為設(shè)置了false

          PUT coerce/_doc/2
          {
          "number_two": "10"
          }
          ?

          錯誤信息

          {
          "error" : {
          "root_cause" : [
          {
          "type" : "mapper_parsing_exception",
          "reason" : "failed to parse field [number_two] of type [integer] in document with id '2'. Preview of field's value: '10'"
          }
          ],
          "type" : "mapper_parsing_exception",
          "reason" : "failed to parse field [number_two] of type [integer] in document with id '2'. Preview of field's value: '10'",
          "caused_by" : {
          "type" : "illegal_argument_exception",
          "reason" : "Integer value passed as String"
          }
          },
          "status" : 400
          }

          整個mapping設(shè)置強(qiáng)制類型轉(zhuǎn)換

          DELETE coerce
          ?
          PUT coerce
          {
          "settings": {
          "index.mapping.coerce": false
          },
          "mappings": {
          "properties": {
          "number_one": {
          "type": "integer",
          "coerce": true
          },
          "number_two": {
          "type": "integer"
          }
          }
          }
          }
          ?
          PUT coerce/_doc/1
          {
          "number_one": "10"
          }
          ?

          拒絕唧领,因為設(shè)置了false

          PUT coerce/_doc/2
          {
          "number_two": "10"
          } </pre>

        • copy_to:ES就是內(nèi)容拼接,放到一個新字段里雌续,所以索引時間會增加斩个,聚合性能取決于和之前的那個字段比較。

          <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n189" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 0px; width: inherit; background-position: initial initial; background-repeat: initial initial;"> DELETE copy_to
          ?
          PUT copy_to
          {
          "mappings": {
          "properties": {
          "field1": {
          "type": "text",
          "copy_to": "field_all"
          },
          "field2": {
          "type": "text",
          "copy_to": "field_all"
          },
          "field_all": {
          "type": "text"
          }
          }
          }
          }
          ?
          PUT copy_to/_doc/1
          {
          "field1": "field1",
          "field2": "field2"
          }
          ?
          GET copy_to/_search
          ?
          GET copy_to/_search
          {
          "query": {
          "match": {
          "field_all": {
          "query": "field1 field2"
          }
          }
          }
          }</pre>

        • doc_value:為了提升排序和聚合效率驯杜,默認(rèn)為true受啥,如果確定不需要對字段進(jìn)行排序和聚合,也不需要通過腳本訪問字段值鸽心,則可以禁用doc值滚局,以節(jié)省磁盤空間(不支持text 和 annotated_text)

        • dynamic:控制是否可以動態(tài)添加新字段

          1. true:新檢測到的字段將添加到映射中(默認(rèn))。

          2. false:新檢測到的字段將被忽略顽频。這些字段將不會被索引藤肢,因此將無法搜索,但仍會出現(xiàn)在_source返回的匹配項中糯景,這些字段不會被添加到映射中嘁圈,必須顯示添加字段。

          3. strict:如果檢測到新字段蟀淮,則會引發(fā)異常最住,并拒絕文檔,必須將新字段顯示添加到映射中怠惶。

        • eager_global_ordinals:用于聚合的字段上涨缚,優(yōu)化聚合性能。

          1. Frozen indices(凍結(jié)索引):有些索引使用率很高策治,會被保存在的內(nèi)存中脓魏,有些使用率特別低,寧愿在使用的時候重新創(chuàng)建索引通惫,在使用完畢丟棄數(shù)據(jù)轧拄,F(xiàn)rozen indices 的數(shù)據(jù)命中頻率小,不適用與高搜索負(fù)載讽膏,數(shù)據(jù)不會被保存在內(nèi)存中檩电,堆空間占用比普通索引少得多,F(xiàn)rozen indices 是只是讀的, 請求可能是秒級或者分鐘級俐末。

          2. eager_global_ordinals 不適用與Frozen indices

        • enable:是否創(chuàng)建倒排索引料按,可以對字段操作,也可以對索引操作卓箫,如果不創(chuàng)建索引载矿,仍然可以檢索并在_source元數(shù)據(jù)中展示,謹(jǐn)慎使用烹卒,該狀態(tài)無法修改闷盔。

          <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n211" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 0px; width: inherit; background-position: initial initial; background-repeat: initial initial;"> // 操作索引
          PUT my_index
          {
          "mappings": {
          "enabled": false
          }
          }
          // 刪除索引
          DELETE my_index
          // 操作字段
          PUT my_index
          {
          "mappings": {
          "properties": {
          "session_data": {
          "type": "object",
          "enabled": false
          }
          }
          }
          }</pre>

        • term_vector:

        • store:設(shè)置字段是否僅查詢

        • similarity:為字段設(shè)置相關(guān)度算法,支持BM25旅急、claassic(TF-IDF)逢勾、boolean。

        • ****search_analyzer:設(shè)置單獨(dú)的查詢分析器

          <pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n266" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 8px; width: inherit; background-position: initial initial; background-repeat: initial initial;">**DELETE my_index

          PUT my_index
          {
          "settings": {
          "analysis": {
          "filter": {
          "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20
          }
          },
          "analyzer": {
          "autocomplete": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
          "lowercase",
          "autocomplete_filter"
          ]
          }
          }
          }
          },
          "mappings": {
          "properties": {
          "text": {
          "type": "text",
          "analyzer": "autocomplete",
          "search_analyzer": "standard"
          }
          }
          }
          }

          PUT my_index/_doc/1
          {
          "text": "Quick Brown Fox"
          }

          GET my_index/_search
          {
          "query": {
          "match": {
          "text": {
          "query": "Quick Br",
          "operator": "and"
          }
          }
          }
          }**</pre>

        • proterties:除了mapping還可用于object的屬性設(shè)置

        • position_increment_gap:

        • null_value:為null值設(shè)置默認(rèn)值藐吮。"null_value": "NULL"

        • norms:是否禁用評分(在filter和聚合字段上應(yīng)該禁用)溺拱。

        • normalizer:

        • meta:附加元數(shù)據(jù)

        • Index_prefixes:前綴搜索

          1. min_chars:前綴最小長度,>0谣辞,默認(rèn)2(包含)

          2. max_chars:前綴最大長度迫摔,<20,默認(rèn)5(包含)

          3. 代碼用例

            <pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n251" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 8px; width: inherit; background-position: initial initial; background-repeat: initial initial;">PUT /my_index
            {
            "mappings": {
            "properties": {
            "number_one": {
            "type": "text",
            "index_prefixes": {
            "min_chars": 1,
            "max_chars": 10
            }
            }
            }
            }
            }
            </pre>

        • Index_phrases:提升exact_value查詢速度泥从,但是要消耗更多磁盤空間

        • index_options:控制將那些信息添加到反向索引中句占,以進(jìn)行搜索和突出顯示。僅用于text字段躯嫉。 類型有:docs辖众、freqspositionsoffsets和敬。

        • ignore_malformed:忽略類型錯誤

          <pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n236" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 8px; width: inherit; background-position: initial initial; background-repeat: initial initial;">**DELETE my_index

          PUT /my_index
          {
          "mappings": {
          "properties": {
          "number_one": {
          "type": "integer",
          "ignore_malformed": true
          },
          "number_two": {
          "type": "integer"
          }
          }
          }
          }

          雖然有異常 但是不拋出

          PUT my_index/_doc/1
          {
          "text": "Some text value",
          "number_one": "foo"
          }

          GET /my_index/_search
          {
          "query": {
          "match_all": {}
          }
          }

          查詢結(jié)果

          {
          "took" : 0,
          "timed_out" : false,
          "_shards" : {
          "total" : 1,
          "successful" : 1,
          "skipped" : 0,
          "failed" : 0
          },
          "hits" : {
          "total" : {
          "value" : 1,
          "relation" : "eq"
          },
          "max_score" : 1.0,
          "hits" : [
          {
          "_index" : "my_index",
          "_type" : "_doc",
          "_id" : "1",
          "_score" : 1.0,
          "_ignored" : [
          "number_one"
          ],
          "_source" : {
          "text" : "Some text value",
          "number_one" : "foo"
          }
          }
          ]
          }
          }

          數(shù)據(jù)格式不對

          PUT my_index/_doc/2
          {
          "text": "Some text value",
          "number_two": "foo"
          }

          錯誤輸出

          {
          "error" : {
          "root_cause" : [
          {
          "type" : "mapper_parsing_exception",
          "reason" : "failed to parse field [number_two] of type [integer] in document with id '2'. Preview of field's value: 'foo'"
          }
          ],
          "type" : "mapper_parsing_exception",
          "reason" : "failed to parse field [number_two] of type [integer] in document with id '2'. Preview of field's value: 'foo'",
          "caused_by" : {
          "type" : "number_format_exception",
          "reason" : "For input string: "foo""
          }
          },
          "status" : 400
          }** </pre>

        • ****ignore_above:超過長度將被忽略

        • format:格式化

          <pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n230" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 8px; width: inherit; background-position: initial initial; background-repeat: initial initial;">PUT /map
          {
          "mappings": {
          "properties": {
          "date":{
          "type": "date"
          , "format": ["yyyy-MM-dd"]
          }
          }
          }
          }
          </pre>

        • ****fields** :給field創(chuàng)建多字段凹炸,用于不同目的(全文檢索或者聚合分析排序)。 比如text字段keyword昼弟。**

          <pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n227" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 8px; width: inherit; background-position: initial initial; background-repeat: initial initial;">**DELETE fields_test

          給city創(chuàng)建一個keyword

          PUT fields_test
          {
          "mappings": {
          "properties": {
          "city": {
          "type": "text",
          "fields": {
          "raw": {
          "type": "keyword"
          }
          }
          }
          }
          }
          }

          PUT fields_test/_doc/1
          {
          "city": "New York"
          }

          PUT fields_test/_doc/2
          {
          "city": "York"
          }

          size = 0 表示不顯示原始結(jié)果

          GET fields_test/_search
          {
          "query": {
          "match": {
          "city": "york"
          }
          },
          "size": 0,
          "sort": {
          "city.raw": "asc"
          },
          "aggs": {
          "Cities": {
          "terms": {
          "field": "city.raw"
          }
          }
          }
          }**</pre>

        • fielddata:

          1. 文本(text)字段使用查詢時內(nèi)存中的數(shù)據(jù)接口啤它。但我們首次將該字段用于聚合、排序或者在腳本中使用時舱痘,將按需構(gòu)建此數(shù)據(jù)結(jié)構(gòu)变骡,它是通過從磁盤讀取每個字段的整個反向索引,翻轉(zhuǎn)術(shù)語<->文檔關(guān)系并將結(jié)果存儲在JVM堆中的內(nèi)存中來構(gòu)建的芭逝。

          2. fielddata會占用大量堆空間塌碌,尤其是在加載大量的文本字段時。一旦將自擔(dān)加載到堆中旬盯,它在該字段的生命周期將一直保留在哪里台妆。同樣翎猛,加載字段數(shù)據(jù)是一個昂貴的過程,可以導(dǎo)致用戶遇到延遲的情況接剩。這是默認(rèn)情況禁用字段數(shù)據(jù)的原因切厘。

          3. 聚合出錯樣例代碼:

            <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n221" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 0px; width: inherit; background-position: initial initial; background-repeat: initial initial;"> DELETE my_index
            ?
            // 默認(rèn)fielddata為false
            PUT myindex
            {
            "mappings": {
            "properties": {
            "address": {
            "type": "text"
            }
            }
            }
            }

            PUT myindex/_doc/1
            {
            "address": "New York"
            }
            // 聚合
            GET myindex/_search
            {
            "aggs": {
            "arrs_name": {
            "terms": {
            "field": "address"
            }
            }
            }
            }
            ?
            // 聚合出錯,出錯原因為
            {
            "error" : {
            "root_cause" : [
            {
            "type" : "illegal_argument_exception",
            "reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [address] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
            }
            ]
            }
            }</pre>

          4. text類型聚合類型(fielddata=true)

            <pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n224" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 8px; width: inherit; background-position: initial initial; background-repeat: initial initial;">**DELETE myindex
            PUT myindex
            {
            "mappings": {
            "properties": {
            "address": {
            "type": "text",
            "fielddata": true
            }
            }
            }
            }

            PUT myindex/_doc/1
            {
            "address": "New York"
            }

            GET myindex/_search
            {
            "aggs": {
            "arrs_name": {
            "terms": {
            "field": "address"
            }
            }
            }
            }

            // 聚合結(jié)果
            {
            "took" : 2,
            "timed_out" : false,
            "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
            },
            "hits" : {
            "total" : {
            "value" : 1,
            "relation" : "eq"
            },
            "max_score" : 1.0,
            "hits" : [
            {
            "_index" : "myindex",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
            "address" : "New York"
            }
            }
            ]
            },
            "aggregations" : {
            "arrs_name" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
            {
            "key" : "new",
            "doc_count" : 1
            },
            {
            "key" : "york",
            "doc_count" : 1
            }
            ]
            }
            }
            }** </pre>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末懊缺,一起剝皮案震驚了整個濱河市疫稿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌鹃两,老刑警劉巖遗座,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異俊扳,居然都是意外死亡途蒋,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門拣度,熙熙樓的掌柜王于貴愁眉苦臉地迎上來碎绎,“玉大人螃壤,你說我怎么就攤上這事抗果。” “怎么了奸晴?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵冤馏,是天一觀的道長。 經(jīng)常有香客問我寄啼,道長逮光,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任墩划,我火速辦了婚禮涕刚,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘乙帮。我一直安慰自己杜漠,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布察净。 她就那樣靜靜地躺著驾茴,像睡著了一般。 火紅的嫁衣襯著肌膚如雪氢卡。 梳的紋絲不亂的頭發(fā)上锈至,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天,我揣著相機(jī)與錄音译秦,去河邊找鬼峡捡。 笑死击碗,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的棋返。 我是一名探鬼主播延都,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼睛竣!你這毒婦竟也來了晰房?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤射沟,失蹤者是張志新(化名)和其女友劉穎殊者,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體验夯,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡猖吴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了挥转。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片海蔽。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖绑谣,靈堂內(nèi)的尸體忽然破棺而出党窜,到底是詐尸還是另有隱情,我是刑警寧澤借宵,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布幌衣,位于F島的核電站,受9級特大地震影響壤玫,放射性物質(zhì)發(fā)生泄漏豁护。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一欲间、第九天 我趴在偏房一處隱蔽的房頂上張望楚里。 院中可真熱鬧,春花似錦猎贴、人聲如沸班缎。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽吝梅。三九已至,卻和暖如春惹骂,著一層夾襖步出監(jiān)牢的瞬間苏携,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工对粪, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留右冻,地道東北人装蓬。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像纱扭,于是被迫代替她去往敵國和親牍帚。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354