r - Elasticsearch返回一个字段

标签 r elasticsearch

我正在将Elasticsearch与R一起用于数据挖掘。我正在使用莎士比亚数据集。我只想检索字段text_entry的文本

我的查询:

Search(index = 'shakespeare',size=1, fields = "text_entry")

我的结果:
$took
[1] 2

$timed_out
[1] FALSE

$`_shards`
$`_shards`$total
[1] 5

$`_shards`$successful
[1] 5

$`_shards`$failed
[1] 0


$hits
$hits$total
[1] 111396

$hits$max_score
[1] 1

$hits$hits
$hits$hits[[1]]
$hits$hits[[1]]$`_index`
[1] "shakespeare"

$hits$hits[[1]]$`_type`
[1] "line"

$hits$hits[[1]]$`_id`
[1] "4"

$hits$hits[[1]]$`_version`
[1] 1

$hits$hits[[1]]$`_score`
[1] 1

$hits$hits[[1]]$fields
$hits$hits[[1]]$fields$text_entry
$hits$hits[[1]]$fields$text_entry[[1]]
[1] "Find we a time for frighted peace to pant,"

我想要的只是这个:
[1] "Find we a time for frighted peace to pant,"

我尝试过这样的事情:
Search(index = 'shakespeare',size=10)$hits$hits[[1]]$`_source`$text_entry

但是显然它将仅返回第一个。

谢谢。

编辑:
dput(a)
structure(list(took = 2L, timed_out = FALSE, `_shards` = structure(list(
    total = 5L, successful = 5L, failed = 0L), .Names = c("total", 
"successful", "failed")), hits = structure(list(total = 111396L, 
    max_score = 1, hits = list(structure(list(`_index` = "shakespeare", 
        `_type` = "line", `_id` = "4", `_version` = 1L, `_score` = 1, 
        fields = structure(list(text_entry = list("Find we a time for frighted peace to pant,")), .Names = "text_entry")), .Names = c("_index", 
    "_type", "_id", "_version", "_score", "fields")))), .Names = c("total", 
"max_score", "hits"))), .Names = c("took", "timed_out", "_shards", 
"hits"))

最佳答案

我可以存储查询结果,然后遍历查询并打印我想要的内容:

max <- count(index = 'shakespeare')
s <- Search(index = 'shakespeare',size=10)
for (i in 1:10) {
  cat(s$hits$hits[[i]]$`_source`$text_entry,"\n")
}

但是我想知道是否可以在Elasticsearch中使用查询来做到这一点。

关于r - Elasticsearch返回一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31265644/

相关文章:

linux - Logstash 在 Ubuntu 中没有显示任何输出

elasticsearch - Opendistro Elasticsearch将管理员级别特权授予匿名用户

r - 在 R 中处理时间段,例如 5 分 30 秒

R:根据组和不同条件对数据框进行子集化

sorting - 根据elasticsearch中数组的长度排序

elasticsearch - 数组中重复值的value_count而不是doc_count

api - 我们如何查询 Kibana?

r - 小时间序列分析

html - 在演示文稿中包含交互式本地 R

c++ - Rcpp: 'operator=' 矩阵和列表的模糊重载