示例#1
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));
            }
        }
示例#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));
            }
        }
示例#3
0
        public void ConversionToTsQuery()
        {
            using (var context = new BloggingContext(ConnectionString))
            {
                context.Database.Log = Console.Out.WriteLine;

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

                const string expected = "'b' & 'c'";
                var          casted   = context.Blogs.Select(x => NpgsqlTextFunctions.AsTsQuery(expected)).First();
                Assert.That(
                    NpgsqlTsQuery.Parse(casted).ToString(),
                    Is.EqualTo(NpgsqlTsQuery.Parse(expected).ToString()));

                var converted = context.Blogs.Select(x => NpgsqlTextFunctions.ToTsQuery("b & c")).First();
                Assert.That(
                    NpgsqlTsQuery.Parse(converted).ToString(),
                    Is.EqualTo(NpgsqlTsQuery.Parse(expected).ToString()));

                converted = context.Blogs.Select(x => NpgsqlTextFunctions.ToTsQuery("english", "b & c")).First();
                Assert.That(
                    NpgsqlTsQuery.Parse(converted).ToString(),
                    Is.EqualTo(NpgsqlTsQuery.Parse(expected).ToString()));

                converted = context.Blogs.Select(x => NpgsqlTextFunctions.PlainToTsQuery("b & c")).First();
                Assert.That(
                    NpgsqlTsQuery.Parse(converted).ToString(),
                    Is.EqualTo(NpgsqlTsQuery.Parse(expected).ToString()));

                converted = context.Blogs.Select(x => NpgsqlTextFunctions.PlainToTsQuery("english", "b & c")).First();
                Assert.That(
                    NpgsqlTsQuery.Parse(converted).ToString(),
                    Is.EqualTo(NpgsqlTsQuery.Parse(expected).ToString()));
            }
        }