Search API
通过search API可以执行一个查询query并获得与query相匹配的查询结果. 可以在一个或多个indices和一个或多个types中执行. 查询条件可以通过查询java API来提供,search请求提可以放在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)
.setQuery(QueryBuilders.termQuery("multi", "test")) // Query
.setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18)) // Filter
.setFrom(0).setSize(60).setExplain(true)
.get();
以上所有参数都是可选的,以下是最小查询代码
// MatchAll on the whole cluster with all default options
SearchResponse response = client.prepareSearch().get();
注意:
Although the Java API defines the additional search types QUERY_AND_FETCH and DFS_QUERY_AND_FETCH, these modes are internal optimizations and should not be specified explicitly by users of the API.
更多有关search operation, 请查看REST search文档.
Using scrolls in Java
先读并了解 scroll documentation
中文介绍