示例#1
0
		public void Title()
		{
			var item = CreateOneItem<PersistableItem1>(2, "Hello world", root);
			indexer.Update(item);

			var searcher = new LuceneSearcher(accessor, persister);
			var hits = searcher.Search(Query.For("hello"));

			Assert.That(hits.Hits.Count(), Is.EqualTo(1));
		}
示例#2
0
        public void AuthorizedPages_AreDisplayed_WhenSearching_WithoutSpecifyingRoles()
        {
            var item = CreateOneItem<PersistableItem1>(2, "Hello world", root);
            item.AuthorizedRoles.Add(new N2.Security.AuthorizedRole(item, "Members"));
            indexer.Update(item);

            var searcher = new LuceneSearcher(accessor, persister);
            var hits = searcher.Search(Query.For("hello"));

            Assert.That(hits.Hits.Count(), Is.EqualTo(1));
        }
示例#3
0
		public void Take()
		{
			indexer.Update(CreateOneItem<PersistableItem1>(3, "Hello country", root));
			indexer.Update(CreateOneItem<PersistableItem1>(2, "Hello world", root));
			indexer.Update(CreateOneItem<PersistableItem1>(4, "Hello universe", root));

			var searcher = new LuceneSearcher(accessor, persister);
			var hits1 = searcher.Search(Query.For("hello").Take(1));

			Assert.That(hits1.Count, Is.EqualTo(1));
		}
示例#4
0
        public void And_Operator_Multiple()
        {
            var page = CreateOneItem<PersistableItem1>(2, "Hello world hunger", root);
            var page2 = CreateOneItem<PersistableItem1>(3, "Hello world fulfillment", root);
            indexer.Update(page);
            indexer.Update(page2);

            var searcher = new LuceneSearcher(accessor, persister);
            var hits = searcher.Search(Query.For("hello") & Query.For("world") & Query.For("hunger"));

            Assert.That(hits.Hits.Count(), Is.EqualTo(1));
            Assert.That(hits.Contains(page));
        }
示例#5
0
        public void And()
        {
            var page = CreateOneItem<PersistableItem1>(2, "Hello world", root);
            var page2 = CreateOneItem<PersistableItem1>(3, "Hello region", root);
            indexer.Update(page);
            indexer.Update(page2);

            var searcher = new LuceneSearcher(accessor, persister);
            var hits = searcher.Search(Query.For("hello").And(Query.For("world")));

            Assert.That(hits.Hits.Count(), Is.EqualTo(1));
            Assert.That(hits.Contains(page));
        }
示例#6
0
		public override void SetUp()
		{
			base.SetUp();

			var definitions = TestSupport.SetupDefinitions(typeof(PersistableItem1), typeof(PersistableItem2), typeof(PersistablePart1));

			accessor = new LuceneAccesor(new ThreadContext(), new DatabaseSection());
			indexer = new LuceneIndexer(accessor, new TextExtractor(new IndexableDefinitionExtractor(definitions)));
			searcher = new LuceneSearcher(accessor, persister);
			worker = new AsyncWorker();
			asyncIndexer = new AsyncIndexer(indexer, persister, worker, Rhino.Mocks.MockRepository.GenerateStub<IErrorNotifier>(), new DatabaseSection());
			tracker = new ContentChangeTracker(asyncIndexer, persister, new N2.Plugin.ConnectionMonitor(), new DatabaseSection());

			accessor.LockTimeout = 1L;
			indexer.Clear();
			root = CreateOneItem<PersistableItem1>(1, "The Root Page", null);
		}
示例#7
0
		public void Multithreading()
		{
			var threads = new List<Thread>();
			var exceptions = new List<Exception>();
			bool loop = true;
			var words = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce nec sagittis mi. Donec pharetra vestibulum facilisis. Sed sodales risus vel nulla vulputate volutpat. Mauris vel arcu in purus porta dapibus. Aliquam erat volutpat. Maecenas suscipit tincidunt purus porttitor auctor. Quisque eget elit at justo facilisis malesuada sit amet sit amet eros. Duis convallis porta congue. Nulla commodo faucibus diam in mollis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec ut nibh eu sapien ornare consectetur.".Split(' ', '.', ',');
			var indexFunction = new ThreadStart(() =>
			{
				Trace.WriteLine("Index start: " + DateTime.Now);
				int counter = 1;
				while (loop)
				{
					var item = CreateOneItem<PersistableItem1>(0, "Item " + counter++, null);
					item["Text"] = words.OrderBy(w => Guid.NewGuid()).Aggregate(new StringBuilder(), (sb, w) => { if (!string.IsNullOrEmpty(w)) sb.Append(w).Append(" "); return sb; }).ToString();
					try
					{
						indexer.Update(item);
						Console.Write('!');
					}
					catch (Exception ex)
					{
						Trace.WriteLine(ex);
						lock (exceptions)
						{
							exceptions.Add(ex);
						}
					}
				}
				Trace.WriteLine("Index stop: " + DateTime.Now);
			});
			var searcher = new LuceneSearcher(accessor, persister);
			int searchCounter = 1;
			var r = new Random();
			var searchFunction = new ThreadStart(() =>
			{
				int searchIndex = searchCounter++;
				Trace.WriteLine(searchIndex + " Search start: " + DateTime.Now);

				while (loop)
				{
					try
					{
						searcher.Search(Query.For(words[r.Next(words.Length)]));
						Console.Write('?');
					}
					catch (Exception ex)
					{
						Trace.WriteLine(ex);
						lock (exceptions)
						{
							exceptions.Add(ex);
						}
					}
				}

				Trace.WriteLine(searchIndex + " Search1 stop: " + DateTime.Now);
			});
			threads.Add(new Thread(indexFunction));
			threads.Add(new Thread(searchFunction));
			threads.Add(new Thread(searchFunction));
			threads.Add(new Thread(searchFunction));

			foreach (var t in threads)
				t.Start();
			Thread.Sleep(500);
			loop = false;
			foreach (var t in threads)
				t.Join();

			exceptions.Count.ShouldBe(0);
		}
示例#8
0
		public void Tags_are_searchable_by_property()
		{
			var page = CreateOneItem<PersistableItem1>(1, "page", null);
			page.Tags = new[] { "Hello", "World" };
			indexer.Update(page);

			var page2 = CreateOneItem<PersistableItem1>(2, "page2", null);
			page2.Tags = new[] { "Howdy", "Universe" };
			indexer.Update(page2);

			var searcher = new LuceneSearcher(accessor, persister);
			var result = searcher.Search(Query.For<PersistableItem1>().Property("Tags", "Hello"));

			result.Single().ShouldBe(page);
		}
示例#9
0
		public void OrOr_And()
		{
			indexer.Update(root);
			var first = CreateOneItem<PersistableItem2>(0, "some other page", root);
			indexer.Update(first);
			var second = CreateOneItem<PersistableItem2>(0, "some other stuff", root);
			indexer.Update(second);

			var searcher = new LuceneSearcher(accessor, persister);
			// TODO: support this
			//var query = (Query.For("some") | Query.For("other"))
			//    .Below(first);
			var query = new Query { Ancestor = first, Intersection = Query.For("some") | Query.For("other") };

			var result = searcher.Search(query);

			result.Hits.Count().ShouldBe(1);
			result.Hits.Any(h => h.Content == second).ShouldBe(false);
		}
示例#10
0
        public void QueryByExpression_ForDetail()
        {
            root.StringProperty = "This is a very special string";
            indexer.Update(root);

            var searcher = new LuceneSearcher(accessor, persister);
            var query = Query.For<PersistableItem1>();

            query.Contains(pi => pi.StringProperty, "special");
            var result = searcher.Search(query);

            Assert.That(result.Hits.Single().Content, Is.EqualTo(root));
        }
示例#11
0
		public void NonDetail_IndexableOnlyGetterProperty_IsIndexed()
		{
			indexer.Update(root);

			var searcher = new LuceneSearcher(accessor, persister);
			var result = searcher.Search(Query.For("ipsum"));

			Assert.That(result.Hits.Single().Content, Is.EqualTo(root));
		}
示例#12
0
		public void Language_IncludesLanguageRoot()
		{
			var sv = CreateOneItem<PersistableItem2>(2, "Svenska", root);
			sv.LanguageCode = "sv";
			indexer.Update(sv);

			var en = CreateOneItem<PersistableItem2>(3, "Engelska", root);
			en.LanguageCode = "en";
			indexer.Update(en);

			var svitem = CreateOneItem<PersistableItem1>(4, "Hello världen", sv);
			indexer.Update(svitem);

			var enitem = CreateOneItem<PersistableItem1>(5, "Hello world", en);
			indexer.Update(enitem);

			var searcher = new LuceneSearcher(accessor, persister);
			var result = searcher.Search(Query.For("").Language(sv));

			Assert.That(result.Hits.Count(), Is.EqualTo(2));
			Assert.That(result.Contains(sv));
		}
示例#13
0
		public void Total_IsNumberOfItemsInIndex()
		{
			indexer.Update(CreateOneItem<PersistableItem1>(3, "Hello country", root));
			indexer.Update(CreateOneItem<PersistableItem1>(2, "Hello world", root));
			indexer.Update(CreateOneItem<PersistableItem1>(4, "Hello universe", root));

			var searcher = new LuceneSearcher(accessor, persister);
			var hits = searcher.Search(Query.For("hello").Take(2));

			Assert.That(hits.Total, Is.EqualTo(3));
		}
示例#14
0
		private System.Collections.Generic.List<ContentItem> Search(params SortFieldData[] sortFields)
		{
			var searcher = new LuceneSearcher(accessor, persister);
			var query = Query.For<PersistableItem1>();
			Array.ForEach(sortFields, sortField => query.OrderBy(sortField.SortField, sortField.SortDescending));

			var hits = searcher.Search(query).Hits.Select(h => h.Content).ToList();
			return hits;
		}
示例#15
0
        public void EditableProperty()
        {
            var item = CreateOneItem<Definitions.PersistableItem1>(2, "Hello world", root);
            item.IntProperty = 444;
            indexer.Update(item);

            var searcher = new LuceneSearcher(accessor, persister);
            var hits = searcher.Search(Query.For("444"));

            Assert.That(hits.Hits.Count(), Is.EqualTo(1));
        }
示例#16
0
		public void PagesCanBeFound_ByObjectType()
		{
			var page = CreateOneItem<PersistableItem1>(2, "Hello world", root);
			var part = CreateOneItem<PersistablePart1>(3, "Hello region", root);
			indexer.Update(page);
			indexer.Update(part);

			var searcher = new LuceneSearcher(accessor, persister);
			var hits = searcher.Search(Query.For("hello").OfType(typeof(object)));

			Assert.That(hits.Hits.Count(), Is.EqualTo(2));
			Assert.That(hits.Hits.Select(h => h.Content).Contains(part));
			Assert.That(hits.Hits.Select(h => h.Content).Contains(page));
		}
示例#17
0
		public void Except_Operator_Multiple()
		{
			var page = CreateOneItem<PersistableItem1>(2, "Hello world", root);
			var part = CreateOneItem<PersistablePart1>(3, "Hello world", root);
			var part2 = CreateOneItem<PersistablePart1>(4, "Hello region", root);
			indexer.Update(page);
			indexer.Update(part);
			indexer.Update(part2);

			var searcher = new LuceneSearcher(accessor, persister);
			var hits = searcher.Search(Query.For("hello") - Query.For(typeof(IPage)) - Query.For("region"));

			Assert.That(hits.Hits.Count(), Is.EqualTo(1));
			Assert.That(hits.Hits.Select(h => h.Content).Contains(part));
		}
示例#18
0
		public void IndexableProperty()
		{
			var item = CreateOneItem<PersistableItem1>(2, "Hello world", root);
			item.StringProperty = "Hej Världen";
			indexer.Update(item);

			var searcher = new LuceneSearcher(accessor, persister);
			var hits = searcher.Search(Query.For("världen"));

			Assert.That(hits.Hits.Count(), Is.EqualTo(1));
		}
示例#19
0
		public void Or_Operator_Multiple()
		{
			var page = CreateOneItem<PersistableItem1>(2, "Hello world", root);
			var page2 = CreateOneItem<PersistableItem1>(3, "Hello region", root);
			var page3 = CreateOneItem<PersistableItem1>(4, "Hello universe", root);
			indexer.Update(page);
			indexer.Update(page2);
			indexer.Update(page3);

			var searcher = new LuceneSearcher(accessor, persister);
			var hits = searcher.Search(Query.For("region") | Query.For("world") | Query.For("universe"));

			Assert.That(hits.Hits.Count(), Is.EqualTo(3));
			Assert.That(hits.Contains(page));
			Assert.That(hits.Contains(page2));
			Assert.That(hits.Contains(page3));
		}
示例#20
0
		public void TitleBoost()
		{
			root.StringProperty = "Hello world";
			indexer.Update(root);

			var item = CreateOneItem<PersistableItem1>(2, root.StringProperty, root);
			item.StringProperty = root.Title;
			indexer.Update(item);

			var searcher = new LuceneSearcher(accessor, persister);

			var hits1 = searcher.Search(Query.For("hello"));
			var hits2 = searcher.Search(Query.For("root"));

			Assert.That(hits1.Hits.Select(h => h.Content).First(), Is.EqualTo(item));
			Assert.That(hits2.Hits.Select(h => h.Content).First(), Is.EqualTo(root));
		}
示例#21
0
		public void NonDetail_IndexableProperty_IsIndexed()
		{
			root.NonDetailProperty = "Lorem dolor";
			indexer.Update(root);

			var searcher = new LuceneSearcher(accessor, persister);
			var result = searcher.Search(Query.For("dolor"));

			Assert.That(result.Hits.Single().Content, Is.EqualTo(root));
		}
示例#22
0
		public void Below()
		{
			var item1 = CreateOneItem<PersistableItem1>(2, "first item", root);
			indexer.Update(item1);
			var item1b = CreateOneItem<PersistableItem1>(3, "first item B", item1);
			indexer.Update(item1b);
			var item2 = CreateOneItem<PersistableItem1>(4, "second item", root);
			indexer.Update(item2);
			var item2b = CreateOneItem<PersistableItem1>(5, "second item B", item2);
			indexer.Update(item2b);

			var searcher = new LuceneSearcher(accessor, persister);
			var hits = searcher.Search(Query.For("item").Below(item1));

			Assert.That(hits.Hits.Count(), Is.EqualTo(2));
			Assert.That(hits.Hits.Select(h => h.Content).Contains(item1));
			Assert.That(hits.Hits.Select(h => h.Content).Contains(item1b));
		}
示例#23
0
		public void NonIndexableClass_IsNotIndexed()
		{
			var child = CreateOneItem<PersistableItem1b>(2, "child", root);
			indexer.Update(child);

			var searcher = new LuceneSearcher(accessor, persister);
			var result = searcher.Search(Query.For("child"));

			Assert.That(result.Hits.Any(), Is.False);
		}
示例#24
0
		public void FindOnlyParts()
		{
			var page1 = CreateOneItem<PersistableItem1>(2, "first page", root);
			indexer.Update(page1);
			var part1 = CreateOneItem<PersistablePart1>(3, "first part", page1);
			indexer.Update(part1);

			var searcher = new LuceneSearcher(accessor, persister);
			var hits = searcher.Search(Query.For("first").Pages(false));

			Assert.That(hits.Hits.Count(), Is.EqualTo(1));
			Assert.That(hits.Hits.Select(h => h.Content).Contains(part1));
		}
示例#25
0
		public void QueryForBelowOfTypeReadableByExceptType()
		{
			indexer.Update(root);
			var part = CreateOneItem<PersistableItem2>(0, "some other page", root);
			indexer.Update(part);

			var searcher = new LuceneSearcher(accessor, persister);
			var query = Query.For("page")
				.OfType(typeof(IPage))
				.Below(root)
				.ReadableBy("Everyone") // superfluous (everyone by default)
				.Except(Query.For<PersistableItem2>());
			
			var result = searcher.Search(query);

			Assert.That(result.Hits.Single().Content, Is.EqualTo(root));
		}
示例#26
0
		public void FindPage_ByText_OnPart_BelowPage()
		{
			var page1 = CreateOneItem<PersistableItem1>(2, "first page", root);
			var part1 = CreateOneItem<PersistablePart1>(3, "first part", page1);
			part1.ZoneName = "Zone";
			indexer.Update(page1);
			indexer.Update(part1);

			var searcher = new LuceneSearcher(accessor, persister);
			var hits = searcher.Search(Query.For("part").Pages(true));

			Assert.That(hits.Hits.Count(), Is.EqualTo(1));
			Assert.That(hits.Hits.Select(h => h.Content).Contains(page1));
		}
示例#27
0
		public void QueryByExpression_ForVisibleProperty()
		{
			indexer.Update(root);

			var searcher = new LuceneSearcher(accessor, persister);
			var query = Query.For<PersistableItem1>();

			query.Contains(pi => pi.Visible, "true");
			var result = searcher.Search(query);

			Assert.That(result.Hits.Single().Content, Is.EqualTo(root));
		}
示例#28
0
		public void ChildrenOf_DeletedItems_DoesntShowUp_InSearch()
		{
			var item = CreateOneItem<PersistableItem1>(2, "Hello world", root);
			var child = CreateOneItem<PersistableItem1>(3, "Child world", item);
			indexer.Update(item);
			indexer.Update(child);

			indexer.Delete(item.ID);

			var searcher = new LuceneSearcher(accessor, persister);
			var hits = searcher.Search(Query.For("world"));

			Assert.That(hits.Hits.Count(), Is.EqualTo(0));
		}
示例#29
0
        //[TestCase(8, 2, 5000, 1000, 500, .5)]
        //[TestCase(16, 4, 30 * 1000, 10000, 1000, .5)]
        //[TestCase(8, 2, 300 * 1000, 20000, 2000, .5)]
		public void Multithreading(int readerCount, int indexerCount, int workMilliseconds, int dictionaryCount, int indexedWordsCount, double updateFrequency)
		{
			var threads = new List<Thread>();
			var exceptions = new List<Exception>();
			bool loop = true;
			
            var generator = new SCG.General.MarkovNameGenerator(Words.Thousand, 3, 2);
            var words = Enumerable.Range(0, dictionaryCount).Select(i => generator.NextName).ToArray();

            int idCounter = 1;
            int creates = 0;
            int updates = 0;
			var indexFunction = new ThreadStart(() =>
			{
                var r = new Random();
                Trace.WriteLine(Thread.CurrentThread.ManagedThreadId + " Index start: " + DateTime.Now);
				while (loop)
				{
                    bool isUpdate = r.NextDouble() < updateFrequency && idCounter > 2;
                    int id = (isUpdate) ? r.Next(0, idCounter) : Interlocked.Increment(ref idCounter);

					var item = CreateOneItem<PersistableItem1>(id, "Item " + id, null);
					item.StringProperty = Enumerable.Range(0, indexedWordsCount).Select(i => words[r.Next(0, words.Length)]).Aggregate(new StringBuilder(), (sb, w) => sb.Append(w).Append(" ")).ToString();
					try
					{
						indexer.Update(item);
						Console.Write(isUpdate ? 'U' : 'C');
                        if (isUpdate)
                            Interlocked.Increment(ref updates);
                        else
                            Interlocked.Increment(ref creates);
					}
					catch (Exception ex)
					{
						Trace.WriteLine(ex);
						lock (exceptions)
						{
							exceptions.Add(ex);
						}
					}
				}
				Trace.WriteLine(Thread.CurrentThread.ManagedThreadId + " Index stop: " + DateTime.Now);
			});
			var searcher = new LuceneSearcher(accessor, persister);
            int searches = 0;
			var searchFunction = new ThreadStart(() =>
			{
                Trace.WriteLine(Thread.CurrentThread.ManagedThreadId + " Search start: " + DateTime.Now);
                var r = new Random();

				while (loop)
				{
					try
					{
						var result = searcher.Search(Query.For(words[r.Next(words.Length)]));
                        Interlocked.Increment(ref searches);
						Console.Write('?');
					}
					catch (Exception ex)
					{
						Trace.WriteLine(ex);
						lock (exceptions)
						{
							exceptions.Add(ex);
						}
					}
				}

                Trace.WriteLine(Thread.CurrentThread.ManagedThreadId + " Search stop: " + DateTime.Now);
			});
            for (int i = 0; i < indexerCount; i++)
            {
			threads.Add(new Thread(indexFunction));
            }
            for (int i = 0; i < readerCount; i++)
            {
			threads.Add(new Thread(searchFunction));
            }

			foreach (var t in threads)
				t.Start();
			Thread.Sleep(workMilliseconds);
			loop = false;
			foreach (var t in threads)
				t.Join();

            Trace.WriteLine("Creates: " + creates + ", Updates: " + updates + ", Searches: " + searches + " Exceptions: " + exceptions.Count);

            foreach (var ex in exceptions)
                Trace.WriteLine(ex.Message);

			exceptions.Count.ShouldBe(0);
		}
示例#30
0
		public void UnSecuredPages_AreDisplayed_WhenSearching_WithRoles()
		{
			var item = CreateOneItem<PersistableItem1>(2, "Hello world", root);
			indexer.Update(item);

			var searcher = new LuceneSearcher(accessor, persister);
			var hits = searcher.Search(Query.For("hello").ReadableBy("Members"));

			Assert.That(hits.Hits.Count(), Is.EqualTo(1));
		}