Java Code Examples for org.elasticsearch.action.search.SearchRequest#routing()
The following examples show how to use
org.elasticsearch.action.search.SearchRequest#routing() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticSqlParseResult.java From elasticsearch-sql with MIT License | 6 votes |
private SearchRequest toRequest() { SearchRequest searchRequest = new SearchRequest(); List<String> indexList = indices.parallelStream().map(StringManager::removeStringSymbol).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(indexList)) { searchRequest.indices(indexList.toArray(new String[0])); } else { throw new ElasticSql2DslException("[syntax error] indices name must be set"); } //这里不会修改from的值 searchSourceBuilder.from(Math.max(from, 0)); searchSourceBuilder.size(Math.max(size, 0)); searchSourceBuilder.trackTotalHits(this.trackTotalHits); if (CollectionUtils.isNotEmpty(highlighter)) { HighlightBuilder highlightBuilder = HighlightBuilders.highlighter(highlighter); searchSourceBuilder.highlighter(highlightBuilder); } searchSourceBuilder.query(whereCondition); if (StringUtils.isNotBlank(distinctName)) { searchSourceBuilder.collapse(new CollapseBuilder(distinctName)); } if (CollectionUtils.isNotEmpty(orderBy)) { for (SortBuilder sortBuilder : orderBy) { searchSourceBuilder.sort(sortBuilder); } } searchSourceBuilder.fetchSource(includeFields.toArray(new String[0]), excludeFields.toArray(new String[0])); if (CollectionUtils.isNotEmpty(routingBy)) { searchRequest.routing(routingBy.toArray(new String[0])); } if (CollectionUtils.isNotEmpty(groupBy)) { for (AggregationBuilder aggItem : groupBy) { searchSourceBuilder.aggregation(aggItem); } } return searchRequest.source(searchSourceBuilder); }
Example 2
Source File: CountRequest.java From Elasticsearch with Apache License 2.0 | 6 votes |
public SearchRequest toSearchRequest() { SearchRequest searchRequest = new SearchRequest(indices()); searchRequest.indicesOptions(indicesOptions()); searchRequest.types(types()); searchRequest.routing(routing()); searchRequest.preference(preference()); searchRequest.source(source()); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.size(0); if (minScore() != DEFAULT_MIN_SCORE) { searchSourceBuilder.minScore(minScore()); } if (terminateAfter() != DEFAULT_TERMINATE_AFTER) { searchSourceBuilder.terminateAfter(terminateAfter()); } searchRequest.extraSource(searchSourceBuilder); return searchRequest; }
Example 3
Source File: InternalEsClient.java From io with Apache License 2.0 | 6 votes |
/** * 非同期でドキュメントを検索. * @param index インデックス名 * @param type タイプ名 * @param routingId routingId * @param query クエリ情報 * @return 非同期応答 */ public ActionFuture<SearchResponse> asyncSearch( String index, String type, String routingId, Map<String, Object> query) { SearchRequest req = new SearchRequest(index).types(type).searchType(SearchType.DEFAULT); if (query != null) { req.source(query); } if (routingFlag) { req = req.routing(routingId); } ActionFuture<SearchResponse> ret = esTransportClient.search(req); this.fireEvent(Event.afterRequest, index, type, null, JSONObject.toJSONString(query), "Search"); return ret; }
Example 4
Source File: InternalEsClient.java From io with Apache License 2.0 | 6 votes |
/** * 非同期でドキュメントを検索. <br /> * Queryの指定方法をMapで直接記述せずにQueryBuilderにするため、非推奨とする. * @param index インデックス名 * @param routingId routingId * @param query クエリ情報 * @return 非同期応答 */ public ActionFuture<SearchResponse> asyncSearch( String index, String routingId, Map<String, Object> query) { SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT); if (query != null) { req.source(query); } if (routingFlag) { req = req.routing(routingId); } ActionFuture<SearchResponse> ret = esTransportClient.search(req); this.fireEvent(Event.afterRequest, index, null, null, JSONObject.toJSONString(query), "Search"); return ret; }
Example 5
Source File: InternalEsClient.java From io with Apache License 2.0 | 6 votes |
/** * 非同期でドキュメントを検索. * @param index インデックス名 * @param routingId routingId * @param query クエリ情報 * @return 非同期応答 */ public ActionFuture<SearchResponse> asyncSearch( String index, String routingId, QueryBuilder query) { SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT); String queryString = "null"; if (query != null) { req.source(new SearchSourceBuilder().query(query)); queryString = query.buildAsBytes().toUtf8(); } if (routingFlag) { req = req.routing(routingId); } ActionFuture<SearchResponse> ret = esTransportClient.search(req); this.fireEvent(Event.afterRequest, index, null, null, queryString, "Search"); return ret; }
Example 6
Source File: InternalEsClient.java From io with Apache License 2.0 | 6 votes |
/** * 非同期でドキュメントを検索. * @param index インデックス名 * @param type タイプ名 * @param routingId routingId * @param query クエリ情報 * @return 非同期応答 */ public ActionFuture<SearchResponse> asyncSearch( String index, String type, String routingId, Map<String, Object> query) { SearchRequest req = new SearchRequest(index).types(type).searchType(SearchType.DEFAULT); if (query != null) { req.source(query); } if (routingFlag) { req = req.routing(routingId); } ActionFuture<SearchResponse> ret = esTransportClient.search(req); this.fireEvent(Event.afterRequest, index, type, null, JSONObject.toJSONString(query), "Search"); return ret; }
Example 7
Source File: InternalEsClient.java From io with Apache License 2.0 | 6 votes |
/** * 非同期でドキュメントを検索. <br /> * Queryの指定方法をMapで直接記述せずにQueryBuilderにするため、非推奨とする. * @param index インデックス名 * @param routingId routingId * @param query クエリ情報 * @return 非同期応答 */ public ActionFuture<SearchResponse> asyncSearch( String index, String routingId, Map<String, Object> query) { SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT); if (query != null) { req.source(query); } if (routingFlag) { req = req.routing(routingId); } ActionFuture<SearchResponse> ret = esTransportClient.search(req); this.fireEvent(Event.afterRequest, index, null, null, JSONObject.toJSONString(query), "Search"); return ret; }
Example 8
Source File: InternalEsClient.java From io with Apache License 2.0 | 6 votes |
/** * 非同期でドキュメントを検索. * @param index インデックス名 * @param routingId routingId * @param query クエリ情報 * @return 非同期応答 */ public ActionFuture<SearchResponse> asyncSearch( String index, String routingId, QueryBuilder query) { SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT); String queryString = "null"; if (query != null) { req.source(new SearchSourceBuilder().query(query)); queryString = query.buildAsBytes().toUtf8(); } if (routingFlag) { req = req.routing(routingId); } ActionFuture<SearchResponse> ret = esTransportClient.search(req); this.fireEvent(Event.afterRequest, index, null, null, queryString, "Search"); return ret; }
Example 9
Source File: ElasticsearchClientTransport.java From c2mon with GNU Lesser General Public License v3.0 | 5 votes |
@Override public boolean isIndexExisting(IndexMetadata indexMetadata) { SearchRequest searchRequest = new SearchRequest(indexMetadata.getName()); searchRequest.types(TYPE); searchRequest.routing(indexMetadata.getRouting()); try { SearchResponse response = client.search(searchRequest).get(); return response.status().equals(RestStatus.OK); } catch (InterruptedException | ExecutionException e) { log.error("Error searching index '{}'.", indexMetadata.getName(), e); } return false; }
Example 10
Source File: InternalEsClient.java From io with Apache License 2.0 | 5 votes |
/** * 非同期でドキュメントを検索. * @param index インデックス名 * @param type タイプ名 * @param routingId routingId * @param builder クエリ情報 * @return 非同期応答 */ public ActionFuture<SearchResponse> asyncSearch( String index, String type, String routingId, SearchSourceBuilder builder) { SearchRequest req = new SearchRequest(index).types(type).searchType(SearchType.DEFAULT).source(builder); if (routingFlag) { req = req.routing(routingId); } ActionFuture<SearchResponse> ret = esTransportClient.search(req); this.fireEvent(Event.afterRequest, index, type, null, new String(builder.buildAsBytes().toBytes()), "Search"); return ret; }
Example 11
Source File: InternalEsClient.java From io with Apache License 2.0 | 5 votes |
/** * 非同期でドキュメントをマルチ検索. * 存在しないインデックスに対して本メソッドを使用すると、TransportSerializationExceptionがスローされるので注意すること * @param index インデックス名 * @param type タイプ名 * @param routingId routingId * @param queryList マルチ検索用のクエリ情報リスト * @return 非同期応答 */ public ActionFuture<MultiSearchResponse> asyncMultiSearch( String index, String type, String routingId, List<Map<String, Object>> queryList) { MultiSearchRequest mrequest = new MultiSearchRequest(); if (queryList == null || queryList.size() == 0) { throw new EsMultiSearchQueryParseException(); } for (Map<String, Object> query : queryList) { SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT); if (type != null) { req.types(type); } // クエリ指定なしの場合はタイプに対する全件検索を行う if (query != null) { req.source(query); } if (routingFlag) { req = req.routing(routingId); } mrequest.add(req); } ActionFuture<MultiSearchResponse> ret = esTransportClient.multiSearch(mrequest); this.fireEvent(Event.afterRequest, index, type, null, JSONArray.toJSONString(queryList), "MultiSearch"); return ret; }
Example 12
Source File: InternalEsClient.java From io with Apache License 2.0 | 5 votes |
/** * 非同期でドキュメントを検索. * @param index インデックス名 * @param type タイプ名 * @param routingId routingId * @param builder クエリ情報 * @return 非同期応答 */ public ActionFuture<SearchResponse> asyncSearch( String index, String type, String routingId, SearchSourceBuilder builder) { SearchRequest req = new SearchRequest(index).types(type).searchType(SearchType.DEFAULT).source(builder); if (routingFlag) { req = req.routing(routingId); } ActionFuture<SearchResponse> ret = esTransportClient.search(req); this.fireEvent(Event.afterRequest, index, type, null, new String(builder.buildAsBytes().toBytes()), "Search"); return ret; }
Example 13
Source File: InternalEsClient.java From io with Apache License 2.0 | 5 votes |
/** * 非同期でドキュメントをマルチ検索. * 存在しないインデックスに対して本メソッドを使用すると、TransportSerializationExceptionがスローされるので注意すること * @param index インデックス名 * @param type タイプ名 * @param routingId routingId * @param queryList マルチ検索用のクエリ情報リスト * @return 非同期応答 */ public ActionFuture<MultiSearchResponse> asyncMultiSearch( String index, String type, String routingId, List<Map<String, Object>> queryList) { MultiSearchRequest mrequest = new MultiSearchRequest(); if (queryList == null || queryList.size() == 0) { throw new EsMultiSearchQueryParseException(); } for (Map<String, Object> query : queryList) { SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT); if (type != null) { req.types(type); } // クエリ指定なしの場合はタイプに対する全件検索を行う if (query != null) { req.source(query); } if (routingFlag) { req = req.routing(routingId); } mrequest.add(req); } ActionFuture<MultiSearchResponse> ret = esTransportClient.multiSearch(mrequest); this.fireEvent(Event.afterRequest, index, type, null, JSONArray.toJSONString(queryList), "MultiSearch"); return ret; }
Example 14
Source File: EsUtil.java From java-study with Apache License 2.0 | 4 votes |
/** * @return boolean * @Author pancm * @Description 根据条件查询 * @Date 2019/3/21 * @Param [] **/ public static List<Map<String, Object>> query(String index, String type, EsQueryCondition esQueryCondition, QueryBuilder... queryBuilders) throws IOException { if (index == null || type == null) { return null; } List<Map<String, Object>> list = new ArrayList<>(); try { // 查询指定的索引库 SearchRequest searchRequest = new SearchRequest(index); searchRequest.types(type); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); if (esQueryCondition != null) { Integer form = esQueryCondition.getIndex(); Integer pagesize = esQueryCondition.getPagesize(); if (form != null && form > 0 && pagesize != null && pagesize > 0) { form = (form - 1) * pagesize; pagesize = form + pagesize; // 设置起止和结束 sourceBuilder.from(form); sourceBuilder.size(pagesize); } String routing = esQueryCondition.getRouting(); if (routing != null && routing.length() > 0) { // 设置路由 searchRequest.routing(routing); } // 设置索引库表达式 searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen()); //设置排序 String order = esQueryCondition.getOrder(); if (order != null) { String[] orderField = esQueryCondition.getOrderField(); SortOrder order2 = order.equals(SortOrder.DESC) ? SortOrder.DESC : SortOrder.ASC; //如果设置了排序字段则用排序的字段进行排序,否则就默认排序 if (orderField != null) { for (String field : orderField) { sourceBuilder.sort(new FieldSortBuilder(field).order(order2)); } } else { sourceBuilder.sort(new ScoreSortBuilder().order(order2)); } } String[] includeFields = esQueryCondition.getIncludeFields(); String[] excludeFields = esQueryCondition.getExcludeFields(); if (includeFields != null && includeFields.length > 0 && excludeFields != null && excludeFields.length > 0) { sourceBuilder.fetchSource(includeFields, excludeFields); } sourceBuilder.fetchSource(esQueryCondition.isCloseSource()); } //设置条件 if (queryBuilders != null) { for (QueryBuilder queryBuilder : queryBuilders) { sourceBuilder.query(queryBuilder); } } searchRequest.source(sourceBuilder); // 同步查询 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); if(queryBuilders != null|| (esQueryCondition != null && esQueryCondition.isQueryData())){ // 结果 searchResponse.getHits().forEach(hit -> { Map<String, Object> map = hit.getSourceAsMap(); list.add(map); }); } if(esQueryCondition != null && esQueryCondition.isNeedTotal()){ Map<String, Object> mapTotal = new HashMap<>(); mapTotal.put("total", searchResponse.getHits().getTotalHits()); list.add(mapTotal); } } finally { if (isAutoClose) { close(); } } return list; }
Example 15
Source File: RestSearchAction.java From Elasticsearch with Apache License 2.0 | 4 votes |
public static void parseSearchRequest(SearchRequest searchRequest, RestRequest request, ParseFieldMatcher parseFieldMatcher, BytesReference bodyContent) { searchRequest.indices(Strings.splitStringByCommaToArray(request.param("index"))); // get the content, and put it in the body // add content/source as template if template flag is set boolean isTemplateRequest = request.path().endsWith("/template"); if (bodyContent == null) { if (RestActions.hasBodyContent(request)) { bodyContent = RestActions.getRestContent(request); } } if (bodyContent != null) { if (isTemplateRequest) { searchRequest.templateSource(bodyContent); } else { searchRequest.source(bodyContent); } } // do not allow 'query_and_fetch' or 'dfs_query_and_fetch' search types // from the REST layer. these modes are an internal optimization and should // not be specified explicitly by the user. String searchType = request.param("search_type"); if (SearchType.fromString(searchType, parseFieldMatcher).equals(SearchType.QUERY_AND_FETCH) || SearchType.fromString(searchType, parseFieldMatcher).equals(SearchType.DFS_QUERY_AND_FETCH)) { throw new IllegalArgumentException("Unsupported search type [" + searchType + "]"); } else { searchRequest.searchType(searchType); } searchRequest.extraSource(parseSearchSource(request)); searchRequest.requestCache(request.paramAsBoolean("request_cache", null)); String scroll = request.param("scroll"); if (scroll != null) { searchRequest.scroll(new Scroll(parseTimeValue(scroll, null, "scroll"))); } searchRequest.types(Strings.splitStringByCommaToArray(request.param("type"))); searchRequest.routing(request.param("routing")); searchRequest.preference(request.param("preference")); searchRequest.indicesOptions(IndicesOptions.fromRequest(request, searchRequest.indicesOptions())); }