Пример #1
0
        /// <summary>
        /// An instance of `MatchData` will be created for every term that matches a
        /// document. However only one instance is required in an index result. This
        /// method combines metadata from another instance of `MatchData` with this
        /// object's metadata.
        /// </summary>
        /// <param name="otherMatchData">Another instance of match data to merge with this one.</param>
        public void Combine(MatchData otherMatchData)
        {
            IEnumerable <string> terms = otherMatchData.Posting.Keys;

            foreach (string term in terms)
            {
                IEnumerable <string> fields = otherMatchData.Posting[term].Keys;
                if (!Posting.ContainsKey(term))
                {
                    Posting.Add(term, new FieldMatches());
                }
                Dictionary <string, FieldMatchMetadata> thisTermEntry = Posting[term];
                foreach (string field in fields)
                {
                    IEnumerable <string> keys = otherMatchData.Posting[term][field].Keys;
                    if (!thisTermEntry.ContainsKey(field))
                    {
                        thisTermEntry.Add(field, new FieldMatchMetadata(capacity: otherMatchData.Posting[term][field].Keys.Count));
                    }
                    FieldMatchMetadata thisFieldEntry = thisTermEntry[field];
                    foreach (string key in keys)
                    {
                        IList <object?> otherData = otherMatchData.Posting[term][field][key];
                        if (!thisFieldEntry.ContainsKey(key))
                        {
                            thisFieldEntry.Add(key, new List <object?>(otherData));
                        }
                        else
                        {
                            thisFieldEntry[key] = thisFieldEntry[key].Concat(otherData).ToList();
                        }
                    }
                }
            }
        }
Пример #2
0
        /// <summary>Constructs a `MatchData`.</summary>
        /// <param name="term">The term this match data is associated with.</param>
        /// <param name="field">The field in which the term was found.</param>
        /// <param name="metadata">The metadata recorded about this term in this field.</param>
        public MatchData(
            string term,
            string field,
            FieldMatchMetadata metadata)
        {
            Term  = term;
            Field = field;

            // Cloning the metadata to prevent the original being mutated during match data combination.
            // Metadata is kept in an array within the inverted index.
            var clonedMetadata = new FieldMatchMetadata(capacity: metadata.Count);

            foreach ((string key, IEnumerable <object?> value) in metadata)
            {
                clonedMetadata.Add(key, new List <object?>(value));
            }

            Posting = new InvertedIndexEntry
            {
                {
                    term,
                    new FieldMatches
                    {
                        { field, clonedMetadata }
                    }
                }
            };
        }