日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

02.elasticsearch-meta-field元字段

發(fā)布時(shí)間:2024/2/28 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 02.elasticsearch-meta-field元字段 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 1. meta-fields
    • 2. 部分meta field詳述
      • 1. _index: 索引名
      • 2. _type: type名
      • 3. _id: doc的id
      • 4. _source : doc文檔的原始json
      • 5. _size: _source的字節(jié)長(zhǎng)度
      • 6. _field_names: 當(dāng)前doc中的所有非空字段
      • 7. _ignored: 這個(gè)記錄了字段設(shè)置忽略格式錯(cuò)誤的字段之后被ignore的情況
      • 8. _routing: 設(shè)置routing value
        • 1. routing的作用
        • 2. _routing的使用方式
        • 3. 可以設(shè)置強(qiáng)制使用routing
        • 4. 使用uniq_id作為自定義routing
      • 9. _meta: application 識(shí)別的專用的meta data

1. meta-fields

es index中的元數(shù)據(jù)主要由這些

  • _index: 索引名
  • _type: type名 這個(gè)在6.0中已經(jīng)過(guò)期了。
  • _id: doc的id
  • _source : doc文檔的原始json
  • _size: _source的字節(jié)長(zhǎng)度
  • _field_names: doc中的所有字段(只要某個(gè)字段再任意一篇文檔中有非null值就算)
  • _ignored: 這個(gè)記錄了字段設(shè)置忽略格式錯(cuò)誤的字段之后被ignore的情況
  • _routing: 設(shè)置routing value
  • _meta: application 識(shí)別的專用的meta data
  • 2. 部分meta field詳述

    1. _index: 索引名

    _index字段是一個(gè)虛擬的字段,他是es自己存儲(chǔ)的,并沒(méi)有存儲(chǔ)在lucene當(dāng)中。
    所以你可以用_index字段做一些term查詢,或者是一些可以被重寫(xiě)為term查詢的查詢,比如match,query_string,simple_query_string query 等。
    但是不能支持正則,前綴查詢等模糊匹配。

    使用樣例

    GET _search {"size": 10, "aggs": {"indices": {"terms": {"field": "_index", "size": 20}}},"sort": [{"_index": { "order": "asc"}}],"script_fields": {"index_name": {"script": {"source": "doc['_index']", "lang": "painless"}}} }

    2. _type: type名

    在6.0中已經(jīng)過(guò)期了

    3. _id: doc的id

    這個(gè) _id字段是es存儲(chǔ)在每個(gè)doc中添加的一個(gè)字段_id,這個(gè)_id字段存儲(chǔ)實(shí)際上只存儲(chǔ)了倒排字典索引,沒(méi)有存儲(chǔ)doc-values,所以不太適合用來(lái)做agg,sort,會(huì)在內(nèi)存中暫用大量空間。
    如果想使用_id字段做agg,sort 建議再建一個(gè)字段存儲(chǔ)_id的值,并開(kāi)啟doc-values 設(shè)置。
    詳細(xì)原理參考這里
    _id字段值適合直接通過(guò)id來(lái)查詢的場(chǎng)景。

    PUT my_index/_doc/1 {"text": "Document with ID 1" }PUT my_index/_doc/2?refresh=true {"text": "Document with ID 2" }GET my_index/_search {"query": {"terms": {"_id": [ "1", "2" ] }} }

    4. _source : doc文檔的原始json

    Elasticsearch中_source字段的主要目的是通過(guò)doc_id讀取該文檔的原始內(nèi)容,所以只需要存儲(chǔ)Store即可。他是query,get 查詢時(shí)默認(rèn)返回的字段。

    _source其實(shí)是名為_(kāi)source的虛擬Store Field。

    Elasticsearch中使用_source字段可以實(shí)現(xiàn)以下功能:

  • Update:部分更新時(shí),需要從讀取文檔保存在_source字段中的原文,然后和請(qǐng)求中的部分字段合并為一個(gè)完整文檔。如果沒(méi)有_source,則不能完成部分字段的Update操作。
  • Rebuild:最新的版本中新增了rebuild接口,可以通過(guò)Rebuild API完成索引重建,過(guò)程中不需要從其他系統(tǒng)導(dǎo)入全量數(shù)據(jù),而是從當(dāng)前文檔的_source中讀取。如果沒(méi)有_source,則不能使用Rebuild API。
  • Script:不管是Index還是Search的Script,都可能用到存儲(chǔ)在Store中的原始內(nèi)容,如果禁用了_source,則這部分功能不再可用。
  • Summary:摘要信息也是來(lái)源于_source字段。
  • 看到這里,突然想到 doc_values和 store之間是不是會(huì)有區(qū)別呢
    看到了在es的社區(qū)中有這樣的答案
    簡(jiǎn)而言之,兩者提供的支持都是通過(guò)doc_id 找到對(duì)應(yīng)的field的內(nèi)容。
    doc_values是為了快速訪問(wèn)而設(shè)計(jì)的,適用于sort,agg等場(chǎng)景中使用,因?yàn)檫@些查詢中需要獲取大量的某個(gè)field的值,所以速度必須快。
    store則是為了優(yōu)化field的存儲(chǔ)而設(shè)計(jì)的,所以他的訪問(wèn)性能沒(méi)有doc_values的好,但是存儲(chǔ)上優(yōu)化比較多,適合存儲(chǔ)大量?jī)?nèi)容,他是用來(lái)在query匹配結(jié)束后用來(lái)返回給client結(jié)果之前獲取對(duì)應(yīng)的field的值,這個(gè)時(shí)候?qū)?yīng)的結(jié)果篩選已經(jīng)結(jié)束,相對(duì)對(duì)filed的訪問(wèn)量很小。
    所以一般情況下是盡量不要在query中對(duì)store field執(zhí)行script的(雖然是可以執(zhí)行的),因?yàn)檫@樣會(huì)比較慢。

    同時(shí),話說(shuō)回來(lái),_source 存儲(chǔ)了所有的文檔內(nèi)容,在磁盤(pán)容量允許的情況下,建議不要關(guān)閉_source

    PUT tweets {"mappings": {"_source": {"enabled": false}} }

    同時(shí),你也可以設(shè)置include,excluds設(shè)置 _source 不存儲(chǔ)哪些數(shù)據(jù)

    PUT logs {"mappings": {"_source": {"includes": ["*.count","meta.*"],"excludes": ["meta.description","meta.other.*"]}} }PUT logs/_doc/1 {"requests": {"count": 10,"foo": "bar" },"meta": {"name": "Some metric","description": "Some metric description", "other": {"foo": "one", "baz": "two" }} }GET logs/_search {"query": {"match": {"meta.other.foo": "one" }} }

    返回

    {"took" : 0,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 0.2876821,"hits" : [{"_index" : "logs","_type" : "_doc","_id" : "1","_score" : 0.2876821,"_source" : {"meta" : {"other" : { },"name" : "Some metric"},"requests" : {"count" : 10}}}]} }

    5. _size: _source的字節(jié)長(zhǎng)度

    _size 存儲(chǔ)了 _source的字節(jié)長(zhǎng)度
    這個(gè)需要安裝 mapper-size plugin

    sudo bin/elasticsearch-plugin install mapper-size

    使用樣例

    PUT my_index {"mappings": {"_size": {"enabled": true}} }PUT my_index/_doc/1 {"text": "This is a document" }PUT my_index/_doc/2 {"text": "This is another document" }GET my_index/_search {"query": {"range": {"_size": { "gt": 10}}},"aggs": {"sizes": {"terms": {"field": "_size", "size": 10}}},"sort": [{"_size": { "order": "desc"}}],"script_fields": {"size": {"script": "doc['_size']" }} }

    6. _field_names: 當(dāng)前doc中的所有非空字段

    是用來(lái)支持 exist查詢的,但是對(duì)于有doc_values或者norm的字段,exist查詢不會(huì)再使用_field_names

    7. _ignored: 這個(gè)記錄了字段設(shè)置忽略格式錯(cuò)誤的字段之后被ignore的情況

    這個(gè)字段主要是配合 ignore_malformed 這個(gè)mapping param 使用
    ignore_malformed 會(huì)讓一個(gè)和mapping中定義的type不一致的數(shù)據(jù)也寫(xiě)入到索引中去
    對(duì)于一個(gè)field,如果設(shè)置了 "ignore_malformed": true, 則對(duì)于那些類型不符的數(shù)據(jù)只會(huì)存儲(chǔ)到_source中,但是不會(huì)進(jìn)行index,這樣的話整個(gè)doc還是正常寫(xiě)入的,只有當(dāng)前的field無(wú)法進(jìn)行搜索。

    如果一個(gè)doc因?yàn)槟硞€(gè)field觸發(fā)了ignore_malformed 規(guī)則,會(huì)產(chǎn)生相應(yīng)的標(biāo)記,對(duì)應(yīng)的doc中會(huì)被添加進(jìn)去一個(gè) _ignore的field,然后就可以根據(jù)這個(gè)來(lái)查找了。

    PUT my_index {"mappings" : {"properties" : {"requests" : {"type" : "text"},"number":{"type": "integer","ignore_malformed": true}}} }PUT my_index/_doc/1 {"requests":"hahaha","number":"5"}PUT my_index/_doc/2 {"requests":"hahaha","number":6}PUT my_index/_doc/3 {"requests":"hahaha","number":"a"}

    這三個(gè)doc的寫(xiě)入都不會(huì)報(bào)錯(cuò),第一個(gè)會(huì)按照數(shù)字存儲(chǔ)。

    然后可以進(jìn)行下面的查詢

    GET my_index/_search {"query": {"exists": {"field": "_ignored"}} }返回{"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" : "3","_score" : 1.0,"_ignored" : ["number"],"_source" : {"requests" : "hahaha","number" : "a"}}]} }

    可以看到該文檔有一個(gè)_ignored 字段
    即使通過(guò)match_all的查詢也是可以看到這個(gè)字段的。只有那些有malformed 字段的才會(huì)有這個(gè)字段。

    8. _routing: 設(shè)置routing value

    1. routing的作用

    這個(gè)字段很重要,每個(gè)doc會(huì)被發(fā)送到哪個(gè)shard其實(shí)都是有計(jì)算規(guī)則的。
    默認(rèn)的計(jì)算規(guī)則為

    routing_factor = num_routing_shards / num_primary_shards shard_num = (hash(_routing) % num_routing_shards) / routing_factor

    num_routing_shards is the value of the index.number_of_routing_shards index setting.
    num_primary_shards is the value of the index.number_of_shards index setting.

    _routing 的默認(rèn)值是doc 的 _id 字段,也就是每個(gè)doc進(jìn)來(lái)之后默認(rèn)是按照id進(jìn)行路由分配到不同的shard的。
    你也可以自己制定路由的方式。

    2. _routing的使用方式

    在index,get,update,delete 的時(shí)候都可以指定_routing規(guī)則。

    PUT my_index/_doc/1?routing=user1&refresh=true {"title": "This is a document" }GET my_index/_doc/1?routing=user1GET my_index/_search?routing=user1,user2 {"query": {"match": {"title": "document"}} }

    在query中是可以使用_routing字段的

    GET my_index/_search {"query": {"terms": {"_routing": [ "user1" ] }} }

    3. 可以設(shè)置強(qiáng)制使用routing

    PUT my_index2 {"mappings": {"_routing": {"required": true }} }put操作會(huì)報(bào)錯(cuò)PUT my_index2/_doc/1 {"text": "No routing value provided" }

    4. 使用uniq_id作為自定義routing

    當(dāng)你使用自定的_routing param進(jìn)行index的時(shí)候,索引中所有的doc的_id字段是不做唯一保證的(只能在同一個(gè)shard上面保證),所以你要自己來(lái)控制id在索引中的唯一性。

    這里里面可以回憶一下join field, 父子文檔都要用相同的routing才行,因?yàn)橐鎯?chǔ)到同一個(gè)分片當(dāng)中。

    9. _meta: application 識(shí)別的專用的meta data

    _meta field對(duì)于es來(lái)說(shuō)目前沒(méi)有任何地方用到,只是方便外部業(yè)務(wù)使用方來(lái)記錄一下自己的數(shù)據(jù),而且這個(gè)數(shù)據(jù)只能存儲(chǔ)在mapping中,并不會(huì)存儲(chǔ)在每個(gè)doc當(dāng)中。
    比如下面。

    PUT my_index {"mappings": {"_meta": { "class": "MyApp::User","version": {"min": "1.0","max": "1.3"}}} }GET my_index

    總結(jié)

    以上是生活随笔為你收集整理的02.elasticsearch-meta-field元字段的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。