es允许执行搜索查询,返回结果匹配(hits),甚至跨多个index和执行多个type
SearchSourceBuilder
- 所有参数都是可选的
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.QueryBuilders.*;
SearchResponse response = client.prepareSearch("index1", "index2") //索引
.setTypes("type1", "type2") //类型
//检索方式
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
//查询条件query_string
.setQuery(QueryBuilders.termQuery("multi", "test"))
//多条件过滤filters
.setPostFilter(
QueryBuilders.rangeQuery("age").from(12).to(20)
)
.setFrom(0)
.setSize(60)
.setExplain(true)
.get();
Scrollings in Java
返回整个”page”, 用来检索大量结果
import static org.elasticsearch.index.query.QueryBuilders.*;
//queryString
QueryBuilder qb = termQuery("multi", "test");
SearchResponse scrollResp = client.prepareSearch(test)
.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC) //属性排序
.setScroll(new TimeValue(60000))
.setQuery(qb) //条件
.setSize(100) //每一页返回100条数据
.get();
//接收scrollResp响应结果
do {
for (SearchHit hit : scrollResp.getHits().getHits()) {
//输出结果(封装在hits属性中)
}
scrollResp = client.
prepareSearchScroll(scrollResp.getScrollId())
.setScroll(new TimeValue(60000))
.execute()
.actionGet();
} while(scrollResp.getHits().getHits().length != 0);//遍历
从滚动请求返回的结果反映出当时的状态指数,最初的搜索请求,就像一个快照。索引、更新或删除只会影响以后的搜索请求
MultSearch
多个搜索API允许在相同API下执行多个搜索请求
请求格式大致如下
header\n //包括 index / indices 、 type
body\n //包括典型搜索查询(查询、聚合、大小等)
header\n
body\n
SearchRequestBuilder srb1 = client
.prepareSearch().setQuery(QueryBuilders.queryStringQuery("elasticsearch")).setSize(1);
SearchRequestBuilder srb2 = client
.prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1);
MultiSearchResponse sr = client.prepareMultiSearch()
.add(srb1)
.add(srb2)
.get();
// You will get all individual responses from MultiSearchResponse#getResponses()
long nbHits = 0;
for (MultiSearchResponse.Item item : sr.getResponses()) {
SearchResponse response = item.getResponse();
nbHits += response.getHits().getTotalHits();
for(SearchHit hit : response.getHits()){
System.out.println(hit.getSourceAsString());
}
}
聚合
SearchResponse sr = client.prepareSearch()
.setQuery(QueryBuilders.matchAllQuery())
.addAggregation(
AggregationBuilders.terms("agg1").field("field")
)
.addAggregation(
AggregationBuilders.dateHistogram("agg2")
.field("birth")
.dateHistogramInterval(DateHistogramInterval.YEAR)
)
.get();
// Get your facet results
Terms agg1 = sr.getAggregations().get("agg1");
DateHistogram agg2 = sr.getAggregations().get("agg2");
Search Template
达到查询最大文档数量,提前终止
SearchResponse sr = client.prepareSearch(INDEX)
.setTerminateAfter(1000)
.get();
if (sr.isTerminatedEarly()) {
// We finished early
}
搜索模板
Map<String, Object> template_params = new HashMap<>();
template_params.put("param_gender", "male");
创建模板请求
SearchResponse sr = new SearchTemplateRequestBuilder(client)
.setScript("template_gender")
.setScriptType(ScriptService.ScriptType.FILE)
.setScriptParams(template_params)
.setRequest(new SearchRequest())
.get()
.getResponse();
在集群中存储模板状态
client.admin().cluster().preparePutStoredScript()
.setScriptLang("mustache")
.setId("template_gender")
.setSource(new BytesArray(
"{\n" +
" \"template\" : {\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"gender\" : \"{{param_gender}}\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}")).get();
执行存储模板
SearchResponse sr = new SearchTemplateRequestBuilder(client)
.setScript("template_gender")
.setScriptType(ScriptType.STORED)
.setScriptParams(template_params)
.setRequest(new SearchRequest())
.get()
.getResponse();
内联模板
sr = new SearchTemplateRequestBuilder(client)
.setScript("{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"gender\" : \"{{param_gender}}\"\n" +
" }\n" +
" }\n" +
"}")
.setScriptType(ScriptType.INLINE)
.setScriptParams(template_params)
.setRequest(new SearchRequest())
.get()
.getResponse();