示例#1
0
        public void Can_search_exact()
        {
            var dir = Path.Combine(Dir, "SearcherTests.Can_search_exact");

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

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

            var  writer    = new DocumentUpsertOperation(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.First(f => f.Key == "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));
            }
        }
示例#2
0
        public void Can_collect_prefixed()
        {
            var dir = Path.Combine(Dir, "Can_collect_prefixed");

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

            var docs = new List <Field>
            {
                new Field(0, "_id", "0"), new Field(0, "title", "rambo"),
                new Field(1, "_id", "1"), new Field(1, "title", "rambo 2"),
                new Field(2, "_id", "2"), new Field(2, "title", "rocky 2"),
                new Field(3, "_id", "3"), new Field(3, "title", "raiders of the lost ark"),
                new Field(4, "_id", "4"), new Field(4, "title", "rain man")
            }.GroupBy(f => f.DocumentId).Select(g => new Document(g.Key, g.ToList()));

            var  writer    = new DocumentUpsertOperation(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));
            }
        }
示例#3
0
        public void Can_collect_exact_phrase_joined_by_not()
        {
            var dir = Path.Combine(Dir, "Can_collect_exact_phrase_joined_by_not");

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

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

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

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

            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 == 3));
                Assert.IsTrue(scores.Any(d => d.DocumentId == 4));
                Assert.IsTrue(scores.Any(d => d.DocumentId == 5));
            }

            query = new QueryParser(new Analyzer()).Parse("+title:the -title:ugly");

            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 == 3));
                Assert.IsTrue(scores.Any(d => d.DocumentId == 4));
            }
        }
示例#4
0
        public void Can_collect_near()
        {
            var dir = Path.Combine(Dir, "Can_collect_near");

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

            var docs = new List <List <Field> >
            {
                new List <Field> {
                    new Field("_id", "0"), new Field("title", "rambo")
                },
                new List <Field> {
                    new Field("_id", "1"), new Field("title", "rambo 2")
                },
                new List <Field> {
                    new Field("_id", "2"), new Field("title", "rocky 2")
                },
                new List <Field> {
                    new Field("_id", "3"), new Field("title", "raiders of the lost ark")
                },
                new List <Field> {
                    new Field("_id", "4"), new Field("title", "tomb raider")
                }
            };

            var  writer    = new DocumentUpsertOperation(dir, new Analyzer(), compression: Compression.QuickLz, 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", "raider")
                {
                    Fuzzy = false, Edits = 1
                }).ToList();

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

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

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

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

            var docs = new List <Field>
            {
                new Field(0, "_id", "0"), new Field(0, "title", "rambo first blood"),
                new Field(1, "_id", "1"), new Field(1, "title", "rambo 2"),
                new Field(2, "_id", "2"), new Field(2, "title", "rocky 2"),
                new Field(3, "_id", "3"), new Field(3, "title", "the raid"),
                new Field(4, "_id", "4"), new Field(4, "title", "the rain man"),
                new Field(5, "_id", "5"), new Field(5, "title", "the good, the bad and the ugly")
            }.GroupBy(f => f.DocumentId).Select(g => new Document(g.Key, g.ToList()));

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

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

            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 == 4));
            }

            query = new QueryParser(new Analyzer(), 0.75f).Parse("+title:rain man~");

            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 == 4));
            }
        }
示例#6
0
        public void Can_collect_by_id()
        {
            var dir = Path.Combine(Dir, "Can_collect_by_id");

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

            var docs = new List <Field>
            {
                new Field(0, "_id", "abc0123"), new Field(0, "title", "rambo first blood"),
                new Field(1, "_id", "1"), new Field(1, "title", "rambo 2"),
                new Field(2, "_id", "2"), new Field(2, "title", "rocky 2"),
                new Field(3, "_id", "3"), new Field(3, "title", "the raiders of the lost ark"),
                new Field(4, "_id", "four"), new Field(4, "title", "the rain man"),
                new Field(5, "_id", "5five"), new Field(5, "title", "the good, the bad and the ugly")
            }.GroupBy(f => f.DocumentId).Select(g => new Document(g.Key, g.ToList())).OrderBy(d => d.Id);

            var  writer    = new DocumentUpsertOperation(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));
            }
        }