Elasticsearch返回一个字段

时间:2015-07-07 10:08:29

标签: r elasticsearch

我正在使用带有R的elasticsearch进行数据挖掘。我正在使用莎士比亚数据集。我想只检索字段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"))

1 个答案:

答案 0 :(得分:0)

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

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中的查询来执行此操作。