/// <inheritdoc />
        public IScorer CreateIndexScorer(IIndexSnapshot indexSnapshot)
        {
            if (indexSnapshot is null)
            {
                throw new ArgumentNullException(nameof(indexSnapshot));
            }

            return(new OkapiBm25Scorer(this.k1, this.b, indexSnapshot.Items));
        }
Example #2
0
        public async Task WriteAsync(IIndexSnapshot <TKey> snapshot)
        {
            await this.WriteHeaderAsync(snapshot).ConfigureAwait(false);

            await this.WriteItemsAsync(snapshot).ConfigureAwait(false);

            await this.WriteNodeAsync(snapshot.Root).ConfigureAwait(false);

            await this.WriteTerminatorAsync().ConfigureAwait(false);
        }
Example #3
0
        public async Task SerializeAsync(IIndexSnapshot <TKey> snapshot, Stream stream, bool disposeStream = true)
        {
            if (snapshot is null)
            {
                throw new ArgumentNullException(nameof(snapshot));
            }

            using (var writer = new IndexWriter <TKey>(stream, disposeStream, this.keySerializer))
            {
                await writer.WriteAsync(snapshot).ConfigureAwait(false);
            }
        }
Example #4
0
        public IIndexNavigator Create(IIndexSnapshot indexSnapshot)
        {
            var node = indexSnapshot.Root;

            if (!this.pool.TryTake(out var navigator))
            {
                navigator = new IndexNavigator();
            }

            navigator.Initialize(node, this, this.scorer.CreateIndexScorer(indexSnapshot));
            return(navigator);
        }
Example #5
0
        /// <inheritdoc />
        public IEnumerable <SearchResult <TKey> > Execute <TKey>(IIndexSnapshot <TKey> index)
        {
            if (index is null)
            {
                throw new ArgumentNullException(nameof(index));
            }

            if (this.Root == EmptyQueryPart.Instance)
            {
                return(Enumerable.Empty <SearchResult <TKey> >());
            }

            var idLookup         = index.Items;
            var fieldLookup      = index.FieldLookup;
            var evaluationResult = this.Root.Evaluate(index.CreateNavigator, QueryContext.Empty);
            var matches          = evaluationResult.Matches;
            var results          = new Dictionary <int, List <ScoredFieldMatch> >();

            foreach (var match in matches)
            {
                if (!results.TryGetValue(match.ItemId, out var itemResults))
                {
                    itemResults           = new List <ScoredFieldMatch>();
                    results[match.ItemId] = itemResults;
                }

                itemResults.AddRange(match.FieldMatches);
            }

            var searchResults = new List <SearchResult <TKey> >(matches.Count);

            foreach (var itemResults in matches)
            {
                var item = idLookup.GetMetadata(itemResults.ItemId);

                searchResults.Add(
                    new SearchResult <TKey>(
                        item.Item,
                        itemResults.FieldMatches.Select(m => new FieldSearchResult(
                                                            fieldLookup.GetFieldForId(m.FieldId),
                                                            m.Score,
                                                            m.FieldMatch.GetTokenLocations()))
                        .ToList()));
            }

            return(searchResults.OrderByDescending(r => r.Score));
        }
Example #6
0
        public IEnumerable <SearchResult <TKey> > Execute <TKey>(IIndexSnapshot <TKey> index)
        {
            if (index is null)
            {
                throw new ArgumentNullException(nameof(index));
            }

            if (this.Root == EmptyQueryPart.Instance)
            {
                yield break;
            }

            var idLookup    = index.IdLookup;
            var fieldLookup = index.FieldLookup;
            var matches     = this.Root.Evaluate(index.CreateNavigator, QueryContext.Empty).Matches;
            var results     = new Dictionary <int, List <FieldMatch> >();

            foreach (var match in matches)
            {
                if (!results.TryGetValue(match.ItemId, out var itemResults))
                {
                    itemResults           = new List <FieldMatch>();
                    results[match.ItemId] = itemResults;
                }

                itemResults.AddRange(match.FieldMatches);
            }

            foreach (var itemResults in matches)
            {
                var item = idLookup.GetItemForId(itemResults.ItemId);
                yield return(new SearchResult <TKey>(
                                 item,
                                 itemResults.FieldMatches.Select(m => new FieldSearchResult(
                                                                     fieldLookup.GetFieldForId(m.FieldId),
                                                                     m.GetWordLocations()))
                                 .ToList()));
            }
        }