Java Code Examples for org.elasticsearch.action.search.SearchRequestBuilder#addSort()
The following examples show how to use
org.elasticsearch.action.search.SearchRequestBuilder#addSort() .
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: SearchRequestBuilderStrategy.java From usergrid with Apache License 2.0 | 6 votes |
/** * Apply our default sort predicate logic */ private void applyDefaultSortPredicates( final SearchRequestBuilder srb, final GeoSortFields geoFields ) { //we have geo fields, sort through them in visit order for ( String geoField : geoFields.fields() ) { final GeoDistanceSortBuilder geoSort = geoFields.applyOrder( geoField, SortOrder.ASC ); srb.addSort( geoSort ); } //now sort by edge timestamp, then entity id //sort by the edge timestamp srb.addSort( SortBuilders.fieldSort( IndexingUtils.EDGE_TIMESTAMP_FIELDNAME ).order( SortOrder.DESC ) ); // removing secondary sort by entity ID -- takes ES resources and provides no benefit //sort by the entity id if our times are equal //srb.addSort( SortBuilders.fieldSort( IndexingUtils.ENTITY_ID_FIELDNAME ).order( SortOrder.ASC ) ); return; }
Example 2
Source File: ElasticSearchDAOV5.java From conductor with Apache License 2.0 | 5 votes |
private void addSortOptionToSearchRequest(SearchRequestBuilder searchRequestBuilder, String sortOption) { SortOrder order = SortOrder.ASC; String field = sortOption; int indx = sortOption.indexOf(':'); if (indx > 0) { // Can't be 0, need the field name at-least field = sortOption.substring(0, indx); order = SortOrder.valueOf(sortOption.substring(indx + 1)); } searchRequestBuilder.addSort(field, order); }
Example 3
Source File: ElasticUtils.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
public static SearchResponse scrollOneTimeWithHits(Client client, SearchRequestBuilder requestBuilder, Select originalSelect, int resultSize) { SearchResponse responseWithHits;SearchRequestBuilder scrollRequest = requestBuilder .setScroll(new TimeValue(60000)) .setSize(resultSize); boolean ordered = originalSelect.isOrderdSelect(); if(!ordered) scrollRequest.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC); responseWithHits = scrollRequest.get(); //on ordered select - not using SCAN , elastic returns hits on first scroll //es5.0 elastic always return docs on scan // if(!ordered) { // responseWithHits = client.prepareSearchScroll(responseWithHits.getScrollId()).setScroll(new TimeValue(600000)).get(); // } return responseWithHits; }
Example 4
Source File: ElasticJoinExecutor.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
protected SearchResponse scrollOneTimeWithMax(Client client,TableInJoinRequestBuilder tableRequest) { SearchResponse responseWithHits;SearchRequestBuilder scrollRequest = tableRequest.getRequestBuilder() .setScroll(new TimeValue(60000)) .setSize(MAX_RESULTS_ON_ONE_FETCH); boolean ordered = tableRequest.getOriginalSelect().isOrderdSelect(); if(!ordered) scrollRequest.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC); responseWithHits = scrollRequest.get(); //on ordered select - not using SCAN , elastic returns hits on first scroll //es5.0 elastic always return docs on scan // if(!ordered) // responseWithHits = client.prepareSearchScroll(responseWithHits.getScrollId()).setScroll(new TimeValue(600000)).get(); return responseWithHits; }
Example 5
Source File: ElasticsearchLengthOfStringSortingStrategy.java From vertexium with Apache License 2.0 | 5 votes |
@Override public void updateElasticsearchQuery( Graph graph, Elasticsearch5SearchIndex searchIndex, SearchRequestBuilder q, QueryParameters parameters, SortDirection direction ) { PropertyDefinition propertyDefinition = graph.getPropertyDefinition(getPropertyName()); SortOrder esOrder = direction == SortDirection.ASCENDING ? SortOrder.ASC : SortOrder.DESC; Map<String, Object> scriptParams = new HashMap<>(); String[] propertyNames = searchIndex.getPropertyNames(graph, getPropertyName(), parameters.getAuthorizations()); List<String> fieldNames = Arrays.stream(propertyNames) .map(propertyName -> { String suffix = propertyDefinition.getDataType() == String.class ? Elasticsearch5SearchIndex.EXACT_MATCH_PROPERTY_NAME_SUFFIX : ""; return propertyName + suffix; }) .collect(Collectors.toList()); scriptParams.put("fieldNames", fieldNames); scriptParams.put("direction", esOrder.name()); Script script = new Script(ScriptType.INLINE, "painless", scriptSource, scriptParams); ScriptSortBuilder.ScriptSortType sortType = ScriptSortBuilder.ScriptSortType.NUMBER; q.addSort(SortBuilders.scriptSort(script, sortType).order(SortOrder.ASC)); }
Example 6
Source File: ElasticsearchLengthOfStringSortingStrategy.java From vertexium with Apache License 2.0 | 5 votes |
@Override public void updateElasticsearchQuery( Graph graph, Elasticsearch7SearchIndex searchIndex, SearchRequestBuilder q, QueryParameters parameters, SortDirection direction ) { PropertyDefinition propertyDefinition = graph.getPropertyDefinition(getPropertyName()); SortOrder esOrder = direction == SortDirection.ASCENDING ? SortOrder.ASC : SortOrder.DESC; Map<String, Object> scriptParams = new HashMap<>(); String[] propertyNames = searchIndex.getPropertyNames(graph, getPropertyName(), parameters.getAuthorizations()); List<String> fieldNames = Arrays.stream(propertyNames) .map(propertyName -> { String suffix = propertyDefinition.getDataType() == String.class ? Elasticsearch7SearchIndex.EXACT_MATCH_PROPERTY_NAME_SUFFIX : ""; return propertyName + suffix; }) .collect(Collectors.toList()); scriptParams.put("fieldNames", fieldNames); scriptParams.put("direction", esOrder.name()); Script script = new Script(ScriptType.INLINE, "painless", scriptSource, scriptParams); ScriptSortBuilder.ScriptSortType sortType = ScriptSortBuilder.ScriptSortType.NUMBER; q.addSort(SortBuilders.scriptSort(script, sortType).order(SortOrder.ASC)); }
Example 7
Source File: SearchQueryServiceImpl.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
private SearchResponse executeSearch(final RepositoryQueryBuilder repoQuery, final String[] searchableIndexes, final int from, final int size, @Nullable final QueryBuilder postFilter) { SearchRequestBuilder searchRequestBuilder = client.get().prepareSearch(searchableIndexes) .setTypes(TYPE) .setQuery(repoQuery) .setFrom(from) .setSize(size) .setProfile(profile); if (repoQuery.sort != null) { for (SortBuilder entry : repoQuery.sort) { searchRequestBuilder.addSort(entry); } } if (postFilter != null) { searchRequestBuilder.setPostFilter(postFilter); } if (repoQuery.timeout != null) { searchRequestBuilder.setTimeout(repoQuery.timeout.getSeconds() + "s"); } SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(); if (profile) { logProfileResults(searchResponse); } return searchResponse; }
Example 8
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 9
Source File: ThreadAnalysisQueryHandler.java From uavstack with Apache License 2.0 | 5 votes |
@SuppressWarnings("rawtypes") private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter, List<SortBuilder> sorts) { String date = data.getRequest("indexdate"); String currentIndex; if (date != null) { // 指定index currentIndex = this.indexMgr.getIndexByDate(date); } else { // current index currentIndex = this.indexMgr.getCurrentIndex(); } SearchRequestBuilder srb = client.getClient().prepareSearch(currentIndex) .setTypes(ThreadAnalysisIndexMgr.JTA_TABLE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH); int from = DataConvertHelper.toInt(data.getRequest("from"), -1); int size = DataConvertHelper.toInt(data.getRequest("size"), -1); if (from != -1 && size != -1) { srb = srb.setFrom(from).setSize(size); } srb.setQuery(queryBuilder); if (postFilter != null) { srb.setPostFilter(postFilter); } for (SortBuilder sb : sorts) { srb.addSort(sb); } SearchResponse sr = srb.get(TimeValue.timeValueMillis(timeout)); return sr; }
Example 10
Source File: QueryComponent.java From elasticsearch-reindex-tool with Apache License 2.0 | 5 votes |
public SearchResponse prepareSearchScrollRequest() { // find out how many indices and shards are affected by this query to not get huge result sets when there are very many indices affected by the name, e.g. when wildcards are used // otherwise we regularly run into OOMs when a query goes against a large number of indices // I did not find a better way to find out the number of shards than to query a list of indices and for each index query the number of shards via the settings GetSettingsResponse getSettingsResponse = client.admin().indices().getSettings(new GetSettingsRequest().indices(dataPointer.getIndexName())).actionGet(); int numShards = 0, numIndices = 0; for(ObjectCursor<Settings> settings : getSettingsResponse.getIndexToSettings().values()) { numShards += settings.value.getAsInt("index.number_of_shards", 0); numIndices++; } int sizePerShard = (int)Math.ceil((double)SCROLL_SHARD_LIMIT/numShards); logger.info("Found " + numIndices + " indices and " + numShards + " shards matching the index-pattern, thus setting the sizePerShard to " + sizePerShard); SearchRequestBuilder searchRequestBuilder = client.prepareSearch(dataPointer.getIndexName()) .setTypes(dataPointer.getTypeName()) .setSearchType(SearchType.SCAN) .addFields("_ttl", "_source") .setScroll(new TimeValue(SCROLL_TIME_LIMIT)) .setSize(sizePerShard); if (!Strings.isNullOrEmpty(query.getQuery())) { searchRequestBuilder.setQuery(query.getQuery()); } if (!Strings.isNullOrEmpty(query.getSortField())) { searchRequestBuilder.addSort(new FieldSortBuilder(query.getSortField()).order(query.getSortOrder())); } bound.map(resolvedBound -> boundedFilterFactory.createBoundedFilter(segmentationField.get(), resolvedBound)) .ifPresent(searchRequestBuilder::setQuery); return searchRequestBuilder.execute().actionGet(); }
Example 11
Source File: Sort.java From yacy_grid_mcp with GNU Lesser General Public License v2.1 | 5 votes |
public SearchRequestBuilder sort(SearchRequestBuilder request) { if (this.option == Option.DATE) { return request.addSort(WebMapping.last_modified.getMapping().name(), this.direction); } if (this.option == Option.METADATA) { return request.addSort(this.metafield, this.direction); } return request; }
Example 12
Source File: ElasticSearchService.java From Mahuta with Apache License 2.0 | 5 votes |
@Override public Page<Metadata> searchDocuments(String indexName, Query query, PageRequest pageRequest) { log.debug("Search documents in ElasticSearch [indexName: {}, query: {}]", indexName, query); // Validation ValidatorUtils.rejectIfNull("pageRequest", pageRequest); // Format index indexName = Optional.ofNullable(indexName).map(String::toLowerCase).orElse(ALL_INDICES); // Build query SearchRequestBuilder requestBuilder = client.prepareSearch(indexName) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(buildQuery(query)) .setFrom(pageRequest.getSize() * pageRequest.getPage()).setSize(pageRequest.getSize()); if (pageRequest.getSort() != null) { requestBuilder.addSort(new FieldSortBuilder(pageRequest.getSort()) .order(pageRequest.isAscending() ? SortOrder.ASC : SortOrder.DESC).unmappedType("date")); } log.trace(requestBuilder.toString()); // Run query SearchResponse searchResponse = requestBuilder.execute().actionGet(); log.trace("Search documents in ElasticSearch [indexName: {}, query: {}]: {}", indexName, query, searchResponse); List<Metadata> result = Arrays.stream(searchResponse.getHits().getHits()) .map(hit -> convert(hit.getIndex(), hit.getId(), hit.getSourceAsMap())).collect(Collectors.toList()); log.debug("Search documents in ElasticSearch [indexName: {}, query: {}]: {}", indexName, query, result); return Page.of(pageRequest, result, Math.toIntExact(searchResponse.getHits().getTotalHits())); }
Example 13
Source File: ESUtil.java From ns4_gear_watchdog with Apache License 2.0 | 5 votes |
public <T> List<T> queryDocumentByParam(String indices, String type, MonitorEventSearchParam param, Class<T> clazz) { SearchRequestBuilder builder = buildRequest(indices,type); builder.addSort("timeHappend", SortOrder.ASC); builder.setQuery(convertParam(param)); builder.setFrom(0).setSize(10); SearchResponse resp = builder.get(); return convertResponse(resp,clazz); }
Example 14
Source File: NewLogQueryHandler.java From uavstack with Apache License 2.0 | 4 votes |
@SuppressWarnings("rawtypes") private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter, SortBuilder[] sorts) { String indexDate = data.getRequest("indexdate"); String currentIndex; if (indexDate != null) { // 获得指定的index currentIndex = this.indexMgr.getIndexByDate(indexDate); } else { // get current index currentIndex = this.indexMgr.getCurrentIndex(); } // get logtype for search SearchRequestBuilder srb = null; if (StringHelper.isEmpty(data.getRequest("logtype"))) { srb = client.getClient().prepareSearch(currentIndex).setSearchType(SearchType.DFS_QUERY_THEN_FETCH); } else { String logType = data.getRequest("logtype").replace('.', '_'); srb = client.getClient().prepareSearch(currentIndex).setTypes(logType) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH); } int from = DataConvertHelper.toInt(data.getRequest("from"), -1); int size = DataConvertHelper.toInt(data.getRequest("size"), -1); if (from != -1 && size != -1) { long startLine = DataConvertHelper.toLong(data.getRequest("sline"), -1); long endLine = DataConvertHelper.toLong(data.getRequest("eline"), -1); // 判断如果只有endline则需要将结果倒序,取其前100行,然后在返回响应时再逆序回来,从而取到endline前的100行 if (startLine == -1 && endLine > -1) { srb = srb.addSort("l_timestamp", SortOrder.DESC).addSort("l_num", SortOrder.DESC).setFrom(from) .setSize(size); } else { srb = srb.setFrom(from).setSize(size); } } srb.setQuery(queryBuilder); if (postFilter != null) { srb.setPostFilter(postFilter); } if (sorts != null && sorts.length > 0) { for (SortBuilder sb : sorts) { srb.addSort(sb); } } SearchResponse sr = srb.get(TimeValue.timeValueMillis(timeout)); return sr; }
Example 15
Source File: SlowOperQueryHandler.java From uavstack with Apache License 2.0 | 4 votes |
@SuppressWarnings("rawtypes") private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter, SortBuilder[] sorts) { String indexDate = data.getRequest("indexdate"); String currentIndex; if (indexDate != null) { // 获得指定的index currentIndex = this.indexMgr.getIndexByDate(indexDate); } else { // get current index currentIndex = this.indexMgr.getCurrentIndex(); } SearchRequestBuilder srb = client.getClient().prepareSearch(currentIndex).setTypes(buildTypes(data)) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH); int from = DataConvertHelper.toInt(data.getRequest("from"), -1); int size = DataConvertHelper.toInt(data.getRequest("size"), -1); if (from != -1 && size != -1) { srb = srb.setFrom(from).setSize(size); } srb.setQuery(queryBuilder); if (postFilter != null) { srb.setPostFilter(postFilter); } if (sorts != null && sorts.length > 0) { for (SortBuilder sb : sorts) { srb.addSort(sb); } } SearchResponse sr = srb.get(TimeValue.timeValueMillis(timeout)); return sr; }
Example 16
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 17
Source File: SearchRequestBuilderStrategy.java From usergrid with Apache License 2.0 | 4 votes |
/** * Invoked when there are sort predicates */ private void applySortPredicates( final SearchRequestBuilder srb, final List<SortPredicate> sortPredicates, final GeoSortFields geoFields, final Map<String, Class> knownFieldsWithType ) { Preconditions.checkNotNull(sortPredicates, "sort predicates list cannot be null"); for ( SortPredicate sp : sortPredicates ) { final SortOrder order = sp.getDirection().toEsSort(); final String propertyName = sp.getPropertyName(); // if the user specified a geo field in their sort, then honor their sort order and use the field they // specified. this is added first so it's known on the response hit when fetching the geo distance later // see org.apache.usergrid.persistence.index.impl.IndexingUtils.parseIndexDocId(org.elasticsearch.search.SearchHit, boolean) if ( geoFields.contains( propertyName ) ) { final GeoDistanceSortBuilder geoSort = geoFields.applyOrder( propertyName, SortOrder.ASC ); srb.addSort( geoSort ); } // fieldsWithType gives the caller an option to provide any schema related details on properties that // might appear in a sort predicate. loop through these and set a specific sort, rather than adding a sort // for all possible types else if ( knownFieldsWithType != null && knownFieldsWithType.size() > 0 && knownFieldsWithType.containsKey(propertyName)) { String esFieldName = EsQueryVistor.getFieldNameForClass(knownFieldsWithType.get(propertyName)); // always make sure string sorts use the unanalyzed field if ( esFieldName.equals(IndexingUtils.FIELD_STRING_NESTED)){ esFieldName = IndexingUtils.FIELD_STRING_NESTED_UNANALYZED; } srb.addSort( createSort( order, esFieldName, propertyName ) ); } //apply regular sort logic which check all possible data types, since this is not a known property name else { //sort order is arbitrary if the user changes data types. Double, long, string, boolean are supported //default sort types srb.addSort( createSort( order, IndexingUtils.FIELD_DOUBLE_NESTED, propertyName ) ); srb.addSort( createSort( order, IndexingUtils.FIELD_LONG_NESTED, propertyName ) ); /** * We always want to sort by the unanalyzed string field to ensure correct ordering */ srb.addSort( createSort( order, IndexingUtils.FIELD_STRING_NESTED_UNANALYZED, propertyName ) ); srb.addSort( createSort( order, IndexingUtils.FIELD_BOOLEAN_NESTED, propertyName ) ); } } }
Example 18
Source File: InvokeChainQueryHandler.java From uavstack with Apache License 2.0 | 4 votes |
/** * * @param data * @param queryBuilder * @param postFilter * @return */ @SuppressWarnings("rawtypes") private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter, SortBuilder[] sorts) { String date = data.getRequest("indexdate"); String currentIndex; if (date != null) { // 指定index currentIndex = this.indexMgr.getIndexByDate(date); } else { // current index currentIndex = this.indexMgr.getCurrentIndex(); } SearchRequestBuilder srb = client.getClient().prepareSearch(currentIndex) .setTypes(InvokeChainIndexMgr.IVC_Table).setSearchType(SearchType.DFS_QUERY_THEN_FETCH); int from = DataConvertHelper.toInt(data.getRequest("from"), -1); int size = DataConvertHelper.toInt(data.getRequest("size"), -1); if (from != -1 && size != -1) { srb = srb.setFrom(from).setSize(size); } srb.setQuery(queryBuilder); if (postFilter != null) { srb.setPostFilter(postFilter); } if (sorts != null && sorts.length > 0) { for (SortBuilder sb : sorts) { srb.addSort(sb); } } SearchResponse sr = srb.get(TimeValue.timeValueMillis(timeout)); return sr; }
Example 19
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 20
Source File: ProductQueryServiceImpl.java From elasticsearch-tutorial with MIT License | 4 votes |
@Override public ProductSearchResult searchProducts(SearchCriteria searchCriteria) { QueryBuilder queryBuilder = getQueryBuilder(searchCriteria); SearchRequestBuilder requestBuilder = getSearchRequestBuilder(searchCriteria.getIndexes(), searchCriteria.getDocumentTypes(), searchCriteria.getFrom(), searchCriteria.getSize()); requestBuilder.addFields(SearchDocumentFieldName.productQueryFields); if(searchCriteria.isRescoreOnSoldOut()) { Rescorer rescorer = RescoreBuilder.queryRescorer(QueryBuilders.termQuery(SearchDocumentFieldName.SOLD_OUT.getFieldName(), false)) .setQueryWeight(1.0f) //default .setRescoreQueryWeight(1.5f) ; requestBuilder.setRescorer(rescorer); } if (searchCriteria.hasFilters()) { AndFilterBuilder andFilterBuilder = getFilterBuilderForSearchCriteria(searchCriteria); requestBuilder.setQuery(QueryBuilders.filteredQuery(queryBuilder, andFilterBuilder)); } else { requestBuilder.setQuery(queryBuilder); } if (!searchCriteria.isNoFacets() && searchCriteria.getFacets().size() > 0) { addFacets(searchCriteria, requestBuilder); } //Add sorting if(searchCriteria.getSortOrder() !=null) { //First on given field requestBuilder.addSort(SortBuilders.fieldSort(SearchDocumentFieldName.AVAILABLE_DATE.getFieldName()).order(searchCriteria.getSortOrder()).missing("_last")); //then on score based requestBuilder.addSort(SortBuilders.scoreSort()); } logger.debug("Executing following search request:" + requestBuilder.internalBuilder().toString()); SearchResponse searchResponse = requestBuilder.execute().actionGet(); printSearchResponseForDebug(searchResponse); return getProductSearchResults(searchResponse); }