public Task<SearchResults> FindAsync(string searchQueryString, ViewFilter view, int count, Direction direction, ulong? referenceId = null) { return Task.Run<SearchResults>(() => Find(searchQueryString, view, count, direction, referenceId)); }
public IEnumerable<LogEntry> Find(string queryString, ViewFilter view, int count, Direction direction, ulong? referenceId, out ulong foundId, out int totalFound) { int fake; var findFirstResult = FindFirstId(queryString, view, direction, referenceId, out totalFound); if(findFirstResult == null) { foundId = 0; return null; } foundId = findFirstResult.Value; var resultsBefore = Filter(string.Empty, view, count, Direction.Backward, foundId, out fake).ToList(); var resultsAfter = Filter(string.Empty, view, count, Direction.Forward, foundId, out fake).Skip(1).ToList(); var foundResult = resultsBefore.Last(); resultsBefore.Remove(foundResult); IEnumerable<LogEntry> result; if(resultsBefore.Count < (count / 2)) { result = resultsBefore.Concat(new [] { foundResult }).Concat(resultsAfter).Take(count).ToList(); } else if(resultsAfter.Count < ((count - 1) / 2)) { result = resultsBefore.Concat(new [] { foundResult }).Concat(resultsAfter).ToList(); result = result.Skip(((List<LogEntry>)result).Count - count).ToList(); } else { result = resultsBefore.Skip(resultsBefore.Count - (count / 2)).Concat(new [] { foundResult }).Concat(resultsAfter.Take((count - 1) / 2)).ToList(); } return result; }
private static string CombineQuery(string queryString, ViewFilter view, Range forcedRange) { var values = new List<string>(); var viewQueryString = view.GenerateQueryString(); if(!string.IsNullOrEmpty(viewQueryString)) { values.Add(viewQueryString); } if(!string.IsNullOrEmpty(queryString)) { values.Add(queryString); } if(forcedRange != null) { values.Add(forcedRange.GenerateQueryString()); } return string.Join(" AND ", values.Select(x => string.Format("({0})", x))); }
public IEnumerable<LogEntry> Filter(string queryString, ViewFilter view, int count, Direction direction, ulong? referenceId, out int totalCount) { var context = new IncermentalSearchContext(count, direction, referenceId); var parser = new CustomQueryParser(LuceneNet.Util.Version.LUCENE_30, MessageFieldName, analyzer); var sortBy = new Sort(new SortField(IdFieldName, SortField.LONG, direction == Direction.Backward)); while(true) { var combinedQueryString = Searcher.CombineQuery(queryString, view, context.Range); if(combinedQueryString.Length == 0) { totalCount = 0; return new LogEntry[0]; } var query = parser.Parse(combinedQueryString); lock(searcherLock) { var searchResults = searcher.Search(query, null, count, sortBy); totalCount = searchResults.TotalHits; var result = searchResults.ScoreDocs.Take(count).Select(d => searcher.Doc(d.Doc)).Select(x => converter.ToLogEntry(x)); context.UpdateResult(result); if(context.IsFinished) { return (direction == Direction.Backward) ? context.Result.Reverse() : context.Result; } context.UpdateRange(); } } }
private ulong? FindFirstId(string queryString, ViewFilter view, Direction direction, ulong? referenceId, out int totalCount) { if(direction == Direction.Backward) { if(referenceId.HasValue && referenceId.Value > 0) { referenceId--; } } else { if(referenceId.HasValue && referenceId.Value < ulong.MaxValue) { referenceId++; } } var context = new IncermentalSearchContext(1, direction, referenceId); while(true) { var combinedQueryString = Searcher.CombineQuery(queryString, view, context.Range); if(combinedQueryString.Length == 0) { totalCount = 0; return null; } var parser = new CustomQueryParser(LuceneNet.Util.Version.LUCENE_30, MessageFieldName, analyzer); var query = parser.Parse(combinedQueryString); var sortBy = new Sort(new SortField(IdFieldName, SortField.LONG, direction == Direction.Backward)); lock(searcherLock) { var searchResults = searcher.Search(query, null, 1, sortBy); var firstResult = searchResults.ScoreDocs.FirstOrDefault(); totalCount = searchResults.TotalHits; if(firstResult != null) { return converter.ToLogEntry(searcher.Doc(firstResult.Doc)).Id; } if(context.IsFinished) { return null; } context.UpdateRange(); } } }
private SearchResults Find(string queryString, ViewFilter view, int count, Direction direction, ulong? referenceId) { ulong foundId; int totalFound; var result = searcher.Find(queryString, view, count, direction, referenceId, out foundId, out totalFound); return new SearchResults(totalFound, result) { FoundId = foundId }; }
private SearchResults FilterHistoryView(string queryString, ViewFilter view, int count, Direction direction, ulong? referenceId) { int totalCount; var result = searcher.Filter(queryString, view, count, direction, referenceId, out totalCount); return new SearchResults(totalCount, result); }
private async Task FilterEntriesAsync(ProgressMonitor progressMonitor, bool selectLastAddedItem = false) { using(var progress = progressMonitor.Start("Filtering log...")) { var view = new ViewFilter { LogLevels = levels.ToList(), CustomFilter = filterTextEntry.Text }; var entries = await luceneStore.FilterHistoryViewAsync(filterTextEntry.Text, view, maxPageCount * pageSize, Direction.Backward); ApplicationExtensions.InvokeInUIThread(() => { listViewLog.ClearItems(); foreach(var entry in entries.Entries) { listViewLog.AddItem(entry, selectLastAddedItem, maxPageCount * pageSize); } RefreshPaging(entries.TotalHits); }); } }
private async Task LoadEntriesAsync(ProgressMonitor progressMonitor, Direction direction, Action afterCallback = null) { var view = new ViewFilter { LogLevels = levels.ToList(), CustomFilter = filterTextEntry.Text }; using(var progress = progressMonitor.Start("Filtering log...")) { var referenceId = (direction == Direction.Backward) ? listViewLog.FirstItemId : listViewLog.LastItemId; var entries = await luceneStore.FilterHistoryViewAsync(filterTextEntry.Text, view, pageSize, direction, referenceId); if(entries.Entries.Any()) { if(direction == Direction.Backward) { ApplicationExtensions.InvokeInUIThread(() => { foreach(var entry in entries.Entries.Where(e => e.Id < referenceId).Reverse()) { listViewLog.InsertItem(entry, false, maxPageCount * pageSize); } }); } else { ApplicationExtensions.InvokeInUIThread(() => { foreach(var entry in entries.Entries.Where(e => e.Id > referenceId)) { listViewLog.AddItem(entry, false, maxPageCount * pageSize); } }); } } } if(afterCallback != null) { afterCallback(); } }
private async Task SearchEntriesAsync(ProgressMonitor progressMonitor, Direction direction) { using(var progress = progressMonitor.Start("Searching in log...")) { var view = new ViewFilter { LogLevels = levels.ToList(), CustomFilter = filterTextEntry.Text }; var entries = await luceneStore.FindAsync(searchTextEntry.Text, view, 2 * pageSize, direction, searchContext != null ? searchContext.PreviousResultId : null); if(entries.Entries == null) { ApplicationExtensions.InvokeInUIThread(() => MessageDialog.ShowWarning("No results found!")); ResetSearch(false); return; } if(searchContext == null) { searchContext = new SearchContext(entries.TotalHits); } else { searchContext.Advance(direction); } searchContext.PreviousResultId = entries.FoundId; ApplicationExtensions.InvokeInUIThread(() => { listViewLog.ClearItems(); foreach(var entry in entries.Entries) { listViewLog.AddItem(entry, entries.FoundId == entry.Id); } searchLabel.Text = string.Format("Showing result: {0} / {1}", searchContext.CurrentResult, searchContext.ResultsCount); nextSearchResultButton.Sensitive = (searchContext.CurrentResult > 1); prevSearchResultButton.Sensitive = (searchContext.CurrentResult < searchContext.ResultsCount); }); } }