SpringBoot:Java High Level REST Client 搜索 API

Prior to the integration of the latest version of Springboot elasticSearch Reference article: SpingBoot: Integration ElasticSearch 7.2.0

Search API

SearchRequest search for documents, polymerization, any proposal relating to the operation, the request also provides methods highlighted in the document results.

In its most basic form, we can add the query to the request:

 searchRequest = indexName==null?new SearchRequest():new SearchRequest(indexName);//创建SeachRequest . 没有参数,这将针对所有索引运行.
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//大多数搜索参数都添加到SearchSourceBuilder . 它为搜索请求正文中的所有内容提供了setter.
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());//将match_all查询添加到SearchSourceBuilder .
        searchRequest.source(searchSourceBuilder);//将SearchSourceBuilder添加到SeachRequest .
        highLevelClient.search(searchRequest, RequestOptions.DEFAULT);

Optional arguments

Let's look at SearchRequestsome optional parameters:

SearchRequest searchRequest = new SearchRequest ( "posts"); // limit the request to index

There are other interesting optional parameters:

searchRequest.routing ( "routing"); // set the routing parameters

What is the routing parameters?

When indexing a document, the document will be in a main memory on a slice. When a plurality of main memory generally slices. Elasticsearch know how a document should be placed in which fragments of it? This process is based on this formula to the following decision:

shard = hash(routing) % number_of_primary_shards
  • routing Is a variable value, the default is the document  _id may be set to a value of a custom
  • number_of_primary_shards Is a front partial number of sheets

All documents are accepted API called  routing routing parameters, this parameter we can customize the mapping document fragment. A custom routing parameters may be used to ensure that all relevant documents - for example, all documents belonging to the same user - are stored in the same slice.

searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen()); // 设置`IndicesOptions`控制如何解析不可用的索引以及如何扩展通配符表达式 
searchRequest.preference("_local"); // 使用首选项参数例如执行搜索以优先选择本地分片. 默认是跨分片随机化. 

Using the SearchSourceBuilder

Most options control the search behavior can be in SearchSourceBuilderthe set  SearchSourceBuilderinclude an option in the main body of search options and Rest API request rather or less.

Here are a few examples of some common options:

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 使用默认选项创建`SearchSourceBuilder`
QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders._queryStringQuery_("关键词");//搜索关键字
sourceBuilder.query(QueryBuilders.termQuery("user", "kimchy")); // 设置查询. 可以是任何类型的`QueryBuilder` 
sourceBuilder.from(0); // 设置确定结果索引的`from`选项以开始搜索. 默认为0\. 
sourceBuilder.size(5); // 设置`size`选项,确定要返回的搜索命中数. 默认为10\. 
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); // 设置一个可选的超时,控制允许搜索的时间. 

After that, simply SearchSourceBuilderadd to SearchRequest :

SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("posts");
searchRequest.source(sourceBuilder);

The official document: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.2/java-rest-high-search.html#java-rest-high-search

Author: onlooker
Source: three non-youth blog
original: https://www.35youth.cn/715.html
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/35youth/p/11411908.html