public object Detect()
        {
            if (string.IsNullOrEmpty(_context.Entity.Version))
            {
                return(null);
            }

            var version = _context.Entity.GetVersionField();

            _context.Debug(() => $"Detecting max output version: {_context.Connection.Folder}.{_context.Entity.Alias}.{version.Alias}.");

            var tflDeleted = _context.Entity.TflDeleted();
            var sort       = new Sort(new SortField(version.Alias, LuceneConversion.TypeSort(version.Type), true));

            using (var searcher = _searcherFactory.Create()) {
                var hits = searcher.Search(LuceneConversion.TypeSearch(tflDeleted, tflDeleted.Alias, false), null, 1, sort);

                if (hits.TotalHits > 0)
                {
                    var doc   = searcher.Doc(hits.ScoreDocs[0].Doc);
                    var value = doc.Get(version.Alias);
                    _context.Debug(() => $"Found value: {value}");
                    return(version.Convert(value));
                }
            }

            _context.Debug(() => "Did not find max output version");
            return(null);
        }
        public object Detect()
        {
            if (string.IsNullOrEmpty(_context.Entity.Version))
            {
                return(null);
            }

            using (var searcher = _searcherFactory.Create()) {
                var version = _context.Entity.GetVersionField();

                _context.Debug(() => $"Detecting max input version: {_context.Connection.Folder}:{version.Name}.");

                var hits = searcher.Search(new MatchAllDocsQuery(), null, 1,
                                           new Sort(new SortField(version.Name, LuceneConversion.TypeSort(version.Type), true))
                                           );

                if (hits.TotalHits > 0)
                {
                    var doc   = searcher.Doc(hits.ScoreDocs[0].Doc);
                    var value = doc.Get(version.Name);
                    _context.Debug(() => $"Found value: {value}");
                    return(version.Convert(value));
                }
            }

            _context.Debug(() => "Did not find max input version");
            return(null);
        }
        // get max tflbatchid, max tflkey
        public override void Start()
        {
            base.Start();
            var tflBatchId = Context.Entity.TflBatchId();
            var tflKey     = Context.Entity.TflKey();

            using (var searcher = _searcherFactory.Create()) {
                var batchHits = searcher.Search(new MatchAllDocsQuery(), null, 1,
                                                new Sort(new SortField(tflBatchId.Alias, LuceneConversion.TypeSort(tflBatchId.Type), true))
                                                );
                Context.Entity.BatchId = (batchHits.TotalHits > 0 ? System.Convert.ToInt32(searcher.Doc(batchHits.ScoreDocs[0].Doc).Get(tflBatchId.Alias)) : 0) + 1;

                var keyHits = searcher.Search(new MatchAllDocsQuery(), null, 1,
                                              new Sort(new SortField(tflKey.Alias, LuceneConversion.TypeSort(tflKey.Type), true))
                                              );
                Context.Entity.Identity = (keyHits.TotalHits > 0 ? System.Convert.ToInt32(searcher.Doc(keyHits.ScoreDocs[0].Doc).Get(tflKey.Alias)) : 0);
            }
            Context.Debug(() => $"Next {tflBatchId.Alias}: {Context.Entity.BatchId}.");
            Context.Debug(() => $"Last {tflKey.Alias}: {Context.Entity.Identity}.");

            using (var reader = _readerFactory.Create()) {
                Context.Entity.IsFirstRun = Context.Entity.MinVersion == null && reader.NumDocs() == 0;
            }
        }