简体   繁体   中英

How to index a document with a condition inside [Javascript+ElasticSearch]

My program does his stuff and creates at the end a .csv with some data. I want to index a document with these data so I also call a function with something inside like this:

client.index({
index: 'blog',
  type: 'post',
  id: 1,
  body: {
    title: 'JavaScript Everywhere!',
    content: 'It all started when...',
    date: '2013-12-17'
  }
}, function (err, resp) {
  // ...
});

Now, what if for example I wanna have an "if" condition inside "body", for example store the title only if it is "Phyton"? . Similar question: what if I want to make an iteration having a "for"?

Thank you for helping.

You can't have if conditions inside body because you are creating a dictionary. You can have a simple ternary operation like

body {
  title: (t == "Python") ? "Python" : "Not Python"
}

You can't have a for loop either.

If you need to use complex condition statements to setup your data structure than you need to do that first and then pass it into your client.index function.

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