Пример #1
0
        public void ConversionToTsVector()
        {
            using (var context = new BloggingContext(ConnectionString))
            {
                context.Database.Log = Console.Out.WriteLine;

                context.Blogs.Add(new Blog {
                    Name = "_"
                });
                context.SaveChanges();

                const string expected = "'a':5 'b':10";
                var          casted   = context.Blogs.Select(x => NpgsqlTextFunctions.AsTsVector(expected)).First();
                Assert.That(
                    NpgsqlTsVector.Parse(casted).ToString(),
                    Is.EqualTo(NpgsqlTsVector.Parse(expected).ToString()));

                var converted = context.Blogs.Select(x => NpgsqlTextFunctions.ToTsVector("banana car")).First();
                Assert.That(
                    NpgsqlTsVector.Parse(converted).ToString(),
                    Is.EqualTo(NpgsqlTsVector.Parse("'banana':1 'car':2").ToString()));

                converted = context.Blogs.Select(x => NpgsqlTextFunctions.ToTsVector("english", "banana car")).First();
                Assert.That(
                    NpgsqlTsVector.Parse(converted).ToString(),
                    Is.EqualTo(NpgsqlTsVector.Parse("'banana':1 'car':2").ToString()));
            }
        }
Пример #2
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));
            }
        }
Пример #3
0
        public void TsRankCd()
        {
            using (var context = new BloggingContext(ConnectionString))
            {
                context.Database.Log = Console.Out.WriteLine;

                var blog1 = new Blog
                {
                    Name = "cooky cookie piano pianos"
                };
                context.Blogs.Add(blog1);
                context.SaveChanges();

                var rank = context
                           .Blogs
                           .Select(
                    x => NpgsqlTextFunctions.TsRankCd(
                        NpgsqlTextFunctions.ToTsVector(x.Name),
                        NpgsqlTextFunctions.PlainToTsQuery("cookie")))
                           .FirstOrDefault();
                Assert.That(rank, Is.GreaterThan(0));

                rank = context
                       .Blogs
                       .Select(
                    x => NpgsqlTextFunctions.TsRankCd(
                        NpgsqlTextFunctions.ToTsVector(x.Name),
                        NpgsqlTextFunctions.PlainToTsQuery("cookie"),
                        NpgsqlRankingNormalization.DivideByLength
                        | NpgsqlRankingNormalization.DivideByUniqueWordCount))
                       .FirstOrDefault();
                Assert.That(rank, Is.GreaterThan(0));

                rank = context
                       .Blogs
                       .Select(
                    x => NpgsqlTextFunctions.TsRankCd(
                        0.1f, 0.2f, 0.4f, 1.0f,
                        NpgsqlTextFunctions.ToTsVector(x.Name),
                        NpgsqlTextFunctions.PlainToTsQuery("cookie")))
                       .FirstOrDefault();
                Assert.That(rank, Is.GreaterThan(0));

                rank = context
                       .Blogs
                       .Select(
                    x => NpgsqlTextFunctions.TsRankCd(
                        0.1f, 0.2f, 0.4f, 1.0f,
                        NpgsqlTextFunctions.ToTsVector(x.Name),
                        NpgsqlTextFunctions.PlainToTsQuery("cookie"),
                        NpgsqlRankingNormalization.DivideByLength
                        | NpgsqlRankingNormalization.DivideByUniqueWordCount))
                       .FirstOrDefault();
                Assert.That(rank, Is.GreaterThan(0));
            }
        }
Пример #4
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));
            }
        }
Пример #5
0
        public void Length()
        {
            using (var context = new BloggingContext(ConnectionString))
            {
                context.Database.Log = Console.Out.WriteLine;

                var blog = new Blog
                {
                    Name = "cooky cookie cookies piano pianos"
                };
                context.Blogs.Add(blog);
                context.SaveChanges();

                var lexemeCount = context
                                  .Blogs
                                  .Select(x => NpgsqlTextFunctions.Length(NpgsqlTextFunctions.ToTsVector(x.Name)))
                                  .FirstOrDefault();

                Assert.That(lexemeCount, Is.EqualTo(2));
            }
        }
Пример #6
0
        public void Strip()
        {
            using (var context = new BloggingContext(ConnectionString))
            {
                context.Database.Log = Console.Out.WriteLine;

                var blog = new Blog
                {
                    Name = "cooky cookie cookies piano pianos"
                };
                context.Blogs.Add(blog);
                context.SaveChanges();

                var strippedTsVector = context
                                       .Blogs
                                       .Select(x => NpgsqlTextFunctions.Strip(NpgsqlTextFunctions.ToTsVector(x.Name)))
                                       .FirstOrDefault();

                Assert.That(
                    NpgsqlTsVector.Parse(strippedTsVector).ToString(),
                    Is.EqualTo(NpgsqlTsVector.Parse("'cooki' 'piano'").ToString()));
            }
        }