Ram*_*amy 45 csv elasticsearch
我正在使用弹性搜索.我需要弹性搜索的结果作为csv文件.任何卷曲网址或任何插件来实现这一目标?提前致谢.
ana*_*ist 64
我已经使用cURL和jq完成了这个("喜欢sed
,但是对于JSON").例如,您可以执行以下操作以获取给定构面的前20个值的CSV输出:
$ curl -X GET 'http://localhost:9200/myindex/item/_search?from=0&size=0' -d '
{"from": 0,
"size": 0,
"facets": {
"sourceResource.subject.name": {
"global": true,
"terms": {
"order": "count",
"size": 20,
"all_terms": true,
"field": "sourceResource.subject.name.not_analyzed"
}
}
},
"sort": [
{
"_score": "desc"
}
],
"query": {
"filtered": {
"query": {
"match_all": {}
}
}
}
}' | jq -r '.facets["subject"].terms[] | [.term, .count] | @csv'
"United States",33755
"Charities--Massachusetts",8304
"Almshouses--Massachusetts--Tewksbury",8304
"Shields",4232
"Coat of arms",4214
"Springfield College",3422
"Men",3136
"Trees",3086
"Session Laws--Massachusetts",2668
"Baseball players",2543
"Animals",2527
"Books",2119
"Women",2004
"Landscape",1940
"Floral",1821
"Architecture, Domestic--Lowell (Mass)--History",1785
"Parks",1745
"Buildings",1730
"Houses",1611
"Snow",1579
Run Code Online (Sandbox Code Playgroud)
Jef*_*etz 14
我成功地使用了Python,脚本方法直观而简洁.python的ES客户端让生活变得轻松.首先在这里获取Python的最新Elasticsearch客户端:http://www.elasticsearch.org/blog/unleash-the-clients-ruby-python-php-perl/#python
然后您的Python脚本可以包含以下调用:
import elasticsearch
import unicodedata
import csv
es = elasticsearch.Elasticsearch(["10.1.1.1:9200"])
# this returns up to 500 rows, adjust to your needs
res = es.search(index="YourIndexName", body={"query": {"match": {"title": "elasticsearch"}}},500)
sample = res['hits']['hits']
# then open a csv file, and loop through the results, writing to the csv
with open('outputfile.tsv', 'wb') as csvfile:
filewriter = csv.writer(csvfile, delimiter='\t', # we use TAB delimited, to handle cases where freeform text may have a comma
quotechar='|', quoting=csv.QUOTE_MINIMAL)
# create column header row
filewriter.writerow(["column1", "column2", "column3"]) #change the column labels here
# fill columns 1, 2, 3 with your data
col1 = hit["some"]["deeply"]["nested"]["field"].decode('utf-8') #replace these nested key names with your own
col1 = col1.replace('\n', ' ')
# col2 = , col3 = , etc...
for hit in sample:
filewriter.writerow([col1,col2,col3])
Run Code Online (Sandbox Code Playgroud)
您可能希望将调用包装在try/catch错误处理中的列['key']引用中,因为文档是非结构化的,并且可能不时具有该字段(取决于您的索引).
我有一个完整的Python示例脚本,使用最新的ES python客户端:
https://github.com/jeffsteinmetz/pyes2csv
小智 8
您可以使用elasticsearch head插件.您可以从elasticsearch head插件 安装http:// localhost:9200/_plugin/head / 安装插件后,导航到结构化查询选项卡,提供查询详细信息,您可以从'输出结果'中选择'csv'格式落下.
我不认为有一个插件会直接从搜索引擎中提供CSV结果,因此您必须查询ElasticSearch以检索结果,然后将其写入CSV文件.
如果您使用的是类Unix操作系统,那么您可以使用es2unix取得一些进展,它将在命令行上以原始文本格式返回搜索结果,因此应该是可编写脚本的.
然后,您可以将这些结果转储到文本文件或管道awk
或类似格式为CSV.有一个-o
标志可用,但目前只提供"原始"格式.
我找到了一个使用Java的例子- 但还没有测试过.
您可以使用类似的东西查询ElasticSearch,pyes
并使用标准编写器库将结果集写入文件csv
.
使用Perl然后你可以使用由Rakesh链接的Clinton Gormley的GIST - https://gist.github.com/clintongormley/2049562