搜索API

搜索查询,返回查询匹配的结果,搜索一个index / type 或者多个index / type,可以使用 query Java API 作为查询条件,下面是例子:

  1. import org.elasticsearch.action.search.SearchResponse;
  2. import org.elasticsearch.action.search.SearchType;
  3. import org.elasticsearch.index.query.QueryBuilders.*;
  1. SearchResponse response = client.prepareSearch("index1", "index2")
  2. .setTypes("type1", "type2")
  3. .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
  4. .setQuery(QueryBuilders.termQuery("multi", "test")) // Query 查询条件
  5. .setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18)) // Filter 过滤
  6. .setFrom(0).setSize(60).setExplain(true)
  7. .get();

所有的参数都是可选的,下面是最简单的调用:

  1. // MatchAll on the whole cluster with all default options
  2. SearchResponse response = client.prepareSearch().get();

尽管Java API默认提供QUERY_AND_FETCHDFS_QUERY_AND_FETCH 两种 search types ,但是这种模式应该由系统选择,用户不要手动指定

更多请移步 REST search 文档