国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

es獲取mapping中所有的字段(回溯)

這篇具有很好參考價值的文章主要介紹了es獲取mapping中所有的字段(回溯)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

????????有時候想看看es中某個索引中所有的字段,直接通過查詢mapping是很難看出所有的字段的,里面包含的屬性太多了。本博文將實現(xiàn)獲取mapping中所有字段的算法。

示例new_user2索引中的mapping值:

{
	"new_user2": {
		"mappings": {
			"properties": {
				"address": {
					"properties": {
						"city": {
							"type": "text",
							"fields": {
								"keyword": {
									"type": "keyword",
									"ignore_above": 256
								}
							}
						},
						"province": {
							"type": "text",
							"fields": {
								"keyword": {
									"type": "keyword",
									"ignore_above": 256
								}
							}
						}
					}
				},
				"age": {
					"type": "long"
				},
				"name": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				}
			}
		}
	}
}

示例代碼:【直接在mapping中獲取】

import json

with open('text.txt', 'r', encoding='utf-8') as f:
    data = f.read()

json_data = json.loads(data)
# print(json_data)

for key, value in json_data.items():
    index = key
    mappings = value


def func(dic):
    res = []

    def traceback(mapping, fields_join):
        if not isinstance(mapping, dict):
            return res.append(fields_join)
        for key, value in mapping.items():
            if fields_join:
                traceback(value, fields_join + '.' + key)
            else:
                traceback(value, key)

    def filter_fields(fields: list):
        fields_lst = []
        for field in fields:
            field = field.replace('mappings.properties.', '').replace('.properties', '')
            # 注意:下面幾個判斷最好替換字符串長的在前面,防止出錯
            if field[-28:] == '.fields.keyword.ignore_above':
                field = field[:-28]
            elif field[-20:] == '.fields.keyword.type':
                field = field[:-20]
            elif field[-15:] == '.fields.keyword':
                field = field[:-15]
            elif field[-5:] == '.type':
                field = field[:-5]
            fields_lst.append(field)
        return list(set(fields_lst))

    traceback(dic, '')
    return filter_fields(res)


ret = func(mappings)
print("index: ", index)
print("fields: ", ret)

運行結(jié)果:

es獲取mapping中所有的字段(回溯)

注意:也可以直接通過查詢語句,直接在es中查詢,通過響應(yīng)的數(shù)據(jù)中獲?。簉es['hits']['hits']。但是,由于查詢出的數(shù)據(jù)未必所有字段都是有數(shù)據(jù)的,所有這種方法不是準(zhǔn)確的。最好還是直接從獲取到的_mapping中直接獲取!文章來源地址http://www.zghlxwxcb.cn/news/detail-512692.html

到了這里,關(guān)于es獲取mapping中所有的字段(回溯)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包