public void Index_Protected_Content_Not_Indexed() { using (var luceneDir = new RandomIdRAMDirectory()) using (var writer = new IndexWriter(luceneDir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), IndexWriter.MaxFieldLength.LIMITED)) using (var indexer = IndexInitializer.GetUmbracoIndexer(writer)) using (var searcher = IndexInitializer.GetUmbracoSearcher(writer)) { indexer.RebuildIndex(); var protectedQuery = new BooleanQuery(); protectedQuery.Add( new BooleanClause( new TermQuery(new Term(LuceneIndexer.IndexTypeFieldName, IndexTypes.Content)), BooleanClause.Occur.MUST)); protectedQuery.Add( new BooleanClause( new TermQuery(new Term(LuceneIndexer.IndexNodeIdFieldName, TestContentService.ProtectedNode.ToString())), BooleanClause.Occur.MUST)); var collector = new AllHitsCollector(false, true); var s = searcher.GetSearcher(); s.Search(protectedQuery, collector); Assert.AreEqual(0, collector.Count, "Protected node should not be indexed"); } }
public void Index_Delete_Index_Item_Ensure_Heirarchy_Removed() { var rebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetInstance <PropertyEditorCollection>(), IndexInitializer.GetMockContentService(), ScopeProvider.SqlContext, false); using (var luceneDir = new RandomIdRamDirectory()) using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir)) using (indexer.ProcessNonAsync()) { var searcher = indexer.GetSearcher(); //create the whole thing rebuilder.Populate(indexer); //now delete a node that has children indexer.DeleteFromIndex(1140.ToString()); //this node had children: 1141 & 1142, let's ensure they are also removed var results = searcher.CreateQuery().Id(1141).Execute(); Assert.AreEqual(0, results.Count()); results = searcher.CreateQuery().Id(1142).Execute(); Assert.AreEqual(0, results.Count()); } }
public void Index_Reindex_Content() { var rebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetInstance <PropertyEditorCollection>(), IndexInitializer.GetMockContentService(), ScopeProvider.SqlContext, false); using (var luceneDir = new RandomIdRamDirectory()) using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir, validator: new ContentValueSetValidator(false))) using (indexer.ProcessNonAsync()) { var searcher = indexer.GetSearcher(); //create the whole thing rebuilder.Populate(indexer); var result = searcher.CreateQuery().Field(LuceneIndex.CategoryFieldName, IndexTypes.Content).Execute(); Assert.AreEqual(21, result.TotalItemCount); //delete all content foreach (var r in result) { indexer.DeleteFromIndex(r.Id); } //ensure it's all gone result = searcher.CreateQuery().Field(LuceneIndex.CategoryFieldName, IndexTypes.Content).Execute(); Assert.AreEqual(0, result.TotalItemCount); //call our indexing methods rebuilder.Populate(indexer); result = searcher.CreateQuery().Field(LuceneIndex.CategoryFieldName, IndexTypes.Content).Execute(); Assert.AreEqual(21, result.TotalItemCount); } }
public void Index_Protected_Content_Not_Indexed() { var rebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetInstance <PropertyEditorCollection>(), IndexInitializer.GetMockContentService(), ScopeProvider.SqlContext, false); using (var luceneDir = new RandomIdRamDirectory()) using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir)) using (indexer.ProcessNonAsync()) using (var searcher = ((LuceneSearcher)indexer.GetSearcher()).GetLuceneSearcher()) { //create the whole thing rebuilder.Populate(indexer); var protectedQuery = new BooleanQuery(); protectedQuery.Add( new BooleanClause( new TermQuery(new Term(LuceneIndex.CategoryFieldName, IndexTypes.Content)), Occur.MUST)); protectedQuery.Add( new BooleanClause( new TermQuery(new Term(LuceneIndex.ItemIdFieldName, ExamineDemoDataContentService.ProtectedNode.ToString())), Occur.MUST)); var collector = TopScoreDocCollector.Create(100, true); searcher.Search(protectedQuery, collector); Assert.AreEqual(0, collector.TotalHits, "Protected node should not be indexed"); } }
public void Events_Ignoring_Node() { using (var luceneDir = new RandomIdRAMDirectory()) using (var writer = new IndexWriter(luceneDir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), IndexWriter.MaxFieldLength.LIMITED)) using (var indexer = IndexInitializer.GetUmbracoIndexer(writer)) using (var searcher = IndexInitializer.GetUmbracoSearcher(writer)) { //change the parent id so that they are all ignored var existingCriteria = indexer.IndexerData; indexer.IndexerData = new IndexCriteria(existingCriteria.StandardFields, existingCriteria.UserFields, existingCriteria.IncludeNodeTypes, existingCriteria.ExcludeNodeTypes, 999); //change to 999 var isIgnored = false; EventHandler <IndexingNodeDataEventArgs> ignoringNode = (s, e) => { isIgnored = true; }; indexer.IgnoringNode += ignoringNode; //get a node from the data repo var node = _contentService.GetPublishedContentByXPath("//*[string-length(@id)>0 and number(@id)>0]") .Root .Elements() .First(); indexer.ReIndexNode(node, IndexTypes.Content); Assert.IsTrue(isIgnored); } }
public void Test_Sort_Order_Sorting() { using (var luceneDir = new RandomIdRAMDirectory()) using (var writer = new IndexWriter(luceneDir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), IndexWriter.MaxFieldLength.LIMITED)) using (var indexer = IndexInitializer.GetUmbracoIndexer(writer, null, new TestDataService() { ContentService = new TestContentService(TestFiles.umbraco_sort) }, supportUnpublishedContent: true)) using (var searcher = IndexInitializer.GetUmbracoSearcher(writer)) { indexer.RebuildIndex(); var s = (LuceneSearcher)searcher; var luceneSearcher = s.GetSearcher(); var i = (LuceneIndexer)indexer; var numberSortedCriteria = searcher.CreateSearchCriteria() .ParentId(1148).And() .OrderBy(new SortableField("sortOrder", SortType.Int)); var numberSortedResult = searcher.Search(numberSortedCriteria.Compile()); var stringSortedCriteria = searcher.CreateSearchCriteria() .ParentId(1148).And() .OrderBy("sortOrder"); //will default to string var stringSortedResult = searcher.Search(stringSortedCriteria.Compile()); Assert.AreEqual(12, numberSortedResult.TotalItemCount); Assert.AreEqual(12, stringSortedResult.TotalItemCount); Assert.IsTrue(IsSortedByNumber(numberSortedResult)); Assert.IsFalse(IsSortedByNumber(stringSortedResult)); } }
public void Events_Ignoring_Node() { using (var luceneDir = new RandomIdRamDirectory()) using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir, //make parent id 999 so all are ignored validator: new ContentValueSetValidator(false, 999))) using (indexer.ProcessNonAsync()) { var searcher = indexer.GetSearcher(); var contentService = new ExamineDemoDataContentService(); //get a node from the data repo var node = contentService.GetPublishedContentByXPath("//*[string-length(@id)>0 and number(@id)>0]") .Root .Elements() .First(); var valueSet = node.ConvertToValueSet(IndexTypes.Content); indexer.IndexItems(new[] { valueSet }); var found = searcher.CreateQuery().Id((string)node.Attribute("id")).Execute(); Assert.AreEqual(0, found.TotalItemCount); } }
public override void TestSetup() { base.TestSetup(); _luceneDir = new RAMDirectory(); _indexer = IndexInitializer.GetUmbracoIndexer(_luceneDir); _indexer.RebuildIndex(); _searcher = IndexInitializer.GetUmbracoSearcher(_luceneDir); }
public void Index_Move_Media_From_Non_Indexable_To_Indexable_ParentID() { using (var luceneDir = new RandomIdRAMDirectory()) using (var writer = new IndexWriter(luceneDir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), IndexWriter.MaxFieldLength.LIMITED)) using (var indexer = IndexInitializer.GetUmbracoIndexer(writer)) using (var searcher = IndexInitializer.GetUmbracoSearcher(writer)) { indexer.RebuildIndex(); var mediaService = new TestMediaService(); //change parent id to 1116 var existingCriteria = indexer.IndexerData; indexer.IndexerData = new IndexCriteria(existingCriteria.StandardFields, existingCriteria.UserFields, existingCriteria.IncludeNodeTypes, existingCriteria.ExcludeNodeTypes, 1116); //rebuild so it excludes children unless they are under 1116 indexer.RebuildIndex(); //ensure that node 2112 doesn't exist var results = searcher.Search(searcher.CreateSearchCriteria().Id(2112).Compile()); Assert.AreEqual(0, results.TotalItemCount); //get a node from the data repo (this one exists underneath 2222) var node = mediaService.GetLatestMediaByXpath("//*[string-length(@id)>0 and number(@id)>0]") .Root .Elements() .First(x => (int)x.Attribute("id") == 2112); var currPath = (string)node.Attribute("path"); //should be : -1,1111,2222,2112 Assert.AreEqual("-1,1111,2222,2112", currPath); //now mimic moving 2112 to 1116 //node.SetAttributeValue("path", currPath.Replace("2222", "1116")); node.SetAttributeValue("path", "-1,1116,2112"); node.SetAttributeValue("parentID", "1116"); //now reindex the node, this should first delete it and then WILL add it because of the parent id constraint indexer.ReIndexNode(node, IndexTypes.Media); //RESET the parent id existingCriteria = ((IndexCriteria)indexer.IndexerData); indexer.IndexerData = new IndexCriteria(existingCriteria.StandardFields, existingCriteria.UserFields, existingCriteria.IncludeNodeTypes, existingCriteria.ExcludeNodeTypes, null); //now ensure it's deleted var newResults = searcher.Search(searcher.CreateSearchCriteria().Id(2112).Compile()); Assert.AreEqual(1, newResults.TotalItemCount); } }
public void Index_Move_Media_From_Non_Indexable_To_Indexable_ParentID() { // create a validator with // publishedValuesOnly false // parentId 1116 (only content under that parent will be indexed) var validator = new ContentValueSetValidator(false, 1116); using (var luceneDir = new RandomIdRamDirectory()) using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir, validator: validator)) using (indexer.ProcessNonAsync()) { var searcher = indexer.GetSearcher(); //get a node from the data repo (this one exists underneath 2222) var node = _mediaService.GetLatestMediaByXpath("//*[string-length(@id)>0 and number(@id)>0]") .Root.Elements() .First(x => (int)x.Attribute("id") == 2112); var currPath = (string)node.Attribute("path"); //should be : -1,1111,2222,2112 Assert.AreEqual("-1,1111,2222,2112", currPath); //ensure it's indexed indexer.IndexItem(node.ConvertToValueSet(IndexTypes.Media)); //it will not exist because it exists under 2222 var results = searcher.CreateQuery().Id(2112).Execute(); Assert.AreEqual(0, results.Count()); //now mimic moving 2112 to 1116 //node.SetAttributeValue("path", currPath.Replace("2222", "1116")); node.SetAttributeValue("path", "-1,1116,2112"); node.SetAttributeValue("parentID", "1116"); //now reindex the node, this should first delete it and then WILL add it because of the parent id constraint indexer.IndexItems(new[] { node.ConvertToValueSet(IndexTypes.Media) }); //now ensure it exists results = searcher.CreateQuery().Id(2112).Execute(); Assert.AreEqual(1, results.Count()); } }
public void Index_Delete_Index_Item_Ensure_Heirarchy_Removed() { using (var luceneDir = new RandomIdRAMDirectory()) using (var writer = new IndexWriter(luceneDir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), IndexWriter.MaxFieldLength.LIMITED)) using (var indexer = IndexInitializer.GetUmbracoIndexer(writer)) using (var searcher = IndexInitializer.GetUmbracoSearcher(writer)) { indexer.RebuildIndex(); //now delete a node that has children indexer.DeleteFromIndex(1140.ToString()); //this node had children: 1141 & 1142, let's ensure they are also removed var results = searcher.Search(searcher.CreateSearchCriteria().Id(1141).Compile()); Assert.AreEqual(0, results.TotalItemCount); results = searcher.Search(searcher.CreateSearchCriteria().Id(1142).Compile()); Assert.AreEqual(0, results.TotalItemCount); } }
public void Rebuild_Index() { var contentRebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetInstance <PropertyEditorCollection>(), IndexInitializer.GetMockContentService(), ScopeProvider.SqlContext, false); var mediaRebuilder = IndexInitializer.GetMediaIndexRebuilder(Factory.GetInstance <PropertyEditorCollection>(), IndexInitializer.GetMockMediaService()); using (var luceneDir = new RandomIdRamDirectory()) using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir, validator: new ContentValueSetValidator(false))) using (indexer.ProcessNonAsync()) { var searcher = indexer.GetSearcher(); //create the whole thing contentRebuilder.Populate(indexer); mediaRebuilder.Populate(indexer); var result = searcher.CreateQuery().All().Execute(); Assert.AreEqual(29, result.TotalItemCount); } }
public void Index_Reindex_Content() { using (var luceneDir = new RandomIdRAMDirectory()) using (var writer = new IndexWriter(luceneDir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), IndexWriter.MaxFieldLength.LIMITED)) using (var indexer = IndexInitializer.GetUmbracoIndexer(writer, supportUnpublishedContent: true)) using (var searcher = IndexInitializer.GetUmbracoSearcher(writer)) { indexer.RebuildIndex(); var s = (IndexSearcher)searcher.GetSearcher(); //first delete all 'Content' (not media). This is done by directly manipulating the index with the Lucene API, not examine! var contentTerm = new Term(LuceneIndexer.IndexTypeFieldName, IndexTypes.Content); writer.DeleteDocuments(contentTerm); writer.Commit(); //make sure the content is gone. This is done with lucene APIs, not examine! var collector = new AllHitsCollector(false, true); var query = new TermQuery(contentTerm); s = (IndexSearcher)searcher.GetSearcher(); //make sure the searcher is up do date. s.Search(query, collector); Assert.AreEqual(0, collector.Count); //call our indexing methods indexer.IndexAll(IndexTypes.Content); collector = new AllHitsCollector(false, true); s = (IndexSearcher)searcher.GetSearcher(); //make sure the searcher is up do date. s.Search(query, collector); //var ids = new List<string>(); //for (var i = 0; i < collector.Count;i++) //{ // ids.Add(s.Doc(collector.GetDocId(i)).GetValues("__NodeId")[0]); //} Assert.AreEqual(21, collector.Count); } }
public void Test_Sort_Order_Sorting() { //var newIndexFolder = new DirectoryInfo(Path.Combine("App_Data\\SearchTests", Guid.NewGuid().ToString())); //System.IO.Directory.CreateDirectory(newIndexFolder.FullName); using (var luceneDir = new RAMDirectory()) //using (var luceneDir = new SimpleFSDirectory(newIndexFolder)) { var indexer = IndexInitializer.GetUmbracoIndexer(luceneDir, null, new TestDataService() { ContentService = new TestContentService(TestFiles.umbraco_sort) }); indexer.RebuildIndex(); var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); var s = (LuceneSearcher)searcher; var luceneSearcher = s.GetSearcher(); var i = (LuceneIndexer)indexer; var numberSortedCriteria = searcher.CreateSearchCriteria() .ParentId(1148).And() .OrderBy(new SortableField("sortOrder", SortType.Int)); var numberSortedResult = searcher.Search(numberSortedCriteria.Compile()); var stringSortedCriteria = searcher.CreateSearchCriteria() .ParentId(1148).And() .OrderBy("sortOrder"); //will default to string var stringSortedResult = searcher.Search(stringSortedCriteria.Compile()); Assert.AreEqual(12, numberSortedResult.TotalItemCount); Assert.AreEqual(12, stringSortedResult.TotalItemCount); Assert.IsTrue(IsSortedByNumber(numberSortedResult)); Assert.IsFalse(IsSortedByNumber(stringSortedResult)); } }
public void Index_Property_Data_With_Value_Indexer() { var contentValueSetBuilder = IndexInitializer.GetContentValueSetBuilder(Factory.GetInstance <PropertyEditorCollection>(), false); using (var luceneDir = new RandomIdRamDirectory()) using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir, validator: new ContentValueSetValidator(false))) using (indexer.ProcessNonAsync()) { indexer.CreateIndex(); var contentType = MockedContentTypes.CreateBasicContentType(); contentType.AddPropertyType(new PropertyType("test", ValueStorageType.Ntext) { Alias = "grid", Name = "Grid", PropertyEditorAlias = Core.Constants.PropertyEditors.Aliases.Grid }); var content = MockedContent.CreateBasicContent(contentType); content.Id = 555; content.Path = "-1,555"; var gridVal = new GridValue { Name = "n1", Sections = new List <GridValue.GridSection> { new GridValue.GridSection { Grid = "g1", Rows = new List <GridValue.GridRow> { new GridValue.GridRow { Id = Guid.NewGuid(), Name = "row1", Areas = new List <GridValue.GridArea> { new GridValue.GridArea { Grid = "g2", Controls = new List <GridValue.GridControl> { new GridValue.GridControl { Editor = new GridValue.GridEditor { Alias = "editor1", View = "view1" }, Value = "value1" }, new GridValue.GridControl { Editor = new GridValue.GridEditor { Alias = "editor1", View = "view1" }, Value = "value2" } } } } } } } } }; var json = JsonConvert.SerializeObject(gridVal); content.Properties["grid"].SetValue(json); var valueSet = contentValueSetBuilder.GetValueSets(content); indexer.IndexItems(valueSet); var searcher = indexer.GetSearcher(); var results = searcher.CreateQuery().Id(555).Execute(); Assert.AreEqual(1, results.TotalItemCount); var result = results.First(); Assert.IsTrue(result.Values.ContainsKey("grid.row1")); Assert.AreEqual("value1", result.AllValues["grid.row1"][0]); Assert.AreEqual("value2", result.AllValues["grid.row1"][1]); Assert.IsTrue(result.Values.ContainsKey("grid")); Assert.AreEqual("value1 value2 ", result["grid"]); Assert.IsTrue(result.Values.ContainsKey($"{UmbracoExamineIndex.RawFieldPrefix}grid")); Assert.AreEqual(json, result[$"{UmbracoExamineIndex.RawFieldPrefix}grid"]); } }
public void Test_Sort_Order_Sorting() { long totalRecs; var demoData = new ExamineDemoDataContentService(TestFiles.umbraco_sort); var allRecs = demoData.GetLatestContentByXPath("//*[@isDoc]") .Root .Elements() .Select(x => Mock.Of <IContent>( m => m.Id == (int)x.Attribute("id") && m.ParentId == (int)x.Attribute("parentID") && m.Level == (int)x.Attribute("level") && m.CreatorId == 0 && m.SortOrder == (int)x.Attribute("sortOrder") && m.CreateDate == (DateTime)x.Attribute("createDate") && m.UpdateDate == (DateTime)x.Attribute("updateDate") && m.Name == (string)x.Attribute("nodeName") && m.GetCultureName(It.IsAny <string>()) == (string)x.Attribute("nodeName") && m.Path == (string)x.Attribute("path") && m.Properties == new PropertyCollection() && m.Published == true && m.ContentType == Mock.Of <ISimpleContentType>(mt => mt.Icon == "test" && mt.Alias == x.Name.LocalName && mt.Id == (int)x.Attribute("nodeType")))) .ToArray(); var contentService = Mock.Of <IContentService>( x => x.GetPagedDescendants( It.IsAny <int>(), It.IsAny <long>(), It.IsAny <int>(), out totalRecs, It.IsAny <IQuery <IContent> >(), It.IsAny <Ordering>()) == allRecs); var propertyEditors = Factory.GetInstance <PropertyEditorCollection>(); var rebuilder = IndexInitializer.GetContentIndexRebuilder(propertyEditors, contentService, ScopeProvider.SqlContext, true); using (var luceneDir = new RandomIdRamDirectory()) using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir)) using (indexer.ProcessNonAsync()) { indexer.CreateIndex(); rebuilder.Populate(indexer); var searcher = indexer.GetSearcher(); var numberSortedCriteria = searcher.CreateQuery() .ParentId(1148) .OrderBy(new SortableField("sortOrder", SortType.Int)); var numberSortedResult = numberSortedCriteria.Execute(); var stringSortedCriteria = searcher.CreateQuery() .ParentId(1148) .OrderBy(new SortableField("sortOrder"));//will default to string var stringSortedResult = stringSortedCriteria.Execute(); Assert.AreEqual(12, numberSortedResult.TotalItemCount); Assert.AreEqual(12, stringSortedResult.TotalItemCount); Assert.IsTrue(IsSortedByNumber(numberSortedResult)); Assert.IsFalse(IsSortedByNumber(stringSortedResult)); } }