@Throws(IOException::class) fun process(rb: ResponseBuilder) { val req: SolrQueryRequest = rb.req val rsp: SolrQueryResponse = rb.rsp val params: SolrParams = req.getParams() if (!params.getBool(COMPONENT_NAME, true)) { return } val searcher: SolrIndexSearcher = req.getSearcher() if (rb.getQueryCommand().getOffset() < 0) { throw SolrException( SolrException.ErrorCode.BAD_REQUEST, "'start' parameter cannot be negative" ) } val timeAllowed = params.getInt(CommonParams.TIME_ALLOWED, -1) as Long if (null != rb.getCursorMark() && 0 < timeAllowed) { throw SolrException( SolrException.ErrorCode.BAD_REQUEST, "Can not search using both " + CursorMarkParams.CURSOR_MARK_PARAM + " and " + CommonParams.TIME_ALLOWED ) } val ids: String = params.get(ShardParams.IDS) if (ids != null) { val idField: SchemaField = searcher.getSchema().getUniqueKeyField() val idArr: List<String> = StrUtils.splitSmart(ids, ",", true) val luceneIds = IntArray(idArr.size) var docs = 0 for (i in idArr.indices) { val id: Int = req.getSearcher().getFirstMatch( Term( idField.getName(), idField.getType().toInternal( idArr[i] ) ) ) if (id >= 0) luceneIds[docs++] = id } val res = DocListAndSet() res.docList = DocSlice(0, docs, luceneIds, null, docs, 0) if (rb.isNeedDocSet()) { val queries: MutableList<Query> = ArrayList() queries.add(rb.getQuery()) val filters: List<Query> = rb.getFilters() if (filters != null) queries.addAll(filters) res.docSet = searcher.getDocSet(queries) } rb.setResults(res) val ctx = ResultContext() ctx.docs = rb.getResults().docList ctx.query = null rsp.add("response", ctx) return } val cmd: SolrIndexSearcher.QueryCommand = rb.getQueryCommand() cmd.setTimeAllowed(timeAllowed) val result: SolrIndexSearcher.QueryResult = QueryResult() val groupingSpec: GroupingSpecification = rb.getGroupingSpec() if (groupingSpec != null) { try { val needScores = cmd.getFlags() and SolrIndexSearcher.GET_SCORES !== 0 if (params.getBool(GroupParams.GROUP_DISTRIBUTED_FIRST, false)) { val topsGroupsActionBuilder: CommandHandler.Builder = Builder().setQueryCommand(cmd).setNeedDocSet(false).setIncludeHitCount(true) .setSearcher(searcher) for (field in groupingSpec.getFields()) { topsGroupsActionBuilder.addCommandField( Builder().setField( searcher.getSchema().getField(field) ).setGroupSort(groupingSpec.getGroupSort()) .setTopNGroups(cmd.getOffset() + cmd.getLen()) .setIncludeGroupCount(groupingSpec.isIncludeGroupCount()).build() ) } val commandHandler: CommandHandler = topsGroupsActionBuilder.build() commandHandler.execute() val serializer = SearchGroupsResultTransformer(searcher) rsp.add("firstPhase", commandHandler.processResult(result, serializer)) rsp.add("totalHitCount", commandHandler.getTotalHitCount()) rb.setResult(result) return } else if (params.getBool(GroupParams.GROUP_DISTRIBUTED_SECOND, false)) { val secondPhaseBuilder: CommandHandler.Builder = Builder().setQueryCommand(cmd) .setTruncateGroups(groupingSpec.isTruncateGroups() && groupingSpec.getFields().length > 0) .setSearcher(searcher) for (field in groupingSpec.getFields()) { var topGroupsParam: Array<String?> = params.getParams(GroupParams.GROUP_DISTRIBUTED_TOPGROUPS_PREFIX + field) if (topGroupsParam == null) { topGroupsParam = arrayOfNulls(0) } val topGroups: MutableList<SearchGroup<BytesRef>> = ArrayList(topGroupsParam.size) for (topGroup in topGroupsParam) { val searchGroup: SearchGroup<BytesRef> = SearchGroup() if (topGroup != TopGroupsShardRequestFactory.GROUP_NULL_VALUE) { searchGroup.groupValue = BytesRef( searcher.getSchema().getField(field).getType() .readableToIndexed(topGroup) ) } topGroups.add(searchGroup) } secondPhaseBuilder.addCommandField( Builder().setField( searcher.getSchema().getField(field) ).setGroupSort(groupingSpec.getGroupSort()) .setSortWithinGroup(groupingSpec.getSortWithinGroup()) .setFirstPhaseGroups(topGroups) .setMaxDocPerGroup(groupingSpec.getGroupOffset() + groupingSpec.getGroupLimit()) .setNeedScores(needScores).setNeedMaxScore(needScores).build() ) } for (query in groupingSpec.getQueries()) { secondPhaseBuilder.addCommandField( Builder().setDocsToCollect(groupingSpec.getOffset() + groupingSpec.getLimit()) .setSort(groupingSpec.getGroupSort()).setQuery(query, rb.req) .setDocSet(searcher).build() ) } val commandHandler: CommandHandler = secondPhaseBuilder.build() commandHandler.execute() val serializer = TopGroupsResultTransformer(rb) rsp.add("secondPhase", commandHandler.processResult(result, serializer)) rb.setResult(result) return } val maxDocsPercentageToCache: Int = params.getInt(GroupParams.GROUP_CACHE_PERCENTAGE, 0) val cacheSecondPassSearch = maxDocsPercentageToCache >= 1 && maxDocsPercentageToCache <= 100 val defaultTotalCount: Grouping.TotalCount = if (groupingSpec.isIncludeGroupCount()) TotalCount.grouped else TotalCount.ungrouped val limitDefault: Int = cmd.getLen() val grouping: Grouping<*, *> = Grouping<Any?, Any?>( searcher, result, cmd, cacheSecondPassSearch, maxDocsPercentageToCache, groupingSpec.isMain() ) grouping.setSort(groupingSpec.getGroupSort()) .setGroupSort(groupingSpec.getSortWithinGroup()) .setDefaultFormat(groupingSpec.getResponseFormat()).setLimitDefault(limitDefault) .setDefaultTotalCount(defaultTotalCount) .setDocsPerGroupDefault(groupingSpec.getGroupLimit()) .setGroupOffsetDefault(groupingSpec.getGroupOffset()) .setGetGroupedDocSet(groupingSpec.isTruncateGroups()) if (groupingSpec.getFields() != null) { for (field in groupingSpec.getFields()) { grouping.addFieldCommand(field, rb.req) } } if (groupingSpec.getFunctions() != null) { for (groupByStr in groupingSpec.getFunctions()) { grouping.addFunctionCommand(groupByStr, rb.req) } } if (groupingSpec.getQueries() != null) { for (groupByStr in groupingSpec.getQueries()) { grouping.addQueryCommand(groupByStr, rb.req) } } if (rb.doHighlights || rb.isDebug() || params.getBool(MoreLikeThisParams.MLT, false)) { cmd.setFlags(SolrIndexSearcher.GET_DOCLIST) } grouping.execute() if (grouping.isSignalCacheWarning()) { rsp.add( "cacheWarning", java.lang.String.format( Locale.ROOT, "Cache limit of %d percent relative to maxdoc has exceeded. Please increase cache size or disable caching.", maxDocsPercentageToCache ) ) } rb.setResult(result) if (grouping.mainResult != null) { val ctx = ResultContext() ctx.docs = grouping.mainResult ctx.query = null rsp.add("response", ctx) rsp.getToLog().add("hits", grouping.mainResult.matches()) } else if (!grouping.getCommands().isEmpty()) { rsp.add("grouped", result.groupedResults) rsp.getToLog().add("hits", grouping.getCommands().get(0).getMatches()) } return } catch (e: jdk.internal.org.jline.reader.SyntaxError) { throw SolrException(SolrException.ErrorCode.BAD_REQUEST, e) } } searcher.search(result, cmd) rb.setResult(result) val ctx = ResultContext() ctx.docs = rb.getResults().docList ctx.query = rb.getQuery() rsp.add("response", ctx) rsp.getToLog().add("hits", rb.getResults().docList.matches()) if (!rb.req.getParams().getBool(ShardParams.IS_SHARD, false)) { if (null != rb.getNextCursorMark()) { rb.rsp.add( CursorMarkParams.CURSOR_MARK_NEXT, rb.getNextCursorMark().getSerializedTotem() ) } } if (rb.mergeFieldHandler != null) { rb.mergeFieldHandler.handleMergeFields(rb, searcher) } else { doFieldSortValues(rb, searcher) } doPrefetch(rb) }
Actually run the query