Example #1
0
        private TestIndex CreateTestIndex(Directory luceneDirectory, string[] fieldNames)
        {
            var indexer = new TestIndex("TestIndex", luceneDirectory, fieldNames);

            using (indexer.ProcessNonAsync())
            {
                //populate with some test data
                indexer.IndexItem(new ValueSet("1", "content", new Dictionary <string, object>
                {
                    [fieldNames[0]] = "Hello world, there are products here",
                    [UmbracoContentIndex.VariesByCultureFieldName] = "n"
                }));
                indexer.IndexItem(new ValueSet("2", "content", new Dictionary <string, object>
                {
                    [fieldNames[1]] = "Hello world, there are products here",
                    [UmbracoContentIndex.VariesByCultureFieldName] = "y"
                }));
                indexer.IndexItem(new ValueSet("3", "content", new Dictionary <string, object>
                {
                    [fieldNames[2]] = "Hello world, there are products here",
                    [UmbracoContentIndex.VariesByCultureFieldName] = "y"
                }));
            }

            return(indexer);
        }
Example #2
0
        public void MultiIndex_Simple_Search()
        {
            var analyzer = new StandardAnalyzer(Version.LUCENE_30);

            using (var luceneDir1 = new RandomIdRAMDirectory())
                using (var luceneDir2 = new RandomIdRAMDirectory())
                    using (var luceneDir3 = new RandomIdRAMDirectory())
                        using (var luceneDir4 = new RandomIdRAMDirectory())
                            using (var indexer1 = new TestIndex(luceneDir1, analyzer))
                                using (var indexer2 = new TestIndex(luceneDir2, analyzer))
                                    using (var indexer3 = new TestIndex(luceneDir3, analyzer))
                                        using (var indexer4 = new TestIndex(luceneDir4, analyzer))

                                        {
                                            indexer1.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "value1", item2 = "The agitated zebras gallop back and forth in short, panicky dashes, then skitter off into the absolute darkness." }));
                                            indexer2.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "value2", item2 = "The festival lasts five days and celebrates the victory of good over evil, light over darkness, and knowledge over ignorance." }));
                                            indexer3.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "value3", item2 = "They are expected to confront the darkness and show evidence that they have done so in their papers" }));
                                            indexer4.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "value4", item2 = "Scientists believe the lake could be home to cold-loving microbial life adapted to living in total darkness." }));
                                            indexer3.IndexItem(ValueSet.FromObject(2.ToString(), "content", new { item1 = "value3", item2 = "Scotch scotch scotch, i love scotch" }));
                                            indexer4.IndexItem(ValueSet.FromObject(2.ToString(), "content", new { item1 = "value4", item2 = "60% of the time, it works everytime" }));

                                            using (var searcher = new MultiIndexSearcher("testSearcher",
                                                                                         new[] { indexer1, indexer2, indexer3, indexer4 },
                                                                                         analyzer))
                                            {
                                                var result = searcher.Search("darkness");

                                                Assert.AreEqual(4, result.TotalItemCount);
                                                foreach (var r in result)
                                                {
                                                    Console.WriteLine("Score = " + r.Score);
                                                }
                                            }
                                        }
        }
        private TestIndex CreateTestIndex(Directory luceneDirectory, string[] fieldNames)
        {
            var index = new TestIndex(LoggerFactory, "TestIndex", luceneDirectory, fieldNames);

            using (index.WithThreadingMode(IndexThreadingMode.Synchronous))
            {
                //populate with some test data
                index.IndexItem(new ValueSet("1", "content", new Dictionary <string, object>
                {
                    [fieldNames[0]] = "Hello world, there are products here",
                    [UmbracoExamineFieldNames.VariesByCultureFieldName] = "n"
                }));
                index.IndexItem(new ValueSet("2", "content", new Dictionary <string, object>
                {
                    [fieldNames[1]] = "Hello world, there are products here",
                    [UmbracoExamineFieldNames.VariesByCultureFieldName] = "y"
                }));
                index.IndexItem(new ValueSet("3", "content", new Dictionary <string, object>
                {
                    [fieldNames[2]] = "Hello world, there are products here",
                    [UmbracoExamineFieldNames.VariesByCultureFieldName] = "y"
                }));
            }

            return(index);
        }
Example #4
0
        public void Number_Field()
        {
            using (var luceneDir = new RandomIdRAMDirectory())
                using (var indexer = new TestIndex(
                           new FieldDefinitionCollection(new FieldDefinition("item2", "number")),
                           luceneDir,
                           new StandardAnalyzer(Version.LUCENE_30)))
                {
                    indexer.IndexItem(new ValueSet(1.ToString(), "content",
                                                   new Dictionary <string, IEnumerable <object> >
                    {
                        { "item1", new List <object>(new[] { "value1" }) },
                        { "item2", new List <object>(new object[] { 123456 }) }
                    }));

                    using (var s = (LuceneSearcher)indexer.GetSearcher())
                    {
                        var luceneSearcher = s.GetLuceneSearcher();
                        var fields         = luceneSearcher.Doc(luceneSearcher.MaxDoc - 1).GetFields().ToArray();

                        var valType = indexer.FieldValueTypeCollection.GetValueType("item2");
                        Assert.AreEqual(typeof(Int32Type), valType.GetType());
                        Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == "item2"));
                    }
                }
        }
Example #5
0
        public void Can_Add_Doc_With_Fields()
        {
            using (var luceneDir = new RandomIdRAMDirectory())
                using (var indexer = new TestIndex(luceneDir, new StandardAnalyzer(Version.LUCENE_30)))
                {
                    indexer.IndexItem(new ValueSet(1.ToString(), "content", "test",
                                                   new Dictionary <string, IEnumerable <object> >
                    {
                        { "item1", new List <object>(new[] { "value1" }) },
                        { "item2", new List <object>(new[] { "value2" }) }
                    }));


                    using (var s = (LuceneSearcher)indexer.GetSearcher())
                    {
                        var luceneSearcher = s.GetLuceneSearcher();
                        var fields         = luceneSearcher.Doc(0).GetFields().ToArray();
                        Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == "item1"));
                        Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == "item2"));
                        Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == LuceneIndex.ItemTypeFieldName));
                        Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == LuceneIndex.ItemIdFieldName));
                        Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == LuceneIndex.CategoryFieldName));

                        Assert.AreEqual("value1", fields.Single(x => x.Name == "item1").StringValue);
                        Assert.AreEqual("value2", fields.Single(x => x.Name == "item2").StringValue);
                        Assert.AreEqual("test", fields.Single(x => x.Name == LuceneIndex.ItemTypeFieldName).StringValue);
                        Assert.AreEqual("1", fields.Single(x => x.Name == LuceneIndex.ItemIdFieldName).StringValue);
                        Assert.AreEqual("content", fields.Single(x => x.Name == LuceneIndex.CategoryFieldName).StringValue);
                    }
                }
        }
Example #6
0
        public void MultiIndex_Field_Count()
        {
            var analyzer = new StandardAnalyzer(Version.LUCENE_30);

            using (var luceneDir1 = new RandomIdRAMDirectory())
                using (var luceneDir2 = new RandomIdRAMDirectory())
                    using (var luceneDir3 = new RandomIdRAMDirectory())
                        using (var luceneDir4 = new RandomIdRAMDirectory())
                            using (var indexer1 = new TestIndex(luceneDir1, analyzer)
                            {
                                RunAsync = false
                            })
                                using (var indexer2 = new TestIndex(luceneDir2, analyzer)
                                {
                                    RunAsync = false
                                })
                                    using (var indexer3 = new TestIndex(luceneDir3, analyzer)
                                    {
                                        RunAsync = false
                                    })
                                        using (var indexer4 = new TestIndex(luceneDir4, analyzer)
                                        {
                                            RunAsync = false
                                        })
                                        {
                                            indexer1.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "hello", item2 = "The agitated zebras gallop back and forth in short, panicky dashes, then skitter off into the absolute darkness." }));
                                            indexer2.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "world", item2 = "The festival lasts five days and celebrates the victory of good over evil, light over darkness, and knowledge over ignorance." }));
                                            indexer3.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "here", item2 = "They are expected to confront the darkness and show evidence that they have done so in their papers" }));
                                            indexer4.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "are", item2 = "Scientists believe the lake could be home to cold-loving microbial life adapted to living in total darkness." }));
                                            indexer3.IndexItem(ValueSet.FromObject(2.ToString(), "content", new { item3 = "some", item2 = "Scotch scotch scotch, i love scotch" }));
                                            indexer4.IndexItem(ValueSet.FromObject(2.ToString(), "content", new { item4 = "values", item2 = "60% of the time, it works everytime" }));

                                            using (var searcher = new MultiIndexSearcher("testSearcher",
                                                                                         new[] { indexer1, indexer2, indexer3, indexer4 },
                                                                                         analyzer))
                                            {
                                                var result = searcher.GetAllIndexedFields();
                                                //will be item1 , item2, item3, and item4
                                                Assert.AreEqual(4, result.Count());
                                                foreach (var s in new[] { "item1", "item2", "item3", "item4" })
                                                {
                                                    Assert.IsTrue(result.Contains(s));
                                                }
                                            }
                                        }
        }
Example #7
0
        public void Can_Add_Same_Document_Twice_Without_Duplication()
        {
            using (var luceneDir = new RandomIdRAMDirectory())
                using (var indexer = new TestIndex(luceneDir, new StandardAnalyzer(Version.LUCENE_30)))
                {
                    var value = new ValueSet(1.ToString(), "content",
                                             new Dictionary <string, IEnumerable <object> >
                    {
                        { "item1", new List <object>(new[] { "value1" }) },
                        { "item2", new List <object>(new[] { "value2" }) }
                    });

                    indexer.IndexItem(value);
                    indexer.IndexItem(value);

                    var indexWriter = indexer.GetIndexWriter();
                    var reader      = indexWriter.GetReader();
                    Assert.AreEqual(1, reader.NumDocs());
                }
        }
Example #8
0
        public void Can_Update_Document()
        {
            using (var luceneDir = new RandomIdRAMDirectory())
                using (var indexer = new TestIndex(luceneDir, new StandardAnalyzer(Version.LUCENE_30)))
                {
                    indexer.IndexItem(ValueSet.FromObject(1.ToString(), "content",
                                                          new { item1 = "value1", item2 = "value2" }));

                    indexer.IndexItem(ValueSet.FromObject(1.ToString(), "content",
                                                          new { item1 = "value3", item2 = "value4" }));

                    using (var s = (LuceneSearcher)indexer.GetSearcher())
                    {
                        var luceneSearcher = s.GetLuceneSearcher();
                        var fields         = luceneSearcher.Doc(luceneSearcher.MaxDoc - 1).GetFields().ToArray();
                        Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == "item1"));
                        Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == "item2"));
                        Assert.AreEqual("value3", fields.Single(x => x.Name == "item1").StringValue);
                        Assert.AreEqual("value4", fields.Single(x => x.Name == "item2").StringValue);
                    }
                }
        }
Example #9
0
        public void Can_Add_Multiple_Docs()
        {
            using (var luceneDir = new RandomIdRAMDirectory())
                using (var indexer = new TestIndex(luceneDir, new StandardAnalyzer(Version.LUCENE_30)))
                {
                    for (var i = 0; i < 10; i++)
                    {
                        indexer.IndexItem(new ValueSet(i.ToString(), "content",
                                                       new Dictionary <string, IEnumerable <object> >
                        {
                            { "item1", new List <object>(new[] { "value1" }) },
                            { "item2", new List <object>(new[] { "value2" }) }
                        }));
                    }

                    var indexWriter = indexer.GetIndexWriter();
                    var reader      = indexWriter.GetReader();
                    Assert.AreEqual(10, reader.NumDocs());
                }
        }
Example #10
0
        public void Can_Have_Multiple_Values_In_Fields()
        {
            using (var luceneDir = new RandomIdRAMDirectory())
                using (var indexer = new TestIndex(luceneDir, new StandardAnalyzer(Version.LUCENE_30)))
                {
                    indexer.IndexItem(new ValueSet(1.ToString(), "content",
                                                   new Dictionary <string, IEnumerable <object> >
                    {
                        {
                            "item1", new List <object> {
                                "subval1", "subval2"
                            }
                        },
                        {
                            "item2", new List <object> {
                                "subval1", "subval2", "subval3"
                            }
                        }
                    }));

                    using (var s = (LuceneSearcher)indexer.GetSearcher())
                    {
                        var luceneSearcher = s.GetLuceneSearcher();
                        var fields         = luceneSearcher.Doc(0).GetFields().ToArray();
                        Assert.AreEqual(2, fields.Count(x => x.Name == "item1"));
                        Assert.AreEqual(3, fields.Count(x => x.Name == "item2"));

                        Assert.AreEqual("subval1", fields.Where(x => x.Name == "item1").ElementAt(0).StringValue);
                        Assert.AreEqual("subval2", fields.Where(x => x.Name == "item1").ElementAt(1).StringValue);

                        Assert.AreEqual("subval1", fields.Where(x => x.Name == "item2").ElementAt(0).StringValue);
                        Assert.AreEqual("subval2", fields.Where(x => x.Name == "item2").ElementAt(1).StringValue);
                        Assert.AreEqual("subval3", fields.Where(x => x.Name == "item2").ElementAt(2).StringValue);
                    }
                }
        }