欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

查询实例

程序员文章站 2022-03-27 12:22:58
...
SearchResponse response =  client.prepareSearch(esIndex).setTypes(esType)
	.setIndicesOptions(IndicesOptions.fromOptions(true, true, false, false)) // 忽略不可用或不存在的index																.setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(boolQuery)
	.setPostFilter(rangeQueryBuilder).addSort(sort, sortOrder).setFrom(start).setSize(rows)
	.setPreference("_primary_first")
	// .setExplain(true) //设置是否按查询匹配度排序(加上这个参数报错)
	.execute().actionGet();

SearchHit[] hits = response.getHits().getHits();
long total = response.getHits().getTotalHits();
StringBuilder context = new StringBuilder();			
for (SearchHit hit : hits) {
	Map<String, Object> sourceMap = hit.getSource();
	Iterator<String> it = sourceMap.keySet().iterator();				
	while (it.hasNext()) {
		String key = it.next();
		Object value = sourceMap.get(key);
	}
}
相关标签: Elasticsearch