Esempio n. 1
0
        public async Task CanDeleteAndUpdateDocumentByIndex()
        {
            var usersByNameIndex = new Users_ByName();
            using (var store = GetDocumentStore())
            {
                store.DatabaseCommands.PutIndex("MyIndex", new IndexDefinition
                {
                    Map = "from doc in docs select new { Name }"
                });

                store.DatabaseCommands.Put("items/1", null, RavenJObject.FromObject(new
                {
                    Name = "testname"
                }), new RavenJObject());
                WaitForIndexing(store);

                store.DatabaseCommands.UpdateByIndex("MyIndex", new IndexQuery { Query = "" }, new[] 
                {
                    new PatchRequest 
                    {
                        Type = PatchCommandType.Set,
                        Name = "NewName",
                        Value = "NewValue"
                    }
                },
                false).WaitForCompletion();

                var document = await store.AsyncDatabaseCommands.GetAsync("items/1");
                Assert.Equal("NewValue", document.DataAsJson.Value<string>("NewName"));

                store.DatabaseCommands.DeleteByIndex("MyIndex", new IndexQuery { Query = "" }, false).WaitForCompletion();
                var documents = store.DatabaseCommands.GetDocuments(0, 25);
                Assert.Equal(0, documents.Length);
            }
        }
Esempio n. 2
0
        public async Task CanDeleteAndUpdateDocumentByIndex()
        {
            var usersByNameIndex = new Users_ByName();
            using (var store = GetDocumentStore())
            {
                store.DatabaseCommands.PutIndex("MyIndex", new IndexDefinition
                {
                    Map = "from doc in docs select new { Name }"
                });

                store.DatabaseCommands.Put("items/1", null, RavenJObject.FromObject(new
                {
                    Name = "testname"
                }), new RavenJObject());
                WaitForIndexing(store);

                store.DatabaseCommands.UpdateByIndex("MyIndex", new IndexQuery { Query = "" }, new[] 
                {
                    new PatchRequest 
                    {
                        Type = PatchCommandType.Set,
                        Name = "NewName",
                        Value = "NewValue"
                    }
                },
                null).WaitForCompletion();

                var document = await store.AsyncDatabaseCommands.GetAsync("items/1");
                Assert.Equal("NewValue", document.DataAsJson.Value<string>("NewName"));

                store.DatabaseCommands.DeleteByIndex("MyIndex", new IndexQuery { Query = "" }, null).WaitForCompletion();
                var documents = store.DatabaseCommands.GetDocuments(0, 25);
                Assert.Equal(0, documents.Length);
            }
        }
Esempio n. 3
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]);
            }
        }
Esempio n. 4
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]);
                }
            }
        }
Esempio n. 5
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);
                }
            }
        }
Esempio n. 6
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);
            }
        }
Esempio n. 7
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);
                }
            }
        }
Esempio n. 8
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);
            }
        }
Esempio n. 9
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]);
            }
        }
Esempio n. 10
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]);
            }
        }
Esempio n. 11
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]);
            }
        }