从 Elasticsearch 获取必填字段
Get Required Field From Elasticsearch
我的 elasticsearch 中有以下字段
"_source": {
"@timestamp":
"cpu_stat_s": {
"temp_in_celsius":
"model_name":,
"cpu_MHz_String": ,
"cache_size_string":
},
"memory_stat_s": {
"total_memory": ,
"swap_total": ,
"swap_free": ,
"used": ,
"free": ,
"swap_used":
},
"process_stat_s": [
{
"process_name": ,
"mem_in_use":,
"process_pid":
}
]
我只是想使用 java api 从 elasticsearch 中假设 cpu_stat_s 和 memory_stat_s 字段,我搜索了查询过滤器并做了
String source="{\"query\":\"ether_stat_s\"}";
SearchResponse response = client.prepareSearch("cn_*")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setFrom(0).setSize(10).setExplain(true)
.setPostFilter(FilterBuilders.rangeFilter("@timestamp").from(TIMESTAMP_FROM).to(TIMESTAMP_TO)).**setSource(source).**execute().actionGet();
但是我收到了所有字段而不是必填字段。
我可以通过获取所有字段然后使用 for 循环并从响应中获取源来做到这一点,但这会增加 CPU 数十亿条记录的时间。我是弹性搜索的新手并且因此,如果有人可以帮助我,那将是很大的帮助
经过最后更多的研究,我找到了答案
SearchResponse response = client.prepareSearch("cn_*")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setFrom(0).setSize(10).setExplain(true)
.setPostFilter(FilterBuilders.rangeFilter("@timestamp").from(TIMESTAMP_FROM).to(TIMESTAMP_TO))
.setFetchSource(new String[]{"ether_stat_s"}, null)
.execute()
.actionGet();
我的 elasticsearch 中有以下字段
"_source": {
"@timestamp":
"cpu_stat_s": {
"temp_in_celsius":
"model_name":,
"cpu_MHz_String": ,
"cache_size_string":
},
"memory_stat_s": {
"total_memory": ,
"swap_total": ,
"swap_free": ,
"used": ,
"free": ,
"swap_used":
},
"process_stat_s": [
{
"process_name": ,
"mem_in_use":,
"process_pid":
}
]
我只是想使用 java api 从 elasticsearch 中假设 cpu_stat_s 和 memory_stat_s 字段,我搜索了查询过滤器并做了
String source="{\"query\":\"ether_stat_s\"}";
SearchResponse response = client.prepareSearch("cn_*")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setFrom(0).setSize(10).setExplain(true)
.setPostFilter(FilterBuilders.rangeFilter("@timestamp").from(TIMESTAMP_FROM).to(TIMESTAMP_TO)).**setSource(source).**execute().actionGet();
但是我收到了所有字段而不是必填字段。
我可以通过获取所有字段然后使用 for 循环并从响应中获取源来做到这一点,但这会增加 CPU 数十亿条记录的时间。我是弹性搜索的新手并且因此,如果有人可以帮助我,那将是很大的帮助
经过最后更多的研究,我找到了答案
SearchResponse response = client.prepareSearch("cn_*")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setFrom(0).setSize(10).setExplain(true)
.setPostFilter(FilterBuilders.rangeFilter("@timestamp").from(TIMESTAMP_FROM).to(TIMESTAMP_TO))
.setFetchSource(new String[]{"ether_stat_s"}, null)
.execute()
.actionGet();