Beispiel #1
0
        public void Match()
        {
            using (var context = new BloggingContext(ConnectionString))
            {
                context.Database.Log = Console.Out.WriteLine;

                var blog1 = new Blog
                {
                    Name = "The quick brown fox jumps over the lazy dog."
                };
                var blog2 = new Blog
                {
                    Name = "Jackdaws loves my big sphinx of quartz."
                };
                context.Blogs.Add(blog1);
                context.Blogs.Add(blog2);
                context.SaveChanges();

                var foundBlog = context
                                .Blogs
                                .FirstOrDefault(
                    x =>
                    NpgsqlTextFunctions.Match(
                        NpgsqlTextFunctions.ToTsVector(x.Name),
                        NpgsqlTextFunctions.ToTsQuery("jump & dog")));

                Assert.That(foundBlog != null);
                Assert.That(foundBlog.Name, Is.EqualTo(blog1.Name));
            }
        }
Beispiel #2
0
        public void SetWeight()
        {
            using (var context = new BloggingContext(ConnectionString))
            {
                context.Database.Log = Console.Out.WriteLine;

                var blog1 = new Blog
                {
                    Name = "The quick brown fox jumps over the lazy dog."
                };
                context.Blogs.Add(blog1);

                var post1 = new Post
                {
                    Blog    = blog1,
                    Title   = "Lorem ipsum",
                    Content = "Dolor sit amet",
                    Rating  = 5
                };
                context.Posts.Add(post1);

                var post2 = new Post
                {
                    Blog    = blog1,
                    Title   = "consectetur adipiscing elit",
                    Content = "Sed sed rhoncus",
                    Rating  = 4
                };
                context.Posts.Add(post2);
                context.SaveChanges();

                var foundPost = context.Posts.FirstOrDefault(
                    x => NpgsqlTextFunctions.Match(
                        NpgsqlTextFunctions.SetWeight(
                            NpgsqlTextFunctions.ToTsVector(x.Title ?? string.Empty),
                            NpgsqlWeightLabel.D)
                        + NpgsqlTextFunctions.SetWeight(
                            NpgsqlTextFunctions.ToTsVector(x.Content ?? string.Empty),
                            NpgsqlWeightLabel.C),
                        NpgsqlTextFunctions.PlainToTsQuery("dolor")));

                Assert.That(foundPost != null);
                Assert.That(foundPost.Title, Is.EqualTo(post1.Title));
            }
        }