Java Code Examples for org.elasticsearch.action.search.SearchRequestBuilder#setSize()
The following examples show how to use
org.elasticsearch.action.search.SearchRequestBuilder#setSize() .
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: TransportFeatureStoreAction.java From elasticsearch-learning-to-rank with Apache License 2.0 | 6 votes |
/** * Perform a test search request to validate the element prior to storing it. * * @param validation validation info * @param element the element stored * @param task the parent task * @param listener the action listener to write to * @param onSuccess action ro run when the validation is successfull */ private void validate(FeatureValidation validation, StorableElement element, Task task, ActionListener<FeatureStoreResponse> listener, Runnable onSuccess) { ValidatingLtrQueryBuilder ltrBuilder = new ValidatingLtrQueryBuilder(element, validation, factory); SearchRequestBuilder builder = new SearchRequestBuilder(client, SearchAction.INSTANCE); builder.setIndices(validation.getIndex()); builder.setQuery(ltrBuilder); builder.setFrom(0); builder.setSize(20); // Bail out early and don't score the whole index. builder.setTerminateAfter(1000); builder.request().setParentTask(clusterService.localNode().getId(), task.getId()); builder.execute(wrap((r) -> { if (r.getFailedShards() > 0) { ShardSearchFailure failure = r.getShardFailures()[0]; throw new IllegalArgumentException("Validating the element caused " + r.getFailedShards() + " shard failures, see root cause: " + failure.reason(), failure.getCause()); } onSuccess.run(); }, (e) -> listener.onFailure(new IllegalArgumentException("Cannot store element, validation failed.", e)))); }
Example 2
Source File: ElasticsearchUtil.java From SpringBootLearn with Apache License 2.0 | 5 votes |
/** * 使用分词查询 排序 高亮 * @param index 索引名称 * @param type 类型名称,可传入多个type逗号分隔 * @param query 查询条件 * @param size 文档大小限制 * @param fields 需要显示的字段,逗号分隔(缺省为全部字段) * @param sortField 排序字段 * @param highlightField 高亮字段 * @return 结果 */ public static List<Map<String, Object>> searchListData(String index, String type, QueryBuilder query, Integer size, String fields, String sortField, String highlightField) { SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index); if (StringUtils.isNotEmpty(type)) { searchRequestBuilder.setTypes(type.split(",")); } if (StringUtils.isNotEmpty(highlightField)) { HighlightBuilder highlightBuilder = new HighlightBuilder(); // 设置高亮字段 highlightBuilder.field(highlightField); searchRequestBuilder.highlighter(highlightBuilder); } searchRequestBuilder.setQuery(query); if (StringUtils.isNotEmpty(fields)) { searchRequestBuilder.setFetchSource(fields.split(","), null); } searchRequestBuilder.setFetchSource(true); if (StringUtils.isNotEmpty(sortField)) { searchRequestBuilder.addSort(sortField, SortOrder.ASC); } if (size != null && size > 0) { searchRequestBuilder.setSize(size); }//打印的内容 可以在 Elasticsearch head 和 Kibana 上执行查询 log.info("\n{}", searchRequestBuilder); SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(); long totalHits = searchResponse.getHits().totalHits; long length = searchResponse.getHits().getHits().length; log.info("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length); if (searchResponse.status().getStatus() == 200) { // 解析对象 return setSearchResponse(searchResponse, highlightField); } return null; }
Example 3
Source File: ModelsAction.java From zentity with Apache License 2.0 | 5 votes |
/** * Retrieve all entity models. * * @param client The client that will communicate with Elasticsearch. * @return The response from Elasticsearch. * @throws ForbiddenException */ public static SearchResponse getEntityModels(NodeClient client) throws ForbiddenException { SearchRequestBuilder request = client.prepareSearch(INDEX_NAME); request.setSize(10000); try { return request.get(); } catch (IndexNotFoundException e) { try { SetupAction.createIndex(client); } catch (ElasticsearchSecurityException se) { throw new ForbiddenException("The .zentity-models index does not exist and you do not have the 'create_index' privilege. An authorized user must create the index by submitting: POST _zentity/_setup"); } return request.get(); } }
Example 4
Source File: ElasticSearchFinder.java From attic-polygene-java with Apache License 2.0 | 5 votes |
@Override public EntityReference findEntity( Class<?> resultType, Predicate<Composite> whereClause, Map<String, Object> variables ) throws EntityFinderException { // Prepare request SearchRequestBuilder request = support.client().prepareSearch( support.index() ); BoolQueryBuilder baseQueryBuilder = baseQuery( resultType ); QueryBuilder whereQueryBuilder = processWhereSpecification( baseQueryBuilder, whereClause, variables ); request.setQuery( boolQuery().must( whereQueryBuilder ).filter( baseQueryBuilder ) ); request.setSize( 1 ); // Log LOGGER.debug( "Will search Entity: {}", request ); // Execute SearchResponse response = request.execute().actionGet(); if( response.getHits().getTotalHits() == 1 ) { return EntityReference.parseEntityReference( response.getHits().getAt( 0 ).getId() ); } return null; }
Example 5
Source File: HashJoinElasticExecutor.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
private List<SearchHit> fetchAllHits(TableInJoinRequestBuilder tableInJoinRequest) { Integer hintLimit = tableInJoinRequest.getHintLimit(); SearchRequestBuilder requestBuilder = tableInJoinRequest.getRequestBuilder(); if (hintLimit != null && hintLimit < MAX_RESULTS_ON_ONE_FETCH) { requestBuilder.setSize(hintLimit); SearchResponse searchResponse = requestBuilder.get(); updateMetaSearchResults(searchResponse); return Arrays.asList(searchResponse.getHits().getHits()); } return scrollTillLimit(tableInJoinRequest, hintLimit); }
Example 6
Source File: NestedLoopsElasticExecutor.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
private MultiSearchRequest createMultiSearchRequest(int multiSearchMaxSize, Where connectedWhere, SearchHit[] hits, Select secondTableSelect, Where originalWhere, int currentIndex) throws SqlParseException { MultiSearchRequest multiSearchRequest = new MultiSearchRequest(); for(int i = currentIndex ; i < currentIndex + multiSearchMaxSize && i< hits.length ; i++ ){ Map<String, Object> hitFromFirstTableAsMap = hits[i].getSourceAsMap(); Where newWhere = Where.newInstance(); if(originalWhere!=null) newWhere.addWhere(originalWhere); if(connectedWhere!=null){ Where connectedWhereCloned = null; try { connectedWhereCloned = (Where) connectedWhere.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } updateValuesOnWhereConditions(hitFromFirstTableAsMap,connectedWhereCloned); newWhere.addWhere(connectedWhereCloned); } // for(Condition c : conditions){ // Object value = deepSearchInMap(hitFromFirstTableAsMap,c.getValue().toString()); // Condition conditionWithValue = new Condition(Where.CONN.AND,c.getName(),c.getOpear(),value); // newWhere.addWhere(conditionWithValue); // } //using the 2nd table select and DefaultAction because we can't just change query on request (need to create lot of requests) if(newWhere.getWheres().size() != 0) { secondTableSelect.setWhere(newWhere); } DefaultQueryAction action = new DefaultQueryAction(this.client , secondTableSelect); action.explain(); SearchRequestBuilder secondTableRequest = action.getRequestBuilder(); Integer secondTableHintLimit = this.nestedLoopsRequest.getSecondTable().getHintLimit(); if(secondTableHintLimit != null && secondTableHintLimit <= MAX_RESULTS_ON_ONE_FETCH) secondTableRequest.setSize(secondTableHintLimit); multiSearchRequest.add(secondTableRequest); } return multiSearchRequest; }
Example 7
Source File: ClientFacade.java From molgenis with GNU Lesser General Public License v3.0 | 5 votes |
private SearchRequestBuilder createSearchRequest( QueryBuilder query, Integer from, Integer size, Sort sort, List<AggregationBuilder> aggregations, List<Index> indexes) { String[] indexNames = toIndexNames(indexes); SearchRequestBuilder searchRequest = client.prepareSearch(indexNames); if (query != null) { searchRequest.setQuery(query); } if (from != null) { searchRequest.setFrom(from); } if (size != null) { searchRequest.setSize(size); } if (sort != null) { List<SortBuilder> sorts = sortContentBuilder.createSorts(sort); sorts.forEach(searchRequest::addSort); } if (aggregations != null) { aggregations.forEach(searchRequest::addAggregation); } return searchRequest; }
Example 8
Source File: ESSearch.java From common-project with Apache License 2.0 | 4 votes |
public List<Map<String, Object>> execute() throws Exception { if (StringUtils.isEmpty(indexName)&& ArrayUtils.isEmpty(types)){ throw new Exception("indexName or types is null"); } SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch(this.indexName).setTypes(this.types); if (page>0&&pageSize>0){ searchRequestBuilder.setFrom(this.page).setSize(this.pageSize); } if (!ArrayUtils.isEmpty(showField)){ searchRequestBuilder.setFetchSource(showField,null); } if (size>0){ searchRequestBuilder.setSize(size); } searchRequestBuilder.setFetchSource(true); //按字段排序 if (!StringUtils.isEmpty(sortField)){ String[] split = sortField.split(" "); if (split[1].equalsIgnoreCase("asc")){ searchRequestBuilder.addSort(split[0], SortOrder.ASC); } if (split[1].equalsIgnoreCase("desc")){ searchRequestBuilder.addSort(split[0], SortOrder.DESC); } } BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); if (bean!=null){ Map<String, Object> fieldNameAndVauleMapping = ClassUtil.getNotNullFieldNameAndVauleMapping(this.bean); if (!fieldNameAndVauleMapping.isEmpty()){ fieldNameAndVauleMapping.forEach((k,v)->{ if (matchPhrase==true){ boolQuery.must(QueryBuilders.matchPhraseQuery(k,v)); }else{ boolQuery.must(QueryBuilders.matchQuery(k,v)); } }); } } searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery()); searchRequestBuilder.setQuery(boolQuery); // 设置是否按查询匹配度排序 searchRequestBuilder.setExplain(true); SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(); long totalHits = searchResponse.getHits().totalHits; long length = searchResponse.getHits().getHits().length; logger.debug("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length); return searchResponse(searchResponse,null); }
Example 9
Source File: ESQueryState.java From sql4es with Apache License 2.0 | 4 votes |
/** * Builds the Elasticsearch query object based on the parsed information from the SQL query * @param searchReq * @param info */ private void buildQuery(SearchRequestBuilder searchReq, ParseResult info) { String[] types = new String[info.getSources().size()]; for(int i=0; i<info.getSources().size(); i++) types[i] = info.getSources().get(i).getSource(); SearchRequestBuilder req = searchReq.setTypes(types); // add filters and aggregations if(info.getAggregation() != null){ // when aggregating the query must be a query and not a filter if(info.getQuery() != null) req.setQuery(info.getQuery()); req.addAggregation(info.getAggregation()); // ordering does not work on aggregations (has to be done in client) }else if(info.getQuery() != null){ if(info.getRequestScore()) req.setQuery(info.getQuery()); // use query instead of filter to get a score else req.setPostFilter(info.getQuery()); // add order for(OrderBy ob : info.getSorts()){ req.addSort(ob.getField(), ob.getOrder()); } } else req.setQuery(QueryBuilders.matchAllQuery()); this.limit = info.getLimit(); if(splitRS) maxRowsRS = fetchSize; //System.out.println("fetch: "+fetchSize+" limit: "+limit+" split: "+splitRS); // add limit and determine to use scroll if(info.getAggregation() != null) { req = req.setSize(0); } else{ if(limit > 0 && limit < fetchSize){ // no scroll needed req.setSize(limit); } else{ // use scrolling req.setSize(fetchSize); req.setScroll(new TimeValue(Utils.getIntProp(props, Utils.PROP_SCROLL_TIMEOUT_SEC, 60)*1000)); if (info.getSorts().isEmpty()) req.addSort("_doc", SortOrder.ASC); // scroll works fast with sort on _doc } } // use query cache when this was indicated in FROM clause if(info.getUseCache()) req.setRequestCache(true); req.setTimeout(TimeValue.timeValueMillis(Utils.getIntProp(props, Utils.PROP_QUERY_TIMEOUT_MS, 10000))); }
Example 10
Source File: TransportBasedClient.java From zeppelin with Apache License 2.0 | 4 votes |
@Override public ActionResponse search(String[] indices, String[] types, String query, int size) { final SearchRequestBuilder reqBuilder = new SearchRequestBuilder( client, SearchAction.INSTANCE); reqBuilder.setIndices(); if (indices != null) { reqBuilder.setIndices(indices); } if (types != null) { reqBuilder.setTypes(types); } if (!StringUtils.isEmpty(query)) { // The query can be either JSON-formatted, nor a Lucene query // So, try to parse as a JSON => if there is an error, consider the query a Lucene one try { @SuppressWarnings("rawtypes") final Map source = gson.fromJson(query, Map.class); reqBuilder.setExtraSource(source); } catch (final JsonSyntaxException e) { // This is not a JSON (or maybe not well formatted...) reqBuilder.setQuery(QueryBuilders.queryStringQuery(query).analyzeWildcard(true)); } } reqBuilder.setSize(size); final SearchResponse searchResp = reqBuilder.get(); final ActionResponse actionResp = new ActionResponse() .succeeded(true) .totalHits(searchResp.getHits().getTotalHits()); if (searchResp.getAggregations() != null) { setAggregations(searchResp.getAggregations(), actionResp); } else { for (final SearchHit hit: searchResp.getHits()) { // Fields can be found either in _source, or in fields (it depends on the query) // => specific for elasticsearch's version < 5 // String src = hit.getSourceAsString(); if (src == null) { final Map<String, Object> hitFields = new HashMap<>(); for (final SearchHitField hitField : hit.getFields().values()) { hitFields.put(hitField.getName(), hitField.getValues()); } src = gson.toJson(hitFields); } actionResp.addHit(new HitWrapper(hit.getIndex(), hit.getType(), hit.getId(), src)); } } return actionResp; }
Example 11
Source File: ElasticSearchFinder.java From attic-polygene-java with Apache License 2.0 | 4 votes |
@Override public Stream<EntityReference> findEntities( Class<?> resultType, Predicate<Composite> whereClause, List<OrderBy> orderBySegments, Integer firstResult, Integer maxResults, Map<String, Object> variables ) throws EntityFinderException { // Prepare request SearchRequestBuilder request = support.client().prepareSearch( support.index() ); BoolQueryBuilder baseQueryBuilder = baseQuery( resultType ); QueryBuilder whereQueryBuilder = processWhereSpecification( baseQueryBuilder, whereClause, variables ); request.setQuery( boolQuery().must( whereQueryBuilder ).filter( baseQueryBuilder ) ); if( firstResult != null ) { request.setFrom( firstResult ); } if( maxResults != null ) { request.setSize( maxResults ); } else { //request.setSize( Integer.MAX_VALUE ); // TODO Use scrolls? } if( orderBySegments != null ) { for( OrderBy order : orderBySegments ) { request.addSort( order.property().toString(), order.order() == OrderBy.Order.ASCENDING ? SortOrder.ASC : SortOrder.DESC ); } } // Log LOGGER.debug( "Will search Entities: {}", request ); // Execute SearchResponse response = request.execute().actionGet(); return StreamSupport.stream( response.getHits().spliterator(), false ) .map( hit -> EntityReference.parseEntityReference( hit.getId() ) ); }
Example 12
Source File: BaseElasticSearchIndexBuilder.java From sakai with Educational Community License v2.0 | 4 votes |
protected SearchRequestBuilder addSearchSuggestionsPagination(SearchRequestBuilder searchRequestBuilder) { return searchRequestBuilder.setSize(maxNumberOfSuggestions); }
Example 13
Source File: BaseElasticSearchIndexBuilder.java From sakai with Educational Community License v2.0 | 4 votes |
protected SearchRequestBuilder addSearchSuggestionsPagination(SearchRequestBuilder searchRequestBuilder) { return searchRequestBuilder.setSize(maxNumberOfSuggestions); }
Example 14
Source File: EsAbstractBehavior.java From fess with Apache License 2.0 | 4 votes |
@Override protected <RESULT extends ENTITY> List<RESULT> delegateSelectList(final ConditionBean cb, final Class<? extends RESULT> entityType) { // #pending check response final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()); final int from; final int size; if (cb.isFetchScopeEffective()) { from = cb.getPageStartIndex(); size = cb.getFetchSize(); } else { from = 0; size = 10; } builder.setFrom(from); builder.setSize(size); final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb; if (esCb.getPreference() != null) { builder.setPreference(esCb.getPreference()); } esCb.request().build(builder); final SearchResponse response = esCb.build(builder).execute().actionGet(searchTimeout); final EsPagingResultBean<RESULT> list = new EsPagingResultBean<>(builder); final SearchHits searchHits = getSearchHits(response); searchHits.forEach(hit -> { final Map<String, Object> source = hit.getSourceAsMap(); final RESULT entity = createEntity(source, entityType); final DocMeta docMeta = ((EsAbstractEntity) entity).asDocMeta(); docMeta.id(hit.getId()); docMeta.version(hit.getVersion()); docMeta.seqNo(hit.getSeqNo()); docMeta.primaryTerm(hit.getPrimaryTerm()); list.add(entity); }); list.setPageSize(size); list.setAllRecordCount((int) searchHits.getTotalHits().value); list.setCurrentPageNumber(cb.getFetchPageNumber()); list.setTook(response.getTook().getMillis()); list.setTotalShards(response.getTotalShards()); list.setSuccessfulShards(response.getSuccessfulShards()); list.setFailedShards(response.getFailedShards()); list.setTotalHits(searchHits.getTotalHits()); list.setAggregation(response.getAggregations()); // #pending others return list; }
Example 15
Source File: EsAbstractBehavior.java From fess with Apache License 2.0 | 4 votes |
@Override protected <RESULT extends ENTITY> List<RESULT> delegateSelectList(final ConditionBean cb, final Class<? extends RESULT> entityType) { // #pending check response final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()); final int from; final int size; if (cb.isFetchScopeEffective()) { from = cb.getPageStartIndex(); size = cb.getFetchSize(); } else { from = 0; size = 10; } builder.setFrom(from); builder.setSize(size); final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb; if (esCb.getPreference() != null) { builder.setPreference(esCb.getPreference()); } esCb.request().build(builder); final SearchResponse response = esCb.build(builder).execute().actionGet(searchTimeout); final EsPagingResultBean<RESULT> list = new EsPagingResultBean<>(builder); final SearchHits searchHits = getSearchHits(response); searchHits.forEach(hit -> { final Map<String, Object> source = hit.getSourceAsMap(); final RESULT entity = createEntity(source, entityType); final DocMeta docMeta = ((EsAbstractEntity) entity).asDocMeta(); docMeta.id(hit.getId()); docMeta.version(hit.getVersion()); docMeta.seqNo(hit.getSeqNo()); docMeta.primaryTerm(hit.getPrimaryTerm()); list.add(entity); }); list.setPageSize(size); list.setAllRecordCount((int) searchHits.getTotalHits().value); list.setCurrentPageNumber(cb.getFetchPageNumber()); list.setTook(response.getTook().getMillis()); list.setTotalShards(response.getTotalShards()); list.setSuccessfulShards(response.getSuccessfulShards()); list.setFailedShards(response.getFailedShards()); list.setTotalHits(searchHits.getTotalHits()); list.setAggregation(response.getAggregations()); // #pending others return list; }
Example 16
Source File: EsAbstractBehavior.java From fess with Apache License 2.0 | 4 votes |
@Override protected <RESULT extends ENTITY> List<RESULT> delegateSelectList(final ConditionBean cb, final Class<? extends RESULT> entityType) { // #pending check response final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()); final int from; final int size; if (cb.isFetchScopeEffective()) { from = cb.getPageStartIndex(); size = cb.getFetchSize(); } else { from = 0; size = 10; } builder.setFrom(from); builder.setSize(size); final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb; if (esCb.getPreference() != null) { builder.setPreference(esCb.getPreference()); } esCb.request().build(builder); final SearchResponse response = esCb.build(builder).execute().actionGet(searchTimeout); final EsPagingResultBean<RESULT> list = new EsPagingResultBean<>(builder); final SearchHits searchHits = getSearchHits(response); searchHits.forEach(hit -> { final Map<String, Object> source = hit.getSourceAsMap(); final RESULT entity = createEntity(source, entityType); final DocMeta docMeta = ((EsAbstractEntity) entity).asDocMeta(); docMeta.id(hit.getId()); docMeta.version(hit.getVersion()); docMeta.seqNo(hit.getSeqNo()); docMeta.primaryTerm(hit.getPrimaryTerm()); list.add(entity); }); list.setPageSize(size); list.setAllRecordCount((int) searchHits.getTotalHits().value); list.setCurrentPageNumber(cb.getFetchPageNumber()); list.setTook(response.getTook().getMillis()); list.setTotalShards(response.getTotalShards()); list.setSuccessfulShards(response.getSuccessfulShards()); list.setFailedShards(response.getFailedShards()); list.setTotalHits(searchHits.getTotalHits()); list.setAggregation(response.getAggregations()); // #pending others return list; }