gpt4 book ai didi

r - Elasticsearch返回一个字段

转载 作者:行者123 更新时间:2023-12-03 00:20:46 25 4
gpt4 key购买 nike

我正在将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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com