public override InvertedIndex Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType != JsonTokenType.StartArray)
            {
                throw new JsonException("An inverted index can only be deserialized from an array.");
            }
            var serializedVectors = new List <(string term, InvertedIndexEntry posting)>();

            reader.ReadOrThrow();
            while (reader.AdvanceTo(JsonTokenType.StartArray, JsonTokenType.EndArray) != JsonTokenType.EndArray)
            {
                reader.AdvanceTo(JsonTokenType.String);
                serializedVectors.Add((
                                          reader.ReadValue <string>(options),
                                          reader.ReadValue <InvertedIndexEntry>(options)));
            }
            return(new InvertedIndex(serializedVectors));
        }
Ejemplo n.º 2
0
        public override Slice Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType != JsonTokenType.StartArray)
            {
                throw new JsonException("A slice can only be deserialized from an array containing two integers.");
            }
            reader.AdvanceTo(JsonTokenType.Number);

            int start  = reader.ReadValue <int>(options);
            int length = reader.ReadValue <int>(options);

            return(new Slice(start, length));
        }
        public override InvertedIndexEntry Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType != JsonTokenType.StartObject)
            {
                throw new JsonException("An inverted index entry can only be deserialized from an object.");
            }
            var result = new InvertedIndexEntry();

            reader.ReadOrThrow();
            while (reader.AdvanceTo(JsonTokenType.PropertyName, JsonTokenType.EndObject) != JsonTokenType.EndObject)
            {
                string fieldName = reader.ReadValue <string>(options);
                if (fieldName is "_index")
                {
                    result.Index = reader.ReadValue <int>(options);
                }
                else
                {
                    var fieldMatches = new FieldMatches();
                    reader.AdvanceTo(JsonTokenType.StartObject);
                    while (reader.AdvanceTo(JsonTokenType.PropertyName, JsonTokenType.EndObject) != JsonTokenType.EndObject)
                    {
                        string token    = reader.ReadValue <string>(options);
                        var    metadata = new FieldMatchMetadata();
                        reader.AdvanceTo(JsonTokenType.StartObject);
                        while (reader.AdvanceTo(JsonTokenType.PropertyName, JsonTokenType.EndObject) != JsonTokenType.EndObject)
                        {
                            string metadataName = reader.ReadValue <string>(options);
                            reader.AdvanceTo(JsonTokenType.StartArray);
                            reader.ReadOrThrow();
                            var data = new List <object?>();
                            while (reader.TokenType != JsonTokenType.EndArray)
                            {
                                // Special-case known metadata
                                if (metadataName is "position")
                                {
                                    data.Add(JsonSerializer.Deserialize <Slice>(ref reader, options));
                                }
                                else
                                {
                                    data.Add(reader.ReadObject(options));
                                }
                            }
                            reader.ReadOrThrow();
                            metadata.Add(metadataName, data);
                        }
                        reader.ReadOrThrow();
                        fieldMatches.Add(token, metadata);
                    }
                    reader.ReadOrThrow();
                    result.Add(fieldName, fieldMatches);
                }
            }
            reader.ReadOrThrow();
            return(result);
        }
Ejemplo n.º 4
0
        public override Vector Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var result = new Vector();

            if (reader.TokenType != JsonTokenType.StartArray)
            {
                throw new JsonException("A vector can only be deserialized from an array.");
            }
            while (reader.AdvanceTo(JsonTokenType.Number, JsonTokenType.EndArray) != JsonTokenType.EndArray)
            {
                int    index = reader.ReadValue <int>(options);
                double value = reader.ReadValue <double>(options);
                result.Insert(index, value);
            }
            reader.ReadOrThrow();
            return(result);
        }
Ejemplo n.º 5
0
        public override Index Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            InvertedIndex?invertedIndex = null;
            Dictionary <string, Vector>?fieldVectors = null;
            Pipeline?            pipeline            = null;
            IEnumerable <string>?fields = null;

            var tokenSetBuilder = new TokenSet.Builder();

            if (reader.TokenType != JsonTokenType.StartObject)
            {
                throw new JsonException("An index can only be deserialized from an object.");
            }
            reader.Read();
            while (reader.AdvanceTo(JsonTokenType.PropertyName, JsonTokenType.EndObject) != JsonTokenType.EndObject)
            {
                string propertyName = reader.ReadValue <string>(options);
                switch (propertyName)
                {
                case "version":
                    var parsedVersion = Version.Parse(reader.ReadValue <string>(options));
                    if (parsedVersion.Major != Version.Major || parsedVersion.Minor != Version.Minor)
                    {
                        System.Diagnostics.Debug.Write($"Version mismatch when loading serialised index. Current version of Lunr '{VersionString}' does not match serialized index '{parsedVersion}'");
                    }

                    break;

                case "invertedIndex":
                    invertedIndex = reader.ReadValue <InvertedIndex>(options);
                    break;

                case "fieldVectors":
                    fieldVectors = reader.ReadDictionaryFromKeyValueSequence <Vector>(options);
                    break;

                case "pipeline":
                    pipeline = new Pipeline(reader.ReadArray <string>(options));
                    break;

                case "fields":
                    fields = reader.ReadArray <string>(options);
                    break;
                }
            }
            if (invertedIndex is null)
            {
                throw new JsonException("Serialized index is missing invertedIndex.");
            }
            if (fieldVectors is null)
            {
                throw new JsonException("Serialized index is missing fieldVectors.");
            }
            if (pipeline is null)
            {
                throw new JsonException("Serialized index is missing a pipeline.");
            }
            if (fields is null)
            {
                throw new JsonException("Serialized index is missing a list of fields.");
            }

            foreach (string term in invertedIndex.Keys)
            {
                tokenSetBuilder.Insert(term);
            }
            tokenSetBuilder.Finish();

            return(new Index(invertedIndex, fieldVectors, tokenSetBuilder.Root, fields, pipeline));
        }