Example #1
0
        public void WillGetStaleResultWhenThereArePendingTasks()
        {
            using (var store = GetDocumentStore())
            {
                var index = new Users_ByName();
                index.Execute(store);

                store.Maintenance.Send(new StopIndexingOperation());

                using (var session = store.OpenSession())
                {
                    session.Store(new User {
                        Name = "Name1"
                    });
                    session.SaveChanges();
                }

                using (var commands = store.Commands())
                {
                    Assert.True(commands.Query(new IndexQuery()
                    {
                        Query = $"FROM INDEX '{index.IndexName}' LIMIT 2 OFFSET 0"
                    }).IsStale);
                }
            }
        }
Example #2
0
        public async Task CanResetIndex()
        {
            var index = new Users_ByName();

            using (var store = GetDocumentStore())
            {
                index.Execute(store);
                for (var i = 0; i < 20; i++)
                {
                    await store.AsyncDatabaseCommands.PutAsync("users/" + i, null, RavenJObject.FromObject(new User {
                    }), new RavenJObject());
                }
                WaitForIndexing(store);

                var stats = await store.AsyncDatabaseCommands.GetStatisticsAsync();

                Assert.Equal(0, stats.StaleIndexes.Length);

                await store.AsyncDatabaseCommands.Admin.StopIndexingAsync();

                await store.AsyncDatabaseCommands.ResetIndexAsync(index.IndexName);

                stats = await store.AsyncDatabaseCommands.GetStatisticsAsync();

                Assert.Equal(1, stats.StaleIndexes.Length);
                Assert.Equal(index.IndexName, stats.StaleIndexes[0]);
            }
        }
Example #3
0
        public async Task CanResetIndex()
        {
            var index = new Users_ByName();

            using (var store = GetDocumentStore())
            {
                index.Execute(store);
                using (var session = store.OpenSession())
                {
                    for (var i = 0; i < 20; i++)
                    {
                        session.Store(new User());
                    }

                    session.SaveChanges();
                }

                WaitForIndexing(store);

                var stats = await store.AsyncDatabaseCommands.GetStatisticsAsync();

                Assert.Equal(0, stats.StaleIndexes.Length);

                await store.AsyncDatabaseCommands.Admin.StopIndexingAsync();

                await store.AsyncDatabaseCommands.ResetIndexAsync(index.IndexName);

                stats = await store.AsyncDatabaseCommands.GetStatisticsAsync();

                Assert.Equal(1, stats.StaleIndexes.Length);
                Assert.Equal(index.IndexName, stats.StaleIndexes[0]);
            }
        }
Example #4
0
        public async Task CanResetIndex()
        {
            var index = new Users_ByName();

            using (var store = GetDocumentStore())
            {
                index.Execute(store);
                using (var session = store.OpenSession())
                {
                    for (var i = 0; i < 20; i++)
                    {
                        session.Store(new User());
                    }

                    session.SaveChanges();
                }

                Indexes.WaitForIndexing(store);

                var stats = await store.Maintenance.SendAsync(new GetStatisticsOperation());

                Assert.Equal(0, stats.StaleIndexes.Length);

                await store.Maintenance.SendAsync(new StopIndexingOperation());

                await store.Maintenance.SendAsync(new ResetIndexOperation(index.IndexName));

                stats = await store.Maintenance.SendAsync(new GetStatisticsOperation());

                Assert.Equal(1, stats.StaleIndexes.Length);
                Assert.Equal(index.IndexName, stats.StaleIndexes[0]);
            }
        }
Example #5
0
        public void CanStreamQueryResult()
        {
            using (var store = GetDocumentStore())
            {
                var index = new Users_ByName();
                index.Execute(store);

                for (int i = 0; i < 30; i++)
                {
                    store.DatabaseCommands.Put("users/" + i, null, RavenJObject.FromObject(new User {
                        Name = "Name" + i
                    }), new RavenJObject {
                        { "Raven-Entity-Name", "Users" }
                    });
                }
                WaitForIndexing(store);

                int count = 0;
                QueryHeaderInformation queryHeaders = null;
                var reader = store.DatabaseCommands.StreamQuery(index.IndexName, new IndexQuery {
                    Query = ""
                }, out queryHeaders);
                while (reader.MoveNext())
                {
                    Assert.Equal("Name" + count, reader.Current.Value <string>("Name"));
                    count++;
                }
                Assert.Equal(30, count);
            }
        }
Example #6
0
        public void CanStreamQueryResult()
        {
            using (var store = GetDocumentStore())
            {
                var index = new Users_ByName();
                index.Execute(store);

                using (var commands = store.Commands())
                {
                    for (int i = 0; i < 30; i++)
                    {
                        commands.Put("users/" + i, null, new User {
                            Name = "Name" + i
                        }, new Dictionary <string, object> {
                            { "@collection", "Users" }
                        });
                    }
                }

                WaitForIndexing(store);

                var count = 0;
                using (var session = store.OpenSession())
                {
                    var enumerator = session.Advanced.Stream(session.Query <User, Users_ByName>());
                    while (enumerator.MoveNext())
                    {
                        count++;
                    }
                }

                Assert.Equal(30, count);
            }
        }
Example #7
0
        public void CanGetSuggestions()
        {
            using (var store = GetDocumentStore())
            {
                var index = new Users_ByName();
                index.Execute(store);

                using (var commands = store.Commands())
                {
                    commands.Put("users/1", null, new User {
                        Name = "John Smith"
                    }, new Dictionary <string, object> {
                        { "@collection", "Users" }
                    });
                    commands.Put("users/2", null, new User {
                        Name = "Jack Johnson"
                    }, new Dictionary <string, object> {
                        { "@collection", "Users" }
                    });
                    commands.Put("users/3", null, new User {
                        Name = "Robery Jones"
                    }, new Dictionary <string, object> {
                        { "@collection", "Users" }
                    });
                    commands.Put("users/4", null, new User {
                        Name = "David Jones"
                    }, new Dictionary <string, object> {
                        { "@collection", "Users" }
                    });
                }

                WaitForIndexing(store);


                using (var session = store.OpenSession())
                {
                    var suggestions = session.Query <User, Users_ByName>().Suggest(new SuggestionQuery()
                    {
                        Field          = "Name",
                        Term           = "<<johne davi>>",
                        Accuracy       = 0.4f,
                        MaxSuggestions = 5,
                        Distance       = StringDistanceTypes.JaroWinkler,
                        Popularity     = true,
                    });

                    Assert.Equal("john", suggestions.Suggestions[0]);
                    Assert.Equal("jones", suggestions.Suggestions[1]);
                    Assert.Equal("johnson", suggestions.Suggestions[2]);
                    Assert.Equal("david", suggestions.Suggestions[3]);
                    Assert.Equal("jack", suggestions.Suggestions[4]);
                }
            }
        }
Example #8
0
        public void CanGetSuggestions()
        {
            using (var store = GetDocumentStore())
            {
                var index = new Users_ByName();
                index.Execute(store);

                using (var commands = store.Commands())
                {
                    commands.Put("users/1", null, new User {
                        Name = "John Smith"
                    }, new Dictionary <string, object> {
                        { "@collection", "Users" }
                    });
                    commands.Put("users/2", null, new User {
                        Name = "Jack Johnson"
                    }, new Dictionary <string, object> {
                        { "@collection", "Users" }
                    });
                    commands.Put("users/3", null, new User {
                        Name = "Robery Jones"
                    }, new Dictionary <string, object> {
                        { "@collection", "Users" }
                    });
                    commands.Put("users/4", null, new User {
                        Name = "David Jones"
                    }, new Dictionary <string, object> {
                        { "@collection", "Users" }
                    });
                }

                WaitForIndexing(store);


                using (var session = store.OpenSession())
                {
                    var suggestions = session.Query <User, Users_ByName>()
                                      .SuggestUsing(f => f.ByField("Name", new[] { "johne", "davi" }).WithOptions(new SuggestionOptions
                    {
                        PageSize = 5,
                        Distance = StringDistanceTypes.JaroWinkler,
                        SortMode = SuggestionSortMode.Popularity,
                        Accuracy = 0.4f
                    }))
                                      .Execute();

                    Assert.Equal("john", suggestions["Name"].Suggestions[0]);
                    Assert.Equal("jones", suggestions["Name"].Suggestions[1]);
                    Assert.Equal("johnson", suggestions["Name"].Suggestions[2]);
                    Assert.Equal("david", suggestions["Name"].Suggestions[3]);
                    Assert.Equal("jack", suggestions["Name"].Suggestions[4]);
                }
            }
        }
Example #9
0
        public void CanWaitOnStaleTimeout()
        {
            using (var store = NewDocumentStore())
            {
                using (var bulkInsert = store.BulkInsert())
                {
                    for (int i = 0; i < 10; i++)
                    {
                        bulkInsert.Store(new User
                        {
                            Name = "Users #" + i
                        }, "users/" + i);
                    }
                }

                store.DatabaseCommands.Admin.StopIndexing();

                var usersByNameIndex = new Users_ByName();
                usersByNameIndex.Execute(store);

                var waits = 0;
                var mres  = new ManualResetEventSlim();
                SystemTime.WaitCalled = ms =>
                {
                    waits++;
                    mres.Set();
                    Thread.Sleep(10);
                };

                var op = store.DatabaseCommands.DeleteByIndex("Users/ByName",
                                                              new IndexQuery {
                    Query = "Name:Users*"
                }
                                                              , new BulkOperationOptions
                {
                    AllowStale   = false,
                    MaxOpsPerSec = null,
                    StaleTimeout = TimeSpan.FromSeconds(15)
                });
                mres.Wait();

                store.DatabaseCommands.Admin.StartIndexing();

                op.WaitForCompletion();
                using (var session = store.OpenSession())
                {
                    Assert.True(waits > 0);

                    Assert.Empty(session.Query <User>());
                }
            }
        }
Example #10
0
        public void CanGetSuggestions()
        {
            using (var store = GetDocumentStore())
            {
                var index = new Users_ByName();
                index.Execute(store);

                using (var s = store.OpenSession())
                {
                    s.Store(new User {
                        Name = "John Smith"
                    }, "users/1");
                    s.Store(new User {
                        Name = "Jack Johnson"
                    }, "users/2");
                    s.Store(new User {
                        Name = "Robery Jones"
                    }, "users/3");
                    s.Store(new User {
                        Name = "David Jones"
                    }, "users/4");
                    s.SaveChanges();
                }

                WaitForIndexing(store);

                using (var session = store.OpenSession())
                {
                    var suggestions = session.Query <User, Users_ByName> ()
                                      .Suggest(new SuggestionQuery
                    {
                        Field          = "Name",
                        Term           = "<<johne davi>>",
                        Accuracy       = 0.4f,
                        MaxSuggestions = 5,
                        Distance       = StringDistanceTypes.JaroWinkler,
                        Popularity     = true,
                    });

                    Assert.Equal(5, suggestions.Suggestions.Length);
                    Assert.Equal("john", suggestions.Suggestions[0]);
                    Assert.Equal("jones", suggestions.Suggestions[1]);
                    Assert.Equal("johnson", suggestions.Suggestions[2]);
                    Assert.Equal("david", suggestions.Suggestions[3]);
                    Assert.Equal("jack", suggestions.Suggestions[4]);
                }
            }
        }
Example #11
0
        public void CanGetSuggestions()
        {
            using (var store = GetDocumentStore())
            {
                var index = new Users_ByName();
                index.Execute(store);

                using (var s = store.OpenSession())
                {
                    s.Store(new User {
                        Name = "John Smith"
                    }, "users/1");
                    s.Store(new User {
                        Name = "Jack Johnson"
                    }, "users/2");
                    s.Store(new User {
                        Name = "Robery Jones"
                    }, "users/3");
                    s.Store(new User {
                        Name = "David Jones"
                    }, "users/4");
                    s.SaveChanges();
                }

                WaitForIndexing(store);

                using (var session = store.OpenSession())
                {
                    var suggestions = session.Query <User, Users_ByName>()
                                      .SuggestUsing(x => x.ByField(y => y.Name, new[] { "johne", "davi" }).WithOptions(new SuggestionOptions
                    {
                        Accuracy = 0.4f,
                        PageSize = 5,
                        Distance = StringDistanceTypes.JaroWinkler,
                        SortMode = SuggestionSortMode.Popularity
                    }))
                                      .Execute();

                    Assert.Equal(5, suggestions["Name"].Suggestions.Count);
                    Assert.Equal("john", suggestions["Name"].Suggestions[0]);
                    Assert.Equal("jones", suggestions["Name"].Suggestions[1]);
                    Assert.Equal("johnson", suggestions["Name"].Suggestions[2]);
                    Assert.Equal("david", suggestions["Name"].Suggestions[3]);
                    Assert.Equal("jack", suggestions["Name"].Suggestions[4]);
                }
            }
        }
Example #12
0
        public async Task CanGetIndexNames()
        {
            var index1 = new Users_ByName();
            var index2 = new Posts_ByTitleAndContent();

            using (var store = GetDocumentStore())
            {
                index1.Execute(store);
                index2.Execute(store);

                var indexes = await store.AsyncDatabaseCommands.GetIndexNamesAsync(0, 10);

                Assert.Equal(2, indexes.Length);
                Assert.Equal(index1.IndexName, indexes[1]);
                Assert.Equal(index2.IndexName, indexes[0]);
            }
        }
Example #13
0
        public async Task CanGetIndexNames()
        {
            var index1 = new Users_ByName();
            var index2 = new Posts_ByTitleAndContent();

            using (var store = GetDocumentStore())
            {
                index1.Execute(store);
                index2.Execute(store);

                var indexes = await store.Maintenance.SendAsync(new GetIndexNamesOperation(0, 10));

                Assert.Equal(2, indexes.Length);
                Assert.Equal(index1.IndexName, indexes[1]);
                Assert.Equal(index2.IndexName, indexes[0]);
            }
        }
Example #14
0
        public void CanGetSuggestions()
        {
            using (var store = GetDocumentStore())
            {
                var index = new Users_ByName();
                index.Execute(store);

                store.DatabaseCommands.Put("users/1", null, RavenJObject.FromObject(new User {
                    Name = "John Smith"
                }), new RavenJObject {
                    { "Raven-Entity-Name", "Users" }
                });
                store.DatabaseCommands.Put("users/2", null, RavenJObject.FromObject(new User {
                    Name = "Jack Johnson"
                }), new RavenJObject {
                    { "Raven-Entity-Name", "Users" }
                });
                store.DatabaseCommands.Put("users/3", null, RavenJObject.FromObject(new User {
                    Name = "Robery Jones"
                }), new RavenJObject {
                    { "Raven-Entity-Name", "Users" }
                });
                store.DatabaseCommands.Put("users/4", null, RavenJObject.FromObject(new User {
                    Name = "David Jones"
                }), new RavenJObject {
                    { "Raven-Entity-Name", "Users" }
                });
                WaitForIndexing(store);

                var suggestions = store.DatabaseCommands.Suggest(index.IndexName, new SuggestionQuery()
                {
                    Field          = "Name",
                    Term           = "<<johne davi>>",
                    Accuracy       = 0.4f,
                    MaxSuggestions = 5,
                    Distance       = StringDistanceTypes.JaroWinkler,
                    Popularity     = true,
                });

                Assert.Equal("john", suggestions.Suggestions[0]);
                Assert.Equal("jones", suggestions.Suggestions[1]);
                Assert.Equal("johnson", suggestions.Suggestions[2]);
                Assert.Equal("david", suggestions.Suggestions[3]);
                Assert.Equal("jack", suggestions.Suggestions[4]);
            }
        }
Example #15
0
        public void FailWaitOnStaleTimeout()
        {
            using (var store = NewDocumentStore())
            {
                using (var bulkInsert = store.BulkInsert())
                {
                    for (int i = 0; i < 10; i++)
                    {
                        bulkInsert.Store(new User
                        {
                            Name = "Users #" + i
                        }, "users/" + i);
                    }
                }

                store.DatabaseCommands.Admin.StopIndexing();

                var usersByNameIndex = new Users_ByName();
                usersByNameIndex.Execute(store);

                var waits = 0;
                SystemTime.WaitCalled = ms => waits++;
                bool exceptionThrown = false;
                try
                {
                    var op = store.DatabaseCommands.DeleteByIndex("Users/ByName",
                                                                  new IndexQuery {
                        Query = "Name:Users*"
                    }
                                                                  , new BulkOperationOptions {
                        AllowStale = false, MaxOpsPerSec = null, StaleTimeout = TimeSpan.FromMilliseconds(1)
                    });

                    store.DatabaseCommands.Admin.StartIndexing();

                    op.WaitForCompletion();
                }
                catch (InvalidOperationException e)
                {
                    Assert.Contains("Operation failed: Bulk operation cancelled because the index is stale", e.Message);
                    exceptionThrown = true;
                }
                Assert.True(exceptionThrown);
            }
        }
Example #16
0
        public void CanUseMoreLikeThisWithIncludes()
        {
            using (var store = GetDocumentStore())
            {
                var index = new Users_ByName();
                index.Execute(store);

                using (var session = store.OpenSession())
                {
                    session.Store(new User {
                        Id = "users/1", AddressId = "addresses/1", Name = "John"
                    });
                    session.Store(new User {
                        Id = "users/2", AddressId = "addresses/2", Name = "John Doe"
                    });
                    session.Store(new Address {
                        Id = "addresses/1", City = "New York", Country = "USA"
                    });
                    session.Store(new Address {
                        Id = "addresses/2", City = "New York2", Country = "USA2"
                    });
                    session.SaveChanges();

                    WaitForIndexing(store);

                    var list = session.Advanced.MoreLikeThis <User>(new MoreLikeThisQuery()
                    {
                        Query      = $"FROM INDEX '{new Users_ByName().IndexName}'",
                        DocumentId = "users/1",
                        Includes   = new[] { "AddressId" },
                        MinimumDocumentFrequency = 1,
                        MinimumTermFrequency     = 0
                    });

                    Assert.Equal(1, list.Count);
                    Assert.Equal("John Doe", list[0].Name);

                    var address = session.Load <Address>(list[0].AddressId);
                    Assert.Equal("USA2", address.Country);
                }
            }
        }
Example #17
0
        public void CanUseMoreLikeThisWithIncludes()
        {
            using (var store = GetDocumentStore())
            {
                var index = new Users_ByName();
                index.Execute(store);

                using (var session = store.OpenSession())
                {
                    session.Store(new User {
                        Id = "users/1", AddressId = "addresses/1", Name = "John"
                    });
                    session.Store(new User {
                        Id = "users/2", AddressId = "addresses/2", Name = "John Doe"
                    });
                    session.Store(new Address {
                        Id = "addresses/1", City = "New York", Country = "USA"
                    });
                    session.Store(new Address {
                        Id = "addresses/2", City = "New York2", Country = "USA2"
                    });
                    session.SaveChanges();

                    WaitForIndexing(store);

                    var list = session.Query <User, Users_ByName>()
                               .Include(x => x.AddressId)
                               .MoreLikeThis(f => f.UsingDocument(x => x.Id == "users/1").WithOptions(new MoreLikeThisOptions
                    {
                        MinimumDocumentFrequency = 1,
                        MinimumTermFrequency     = 0
                    }))
                               .ToList();

                    Assert.Equal(1, list.Count);
                    Assert.Equal("John Doe", list[0].Name);

                    var address = session.Load <Address>(list[0].AddressId);
                    Assert.Equal("USA2", address.Country);
                }
            }
        }
Example #18
0
        public void CanWaitOnStaleTimeout()
        {
            using (var store = NewDocumentStore())
            {
                using (var bulkInsert = store.BulkInsert())
                {
                    for (int i = 0; i < 10; i++)
                    {
                        bulkInsert.Store(new User
                        {
                            Name = "Users #" + i
                        }, "users/" + i);
                    }
                }

                store.DatabaseCommands.Admin.StopIndexing();

                var usersByNameIndex = new Users_ByName();
                usersByNameIndex.Execute(store);

                var waits = 0;
                SystemTime.WaitCalled = ms => waits++;

                var op = store.DatabaseCommands.DeleteByIndex("Users/ByName",
                                                              new IndexQuery {
                    Query = "Name:Users*"
                }
                                                              , new BulkOperationOptions {
                    AllowStale = false, MaxOpsPerSec = null, StaleTimeout = TimeSpan.FromSeconds(15)
                });

                store.DatabaseCommands.Admin.StartIndexing();

                op.WaitForCompletion();
                using (var session = store.OpenSession())
                {
                    var results = from user in session.Query <User>() select user;
                    Assert.True(waits > 0 && !results.Any());
                }
            }
        }
Example #19
0
        public void CanStreamQueryResult()
        {
            using (var store = GetDocumentStore())
            {
                var index = new Users_ByName();
                index.Execute(store);

                for (int i = 0; i < 30; i++)
                {
                    store.DatabaseCommands.Put("users/" + i, null, RavenJObject.FromObject(new User { Name = "Name"+i }), new RavenJObject { { "Raven-Entity-Name", "Users" } });
                }
                WaitForIndexing(store);

                int count = 0;
                QueryHeaderInformation queryHeaders = null;
                var reader = store.DatabaseCommands.StreamQuery(index.IndexName, new IndexQuery { Query = "" }, out queryHeaders);
                while (reader.MoveNext())
                {
                    Assert.Equal("Name" + count, reader.Current.Value<string>("Name"));
                    count++;
                }
                Assert.Equal(30, count);
            }
        }
Example #20
0
        public void FailWaitOnStaleTimeout()
        {
            using (var store = GetDocumentStore())
            {
                using (var bulkInsert = store.BulkInsert())
                {
                    for (var i = 0; i < 10; i++)
                    {
                        bulkInsert.Store(new User
                        {
                            Name = "Users #" + i
                        });
                    }
                }

                store.Maintenance.Send(new StopIndexingOperation());

                var usersByNameIndex = new Users_ByName();
                usersByNameIndex.Execute(store);

                var exception = Assert.Throws <RavenException>(() =>
                {
                    var op = store.Operations.Send(new DeleteByQueryOperation(
                                                       new IndexQuery {
                        Query = "FROM INDEX 'Users/ByName' WHERE startsWith(Name, 'Users')"
                    },
                                                       new QueryOperationOptions {
                        AllowStale = false, MaxOpsPerSecond = null, StaleTimeout = TimeSpan.FromMilliseconds(10)
                    }));

                    op.WaitForCompletion(TimeSpan.FromSeconds(15));
                });

                Assert.Contains("Cannot perform bulk operation. Query is stale.", exception.Message);
            }
        }
Example #21
0
        public void WillGetStaleResultWhenThereArePendingTasks()
        {
            using (var store = GetDocumentStore())
            {
                var index = new Users_ByName();
                index.Execute(store);

                store.DatabaseCommands.Admin.StopIndexing();

                using (var session = store.OpenSession())
                {
                    session.Store(new User {
                        Name = "Name1"
                    });
                    session.SaveChanges();
                }

                Assert.True(store.DatabaseCommands.Query(index.IndexName, new IndexQuery
                {
                    PageSize = 2,
                    Start    = 0,
                }).IsStale);
            }
        }
Example #22
0
        public void CanSubscribeToIndexChanges()
        {
            using (var store = GetDocumentStore())
            {
                store.Changes().Task.Result
                .ForAllIndexes().Task.Result
                .Subscribe(new IndexChangeActionObserver(change =>
                {
                    Console.WriteLine(JsonConvert.SerializeObject(change));
                    if (change.Type == IndexChangeTypes.IndexAdded)
                    {
                        output.Add("passed_forallindexesadded");
                    }
                }));

                new Companies_CompanyByType().Execute(store);
                WaitForIndexing(store);
                WaitUntilOutput("passed_forallindexesadded");

                var usersByName = new Users_ByName();
                usersByName.Execute(store);
                WaitForIndexing(store);
                store.Changes().Task.Result
                .ForIndex(usersByName.IndexName).Task.Result
                .Subscribe(new IndexChangeActionObserver(change =>
                {
                    Console.WriteLine(JsonConvert.SerializeObject(change));
                    if (change.Type == IndexChangeTypes.MapCompleted)
                    {
                        output.Add("passed_forindexmapcompleted");
                    }
                }));

                var companiesSompanyByType = new Companies_CompanyByType();
                companiesSompanyByType.Execute(store);
                WaitForIndexing(store);
                store.Changes().Task.Result
                .ForIndex(companiesSompanyByType.IndexName).Task.Result
                .Subscribe(new IndexChangeActionObserver(change =>
                {
                    Console.WriteLine(JsonConvert.SerializeObject(change));
                    if (change.Type == IndexChangeTypes.RemoveFromIndex)
                    {
                        output2.Add("passed_forindexremovecompleted");
                    }
                    if (change.Type == IndexChangeTypes.ReduceCompleted)
                    {
                        output.Add("passed_forindexreducecompleted");
                    }
                }));

                using (var session = store.OpenSession())
                {
                    session.Store(new User {
                        Name = "user", LastName = "user"
                    });
                    session.SaveChanges();
                    WaitForIndexing(store);
                    WaitUntilOutput("passed_forindexmapcompleted");

                    session.Store(new Company {
                        Id = "companies/1", Name = "company", Type = Company.CompanyType.Public
                    });
                    session.SaveChanges();
                    WaitForIndexing(store);
                    WaitUntilOutput("passed_forindexreducecompleted");

                    session.Delete("companies/1");
                    session.SaveChanges();
                    WaitForIndexing(store);
                    WaitUntilOutput2("passed_forindexremovecompleted");
                }


                store.Changes().Task.Result
                .ForAllIndexes().Task.Result
                .Subscribe(new IndexChangeActionObserver(change =>
                {
                    if (change.Type == IndexChangeTypes.IndexRemoved)
                    {
                        output.Add("passed_forallindexesremoved");
                    }
                }));
                store.DatabaseCommands.DeleteIndex("Companies/CompanyByType");
                WaitForIndexing(store);
                Assert.Contains("passed_forallindexesremoved", output);
            }
        }
Example #23
0
        public async Task CanResetIndex()
        {
            var index = new Users_ByName();
            using (var store = GetDocumentStore())
            {
                index.Execute(store);
                for (var i = 0; i < 20; i++)
                {
                    await store.AsyncDatabaseCommands.PutAsync("users/" + i, null, RavenJObject.FromObject(new User { }), new RavenJObject());
                }
                WaitForIndexing(store);

                var stats = await store.AsyncDatabaseCommands.GetStatisticsAsync();
                Assert.Equal(0, stats.StaleIndexes.Length);

                await store.AsyncDatabaseCommands.Admin.StopIndexingAsync();
                await store.AsyncDatabaseCommands.ResetIndexAsync(index.IndexName);

                stats = await store.AsyncDatabaseCommands.GetStatisticsAsync();
                Assert.Equal(1, stats.StaleIndexes.Length);
                Assert.Equal(index.IndexName, stats.StaleIndexes[0]);
            }
        }
Example #24
0
        public async Task CanGetIndexNames()
        {
            var index1 = new Users_ByName();
            var index2 = new Posts_Recurse();
            using (var store = GetDocumentStore())
            {
                index1.Execute(store);
                index2.Execute(store);

                var indexes = await store.AsyncDatabaseCommands.GetIndexNamesAsync(0, 10);
                Assert.Equal(2, indexes.Length);
                Assert.Equal(index1.IndexName, indexes[1]);
                Assert.Equal(index2.IndexName, indexes[0]);
            }
        }
Example #25
0
        public void CanGetSuggestions()
        {
            using (var store = GetDocumentStore())
            {
                var index = new Users_ByName();
                index.Execute(store);

                store.DatabaseCommands.Put("users/1", null, RavenJObject.FromObject(new User { Name = "John Smith" }), new RavenJObject { { "Raven-Entity-Name", "Users" } });
                store.DatabaseCommands.Put("users/2", null, RavenJObject.FromObject(new User { Name = "Jack Johnson" }), new RavenJObject { { "Raven-Entity-Name", "Users" } });
                store.DatabaseCommands.Put("users/3", null, RavenJObject.FromObject(new User { Name = "Robery Jones" }), new RavenJObject { { "Raven-Entity-Name", "Users" } });
                store.DatabaseCommands.Put("users/4", null, RavenJObject.FromObject(new User { Name = "David Jones" }), new RavenJObject { { "Raven-Entity-Name", "Users" } });
                WaitForIndexing(store);

                var suggestions = store.DatabaseCommands.Suggest(index.IndexName, new SuggestionQuery()
                {
                    Field = "Name",
                    Term = "<<johne davi>>",
                    Accuracy = 0.4f,
                    MaxSuggestions = 5,
                    Distance = StringDistanceTypes.JaroWinkler,
                    Popularity = true,
                });

                Assert.Equal("john", suggestions.Suggestions[0]);
                Assert.Equal("jones", suggestions.Suggestions[1]);
                Assert.Equal("johnson", suggestions.Suggestions[2]);
                Assert.Equal("david", suggestions.Suggestions[3]);
                Assert.Equal("jack", suggestions.Suggestions[4]);
            }
        }
Example #26
0
        public void CanUseMoreLikeThisWithIncludes()
        {
            using (var store = GetDocumentStore())
            {
                var index = new Users_ByName();
                index.Execute(store);

                using (var session = store.OpenSession())
                {
                    session.Store(new User { Id = "users/1", AddressId = "addresses/1", Name = "John" });
                    session.Store(new User { Id = "users/2", AddressId = "addresses/2", Name = "John Doe" });
                    session.Store(new Address { Id = "addresses/1", City = "New York", Country = "USA" });
                    session.Store(new Address { Id = "addresses/2", City = "New York2", Country = "USA2" });
                    session.SaveChanges();

                    WaitForIndexing(store);

                    var list = session.Advanced.MoreLikeThis<User, Users_ByName>(new MoreLikeThisQuery
                    {
                        DocumentId = "users/1",
                        Includes = new [] {"AddressId"},
                        MinimumDocumentFrequency = 1,
                        MinimumTermFrequency = 0
                    });

                    Assert.Equal(1, list.Length);
                    Assert.Equal("John Doe", list[0].Name);

                    var address = session.Load<Address>(list[0].AddressId);
                    Assert.Equal("USA2", address.Country);
                }
            }
        }
Example #27
0
        public void CanSubscribeToIndexChanges()
        {
            using (var store = GetDocumentStore())
            {
                store.Changes().Task.Result
                    .ForAllIndexes().Task.Result
                    .Subscribe(change => 
                    {
                        if (change.Type == IndexChangeTypes.IndexAdded)
                        {
                            output = "passed_forallindexesadded";
                        }
                    });

                new Companies_CompanyByType().Execute(store);
                WaitForIndexing(store);
                WaitUntilOutput("passed_forallindexesadded");

                var usersByName = new Users_ByName();
                usersByName.Execute(store);
                WaitForIndexing(store);
                store.Changes().Task.Result
                    .ForIndex(usersByName.IndexName).Task.Result
                    .Subscribe(change =>
                    {
                        if (change.Type == IndexChangeTypes.MapCompleted)
                        {
                            output = "passed_forindexmapcompleted";
                        }
                    });

                var companiesSompanyByType = new Companies_CompanyByType();
                companiesSompanyByType.Execute(store);
                WaitForIndexing(store);
                store.Changes().Task.Result
                    .ForIndex(companiesSompanyByType.IndexName).Task.Result
                    .Subscribe(change =>
                    {
                        if (change.Type == IndexChangeTypes.RemoveFromIndex)
                        {
                            output2 = "passed_forindexremovecompleted";
                        }
                        if (change.Type == IndexChangeTypes.ReduceCompleted)
                        {
                            output = "passed_forindexreducecompleted";
                        }
                    });

                using (var session = store.OpenSession())
                {
                    session.Store(new User { Name = "user", LastName = "user" });
                    session.SaveChanges();
                    WaitForIndexing(store);
                    WaitUntilOutput("passed_forindexmapcompleted");

                    session.Store(new Company { Id = "companies/1", Name = "company", Type = Company.CompanyType.Public });
                    session.SaveChanges();
                    WaitForIndexing(store);
                    WaitUntilOutput("passed_forindexreducecompleted");

                    session.Delete("companies/1");
                    session.SaveChanges();
                    WaitForIndexing(store);
                    WaitUntilOutput2("passed_forindexremovecompleted");
                }


                store.Changes().Task.Result
                    .ForAllIndexes().Task.Result
                    .Subscribe(change =>
                    {
                        if (change.Type == IndexChangeTypes.IndexRemoved)
                        {
                            output = "passed_forallindexesremoved";
                        }
                    });
                store.DatabaseCommands.DeleteIndex("Companies/CompanyByType");
                WaitForIndexing(store);
                Assert.Equal("passed_forallindexesremoved", output);
            }
        }