using Raven.Client.Documents;using Raven.Client.Documents.Indexes;using Raven.Client.Documents.Operations;using Raven.Client.Documents.Queries;using System;using System.Collections.Generic;using System.Linq;
namespace RavenDbEntityFilterBug{ public static class WhereClauseIndex {
public class Matter { public string Title { get; set; }
public string Author { get; set; }
public Dictionary<string, string> CustomFields { get; set; }
}
public class Filter_key_Index : AbstractIndexCreationTask<Matter> { public Filter_key_Index() { Map = matters => from e in matters select new { system_Key_Index = e.CustomFields.Where(x => x.Key == "filter_key") // Commenting this line creates the index .Select(x => new KeyValuePair<string, string>(x.Key + "some_Text", x.Value.ToString())) }; } }
public static void Main(string[] args) { DocumentStore documentStore = new DocumentStore();
documentStore.Urls = new[] { "http://live-test.ravendb.net" }; documentStore.Database = "AmaltheaTest";
using (var store = documentStore) {
store.Initialize(); documentStore.Operations.Send(new DeleteByQueryOperation( new IndexQuery { Query = "from @all_docs as d" }, new QueryOperationOptions { RetrieveDetails = true } ));
new Filter_key_Index().Execute(store);
List<Matter> books = new List<Matter>(); books.Add(new Matter { Title = "abc", Author = "john", CustomFields = new Dictionary<string, string> { { "filter_key", "value1" }, { "some_other_key", "value2" } } }); books.Add(new Matter { Title = "xyz", Author = "doe", CustomFields = new Dictionary<string, string> { { "filter_key", "value3" }, { "some_other_key", "value4" } } }); using (var s = store.OpenSession()) { s.Store(books.First()); s.Store(books.Last());
s.Advanced.WaitForIndexesAfterSaveChanges(); s.SaveChanges(); }
using (var s = store.OpenSession()) { var booksByJohn = s.Query<Matter, Filter_key_Index>().ToList();
Console.WriteLine(booksByJohn.ToString());
foreach (var book in booksByJohn) { Console.WriteLine(book); } }
Console.ReadLine();
} }
}}
--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
![]() | Oren Eini CEO / Hibernating Rhinos LTD
|
--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
using Raven.Client.Documents;using Raven.Client.Documents.Indexes;using Raven.Client.Documents.Linq;
using Raven.Client.Documents.Operations;using Raven.Client.Documents.Queries;using System;using System.Collections.Generic;using System.Linq;
namespace RavenDB.Sandbox{ public static class WhereClauseIndex {
public class MatterDto { public string Id { get; set; } public string Name { get; set; } public IEnumerable<EntityReference> ContactFields { get; set; }
}
public static void Main(string[] args) { DocumentStore documentStore = new DocumentStore();
documentStore.Urls = new[] { "http://live-test.ravendb.net" };
documentStore.Database = "Test";
using (var store = documentStore) {
store.Initialize(); documentStore.Operations.Send(new DeleteByQueryOperation( new IndexQuery { Query = "from @all_docs as d" }, new QueryOperationOptions { RetrieveDetails = true } ));
new Matters_ForList().Execute(store);
using (var s = store.OpenSession()) {
var cf = new CustomField { Id = "CustomFields/1-A", Name = "Father's Name", FieldType = CustomFieldType.Contact }; s.Store(cf); cf = new CustomField { Id = "CustomFields/2-A", Name = "Salary", FieldType = CustomFieldType.Numeric }; s.Store(cf); cf = new CustomField { Id = "CustomFields/3-A", Name = "Referred by", FieldType = CustomFieldType.Contact }; s.Store(cf);
s.Advanced.WaitForIndexesAfterSaveChanges(); s.SaveChanges(); }
var matters = new List<Matter>(); matters.Add(CreateTestMatterWithCustomFields("Matter 1", new Dictionary<string, object> { { "F_1-A", "customFieldValue1_1" }, { "F_2-A", "customFieldValue2_1" }, { "F_3-A", "customFieldValue3_1" } })); matters.Add(CreateTestMatterWithCustomFields("Matter 2", new Dictionary<string, object> { { "F_1-A", "customFieldValue1_2" }, { "F_2-A", "customFieldValue2_2" }, { "F_3-A", "customFieldValue3_3" } }));
using (var s = store.OpenSession()) {
s.Store(matters.First()); s.Store(matters.Last());
s.Advanced.WaitForIndexesAfterSaveChanges(); s.SaveChanges(); }
using (var s = store.OpenSession()) {
var result = (from m in s.Query<Matter, Matters_ForList>().As<Matters_Index>()
//The desirialization fails after Adding the below line. IF the below line is commented, deserialization succeeds. let custField = RavenQuery.Load<CustomField>($"CustomFields/1-A")
select new MatterDto() { ContactFields = m.ContactFields, Id = m.Id, Name = m.Title, }).ToList();
foreach (var contactField in result) { Console.WriteLine(contactField); } }
Console.ReadLine();
} }
private static Matter CreateTestMatterWithCustomFields(string title, Dictionary<string, object> customFields) { return new Matter { Title = title, CustomFields = customFields }; }
}
public class EntityReference { public EntityReference() { } public EntityReference(string id, string name) { Id = id; Name = name;
}
public string Id { get; set; }
public string Name { get; set; } }
public class Matters_ForList : AbstractIndexCreationTask<Matter, Matters_Index> { public Matters_ForList() { // Add fields that are used for filtering and sorting
Map = matters => from e in matters select new {
ContactFields = e.CustomFields.Select(x => x).Where(x => LoadDocument<CustomField>($"CustomFields/{x.Key.Replace("F_", "")}").FieldType == CustomFieldType.Contact) .Select(x => new EntityReference { Name = LoadDocument<CustomField>($"CustomFields/{x.Key.Replace("F_", "")}").Name, Id = x.Value.ToString() }) };
Store(x => x.ContactFields, FieldStorage.Yes); } }
public class Matters_Index : Matter { public IEnumerable<EntityReference> ContactFields { get; set; } }
public class Matter { public Matter() { }
public string Id { get; set; }
public string Title { get; set; }
public Dictionary<string, object> CustomFields { get; set; } }
public class CustomField { public string Id { get; set; } public string Name { get; set; } public CustomFieldType FieldType { get; set; } }
public enum CustomFieldType { Text, Date, Checkbox, Contact, Email, Numeric, List, Currency }}
![]() | Maxim Buryak Team Leader / Hibernating Rhinos LTD
|