Пример #1
0
		public void Can_define_index_with_WhereEntityIs()
		{
			var idxBuilder = new IndexDefinitionBuilder<object>
			{
				Map =
					docs =>
					from course in (IEnumerable<Course>) docs
					select new {course.Id, course},

				TransformResults =
					(database, docs) =>
					from course in (IEnumerable<Course>) docs
					select new
					{
						item = course.Name,
						id = course.Id,
						iconCls = "course",
						leaf = false,
						expanded = true,
						children =
						from unit in course.Syllabus
						select new
						{
							item = unit.Name,
							id = unit.Name,
							iconCls = "unit",
							leaf = false,
							expanded = true,
							children =
							from notebook in unit.Notebooks
							select new
							{
								item = notebook.Name,
								id = notebook.Id,
								courseId = course.Id,
								iconCls = "notebook",
								type = notebook.Type,
								leaf = true,
							}
						}
					}
			};

			using(var store = NewDocumentStore())
			{
				var indexDefinition = idxBuilder.ToIndexDefinition(store.Conventions);
				store.DatabaseCommands.PutIndex("test", indexDefinition);
			}
		}
Пример #2
0
		public void Can_define_index_with_WhereEntityIs()
		{
			var idxBuilder = new IndexDefinitionBuilder<object>("test")
			{
				Map =
					docs =>
					from course in (IEnumerable<Course>) docs
					select new {course.Id, course},
			};

			using(var store = NewDocumentStore())
			{
				var indexDefinition = idxBuilder.ToIndexDefinition(store.Conventions);
				store.DatabaseCommands.PutIndex("test", indexDefinition);
			}
		}
Пример #3
0
        public void get_index_names()
        {
            using (IDocumentStore store = NewDocumentStore())
            {
                for (int i = 1; i <= 10; i++)
                {
                    using (var session = store.OpenSession())
                    {
                        session.Store(new Entity()
                        {
                            OrganizationId = 1,
                            HistoryCode = 2,
                            CaseId = 3
                        });
                        session.SaveChanges();
                    }
                }

                for (int i = 1; i <= 30; i++)
                {
                    IndexDefinitionBuilder<Entity> builder = new IndexDefinitionBuilder<Entity>
                    {
                        Map = entities => from e in entities
                            select new
                            {
                                Id = e.OrganizationId,
                                Code = e.HistoryCode,
                                Case = e.CaseId
                            }
                    };

                    store.DatabaseCommands.PutIndex("TestIndex/Numer"+i , builder.ToIndexDefinition(store.Conventions));
                }

                WaitForIndexing(store);

                using (var session = store.OpenSession())
                {
                    Task.Run(() => LoopResetIndex(session)).Wait();
                }
            }
        }
Пример #4
0
        public void CannotPutIndexWithNameClash()
        {
            using (var store = GetDocumentStore())
            {
                var indexDefBuilder1 = new IndexDefinitionBuilder <Order>()
                {
                    Map = docs => from doc in docs select new { Index = doc.Company },
                };

                var indexDefinition1 = indexDefBuilder1.ToIndexDefinition(store.Conventions);
                indexDefinition1.Name = "Index_With_Name_Clash";

                var indexDefBuilder2 = new IndexDefinitionBuilder <Order>()
                {
                    Map = docs => from doc in docs select new { Index = doc.Company },
                };

                var indexDefinition2 = indexDefBuilder2.ToIndexDefinition(store.Conventions);
                indexDefinition2.Name = "Index/With/Name/Clash";

                Assert.Throws <IndexCreationException>(() => store.Maintenance.Send(new PutIndexesOperation(indexDefinition1, indexDefinition2)));
            }
        }
Пример #5
0
        public async Task Sample()
        {
            using (var store = new DocumentStore())
            {
                #region indexes_2
                // deploy index to `DefaultDatabase` for given `DocumentStore`
                // using store `Conventions`
                new Orders_Totals().Execute(store);

                // deploy asynchronously index to `DefaultDatabase` for given `DocumentStore`
                // using store `Conventions`
                await new Orders_Totals().ExecuteAsync(store.AsyncDatabaseCommands, store.Conventions);
                #endregion

                #region indexes_3
                // deploy index to `Northwind` database
                // using store `Conventions`
                new Orders_Totals().Execute(store.DatabaseCommands.ForDatabase("Northwind"), store.Conventions);
                #endregion

                #region indexes_4
                // deploy all indexes (and transformers)
                // from assembly where `Orders_Totals` is found
                // to `DefaultDatabase` for given `DocumentStore`
                IndexCreation.CreateIndexes(typeof(Orders_Totals).Assembly, store);
                #endregion

                #region indexes_5
                store
                .DatabaseCommands
                .PutIndex("Orders/Totals", new IndexDefinition
                {
                    Map = @"from order in docs.Orders
							select new { order.Employee,  order.Company, Total = order.Lines.Sum(l => (l.Quantity * l.PricePerUnit) * (1 - l.Discount)) }"
                });
                #endregion

                #region indexes_6
                IndexDefinitionBuilder <Order> builder = new IndexDefinitionBuilder <Order>();
                builder.Map = orders => from order in orders
                              select new
                {
                    order.Employee,
                    order.Company,
                    Total = order.Lines.Sum(l => (l.Quantity * l.PricePerUnit) * (1 - l.Discount))
                };

                store
                .DatabaseCommands
                .PutIndex("Orders/Totals", builder.ToIndexDefinition(store.Conventions));
                #endregion

                using (var session = store.OpenSession())
                {
                    #region indexes_7
                    List <Employee> employees = session
                                                .Query <Employee>()
                                                .Where(x => x.FirstName == "Robert" && x.LastName == "King")
                                                .ToList();
                    #endregion
                }
            }
        }
Пример #6
0
 /// <summary>
 /// Puts the index for the specified name
 /// </summary>
 /// <typeparam name="TDocument">The type of the document.</typeparam>
 /// <typeparam name="TReduceResult">The type of the reduce result.</typeparam>
 /// <param name="name">The name.</param>
 /// <param name="indexDef">The index def.</param>
 /// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
 public string PutIndex <TDocument, TReduceResult>(string name, IndexDefinitionBuilder <TDocument, TReduceResult> indexDef, bool overwrite)
 {
     return(PutIndex(name, indexDef.ToIndexDefinition(convention), overwrite));
 }
Пример #7
0
        public async Task Sample()
        {
            using (var store = new DocumentStore())
            {
                #region indexes_2
                // deploy index to database defined in `DocumentStore.Database` property
                // using default DocumentStore `Conventions`
                new Orders_Totals().Execute(store);

                // deploy asynchronously index to database defined in `DocumentStore.Database` property
                // using default DocumentStore `Conventions`
                await new Orders_Totals().ExecuteAsync(store, store.Conventions);
                #endregion

                #region indexes_3
                // deploy index to `Northwind` database
                // using default DocumentStore `Conventions`
                new Orders_Totals().Execute(store, store.Conventions, "Northwind");
                #endregion

                #region indexes_4
                // deploy all indexes
                // from assembly where `Orders_Totals` is found
                // to database defined in `DocumentStore.Database` property
                IndexCreation.CreateIndexes(typeof(Orders_Totals).Assembly, store);
                #endregion

                #region indexes_5
                store
                .Maintenance
                .Send(new PutIndexesOperation(new IndexDefinition
                {
                    Name = "Orders/Totals",
                    Maps =
                    {
                        @"from order in docs.Orders	
                              select new 
                              { 
                                  order.Employee, 
                                  order.Company,
                                  Total = order.Lines.Sum(l => (l.Quantity * l.PricePerUnit) * (1 - l.Discount))
                              }"
                    }
                }));
                #endregion

                #region indexes_6
                IndexDefinitionBuilder <Order> builder = new IndexDefinitionBuilder <Order>();
                builder.Map = orders => from order in orders
                              select new
                {
                    order.Employee,
                    order.Company,
                    Total = order.Lines.Sum(l => (l.Quantity * l.PricePerUnit) * (1 - l.Discount))
                };

                store
                .Maintenance
                .Send(new PutIndexesOperation(builder.ToIndexDefinition(store.Conventions)));
                #endregion

                using (var session = store.OpenSession())
                {
                    #region indexes_7
                    List <Employee> employees = session
                                                .Query <Employee>()
                                                .Where(x => x.FirstName == "Robert" && x.LastName == "King")
                                                .ToList();
                    #endregion
                }
            }
        }
        public override IndexDefinition CreateIndexDefinition()
        {
            var foo = new IndexDefinitionBuilder<Package, Package>() {
                Map = pkgs => from p in pkgs
                              select new {
                                  Tags = p.Tags
                              },
                Indexes = { { x => x.Tags, FieldIndexing.Analyzed } }
            };

            return foo.ToIndexDefinition(this.Conventions);
        }
Пример #9
0
        public void TestFailingProjection()
        {
            using (var store = GetDocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    session.Store(new MyClass {
                        Index = 1, Prop1 = "prop1", Prop2 = "prop2"
                    });
                    session.Store(new MyClass {
                        Index = 2, Prop1 = "prop1", Prop2 = "prop2"
                    });
                    session.Store(new MyClass {
                        Index = 3, Prop1 = "prop1", Prop2 = "prop2"
                    });

                    session.SaveChanges();
                }
                using (var session = store.OpenSession())
                {
                    JsonOperationContext context;
                    store.GetRequestExecuter(store.DefaultDatabase).ContextPool.AllocateOperationContext(out context);

                    var indexDef = new IndexDefinitionBuilder <MyClass>()
                    {
                        Map = docs => from doc in docs select new { Index = doc.Index }
                    };
                    ;

                    var putIndexOperation = new PutIndexOperation(context);
                    var indexCommand      = putIndexOperation.CreateRequest(store.Conventions, "MyClass/ByIndex", indexDef.ToIndexDefinition(store.Conventions, true));
                    if (indexCommand != null)
                    {
                        store.GetRequestExecuter(store.DefaultDatabase).Execute(indexCommand, context);
                    }

                    WaitForIndexing(store);

                    var query = session.Query <MyClass>("MyClass/ByIndex")
                                .Select(x => new MyClass
                    {
                        Index = x.Index,
                        Prop1 = x.Prop1
                    });

                    var enumerator = session.Advanced.Stream(query);
                    int count      = 0;
                    while (enumerator.MoveNext())
                    {
                        Assert.IsType <MyClass>(enumerator.Current.Document);
                        Assert.Null(((MyClass)enumerator.Current.Document).Prop2);
                        count++;
                    }

                    Assert.Equal(3, count);
                }
            }
        }