简体   繁体   English

如何在[Javascript + ElasticSearch]中带有条件的文档建立索引

[英]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. 我的程序完成了他的工作,并最终在其中创建了一些数据的.csv。 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"? 现在,例如,如果我想在“ body”中有一个“ if”条件,例如仅在标题为“ Phyton”的情况下存储标题,该怎么办? . Similar question: what if I want to make an iteration having a "for"? 类似的问题:如果我想进行一个“ for”迭代怎么办?

Thank you for helping. 感谢您的帮助。

You can't have if conditions inside body because you are creating a dictionary. 您无法在body内部设置条件,因为您正在创建字典。 You can have a simple ternary operation like 您可以进行简单的三元运算,例如

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

You can't have a for loop either. 您也不能有for循环。

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. 如果您需要使用复杂的条件语句来设置数据结构,则需要先执行该操作,然后再将其传递到client.index函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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