- 87_熟练掌握ES Java API_基于search temp
- 83_熟练掌握ES Java API_基于upsert实现汽车最
- 86_熟练掌握ES Java API_基于scroll实现月度销
- 85_熟练掌握ES Java API_基于bulk实现多4S店销
- 90_熟练掌握ES Java API_基于地理位置对周围汽车4S
- 84_熟练掌握ES Java API_基于mget实现多辆汽车的
- 88_熟练掌握ES Java API_对汽车品牌进行全文检索、精
- 89_熟练掌握ES Java API_对汽车品牌进行多种条件的组
- ES7.3支持语义句向量查询
- search-gurad权限整合es6.3.1后,java cl
<meta charset="utf-8">
87_熟练掌握ES Java API_基于search template实现按品牌分页查询模板
搜索模板的功能,java api怎么去调用一个搜索模板
新建maven工程
image.pngpage_query_by_brand.mustache
image.png脚本内容,实现分页
{ "from": {{from}}, "size": {{size}}, "query": { "match": { "brand.keyword": "{{brand}}" } } }
利用java实现
SearchResponse sr = new SearchTemplateRequestBuilder(client)
.setScript("page_query_by_brand")
.setScriptType(ScriptService.ScriptType.FILE)
.setScriptParams(template_params)
.setRequest(new SearchRequest())
.get()
.getResponse();
源码如下
public class SearchTemplatePageQuery {
@SuppressWarnings({ "resource", "unchecked" })
public static void main(String[] args) throws Exception {
Settings settings = Settings.builder()
.put("cluster.name", "elasticsearch")
.build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
Map<String, Object> scriptParams = new HashMap<String, Object>();
scriptParams.put("from", 0);
scriptParams.put("size", 1);
scriptParams.put("brand", "宝马");
SearchResponse searchResponse = new SearchTemplateRequestBuilder(client)
.setScript("page_query_by_brand")
.setScriptType(ScriptType.FILE)
.setScriptParams(scriptParams)
.setRequest(new SearchRequest("car_shop").types("sales"))
.get()
.getResponse();
for(SearchHit searchHit : searchResponse.getHits().getHits()) {
System.out.println(searchHit.getSourceAsString());
}
client.close();
}
}
网友评论