Esempio n. 1
0
        internal IEnumerable <object> Run()
        {
            int startRow = StartRow;
            int rowLimit = MaxResults;
            // Used for internal Microsoft telemetry
            string clientFunction = "Search";

            if (All.IsPresent)
            {
                clientFunction = "Scanner";
                startRow       = 0;
                rowLimit       = 500;
                SortList       = new Hashtable()
                {
                    { "[DocId]", "ascending" }      // Special optimized sorting when iterating all items
                };
            }

            var    currentCount = 0;
            string lastDocId    = "0";
            PnPResultTableCollection finalResults = null;

            do
            {
                KeywordQuery keywordQuery = CreateKeywordQuery(clientFunction);
                keywordQuery.StartRow = startRow;
                keywordQuery.RowLimit = rowLimit;

                if (All.IsPresent)
                {
                    if (currentCount != 0)
                    {
                        keywordQuery.Refiners = null; // Only need to set on first page for auto paging
                    }
                    keywordQuery.StartRow   = 0;
                    keywordQuery.QueryText += " IndexDocId>" + lastDocId;
                }

                var searchExec = new SearchExecutor(ClientContext);
                var results    = searchExec.ExecuteQuery(keywordQuery);
                ClientContext.ExecuteQueryRetry();

                if (results.Value != null)
                {
                    if (finalResults == null)
                    {
                        finalResults = (PnPResultTableCollection)results.Value;
                        foreach (ResultTable resultTable in results.Value)
                        {
                            if (resultTable.TableType == "RelevantResults")
                            {
                                currentCount = resultTable.RowCount;
                                lastDocId    = resultTable.ResultRows.Last()["DocId"].ToString();
                            }
                        }
                    }
                    else
                    {
                        // we're in paging mode
                        foreach (ResultTable resultTable in results.Value)
                        {
                            PnPResultTable pnpResultTable = (PnPResultTable)resultTable;
                            var            existingTable  = finalResults.SingleOrDefault(t => t.TableType == resultTable.TableType);
                            if (existingTable != null)
                            {
                                existingTable.ResultRows.AddRange(pnpResultTable.ResultRows);
                            }
                            else
                            {
                                finalResults.Add(pnpResultTable);
                            }
                            if (pnpResultTable.TableType == "RelevantResults")
                            {
                                currentCount = resultTable.RowCount;
                                if (currentCount > 0)
                                {
                                    lastDocId = resultTable.ResultRows.Last()["DocId"].ToString();
                                }
                            }
                        }
                    }
                }
                startRow += rowLimit;
            } while (currentCount == rowLimit && All.IsPresent);

            if (!RelevantResults.IsPresent)
            {
                return(finalResults);
            }
            else
            {
                var results = finalResults.FirstOrDefault(t => t.TableType == "RelevantResults")?
                              .ResultRows.Select(r => ConvertToPSObject(r));
                return(results);
            }
        }
Esempio n. 2
0
        protected override void ExecuteCmdlet()
        {
            int startRow = StartRow;
            int rowLimit = MaxResults;

            if (All.IsPresent)
            {
                startRow = 0;
                rowLimit = 500;
                SortList = new Hashtable()
                {
                    { "IndexDocId", "ascending" }
                };
            }

            var currentCount = 0;
            PnPResultTableCollection finalResults = null;

            do
            {
                KeywordQuery keywordQuery = CreateKeywordQuery();
                keywordQuery.StartRow = startRow;
                keywordQuery.RowLimit = rowLimit;

                var searchExec = new SearchExecutor(ClientContext);
                if (startRow > 0 && All.IsPresent)
                {
                    keywordQuery.Refiners = null; // Only need to set on first page for auto paging
                }

                var results = searchExec.ExecuteQuery(keywordQuery);
                ClientContext.ExecuteQueryRetry();

                if (results.Value != null)
                {
                    if (finalResults == null)
                    {
                        finalResults = (PnPResultTableCollection)results.Value;
                        foreach (ResultTable resultTable in results.Value)
                        {
                            if (resultTable.TableType == "RelevantResults")
                            {
                                currentCount = resultTable.RowCount;
                            }
                        }
                    }
                    else
                    {
                        // we're in paging mode
                        foreach (ResultTable resultTable in results.Value)
                        {
                            PnPResultTable pnpResultTable = (PnPResultTable)resultTable;
                            var            existingTable  = finalResults.SingleOrDefault(t => t.TableType == resultTable.TableType);
                            if (existingTable != null)
                            {
                                existingTable.ResultRows.AddRange(pnpResultTable.ResultRows);
                            }
                            else
                            {
                                finalResults.Add(pnpResultTable);
                            }
                            if (pnpResultTable.TableType == "RelevantResults")
                            {
                                currentCount = resultTable.RowCount;
                            }
                        }
                    }
                }
                startRow += rowLimit;
            } while (currentCount == rowLimit && All.IsPresent);
            if (!RelevantResults.IsPresent)
            {
                WriteObject(finalResults, true);
            }
            else
            {
                var results = finalResults.FirstOrDefault(t => t.TableType == "RelevantResults")?
                              .ResultRows.Select(r => ConvertToPSObject(r));
                WriteObject(results, true);
            }
        }