最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

elasticsearch索引index之put?mapping的設(shè)置分析

 更新時(shí)間:2022年04月22日 11:39:46   作者:zziawan  
這篇文章主要為大家介紹了elasticsearch索引index之put?mapping的設(shè)置分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

mapping的設(shè)置過(guò)程

mapping機(jī)制使得elasticsearch索引數(shù)據(jù)變的更加靈活,近乎于no schema。mapping可以在建立索引時(shí)設(shè)置,也可以在后期設(shè)置。

后期設(shè)置可以是修改mapping(無(wú)法對(duì)已有的field屬性進(jìn)行修改,一般來(lái)說(shuō)只是增加新的field)或者對(duì)沒(méi)有mapping的索引設(shè)置mapping。

put mapping操作必須是master節(jié)點(diǎn)來(lái)完成,因?yàn)樗婕暗郊簃atedata的修改,同時(shí)它跟index和type密切相關(guān)。修改只是針對(duì)特定index的特定type。

Action support分析中我們分析過(guò)幾種Action的抽象類(lèi)型,put mapping Action屬于TransportMasterNodeOperationAction的子類(lèi)。

put mapping

它實(shí)現(xiàn)了masterOperation方法,每個(gè)繼承自TransportMasterNodeOperationAction的子類(lèi)都會(huì)根據(jù)自己的具體功能來(lái)實(shí)現(xiàn)這個(gè)方法。

這里的實(shí)現(xiàn)如下所示:

protected void masterOperation(final PutMappingRequest request, final ClusterState state, final ActionListener<PutMappingResponse> listener) throws ElasticsearchException {
        final String[] concreteIndices = clusterService.state().metaData().concreteIndices(request.indicesOptions(), request.indices());
      //構(gòu)造request
        PutMappingClusterStateUpdateRequest updateRequest = new PutMappingClusterStateUpdateRequest()
                .ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout())
                .indices(concreteIndices).type(request.type())
                .source(request.source()).ignoreConflicts(request.ignoreConflicts());
      //調(diào)用putMapping方法,同時(shí)傳入一個(gè)Listener
        metaDataMappingService.putMapping(updateRequest, new ActionListener<ClusterStateUpdateResponse>() {
            @Override
            public void onResponse(ClusterStateUpdateResponse response) {
                listener.onResponse(new PutMappingResponse(response.isAcknowledged()));
            }
            @Override
            public void onFailure(Throwable t) {
                logger.debug("failed to put mappings on indices [{}], type [{}]", t, concreteIndices, request.type());
                listener.onFailure(t);
            }
        });
    }

以上是TransportPutMappingAction對(duì)masterOperation方法的實(shí)現(xiàn),這里并沒(méi)有多少?gòu)?fù)雜的邏輯和操作。具體操作在matedataMappingService中。

updateTask響應(yīng)

跟之前的CreateIndex一樣,put Mapping也是向master提交一個(gè)updateTask。所有邏輯也都在execute方法中。這個(gè)task的基本跟CreateIndex一樣,也需要在給定的時(shí)間內(nèi)響應(yīng)。它的代碼如下所示:

public void putMapping(final PutMappingClusterStateUpdateRequest request, final ActionListener&lt;ClusterStateUpdateResponse&gt; listener) {
    //提交一個(gè)高基本的updateTask
        clusterService.submitStateUpdateTask("put-mapping [" + request.type() + "]", Priority.HIGH, new AckedClusterStateUpdateTask&lt;ClusterStateUpdateResponse&gt;(request, listener) {
            @Override
            protected ClusterStateUpdateResponse newResponse(boolean acknowledged) {
                return new ClusterStateUpdateResponse(acknowledged);
            }
            @Override
            public ClusterState execute(final ClusterState currentState) throws Exception {
                List&lt;String&gt; indicesToClose = Lists.newArrayList();
                try {
            //必須針對(duì)已經(jīng)在matadata中存在的index,否則拋出異常
                    for (String index : request.indices()) {
                        if (!currentState.metaData().hasIndex(index)) {
                            throw new IndexMissingException(new Index(index));
                        }
                    }
                    //還需要存在于indices中,否則無(wú)法進(jìn)行操作。所以這里要進(jìn)行預(yù)建
                    for (String index : request.indices()) {
                        if (indicesService.hasIndex(index)) {
                            continue;
                        }
                        final IndexMetaData indexMetaData = currentState.metaData().index(index);
              //不存在就進(jìn)行創(chuàng)建
                        IndexService indexService = indicesService.createIndex(indexMetaData.index(), indexMetaData.settings(), clusterService.localNode().id());
                        indicesToClose.add(indexMetaData.index());
                        // make sure to add custom default mapping if exists
                        if (indexMetaData.mappings().containsKey(MapperService.DEFAULT_MAPPING)) {
                            indexService.mapperService().merge(MapperService.DEFAULT_MAPPING, indexMetaData.mappings().get(MapperService.DEFAULT_MAPPING).source(), false);
                        }
                        // only add the current relevant mapping (if exists)
                        if (indexMetaData.mappings().containsKey(request.type())) {
                            indexService.mapperService().merge(request.type(), indexMetaData.mappings().get(request.type()).source(), false);
                        }
                    }
            //合并更新Mapping
                    Map&lt;String, DocumentMapper&gt; newMappers = newHashMap();
                    Map&lt;String, DocumentMapper&gt; existingMappers = newHashMap();
            //針對(duì)每個(gè)index進(jìn)行Mapping合并
                    for (String index : request.indices()) {
                        IndexService indexService = indicesService.indexServiceSafe(index);
                        // try and parse it (no need to add it here) so we can bail early in case of parsing exception
                        DocumentMapper newMapper;
                        DocumentMapper existingMapper = indexService.mapperService().documentMapper(request.type());
                        if (MapperService.DEFAULT_MAPPING.equals(request.type())) {//存在defaultmapping則合并default mapping
                            // _default_ types do not go through merging, but we do test the new settings. Also don't apply the old default
                            newMapper = indexService.mapperService().parse(request.type(), new CompressedString(request.source()), false);
                        } else {
                            newMapper = indexService.mapperService().parse(request.type(), new CompressedString(request.source()), existingMapper == null);
                            if (existingMapper != null) {
                                // first, simulate
                                DocumentMapper.MergeResult mergeResult = existingMapper.merge(newMapper, mergeFlags().simulate(true));
                                // if we have conflicts, and we are not supposed to ignore them, throw an exception
                                if (!request.ignoreConflicts() &amp;&amp; mergeResult.hasConflicts()) {
                                    throw new MergeMappingException(mergeResult.conflicts());
                                }
                            }
                        }
                        newMappers.put(index, newMapper);
                        if (existingMapper != null) {
                            existingMappers.put(index, existingMapper);
                        }
                    }
                    String mappingType = request.type();
                    if (mappingType == null) {
                        mappingType = newMappers.values().iterator().next().type();
                    } else if (!mappingType.equals(newMappers.values().iterator().next().type())) {
                        throw new InvalidTypeNameException("Type name provided does not match type name within mapping definition");
                    }
                    if (!MapperService.DEFAULT_MAPPING.equals(mappingType) &amp;&amp; !PercolatorService.TYPE_NAME.equals(mappingType) &amp;&amp; mappingType.charAt(0) == '_') {
                        throw new InvalidTypeNameException("Document mapping type name can't start with '_'");
                    }
                    final Map&lt;String, MappingMetaData&gt; mappings = newHashMap();
                    for (Map.Entry&lt;String, DocumentMapper&gt; entry : newMappers.entrySet()) {
                        String index = entry.getKey();
                        // do the actual merge here on the master, and update the mapping source
                        DocumentMapper newMapper = entry.getValue();
                        IndexService indexService = indicesService.indexService(index);
                        if (indexService == null) {
                            continue;
                        }
                        CompressedString existingSource = null;
                        if (existingMappers.containsKey(entry.getKey())) {
                            existingSource = existingMappers.get(entry.getKey()).mappingSource();
                        }
                        DocumentMapper mergedMapper = indexService.mapperService().merge(newMapper.type(), newMapper.mappingSource(), false);
                        CompressedString updatedSource = mergedMapper.mappingSource();
                        if (existingSource != null) {
                            if (existingSource.equals(updatedSource)) {
                                // same source, no changes, ignore it
                            } else {
                                // use the merged mapping source
                                mappings.put(index, new MappingMetaData(mergedMapper));
                                if (logger.isDebugEnabled()) {
                                    logger.debug("[{}] update_mapping [{}] with source [{}]", index, mergedMapper.type(), updatedSource);
                                } else if (logger.isInfoEnabled()) {
                                    logger.info("[{}] update_mapping [{}]", index, mergedMapper.type());
                                }
                            }
                        } else {
                            mappings.put(index, new MappingMetaData(mergedMapper));
                            if (logger.isDebugEnabled()) {
                                logger.debug("[{}] create_mapping [{}] with source [{}]", index, newMapper.type(), updatedSource);
                            } else if (logger.isInfoEnabled()) {
                                logger.info("[{}] create_mapping [{}]", index, newMapper.type());
                            }
                        }
                    }
                    if (mappings.isEmpty()) {
                        // no changes, return
                        return currentState;
                    }
            //根據(jù)mapping的更新情況重新生成matadata
                    MetaData.Builder builder = MetaData.builder(currentState.metaData());
                    for (String indexName : request.indices()) {
                        IndexMetaData indexMetaData = currentState.metaData().index(indexName);
                        if (indexMetaData == null) {
                            throw new IndexMissingException(new Index(indexName));
                        }
                        MappingMetaData mappingMd = mappings.get(indexName);
                        if (mappingMd != null) {
                            builder.put(IndexMetaData.builder(indexMetaData).putMapping(mappingMd));
                        }
                    }
                    return ClusterState.builder(currentState).metaData(builder).build();
                } finally {
                    for (String index : indicesToClose) {
                        indicesService.removeIndex(index, "created for mapping processing");
                    }
                }
            }
        });
    }

以上就是mapping的設(shè)置過(guò)程,首先它跟Create index一樣,只有master節(jié)點(diǎn)才能操作,而且是以task的形式提交給master;其次它的本質(zhì)是將request中的mapping和index現(xiàn)存的或者是default mapping合并,并最終生成新的matadata更新到集群的各個(gè)節(jié)點(diǎn)。

總結(jié)

集群中的master操作無(wú)論是index方面還是集群方面,最終都是集群matadata的更新過(guò)程。而這些操作只能在master上進(jìn)行,并且都是會(huì)超時(shí)的任務(wù)。put mapping當(dāng)然也不例外。上面的兩段代碼基本概況了mapping的設(shè)置過(guò)程。這里就不再重復(fù)了。

這里還有一個(gè)問(wèn)題沒(méi)有涉及到就是mapping的合并。mapping合并會(huì)在很多地方用到。在下一節(jié)中會(huì)它進(jìn)行詳細(xì)分析,更多關(guān)于elasticsearch索引index put mapping設(shè)置的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java多線程編程安全退出線程方法介紹

    Java多線程編程安全退出線程方法介紹

    這篇文章主要介紹了Java多線程編程安全退出線程方法介紹,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • 從零開(kāi)始講解Java微信公眾號(hào)消息推送實(shí)現(xiàn)

    從零開(kāi)始講解Java微信公眾號(hào)消息推送實(shí)現(xiàn)

    微信公眾號(hào)分為訂閱號(hào)和服務(wù)號(hào),無(wú)論有沒(méi)有認(rèn)證,訂閱號(hào)每天都能推送一條消息,也就是每天只能推送一次消息給粉絲,這篇文章主要給大家介紹了關(guān)于Java微信公眾號(hào)消息推送實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • SpringBoot常見(jiàn)get/post請(qǐng)求參數(shù)處理、參數(shù)注解校驗(yàn)及參數(shù)自定義注解校驗(yàn)詳解

    SpringBoot常見(jiàn)get/post請(qǐng)求參數(shù)處理、參數(shù)注解校驗(yàn)及參數(shù)自定義注解校驗(yàn)詳解

    這篇文章主要給大家介紹了關(guān)于SpringBoot常見(jiàn)get/post請(qǐng)求參數(shù)處理、參數(shù)注解校驗(yàn)及參數(shù)自定義注解校驗(yàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • Java如何基于wsimport調(diào)用wcf接口

    Java如何基于wsimport調(diào)用wcf接口

    這篇文章主要介紹了Java如何基于wsimport調(diào)用wcf接口,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • IntelliJ?IDEA的代碼擱置功能實(shí)現(xiàn)

    IntelliJ?IDEA的代碼擱置功能實(shí)現(xiàn)

    本文主要介紹了IntelliJ?IDEA的代碼擱置功能實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Spring Security和自定義filter的沖突導(dǎo)致多執(zhí)行的解決方案

    Spring Security和自定義filter的沖突導(dǎo)致多執(zhí)行的解決方案

    這篇文章主要介紹了Spring Security和自定義filter的沖突導(dǎo)致多執(zhí)行的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • SpringCloud?GateWay網(wǎng)關(guān)示例代碼詳解

    SpringCloud?GateWay網(wǎng)關(guān)示例代碼詳解

    這篇文章主要介紹了SpringCloud?GateWay網(wǎng)關(guān),Spring?cloud?Gateway的功能很多很強(qiáng)大,文中提到了Spring?Cloud?Gateway中幾個(gè)重要的概念,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-04-04
  • Java JDK 1.8 lambda的用法詳解

    Java JDK 1.8 lambda的用法詳解

    這篇文章主要介紹了Java JDK 1.8 lambda的用法詳解,文中給大家提到了jdk 1.8 Lambda 表達(dá)式 遍歷數(shù)組的方法,需要的朋友可以參考下
    2019-09-09
  • 詳解Spring框架入門(mén)

    詳解Spring框架入門(mén)

    這篇文章主要介紹了詳解Spring框架入門(mén),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • springboot注解Aspect實(shí)現(xiàn)方案

    springboot注解Aspect實(shí)現(xiàn)方案

    本文提供一種自定義注解,來(lái)實(shí)現(xiàn)業(yè)務(wù)審批操作的DEMO,不包含審批流程的配置功能。對(duì)springboot注解Aspect實(shí)現(xiàn)方案感興趣的朋友一起看看吧
    2022-01-01

最新評(píng)論

嘉义县| 永靖县| 旬邑县| 聂荣县| 阿拉善左旗| 桦川县| 永州市| 炉霍县| 苍溪县| 洛隆县| 琼海市| 利津县| 邻水| 黎平县| 万宁市| 长丰县| 伊金霍洛旗| 天镇县| 霍邱县| 志丹县| 荔波县| 吉安县| 青浦区| 望谟县| 盐城市| 泾川县| 泸西县| 蒙阴县| 思南县| 南康市| 富川| 南昌县| 博湖县| 新郑市| 西昌市| 凉山| 齐河县| 凯里市| 皋兰县| 拜泉县| 万山特区|