我想创建单词云来可视化Elasticsearch查询的结果.在词云中,应显示与查询匹配的文档中出现的所有术语.因此,我需要计算某些任意文档集中出现的所有术语的术语频率.问题在于我需要文档中所有术语的实际频率,而不仅仅是术语出现的文档数量(这可以使用术语聚合或方面轻松解决).
给出以下测试指数
curl -XPOST localhost:9200/test -d '{
"mappings": {
"testdoc" : {
"properties" : {
"text" : {
"type" : "string",
"term_vector": "yes"
}
}
}
}
}'
Run Code Online (Sandbox Code Playgroud)
和数据:
curl -XPOST "http://localhost:9200/sports/_bulk" -d'
{"index":{"_index":"test","_type":"testdoc"}}
{"text":"bike bike car"}
{"index":{"_index":"test","_type":"testdoc"}}
{"text":"car"}
{"index":{"_index":"test","_type":"testdoc"}}
{"text":"car car bus bus"}
{"index":{"_index":"test","_type":"testdoc"}}
{"text":"bike car bus"}
'
Run Code Online (Sandbox Code Playgroud)
以下查询返回术语"bike"的术语频率.
curl -XPOST "http://localhost:9200/test/testdoc/_search" -d'
{
"query": {
"match_all": {}
},
"aggs": {
"words": {
"terms": {
"field": "text"
},
"aggs": {
"tf_sum": {
"sum": {
"script": "_index[\"text\"][\"bike\"].tf()"
}
}
} …Run Code Online (Sandbox Code Playgroud)