public void Load(TextReader reader) { Clear(); var deserialized = (JObject)(JsonSerializer.Create(SerializerSettings).Deserialize(new JsonTextReader(reader))); var index = deserialized["Index"]; var fields = (JObject)index; foreach (var field in fields) { try { var fieldName = field.Key; var values = (JArray)field.Value; var data = new Dictionary <string, List <int> >(); foreach (var termData in values.Select(v => v.ToString()).ToArray()) { var p = termData.LastIndexOf(":", StringComparison.Ordinal); var name = termData.Substring(0, p); var idSrc = termData.Substring(p + 1); var ids = idSrc.Split(',').Select(int.Parse).ToList(); data.Add(name, ids); } this.IndexData.Add(fieldName, data); } catch (Exception e) { throw; } } var stored = (JArray)deserialized["Stored"]; foreach (var storedDoc in stored) { if (storedDoc["VersionId"] == null) { // Old algorithm (backward compatibility) var versionId = (int)storedDoc["Item1"]; var indexFields = ((JArray)storedDoc["Item2"]).Select(x => CreateIndexField((JObject)x)).ToList(); StoredData.Add(new Tuple <int, List <IndexField> >(versionId, indexFields)); } else { // New algorithm var versionId = storedDoc["VersionId"].Value <int>(); var indexFields = storedDoc["IndexFields"] .Select(x => x.ToString()) .Select(x => IndexField.Parse(x, true)) .ToList(); StoredData.Add(new Tuple <int, List <IndexField> >(versionId, indexFields)); } } }
public void InMemoryDatabaseGenerator_IndexField_DefaultFlags() { var field = new IndexField("FieldName", "value", IndexingMode.Analyzed, IndexStoringMode.No, IndexTermVector.No); Assert.AreEqual("FieldName:value:S", field.ToString()); var parsed = IndexField.Parse(field.ToString(), false); Assert.AreEqual(field.Name, parsed.Name); Assert.AreEqual(field.Type, parsed.Type); Assert.AreEqual(field.ValueAsString, parsed.ValueAsString); Assert.AreEqual(IndexingMode.Default, parsed.Mode); Assert.AreEqual(IndexStoringMode.Default, parsed.Store); Assert.AreEqual(IndexTermVector.Default, parsed.TermVector); }
public void InMemoryDatabaseGenerator_IndexField_AllFlags_Stored() { var field = new IndexField("FieldName", "value", IndexingMode.No, IndexStoringMode.Yes, IndexTermVector.WithPositions); Assert.AreEqual("IM3,TV3,FieldName:value:S", field.ToString(true)); var parsed = IndexField.Parse(field.ToString(), true); Assert.AreEqual(field.Name, parsed.Name); Assert.AreEqual(field.Type, parsed.Type); Assert.AreEqual(field.ValueAsString, parsed.ValueAsString); Assert.AreEqual(field.Mode, parsed.Mode); Assert.AreEqual(field.Store, parsed.Store); Assert.AreEqual(field.TermVector, parsed.TermVector); }