小编典典

带弹性包装的R中的for环

elasticsearch

我有一个带有用户ID的userIds(class-integer)的向量:

873611  875908  876399  877630  878295  878794  880618  881646

和一个代码:

aggs <- '{
  "size": 10000, 
  "filter": {
    "term": {
      "user_id": "593586" #<- there will be all user ids
    }
  },
  "sort": [{
    "@timestamp": { "order": "asc" }
  }] 
}'
tablTogether = Search(index="fort", body=aggs, asdf = TRUE)

我需要使用所有用户ID创建此代码的循环。

像这样的东西:

 for (int i = 0; i<userIds.lengths; i++)
{
aggs <- '{
"size": 10000, 
"filter": {
"term": {
  "user_id": "+userIds[i]+" #<- Is it right?
}
},
"sort": [{
"@timestamp": { "order": "asc" }
 }] 
 }'
 tablTogether = Search(index="fort", body=aggs, asdf = TRUE)
 }

如何将此代码翻译为R.Thx以帮助我!!!


阅读 315

收藏
2020-06-22

共1个答案

小编典典

我是elastic软件包的作者-希望我可以提供帮助:/

这是一种使用for循环的方法

aggs <- '{
  "size": 10000, 
  "filter": {
    "term": {
      "user_id": "%s"
    }
  },
  "sort": [{
    "@timestamp": { "order": "asc" }
  }] 
}'
out = list()
for (i in seq_along(users)) {
  out[[i]] <- Search(index = "fort", 
                     body = sprintf(aggs, users[i]), 
                     asdf = TRUE)
}

让我知道是否可行。

2020-06-22