/// <summary> /// Queries the specified index. /// </summary> /// <param name="index">The index.</param> /// <param name="query">The query.</param> /// <param name="includes">The includes are ignored for this implementation.</param> /// <param name="metadataOnly">Load just the document metadata</param> /// <param name="indexEntriesOnly">Include index entries</param> public QueryResult Query(string index, IndexQuery query, string[] includes, bool metadataOnly = false, bool indexEntriesOnly = false) { query.PageSize = Math.Min(query.PageSize, database.Configuration.MaxPageSize); CurrentOperationContext.Headers.Value = OperationsHeaders; // metadataOnly is not supported for embedded // indexEntriesOnly is not supported for embedded QueryResultWithIncludes queryResult; if (index.StartsWith("dynamic/", StringComparison.InvariantCultureIgnoreCase) || index.Equals("dynamic", StringComparison.InvariantCultureIgnoreCase)) { string entityName = null; if (index.StartsWith("dynamic/")) { entityName = index.Substring("dynamic/".Length); } queryResult = database.ExecuteDynamicQuery(entityName, query.Clone()); } else { queryResult = database.Query(index, query.Clone()); } EnsureLocalDate(queryResult.Results); var loadedIds = new HashSet <string>( queryResult.Results .Where(x => x["@metadata"] != null) .Select(x => x["@metadata"].Value <string>("@id")) .Where(x => x != null) ); if (includes != null) { var includeCmd = new AddIncludesCommand(database, TransactionInformation, (etag, doc) => queryResult.Includes.Add(doc), includes, loadedIds); foreach (var result in queryResult.Results) { includeCmd.Execute(result); } includeCmd.AlsoInclude(queryResult.IdsToInclude); EnsureLocalDate(queryResult.Includes); } return(queryResult); }
public override Task <DocumentAndNavigationInfo> GetDocument() { if (string.IsNullOrEmpty(id)) { var query = templateQuery.Clone(); query.Start = itemIndex; query.PageSize = 1; return(DatabaseCommands.QueryAsync(indexName, query, null) .ContinueWith( t => { var info = PopulateDocumentOrConflictsFromDocuments( t.Result.Results.Select(r => r.ToJsonDocument())); info.TotalDocuments = t.Result.TotalResults; info.Index = itemIndex; info.ParentPath = GetParentPath(); info.UrlForFirst = GetUrlForIndex(0); info.UrlForPrevious = itemIndex > 0 ? GetUrlForIndex(itemIndex - 1) : null; info.UrlForNext = itemIndex < t.Result.TotalResults - 1 ? GetUrlForIndex(itemIndex + 1) : null; info.UrlForLast = GetUrlForIndex(t.Result.TotalResults - 1); return info; })); } var getDocumentTask = DatabaseCommands.GetAsync(id); var getStatisticsTask = QueryIndexForDocument(); return(TaskEx.WhenAll(getDocumentTask, getStatisticsTask) .ContinueWith(_ => { var info = PopulateDocumentOrConflictsFromTask(getDocumentTask, id); info.Index = itemIndex; info.TotalDocuments = getStatisticsTask.Result.TotalResults; info.ParentPath = GetParentPath(); info.UrlForFirst = GetUrlForIndex(0); info.UrlForPrevious = itemIndex > 0 ? GetUrlForIndex(itemIndex - 1) : null; info.UrlForNext = itemIndex < getStatisticsTask.Result.TotalResults - 1 ? GetUrlForIndex(itemIndex + 1) : null; info.UrlForLast = GetUrlForIndex(getStatisticsTask.Result.TotalResults - 1); return info; } )); }
public static List <DynamicQueryOptimizer.Explanation> ExplainDynamicIndexSelection(this DocumentDatabase self, string entityName, IndexQuery query) { var explanations = new List <DynamicQueryOptimizer.Explanation>(); new DynamicQueryOptimizer(self) .SelectAppropriateIndex(entityName, query.Clone(), explanations); return(explanations); }
public override Task <DocumentAndNavigationInfo> GetDocument() { if (string.IsNullOrEmpty(id)) { var query = templateQuery.Clone(); query.Start = itemIndex; query.PageSize = 1; return (DatabaseCommands.QueryAsync(indexName, query, null) .ContinueWith( t => new DocumentAndNavigationInfo { Document = t.Result.Results.Count > 0 ? t.Result.Results[0].ToJsonDocument() : null, TotalDocuments = t.Result.TotalResults, Index = itemIndex, ParentPath = GetParentPath(), UrlForFirst = GetUrlForIndex(0), UrlForPrevious = itemIndex > 0 ? GetUrlForIndex(itemIndex - 1) : null, UrlForNext = itemIndex < t.Result.TotalResults - 1 ? GetUrlForIndex(itemIndex + 1) : null, UrlForLast = GetUrlForIndex(t.Result.TotalResults - 1), })); } else { var getDocumentTask = DatabaseCommands.GetAsync(id); var getStatisticsTask = QueryIndexForDocument(); return(TaskEx.WhenAll(getDocumentTask, getStatisticsTask) .ContinueWith(_ => new DocumentAndNavigationInfo { Document = getDocumentTask.Result, Index = itemIndex, TotalDocuments = getStatisticsTask.Result.TotalResults, ParentPath = GetParentPath(), UrlForFirst = GetUrlForIndex(0), UrlForPrevious = itemIndex > 0 ? GetUrlForIndex(itemIndex - 1) : null, UrlForNext = itemIndex < getStatisticsTask.Result.TotalResults - 1 ? GetUrlForIndex(itemIndex + 1) : null, UrlForLast = GetUrlForIndex(getStatisticsTask.Result.TotalResults - 1), } )); } }
/// <summary> /// Queries the specified index. /// </summary> /// <param name="index">The index.</param> /// <param name="query">The query.</param> /// <param name="includes">The includes are ignored for this implementation.</param> public QueryResult Query(string index, IndexQuery query, string[] includes) { query.PageSize = Math.Min(query.PageSize, database.Configuration.MaxPageSize); CurrentOperationContext.Headers.Value = OperationsHeaders; if (index.StartsWith("dynamic/", StringComparison.InvariantCultureIgnoreCase) || index.Equals("dynamic", StringComparison.InvariantCultureIgnoreCase)) { string entityName = null; if (index.StartsWith("dynamic/")) { entityName = index.Substring("dynamic/".Length); } return(database.ExecuteDynamicQuery(entityName, query.Clone())); } var queryResult = database.Query(index, query.Clone()); EnsureLocalDate(queryResult.Results); EnsureLocalDate(queryResult.Includes); return(queryResult); }
public static string FindDynamicIndexName(this DocumentDatabase self, string entityName, IndexQuery query) { return(new DynamicQueryOptimizer(self).SelectAppropriateIndex(entityName, query.Clone())); }
public static string FindDynamicIndexName(this DocumentDatabase self, string entityName, IndexQuery query) { var result = new DynamicQueryOptimizer(self).SelectAppropriateIndex(entityName, query.Clone()); if (result.MatchType == DynamicQueryMatchType.Complete) { return(result.IndexName); } return(null); }