lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From synhers...@apache.org
Subject [07/17] lucenenet git commit: All ToChildBlockJoinQuery tests passing
Date Sun, 23 Aug 2015 22:34:06 GMT
All ToChildBlockJoinQuery tests passing


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/1213ca7c
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/1213ca7c
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/1213ca7c

Branch: refs/heads/master
Commit: 1213ca7c61e990ba008f087f00f167eb554d783c
Parents: dff959f
Author: Josh Sullivan <jaysche.sullivan@gmail.com>
Authored: Mon Aug 17 15:24:46 2015 -0400
Committer: Josh Sullivan <jaysche.sullivan@gmail.com>
Committed: Mon Aug 17 15:24:46 2015 -0400

----------------------------------------------------------------------
 Lucene.Net.Join/ToChildBlockJoinQuery.cs        |   2 +-
 .../Lucene.Net.Tests.Join.csproj                |   1 +
 .../TestBlockJoinValidation.cs                  | 227 +++++++++++++++++++
 3 files changed, 229 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1213ca7c/Lucene.Net.Join/ToChildBlockJoinQuery.cs
----------------------------------------------------------------------
diff --git a/Lucene.Net.Join/ToChildBlockJoinQuery.cs b/Lucene.Net.Join/ToChildBlockJoinQuery.cs
index f16e2a9..3d4f2d5 100644
--- a/Lucene.Net.Join/ToChildBlockJoinQuery.cs
+++ b/Lucene.Net.Join/ToChildBlockJoinQuery.cs
@@ -38,7 +38,7 @@ namespace Lucene.Net.Join
         /// Message thrown from <see cref="ToChildBlockJoinScorer.ValidateParentDoc"/>
         /// on mis-use, when the parent query incorrectly returns child docs. 
         /// </summary>
-        internal const string InvalidQueryMessage = "Parent query yields document which is
not matched by parents filter, docID=";
+        public const string InvalidQueryMessage = "Parent query yields document which is
not matched by parents filter, docID=";
 
         private readonly Filter _parentsFilter;
         private readonly Query _parentQuery;

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1213ca7c/Lucene.Net.Tests.Join/Lucene.Net.Tests.Join.csproj
----------------------------------------------------------------------
diff --git a/Lucene.Net.Tests.Join/Lucene.Net.Tests.Join.csproj b/Lucene.Net.Tests.Join/Lucene.Net.Tests.Join.csproj
index eff35a9..9c959f8 100644
--- a/Lucene.Net.Tests.Join/Lucene.Net.Tests.Join.csproj
+++ b/Lucene.Net.Tests.Join/Lucene.Net.Tests.Join.csproj
@@ -51,6 +51,7 @@
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="TestBlockJoin.cs" />
     <Compile Include="TestBlockJoinSorting.cs" />
+    <Compile Include="TestBlockJoinValidation.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\Lucene.Net.Grouping\Lucene.Net.Grouping.csproj">

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1213ca7c/Lucene.Net.Tests.Join/TestBlockJoinValidation.cs
----------------------------------------------------------------------
diff --git a/Lucene.Net.Tests.Join/TestBlockJoinValidation.cs b/Lucene.Net.Tests.Join/TestBlockJoinValidation.cs
new file mode 100644
index 0000000..5fdd35f
--- /dev/null
+++ b/Lucene.Net.Tests.Join/TestBlockJoinValidation.cs
@@ -0,0 +1,227 @@
+´╗┐using System;
+using System.Collections.Generic;
+using System.Text;
+using Apache.NMS;
+using Lucene.Net.Analysis;
+using Lucene.Net.Documents;
+using Lucene.Net.Index;
+using Lucene.Net.Join;
+using Lucene.Net.Randomized.Generators;
+using Lucene.Net.Search;
+using Lucene.Net.Store;
+using Lucene.Net.Support;
+using Lucene.Net.Util;
+using NUnit.Framework;
+
+namespace Lucene.Net.Tests.Join
+{
+    /*
+	 * Licensed to the Apache Software Foundation (ASF) under one or more
+	 * contributor license agreements.  See the NOTICE file distributed with
+	 * this work for additional information regarding copyright ownership.
+	 * The ASF licenses this file to You under the Apache License, Version 2.0
+	 * (the "License"); you may not use this file except in compliance with
+	 * the License.  You may obtain a copy of the License at
+	 *
+	 *     http://www.apache.org/licenses/LICENSE-2.0
+	 *
+	 * Unless required by applicable law or agreed to in writing, software
+	 * distributed under the License is distributed on an "AS IS" BASIS,
+	 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+	 * See the License for the specific language governing permissions and
+	 * limitations under the License.
+	 */
+
+    public class TestBlockJoinValidation : LuceneTestCase
+    {
+
+        public const int AMOUNT_OF_SEGMENTS = 5;
+        public const int AMOUNT_OF_PARENT_DOCS = 10;
+        public const int AMOUNT_OF_CHILD_DOCS = 5;
+        public static readonly int AMOUNT_OF_DOCS_IN_SEGMENT = AMOUNT_OF_PARENT_DOCS + AMOUNT_OF_PARENT_DOCS
* AMOUNT_OF_CHILD_DOCS;
+
+        private Directory Directory;
+        private IndexReader IndexReader;
+        private IndexSearcher IndexSearcher;
+        private Filter ParentsFilter;
+        
+        [SetUp]
+        public override void SetUp()
+        {
+            Directory = NewDirectory();
+            IndexWriterConfig config = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()));
+            IndexWriter indexWriter = new IndexWriter(Directory, config);
+            for (int i = 0; i < AMOUNT_OF_SEGMENTS; i++)
+            {
+                IList<Document> segmentDocs = CreateDocsForSegment(i);
+                indexWriter.AddDocuments(segmentDocs);
+                indexWriter.Commit();
+            }
+            IndexReader = DirectoryReader.Open(indexWriter, Random().NextBoolean());
+            indexWriter.Dispose();
+            IndexSearcher = new IndexSearcher(IndexReader);
+            ParentsFilter = new FixedBitSetCachingWrapperFilter(new QueryWrapperFilter(new
WildcardQuery(new Term("parent", "*"))));
+        }
+
+        [TearDown]
+        public override void TearDown()
+        {
+            IndexReader.Dispose();
+            Directory.Dispose();
+        }
+
+        [Test]
+        public void TestNextDocValidationForToParentBjq()
+        {
+            Query parentQueryWithRandomChild = CreateChildrenQueryWithOneParent(GetRandomChildNumber(0));
+            var blockJoinQuery = new ToParentBlockJoinQuery(parentQueryWithRandomChild, ParentsFilter,
ScoreMode.None);
+
+            var ex = Throws<InvalidOperationException>(() => IndexSearcher.Search(blockJoinQuery,
1));
+            StringAssert.Contains("child query must only match non-parent docs", ex.Message);
+
+        }
+
+        [Test]
+        public void TestAdvanceValidationForToParentBjq()
+        {
+            int randomChildNumber = GetRandomChildNumber(0);
+            // we need to make advance method meet wrong document, so random child number
+            // in BJQ must be greater than child number in Boolean clause
+            int nextRandomChildNumber = GetRandomChildNumber(randomChildNumber);
+            Query parentQueryWithRandomChild = CreateChildrenQueryWithOneParent(nextRandomChildNumber);
+            ToParentBlockJoinQuery blockJoinQuery = new ToParentBlockJoinQuery(parentQueryWithRandomChild,
ParentsFilter, ScoreMode.None);
+            // advance() method is used by ConjunctionScorer, so we need to create Boolean
conjunction query
+            BooleanQuery conjunctionQuery = new BooleanQuery();
+            WildcardQuery childQuery = new WildcardQuery(new Term("child", CreateFieldValue(randomChildNumber)));
+            conjunctionQuery.Add(new BooleanClause(childQuery, BooleanClause.Occur.MUST));
+            conjunctionQuery.Add(new BooleanClause(blockJoinQuery, BooleanClause.Occur.MUST));
+
+            var ex = Throws<InvalidOperationException>(() => IndexSearcher.Search(conjunctionQuery,
1));
+            StringAssert.Contains("child query must only match non-parent docs", ex.Message);
+        }
+
+        [Test]
+        public void TestNextDocValidationForToChildBjq()
+        {
+            Query parentQueryWithRandomChild = CreateParentsQueryWithOneChild(GetRandomChildNumber(0));
+            var blockJoinQuery = new ToChildBlockJoinQuery(parentQueryWithRandomChild, ParentsFilter,
false);
+
+            var ex = Throws<InvalidOperationException>(() => IndexSearcher.Search(blockJoinQuery,
1));
+            StringAssert.Contains(ToChildBlockJoinQuery.InvalidQueryMessage, ex.Message);
+        }
+        
+        [Test]
+        public void TestAdvanceValidationForToChildBjq()
+        {
+            int randomChildNumber = GetRandomChildNumber(0);
+            // we need to make advance method meet wrong document, so random child number
+            // in BJQ must be greater than child number in Boolean clause
+            int nextRandomChildNumber = GetRandomChildNumber(randomChildNumber);
+            Query parentQueryWithRandomChild = CreateParentsQueryWithOneChild(nextRandomChildNumber);
+            var blockJoinQuery = new ToChildBlockJoinQuery(parentQueryWithRandomChild, ParentsFilter,
false);
+            // advance() method is used by ConjunctionScorer, so we need to create Boolean
conjunction query
+            var conjunctionQuery = new BooleanQuery();
+            var childQuery = new WildcardQuery(new Term("child", CreateFieldValue(randomChildNumber)));
+            conjunctionQuery.Add(new BooleanClause(childQuery, BooleanClause.Occur.MUST));
+            conjunctionQuery.Add(new BooleanClause(blockJoinQuery, BooleanClause.Occur.MUST));
+            
+            var ex = Throws<InvalidOperationException>(() => IndexSearcher.Search(conjunctionQuery,
1));
+            StringAssert.Contains(ToChildBlockJoinQuery.InvalidQueryMessage, ex.Message);
+        }
+
+        private static IList<Document> CreateDocsForSegment(int segmentNumber)
+        {
+            IList<IList<Document>> blocks = new List<IList<Document>>(AMOUNT_OF_PARENT_DOCS);
+            for (int i = 0; i < AMOUNT_OF_PARENT_DOCS; i++)
+            {
+                blocks.Add(CreateParentDocWithChildren(segmentNumber, i));
+            }
+            IList<Document> result = new List<Document>(AMOUNT_OF_DOCS_IN_SEGMENT);
+            foreach (IList<Document> block in blocks)
+            {
+                result.AddRange(block);
+            }
+            return result;
+        }
+
+        private static IList<Document> CreateParentDocWithChildren(int segmentNumber,
int parentNumber)
+        {
+            IList<Document> result = new List<Document>(AMOUNT_OF_CHILD_DOCS
+ 1);
+            for (int i = 0; i < AMOUNT_OF_CHILD_DOCS; i++)
+            {
+                result.Add(CreateChildDoc(segmentNumber, parentNumber, i));
+            }
+            result.Add(CreateParentDoc(segmentNumber, parentNumber));
+            return result;
+        }
+
+        private static Document CreateParentDoc(int segmentNumber, int parentNumber)
+        {
+            Document result = new Document();
+            result.Add(NewStringField("id", CreateFieldValue(segmentNumber * AMOUNT_OF_PARENT_DOCS
+ parentNumber), Field.Store.YES));
+            result.Add(NewStringField("parent", CreateFieldValue(parentNumber), Field.Store.NO));
+            return result;
+        }
+
+        private static Document CreateChildDoc(int segmentNumber, int parentNumber, int childNumber)
+        {
+            Document result = new Document();
+            result.Add(NewStringField("id", CreateFieldValue(segmentNumber * AMOUNT_OF_PARENT_DOCS
+ parentNumber, childNumber), Field.Store.YES));
+            result.Add(NewStringField("child", CreateFieldValue(childNumber), Field.Store.NO));
+            return result;
+        }
+
+        private static string CreateFieldValue(params int[] documentNumbers)
+        {
+            StringBuilder stringBuilder = new StringBuilder();
+            foreach (int documentNumber in documentNumbers)
+            {
+                if (stringBuilder.Length > 0)
+                {
+                    stringBuilder.Append("_");
+                }
+                stringBuilder.Append(documentNumber);
+            }
+            return stringBuilder.ToString();
+        }
+
+        private static Query CreateChildrenQueryWithOneParent(int childNumber)
+        {
+            TermQuery childQuery = new TermQuery(new Term("child", CreateFieldValue(childNumber)));
+            Query randomParentQuery = new TermQuery(new Term("id", CreateFieldValue(RandomParentId)));
+            BooleanQuery childrenQueryWithRandomParent = new BooleanQuery();
+            childrenQueryWithRandomParent.Add(new BooleanClause(childQuery, BooleanClause.Occur.SHOULD));
+            childrenQueryWithRandomParent.Add(new BooleanClause(randomParentQuery, BooleanClause.Occur.SHOULD));
+            return childrenQueryWithRandomParent;
+        }
+
+        private static Query CreateParentsQueryWithOneChild(int randomChildNumber)
+        {
+            BooleanQuery childQueryWithRandomParent = new BooleanQuery();
+            Query parentsQuery = new TermQuery(new Term("parent", CreateFieldValue(RandomParentNumber)));
+            childQueryWithRandomParent.Add(new BooleanClause(parentsQuery, BooleanClause.Occur.SHOULD));
+            childQueryWithRandomParent.Add(new BooleanClause(RandomChildQuery(randomChildNumber),
BooleanClause.Occur.SHOULD));
+            return childQueryWithRandomParent;
+        }
+
+        private static int RandomParentId
+        {
+            get { return Random().Next(AMOUNT_OF_PARENT_DOCS*AMOUNT_OF_SEGMENTS); }
+        }
+
+        private static int RandomParentNumber
+        {
+            get { return Random().Next(AMOUNT_OF_PARENT_DOCS); }
+        }
+
+        private static Query RandomChildQuery(int randomChildNumber)
+        {
+            return new TermQuery(new Term("id", CreateFieldValue(RandomParentId, randomChildNumber)));
+        }
+
+        private static int GetRandomChildNumber(int notLessThan)
+        {
+            return notLessThan + Random().Next(AMOUNT_OF_CHILD_DOCS - notLessThan);
+        }
+    }
+}
\ No newline at end of file


Mime
View raw message