示例#1
0
        public void Can_collect_prefixed()
        {
            var dir = Path.Combine(CreateDir(), "Can_collect_prefixed");

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            var docs = new List <dynamic>
            {
                new { _id = "0", title = "rambo" },
                new { _id = "1", title = "rambo 2" },
                new { _id = "2", title = "rocky 2" },
                new { _id = "3", title = "raiders of the lost ark" },
                new { _id = "4", title = "rain man" }
            }.ToDocuments();

            var  writer    = new DocumentsUpsertOperation(dir, new Analyzer(), compression: Compression.Lz, primaryKey: "_id", documents: docs);
            long indexName = writer.Commit();

            using (var collector = new Collector(dir, IxInfo.Load(Path.Combine(dir, indexName + ".ix")), new Tfidf()))
            {
                var scores = collector.Collect(new QueryContext("title", "ra")
                {
                    Prefix = true
                }).ToList();

                Assert.AreEqual(4, scores.Count);
                Assert.IsTrue(scores.Any(d => d.DocumentId == 0));
                Assert.IsTrue(scores.Any(d => d.DocumentId == 1));
                Assert.IsTrue(scores.Any(d => d.DocumentId == 3));
                Assert.IsTrue(scores.Any(d => d.DocumentId == 4));
            }
        }
示例#2
0
        public void Can_collect_exact_phrase_joined_by_or()
        {
            var dir = Path.Combine(CreateDir(), "Can_collect_exact_phrase_joined_by_or");

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            var docs = new List <dynamic>
            {
                new { _id = "0", title = "rambo first blood" },
                new { _id = "1", title = "rambo 2" },
                new { _id = "2", title = "rocky 2" },
                new { _id = "3", title = "raiders of the lost ark" },
                new { _id = "4", title = "the rain man" },
                new { _id = "5", title = "the good, the bad and the ugly" }
            }.ToDocuments();

            var  writer    = new DocumentsUpsertOperation(dir, new Analyzer(), compression: Compression.Lz, primaryKey: "_id", documents: docs);
            long indexName = writer.Commit();

            var query = new QueryParser(new Analyzer()).Parse("+title:rocky");

            using (var collector = new Collector(dir, IxInfo.Load(Path.Combine(dir, indexName + ".ix")), new Tfidf()))
            {
                var scores = collector.Collect(query).ToList();

                Assert.AreEqual(1, scores.Count);
                Assert.IsTrue(scores.Any(d => d.DocumentId == 2));
            }

            query = new QueryParser(new Analyzer()).Parse("+title:rambo");

            using (var collector = new Collector(dir, IxInfo.Load(Path.Combine(dir, indexName + ".ix")), new Tfidf()))
            {
                var scores = collector.Collect(query).ToList();

                Assert.AreEqual(2, scores.Count);
                Assert.IsTrue(scores.Any(d => d.DocumentId == 0));
                Assert.IsTrue(scores.Any(d => d.DocumentId == 1));
            }

            query = new QueryParser(new Analyzer()).Parse("+title:rocky title:rambo");

            using (var collector = new Collector(dir, IxInfo.Load(Path.Combine(dir, indexName + ".ix")), new Tfidf()))
            {
                var scores = collector.Collect(query).ToList();

                Assert.AreEqual(3, scores.Count);
                Assert.IsTrue(scores.Any(d => d.DocumentId == 0));
                Assert.IsTrue(scores.Any(d => d.DocumentId == 1));
                Assert.IsTrue(scores.Any(d => d.DocumentId == 2));
            }
        }
示例#3
0
        public void Can_search_exact()
        {
            var dir = Path.Combine(CreateDir(), "SearcherTests.Can_search_exact");

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            var docs = new List <dynamic>
            {
                new { _id = "0", title = "Rambo First Blood" },
                new { _id = "1", title = "rambo 2" },
                new { _id = "2", title = "rocky 2" },
                new { _id = "3", title = "the raiders of the lost ark" },
                new { _id = "4", title = "the rain man" },
                new { _id = "5", title = "the good, the bad and the ugly" }
            }.ToDocuments();

            var writer = new DocumentsUpsertOperation(
                dir, new Analyzer(), compression: Compression.NoCompression, primaryKey: "_id", documents: docs);
            long indexName = writer.Commit();

            using (var searcher = new Searcher(dir))
            {
                var result = searcher.Search("title:rambo");

                Assert.AreEqual(2, result.Total);
                Assert.AreEqual(2, result.Docs.Count);

                Assert.IsTrue(result.Docs.Any(d => d.Document.Id == 0));
                Assert.IsTrue(result.Docs.Any(d => d.Document.Id == 1));

                Assert.AreEqual(
                    "Rambo First Blood",
                    result.Docs.First(d => d.Document.Id == 0).Document.Fields["title"].Value);
            }

            using (var searcher = new Searcher(dir))
            {
                var result = searcher.Search("title:the");

                Assert.AreEqual(3, result.Total);
                Assert.AreEqual(3, result.Docs.Count);
                Assert.IsTrue(result.Docs.Any(d => d.Document.Id == 3));
                Assert.IsTrue(result.Docs.Any(d => d.Document.Id == 4));
                Assert.IsTrue(result.Docs.Any(d => d.Document.Id == 5));
            }
        }
示例#4
0
        public void Can_collect_by_id()
        {
            var dir = Path.Combine(CreateDir(), "Can_collect_by_id");

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            var docs = new List <dynamic>
            {
                new { _id = "abc0123", title = "rambo first blood" },
                new { _id = "1", title = "rambo 2" },
                new { _id = "2", title = "rocky 2" },
                new { _id = "3", title = "the raiders of the lost ark" },
                new { _id = "four", title = "the rain man" },
                new { _id = "5five", title = "the good, the bad and the ugly" }
            }.ToDocuments();

            var  writer    = new DocumentsUpsertOperation(dir, new Analyzer(), compression: Compression.Lz, primaryKey: "_id", documents: docs);
            long indexName = writer.Commit();

            using (var collector = new Collector(dir, IxInfo.Load(Path.Combine(dir, indexName + ".ix")), new Tfidf()))
            {
                var scores = collector.Collect(new QueryContext("_id", "3")).ToList();

                Assert.AreEqual(1, scores.Count);
                Assert.IsTrue(scores.Any(d => d.DocumentId == 3));
            }

            using (var collector = new Collector(dir, IxInfo.Load(Path.Combine(dir, indexName + ".ix")), new Tfidf()))
            {
                var scores = collector.Collect(new QueryContext("_id", "5five")).ToList();

                Assert.AreEqual(1, scores.Count);
                Assert.IsTrue(scores.Any(d => d.DocumentId == 5));
            }
        }