public IEnumerable <Document> IntersectQuery(IndexQueryServerSide query, FieldsToFetch fieldsToFetch, Reference <int> totalResults, Reference <int> skippedResults, IQueryResultRetriever retriever, JsonOperationContext documentsContext, Func <string, SpatialField> getSpatialField, CancellationToken token) { var method = query.Metadata.Query.Where as MethodExpression; if (method == null) { throw new InvalidQueryException($"Invalid intersect query. WHERE clause must contains just an intersect() method call while it got {query.Metadata.Query.Where.Type} expression", query.Metadata.QueryText, query.QueryParameters); } var methodName = method.Name; if (string.Equals("intersect", methodName) == false) { throw new InvalidQueryException($"Invalid intersect query. WHERE clause must contains just a single intersect() method call while it got '{methodName}' method", query.Metadata.QueryText, query.QueryParameters); } if (method.Arguments.Count <= 1) { throw new InvalidQueryException("The valid intersect query must have multiple intersect clauses.", query.Metadata.QueryText, query.QueryParameters); } var subQueries = new Query[method.Arguments.Count]; for (var i = 0; i < subQueries.Length; i++) { var whereExpression = method.Arguments[i] as QueryExpression; if (whereExpression == null) { throw new InvalidQueryException($"Invalid intersect query. The intersect clause at position {i} isn't a valid expression", query.Metadata.QueryText, query.QueryParameters); } subQueries[i] = GetLuceneQuery(documentsContext, query.Metadata, whereExpression, query.QueryParameters, _analyzer, getSpatialField); } //Not sure how to select the page size here??? The problem is that only docs in this search can be part //of the final result because we're doing an intersection query (but we might exclude some of them) var pageSize = GetPageSize(_searcher, query.PageSize); int pageSizeBestGuess = GetPageSize(_searcher, ((long)query.Start + query.PageSize) * 2); int skippedResultsInCurrentLoop = 0; int previousBaseQueryMatches = 0; var firstSubDocumentQuery = subQueries[0]; var sort = GetSort(query, getSpatialField); using (var scope = new IndexQueryingScope(_indexType, query, fieldsToFetch, _searcher, retriever, _state)) { //Do the first sub-query in the normal way, so that sorting, filtering etc is accounted for var search = ExecuteQuery(firstSubDocumentQuery, 0, pageSizeBestGuess, sort); var currentBaseQueryMatches = search.ScoreDocs.Length; var intersectionCollector = new IntersectionCollector(_searcher, search.ScoreDocs, _state); int intersectMatches; do { token.ThrowIfCancellationRequested(); if (skippedResultsInCurrentLoop > 0) { // We get here because out first attempt didn't get enough docs (after INTERSECTION was calculated) pageSizeBestGuess = pageSizeBestGuess * 2; search = ExecuteQuery(firstSubDocumentQuery, 0, pageSizeBestGuess, sort); previousBaseQueryMatches = currentBaseQueryMatches; currentBaseQueryMatches = search.ScoreDocs.Length; intersectionCollector = new IntersectionCollector(_searcher, search.ScoreDocs, _state); } for (var i = 1; i < subQueries.Length; i++) { _searcher.Search(subQueries[i], null, intersectionCollector, _state); } var currentIntersectResults = intersectionCollector.DocumentsIdsForCount(subQueries.Length).ToList(); intersectMatches = currentIntersectResults.Count; skippedResultsInCurrentLoop = pageSizeBestGuess - intersectMatches; } while (intersectMatches < pageSize && //stop if we've got enough results to satisfy the pageSize currentBaseQueryMatches < search.TotalHits && //stop if increasing the page size wouldn't make any difference previousBaseQueryMatches < currentBaseQueryMatches); //stop if increasing the page size didn't result in any more "base query" results var intersectResults = intersectionCollector.DocumentsIdsForCount(subQueries.Length).ToList(); //It's hard to know what to do here, the TotalHits from the base search isn't really the TotalSize, //because it's before the INTERSECTION has been applied, so only some of those results make it out. //Trying to give an accurate answer is going to be too costly, so we aren't going to try. totalResults.Value = search.TotalHits; skippedResults.Value = skippedResultsInCurrentLoop; //Using the final set of results in the intersectionCollector int returnedResults = 0; for (int i = query.Start; i < intersectResults.Count && (i - query.Start) < pageSizeBestGuess; i++) { var indexResult = intersectResults[i]; var document = _searcher.Doc(indexResult.LuceneId, _state); if (retriever.TryGetKey(document, _state, out string key) && scope.WillProbablyIncludeInResults(key) == false) { skippedResults.Value++; skippedResultsInCurrentLoop++; continue; } var result = retriever.Get(document, indexResult.Score, _state); if (scope.TryIncludeInResults(result) == false) { skippedResults.Value++; skippedResultsInCurrentLoop++; continue; } returnedResults++; yield return(result); if (returnedResults == pageSize) { yield break; } } } }
public IEnumerable <Document> IntersectQuery(IndexQueryServerSide query, FieldsToFetch fieldsToFetch, Reference <int> totalResults, Reference <int> skippedResults, IQueryResultRetriever retriever, CancellationToken token) { var subQueries = query.Query.Split(IntersectSeparators, StringSplitOptions.RemoveEmptyEntries); if (subQueries.Length <= 1) { throw new InvalidOperationException("Invalid INTERSECT query, must have multiple intersect clauses."); } //Not sure how to select the page size here??? The problem is that only docs in this search can be part //of the final result because we're doing an intersection query (but we might exclude some of them) int pageSizeBestGuess = (query.Start + query.PageSize) * 2; int intersectMatches, skippedResultsInCurrentLoop = 0; int previousBaseQueryMatches = 0, currentBaseQueryMatches; var firstSubDocumentQuery = GetLuceneQuery(subQueries[0], query.DefaultOperator, query.DefaultField, _analyzer); var sort = GetSort(query.SortedFields); using (var scope = new IndexQueryingScope(_indexType, query, fieldsToFetch, _searcher, retriever, _maxIndexOutputsPerDocument, _actualMaxIndexOutputsPerDocument)) { //Do the first sub-query in the normal way, so that sorting, filtering etc is accounted for var search = ExecuteQuery(firstSubDocumentQuery, 0, pageSizeBestGuess, sort); currentBaseQueryMatches = search.ScoreDocs.Length; var intersectionCollector = new IntersectionCollector(_searcher, search.ScoreDocs); do { token.ThrowIfCancellationRequested(); if (skippedResultsInCurrentLoop > 0) { // We get here because out first attempt didn't get enough docs (after INTERSECTION was calculated) pageSizeBestGuess = pageSizeBestGuess * 2; search = ExecuteQuery(firstSubDocumentQuery, 0, pageSizeBestGuess, sort); previousBaseQueryMatches = currentBaseQueryMatches; currentBaseQueryMatches = search.ScoreDocs.Length; intersectionCollector = new IntersectionCollector(_searcher, search.ScoreDocs); } for (var i = 1; i < subQueries.Length; i++) { var luceneSubQuery = GetLuceneQuery(subQueries[i], query.DefaultOperator, query.DefaultField, _analyzer); _searcher.Search(luceneSubQuery, null, intersectionCollector); } var currentIntersectResults = intersectionCollector.DocumentsIdsForCount(subQueries.Length).ToList(); intersectMatches = currentIntersectResults.Count; skippedResultsInCurrentLoop = pageSizeBestGuess - intersectMatches; } while (intersectMatches < query.PageSize && //stop if we've got enough results to satisfy the pageSize currentBaseQueryMatches < search.TotalHits && //stop if increasing the page size wouldn't make any difference previousBaseQueryMatches < currentBaseQueryMatches); //stop if increasing the page size didn't result in any more "base query" results var intersectResults = intersectionCollector.DocumentsIdsForCount(subQueries.Length).ToList(); //It's hard to know what to do here, the TotalHits from the base search isn't really the TotalSize, //because it's before the INTERSECTION has been applied, so only some of those results make it out. //Trying to give an accurate answer is going to be too costly, so we aren't going to try. totalResults.Value = search.TotalHits; skippedResults.Value = skippedResultsInCurrentLoop; //Using the final set of results in the intersectionCollector int returnedResults = 0; for (int i = query.Start; i < intersectResults.Count && (i - query.Start) < pageSizeBestGuess; i++) { var indexResult = intersectResults[i]; var document = _searcher.Doc(indexResult.LuceneId); string key; if (retriever.TryGetKey(document, out key) && scope.WillProbablyIncludeInResults(key) == false) { skippedResults.Value++; skippedResultsInCurrentLoop++; continue; } var result = retriever.Get(document, indexResult.Score); if (scope.TryIncludeInResults(result) == false) { skippedResults.Value++; skippedResultsInCurrentLoop++; continue; } returnedResults++; yield return(result); if (returnedResults == query.PageSize) { yield break; } } } }