简体   繁体   中英

Creating an empty index in elasticsearch in node.js

I am using offical elasticsearch npm. In my node app i am creating index using that client as follows

client.create({
   index: orgID, ---------->This is dynamic
   type: "places",
   body:{}
   }, function(error, response){
         if(error){                                    
         } else {                                                           
         }                                                        
   })

I need to PUT MAPPINGs for the above index, So for that i have to create an empty index(index without data) first and then i have to execute put mappings and then i have to put data into index.

So for that i have to create an empty index, but in offical elasticsearch client the above method(create) is written in " POST " method so its expecting an body.

For that i have to give an empty body {} as above. So how can i create an empty index using official javascript client. Please share your ideas.

Another way to go about this is to use index templates . The idea of index templates is to let ES automatically create a missing index whenever needed, ie when you're indexing a document whose index name matches some pattern and also apply a pre-defined mapping (+ eventually aliases) to that new index.

For instance, you can create an index template like this:

curl -XPUT localhost:9200/_template/template_1 -d '{
    "template" : "your_dynamic_index_name_pattern",
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "places" : {
            // mapping definition of your places type
        }
    }
}'

When that's done, anytime you will index a new document (eg using your client.create() example above) whose index name matches the pattern, ES will create the index automatically and also PUT the mapping, before indexing your document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM