Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="umbracoHelper"></param>
        /// <param name="indexerName"></param>
        private void Indexer_DocumentWriting(object sender, DocumentWritingEventArgs e, UmbracoHelper umbracoHelper, string indexerName)
        {
            IPublishedContent publishedContent = null;

            publishedContent = umbracoHelper.TypedContent(e.NodeId);

            if (publishedContent == null)
            {
                // attempt to get as media
                publishedContent = umbracoHelper.TypedMedia(e.NodeId);

                if (publishedContent == null)
                {
                    // attempt to get as member
                    publishedContent = umbracoHelper.SafeTypedMember(e.NodeId);
                }
            }

            if (publishedContent != null)
            {
                this.EnsureUmbracoContext();

                var indexingContext = new IndexingContext(null, publishedContent, indexerName);

                LookService.Index(indexingContext, e.Document);
            }
        }
Exemple #2
0
        /// <summary>
        /// Umbraco started
        /// </summary>
        /// <param name="umbracoApplication"></param>
        /// <param name="applicationContext"></param>
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            var indexProviders = ExamineManager
                                 .Instance
                                 .IndexProviderCollection
                                 .Select(x => x as BaseUmbracoIndexer)    // UmbracoContentIndexer, UmbracoMemberIndexer
                                 .Where(x => x != null)
                                 .ToArray();

            if (!indexProviders.Any())
            {
                LogHelper.Warn(typeof(LookService), "Unable to find any Umbraco Examine indexers to hook into !");
            }
            else
            {
                var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);

                LookService.Initialize(umbracoHelper);

                foreach (var indexProvider in indexProviders)
                {
                    indexProvider.DocumentWriting += (sender, e) => this.Indexer_DocumentWriting(sender, e, umbracoHelper, indexProvider.Name);
                }
            }
        }
        /// <summary>
        /// Event used to maintain any detached indexes, as and when Umbraco data changes
        /// </summary>
        /// <param name="umbracoApplication"></param>
        /// <param name="applicationContext"></param>
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            // set once, as they will be configured in the config
            this._lookIndexers = ExamineManager
                                 .Instance
                                 .IndexProviderCollection
                                 .Select(x => x as LookIndexer)
                                 .Where(x => x != null)
                                 .ToArray();

            if (this._lookIndexers.Any())
            {
                this._umbracoHelper = new UmbracoHelper(UmbracoContext.Current);

                LookService.Initialize(this._umbracoHelper);

                ContentService.Published += ContentService_Published;
                MediaService.Saved       += this.MediaService_Saved;
                MemberService.Saved      += this.MemberService_Saved;

                ContentService.UnPublished += ContentService_UnPublished;
                MediaService.Deleted       += this.MediaService_Deleted;
                MemberService.Deleted      += this.MemberService_Deleted;
            }
        }
Exemple #4
0
        public void Null_Query()
        {
            var lookResult = LookService.RunQuery(null);

            Assert.IsNotNull(lookResult);
            Assert.IsFalse(lookResult.Success);
            Assert.IsTrue(lookResult.TotalItemCount == 0);
        }
Exemple #5
0
        public void New_Query_No_Clauses_Not_Compiled()
        {
            var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

            var lookResult = LookService.RunQuery(lookQuery);

            Assert.IsNull(lookQuery.Compiled);
        }
Exemple #6
0
        public static void AssemblyInitialize(TestContext testContext)
        {
            // always start with an empty index
            TestHelper.DeleteIndex();

            // Wire up the location indexers
            LookService.Initialize(null);
        }
Exemple #7
0
        public void Empty_Query()
        {
            var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

            var lookResult = LookService.RunQuery(lookQuery);

            Assert.IsNotNull(lookResult);
            Assert.IsFalse(lookResult.Success);
            Assert.IsTrue(lookResult.TotalItemCount == 0);
        }
Exemple #8
0
        public void New_Query_Executed_To_Make_Compiled()
        {
            var lookQuery = new LookQuery(TestHelper.GetSearchingContext())
            {
                NodeQuery = new NodeQuery("thing")
            };

            var lookResult = LookService.RunQuery(lookQuery);

            Assert.IsNotNull(lookQuery.Compiled);
        }
        public void Highlighting()
        {
            var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

            lookQuery.TextQuery = new TextQuery("dolor", true);

            var lookResult = LookService.RunQuery(lookQuery);

            Assert.IsTrue(lookResult.Success);
            Assert.IsTrue(lookResult.TotalItemCount > 0);
            Assert.IsFalse(string.IsNullOrWhiteSpace(lookResult.Matches.First().Highlight.ToString()));
        }
Exemple #10
0
        public void Query_With_Node_Type_Clause()
        {
            var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

            lookQuery.NodeQuery       = new NodeQuery();
            lookQuery.NodeQuery.Types = new PublishedItemType[] { PublishedItemType.Content };

            var lookResult = LookService.RunQuery(lookQuery);

            Assert.IsNotNull(lookResult);
            Assert.IsTrue(lookResult.Success);
        }
        public void All_Circles()
        {
            var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

            lookQuery.TagQuery     = new TagQuery();
            lookQuery.TagQuery.All = new LookTag[] { new LookTag("shape", "circle") };

            var lookResult = LookService.RunQuery(lookQuery);

            Assert.IsTrue(lookResult.Success);
            Assert.AreEqual(3, lookResult.TotalItemCount);
        }
        public void All_Small_Or_Medium_Circles()
        {
            var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

            lookQuery.TagQuery     = new TagQuery();
            lookQuery.TagQuery.All = TagQuery.MakeTags("shape:circle");
            lookQuery.TagQuery.Any = TagQuery.MakeTags("size:small", "size:medium");

            var lookResult = LookService.RunQuery(lookQuery);

            Assert.IsTrue(lookResult.Success);
            Assert.AreEqual(2, lookResult.TotalItemCount);
        }
Exemple #13
0
        public void Invalidate_Compiled_By_Node_Query_Change()
        {
            var lookQuery = new LookQuery(TestHelper.GetSearchingContext())
            {
                NodeQuery = new NodeQuery("thing")
            };

            var lookResult = LookService.RunQuery(lookQuery);

            lookQuery.NodeQuery = new NodeQuery();

            Assert.IsNull(lookQuery.Compiled);
        }
Exemple #14
0
        public void Invalidate_Compiled_By_Location_Query_Change()
        {
            var lookQuery = new LookQuery(TestHelper.GetSearchingContext())
            {
                NodeQuery = new NodeQuery("thing")
            };

            var lookResult = LookService.RunQuery(lookQuery);

            lookQuery.LocationQuery             = new LocationQuery();
            lookQuery.LocationQuery.MaxDistance = new Distance(1, DistanceUnit.Miles);

            Assert.IsNull(lookQuery.Compiled);
        }
Exemple #15
0
        public void Max_Distance()
        {
            var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

            lookQuery.LocationQuery             = new LocationQuery();
            lookQuery.LocationQuery.Location    = _london;
            lookQuery.LocationQuery.MaxDistance = new Distance(300, DistanceUnit.Miles);

            var lookResult = LookService.RunQuery(lookQuery);

            Assert.IsTrue(lookResult.Success);
            Assert.IsTrue(lookResult.TotalItemCount == 2);

            Assert.IsTrue(lookResult.Matches.Any(x => x.Location.Equals(_london)));
            Assert.IsTrue(lookResult.Matches.Any(x => x.Location.Equals(_paris)));
        }
        /// <summary>
        /// Index the supplied things
        /// </summary>
        /// <param name="things">The things to add into the index</param>
        /// <param name="beforeIndexing">optional action to call before indexing each thing</param>
        /// <param name="afterIndexing">optional acton to call after indexing each thing</param>
        internal static void IndexThings(IEnumerable <Thing> things, Action <IndexingContext> beforeIndexing = null, Action <IndexingContext> afterIndexing = null)
        {
            var nameStack     = new Stack <string>(things.Select(x => x.Name));
            var dateStack     = new Stack <DateTime?>(things.Select(x => x.Date));
            var textStack     = new Stack <string>(things.Select(x => x.Text));
            var tagStack      = new Stack <LookTag[]>(things.Select(x => x.Tags));
            var locationStack = new Stack <Location>(things.Select(x => x.Location));

            // use supplied, or do nothing
            LookConfiguration.BeforeIndexing = beforeIndexing;
            LookConfiguration.AfterIndexing  = afterIndexing;

            // setup indexers
            LookConfiguration.DefaultNameIndexer     = x => nameStack.Pop();
            LookConfiguration.DefaultDateIndexer     = x => dateStack.Pop();
            LookConfiguration.DefaultTextIndexer     = x => textStack.Pop();
            LookConfiguration.DefaultTagIndexer      = x => tagStack.Pop();
            LookConfiguration.DefaultLocationIndexer = x => locationStack.Pop();

            List <Document> documents = new List <Document>();

            foreach (var thing in things)
            {
                var document = new Document();

                var indexingContext = new IndexingContext(null, null, "UnitTestIndexer"); // null for IPublishedContent as not required

                LookService.Index(indexingContext, document);

                if (!indexingContext.Cancelled)
                {
                    documents.Add(document);
                }
            }

            //reset
            LookConfiguration.BeforeIndexing = null;
            LookConfiguration.AfterIndexing  = null;

            LookConfiguration.DefaultNameIndexer     = null;
            LookConfiguration.DefaultDateIndexer     = null;
            LookConfiguration.DefaultTextIndexer     = null;
            LookConfiguration.DefaultTagIndexer      = null;
            LookConfiguration.DefaultLocationIndexer = null;

            TestHelper.IndexDocuments(documents);
        }
Exemple #17
0
        public void Distance_Sorting()
        {
            var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

            lookQuery.LocationQuery          = new LocationQuery();
            lookQuery.LocationQuery.Location = _london;

            lookQuery.SortOn = SortOn.Distance;

            var lookResult = LookService.RunQuery(lookQuery);

            Assert.IsTrue(lookResult.Success);
            Assert.IsTrue(lookResult.TotalItemCount == 4);

            var locations = lookResult.Matches.Select(x => x.Location).ToArray();

            Assert.IsTrue(locations[0].Equals(_london));
            Assert.IsTrue(locations[1].Equals(_paris));
            Assert.IsTrue(locations[2].Equals(_copenhagen));
            Assert.IsTrue(locations[3].Equals(_newYork));
        }
Exemple #18
0
        public void Facet_Counts()
        {
            var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

            lookQuery.TagQuery         = new TagQuery();
            lookQuery.TagQuery.All     = new LookTag[] { _red };
            lookQuery.TagQuery.FacetOn = new TagFacetQuery(_colour);

            var lookResult = LookService.RunQuery(lookQuery);

            Assert.IsNotNull(lookResult);
            Assert.IsTrue(lookResult.Success);
            Assert.IsTrue(lookResult.TotalItemCount > 0);
            Assert.IsTrue(lookResult.Facets.Length == 7);
            Assert.IsTrue(lookResult.Facets.Single(x => _red.Equals(x.Tags.Single())).Count == 7);
            Assert.IsTrue(lookResult.Facets.Single(x => _orange.Equals(x.Tags.Single())).Count == 6);
            Assert.IsTrue(lookResult.Facets.Single(x => _yellow.Equals(x.Tags.Single())).Count == 5);
            Assert.IsTrue(lookResult.Facets.Single(x => _green.Equals(x.Tags.Single())).Count == 4);
            Assert.IsTrue(lookResult.Facets.Single(x => _blue.Equals(x.Tags.Single())).Count == 3);
            Assert.IsTrue(lookResult.Facets.Single(x => _indigo.Equals(x.Tags.Single())).Count == 2);
            Assert.IsTrue(lookResult.Facets.Single(x => _violet.Equals(x.Tags.Single())).Count == 1);
        }
Exemple #19
0
        public void Re_Execute_Compiled_Expect_Same_Results()
        {
            TestHelper.IndexThings(new Thing[] { new Thing()
                                                 {
                                                     Name = "thing"
                                                 } });

            var lookQuery = new LookQuery(TestHelper.GetSearchingContext())
            {
                NameQuery = new NameQuery("thing")
            };

            Assert.IsNull(lookQuery.Compiled);

            var lookResults = LookService.RunQuery(lookQuery);
            var total       = lookResults.TotalItemCount;

            Assert.IsNotNull(lookQuery.Compiled);
            Assert.IsTrue(total > 0);

            Assert.AreEqual(total, LookService.RunQuery(lookQuery).TotalItemCount);
        }
Exemple #20
0
        //internal static void GenerateTestData()
        //{
        //    // generate a load of random test data, just to bulk it out
        //}

        /// <summary>
        /// Add supplied collection into the test index
        /// </summary>
        /// <param name="things"></param>
        internal static void IndexThings(IEnumerable <Thing> things)
        {
            var nameStack     = new Stack <string>(things.Select(x => x.Name));
            var dateStack     = new Stack <DateTime?>(things.Select(x => x.Date));
            var textStack     = new Stack <string>(things.Select(x => x.Text));
            var tagStack      = new Stack <LookTag[]>(things.Select(x => x.Tags));
            var locationStack = new Stack <Location>(things.Select(x => x.Location));

            // setup indexers
            LookConfiguration.NameIndexer     = x => nameStack.Pop();
            LookConfiguration.DateIndexer     = x => dateStack.Pop();
            LookConfiguration.TextIndexer     = x => textStack.Pop();
            LookConfiguration.TagIndexer      = x => tagStack.Pop();
            LookConfiguration.LocationIndexer = x => locationStack.Pop();

            // null for IPublishedContent as not required
            var indexingContext = new IndexingContext(null, null, null);

            List <Document> documents = new List <Document>();

            foreach (var thing in things)
            {
                var document = new Document();

                LookService.Index(indexingContext, document);

                documents.Add(document);
            }

            // reset indexers
            LookConfiguration.NameIndexer     = null;
            LookConfiguration.DateIndexer     = null;
            LookConfiguration.TextIndexer     = null;
            LookConfiguration.TagIndexer      = null;
            LookConfiguration.LocationIndexer = null;

            TestHelper.IndexDocuments(documents);
        }
Exemple #21
0
        public void Query_Apply_Random_Facet_Then_Re_Query()
        {
            var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

            lookQuery.TagQuery         = new TagQuery();
            lookQuery.TagQuery.All     = new LookTag[] { _red };
            lookQuery.TagQuery.FacetOn = new TagFacetQuery(_colour);

            // first query
            var lookResult = LookService.RunQuery(lookQuery);

            Assert.IsNotNull(lookResult);
            Assert.IsTrue(lookResult.Success);
            Assert.IsTrue(lookResult.TotalItemCount == 7);
            Assert.IsTrue(lookResult.Facets.Length == 7);

            // pick a random facet
            var random = new Random();
            var facet  = lookResult
                         .Facets
                         .OrderBy(x => random.Next())
                         .First();

            // get the expected count
            var facetCount = facet.Count;

            // apply facet to query
            lookQuery.ApplyFacet(facet);

            // second query
            lookResult = LookService.RunQuery(lookQuery);

            Assert.IsNotNull(lookResult);
            Assert.IsTrue(lookResult.Success);
            Assert.AreEqual(facetCount, lookResult.TotalItemCount);
        }
Exemple #22
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="umbracoApplication"></param>
 /// <param name="applicationContext"></param>
 protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
 {
     LookService.Initialize(
         this.Indexer_GatheringNodeData,
         new UmbracoHelper(UmbracoContext.Current));
 }