Elasticsearch Index put mapping

Source: Internet
Author: User

The mapping mechanism makes the Elasticsearch index data more flexible, near to the no schema. Mapping can be set when indexing is established, or it can be set later. Later settings can be modified mapping (you cannot modify an existing field property, generally just add a new field) or set mapping to an index that has no mapping. The put mapping operation must be done by the master node because it involves changes to the cluster matedata, and it is closely related to index and type. Modify only specific type for a specific index.

In the Action support analysis we analyzed the abstract types of action, put mapping action belongs to transportmasternodeoperationaction subclass. It implements the Masteroperation method, and each subclass that inherits from Transportmasternodeoperationaction will implement this method according to its own specific function. The implementation here is as follows:

    protected voidMasteroperation (FinalPutmappingrequest request,FinalClusterstate State,FinalActionlistener<putmappingresponse> listener)throwselasticsearchexception {Finalstring[] Concreteindices =clusterservice.state (). MetaData (). Concreteindices (Request.indicesoptions (), request.indices ());
Construction Request Putmappingclusterstateupdaterequest Updaterequest=Newputmappingclusterstateupdaterequest (). AckTimeout (Request.timeout ()). Masternodetimeout (Request.masterN Odetimeout ()). Indices (concreteindices). Type (Request.type ()). Source (Request.source ()). Ignor      Econflicts (Request.ignoreconflicts ()); Call the Putmapping method while passing in a listener metadatamappingservice.putmapping (Updaterequest,NewActionlistener<clusterstateupdateresponse>() {@Override Public voidOnresponse (clusterstateupdateresponse response) {Listener.onresponse (NewPutmappingresponse (response.isacknowledged ())); } @Override Public voidonfailure (Throwable t) {Logger.debug ("Failed to put mappings on indices [{}], type [{}]", T, Concreteindices, Request.type ()); Listener.onfailure (t); } }); }

The above is the implementation of the Masteroperation method Transportputmappingaction, there is not much complex logic and operation. The specific operation is in Matedatamappingservice. Like the previous createindex, put mapping also submits a updatetask to master. All logic is also in the Execute method. This task is basically the same as CreateIndex, and it needs to respond within a given time. Its code is as follows:

  Public voidPutmapping (FinalPutmappingclusterstateupdaterequest request,FinalActionlistener<clusterstateupdateresponse>listener) {//Submit a high-basic updatetask clusterservice.submitstateupdatetask ("Put-mapping [" + request.type () + "]", Priority.high,NewAckedclusterstateupdatetask<clusterstateupdateresponse>(request, listener) {@OverrideprotectedClusterstateupdateresponse Newresponse (Booleanacknowledged) {                return NewClusterstateupdateresponse (acknowledged); } @Override PublicClusterstate Execute (FinalClusterstate currentstate)throwsException {List<String> Indicestoclose =lists.newarraylist (); Try {
An exception must be thrown for index that already exists in the Matadata for(String index:request.indices ()) {if(!currentstate.metadata (). Hasindex (Index)) { Throw NewIndexmissingexception (NewIndex (index)); } } //needs to exist in indices, otherwise it cannot be manipulated. So it's going to be pre-built . for(String index:request.indices ()) {if(Indicesservice.hasindex (index)) {Continue; } FinalIndexmetadata Indexmetadata =currentstate.metadata (). index (index);
Create Indexservice if not present 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.mappe Rservice (). 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 (). Merg E (Request.type (), Indexmetadata.mappings (). Get (Request.type ()). Source (),false); }}//merge update mapping Map<string, documentmapper> newmappers =Newhashmap (); Map<string, documentmapper> existingmappers =Newhashmap ();
Mapping merge for each index for(String index:request.indices ()) {Indexservice Indexservice=Indicesservice.indexservicesafe (index); //try and parse it (no need to add it) so we can bail early in case of parsing exceptionDocumentmapper Newmapper; Documentmapper Existingmapper=Indexservice.mapperservice (). Documentmapper (Request.type ()); if(MapperService.DEFAULT_MAPPING.equals (Request.type ())) {//presence defaultmapping Merge DEFAULT MAPPING //_default_ types does not go through merging, but we do test the new settings. Also don ' t apply the old defaultNewmapper = Indexservice.mapperservice (). Parse (Request.type (),NewCompressedstring (Request.source ()),false); } Else{newmapper= Indexservice.mapperservice (). Parse (Request.type (),NewCompressedstring (Request.source ()), Existingmapper = =NULL); if(Existingmapper! =NULL) { //First , simulateDocumentmapper.mergeresult Mergeresult = Existingmapper.merge (Newmapper, Mergeflags (). Simulate (true)); //If we have conflicts, and we is not supposed to ignore them, the throw an exception if(!request.ignoreconflicts () &&mergeresult.hasconflicts ()) { Throw Newmergemappingexception (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 NewInvalidtypenameexception ("Type name provided does not match Type name within mapping definition"); } if(! MapperService.DEFAULT_MAPPING.equals (MappingType) &&! PercolatorService.TYPE_NAME.equals (MappingType) && mappingtype.charat (0) = = ' _ ') { Throw NewInvalidtypenameexception ("Document mapping type name can ' t start with ' _ '"); } Finalmap<string, mappingmetadata> mappings =Newhashmap (); for(Map.entry<string, documentmapper>Entry:newMappers.entrySet ()) {String Index=Entry.getkey (); //Do the actual merge here on the master, and update the mapping sourceDocumentmapper 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 sourceMappings.put (Index,NewMappingmetadata (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,NewMappingmetadata (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 returnCurrentState; }//Regenerate Matadata Metadata.builder Builder based on mapping update=Metadata.builder (Currentstate.metadata ()); for(String indexName:request.indices ()) {Indexmetadata Indexmetadata=currentstate.metadata (). index (IndexName); if(Indexmetadata = =NULL) { Throw NewIndexmissingexception (NewIndex (IndexName)); } mappingmetadata MAPPINGMD=Mappings.get (IndexName); if(MAPPINGMD! =NULL) {builder.put (Indexmetadata.builder (indexmetadata). putmapping (MAPPINGMD)); } } returnClusterstate.builder (currentstate). MetaData (builder). Build (); } finally { for(String index:indicestoclose) {indicesservice.removeindex (index,"Created for mapping Processing"); } } } }); }

The above is the setup process of mapping, first it is the same as the CREATE index, only the master node can operate, and is submitted as a task to master Secondly, the essence of this is to merge the mapping and index existing or default mapping in the request, and eventually generate a new Matadata update to each node of the cluster.

Summary: The master operation in the cluster, whether it is index or cluster, is ultimately the update process of cluster matadata. These operations can only be performed on master and are tasks that will time out. Put mapping is certainly no exception. The above two sections of the code basically outline the mapping setup process. There is no repetition here. There is one more question that is not related to the merger of mapping. The mapping merger will be used in many places. It is analyzed in detail in the next section.

Elasticsearch Index put mapping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.