您的位置:首页 > 编程语言 > Java开发

ElasticSearch中Java Search API

2017-07-14 13:48 399 查看
原文地址:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search.html

Java
API [5.5] » Search API

« 
Using Bulk Processor    Using
scrolls in Java  »


Search APIedit

The search API allows one to execute a search query and get back search hits that match the query. It can be executed across one or more indices and across one or more types. The query can be provided using the query
Java API. The body of the search request is built using the 
SearchSourceBuilder
.
Here is an example:

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();


Note that all parameters are optional. Here is the smallest search call you can write:

// 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.

For more information on the search operation, check out the REST search docs.

« 
Using Bulk Processor    Using
scrolls in Java  »
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: