lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From synhers...@apache.org
Subject [2/4] lucenenet git commit: Ported the ChainedFilterTest class.
Date Wed, 12 Aug 2015 17:52:04 GMT
Ported the ChainedFilterTest class.

Think I did the right thing with the GregorianCalendar usage...


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

Branch: refs/heads/master
Commit: 7b4987f77e7395618c2fd62a0565d93aec82e5dc
Parents: 9611865
Author: Josh Sullivan <jaysche.sullivan@gmail.com>
Authored: Mon Aug 10 20:28:41 2015 -0400
Committer: Josh Sullivan <jaysche.sullivan@gmail.com>
Committed: Mon Aug 10 20:28:41 2015 -0400

----------------------------------------------------------------------
 .../ChainedFilterTest.cs                        | 188 +++++++++++++++++++
 .../Lucene.Net.Tests.Queries.csproj             |   2 +
 2 files changed, 190 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/7b4987f7/src/Lucene.Net.Tests.Queries/ChainedFilterTest.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.Queries/ChainedFilterTest.cs b/src/Lucene.Net.Tests.Queries/ChainedFilterTest.cs
new file mode 100644
index 0000000..66be39a
--- /dev/null
+++ b/src/Lucene.Net.Tests.Queries/ChainedFilterTest.cs
@@ -0,0 +1,188 @@
+´╗┐using System;
+using System.Globalization;
+using Lucene.Net.Documents;
+using Lucene.Net.Index;
+using Lucene.Net.Queries;
+using Lucene.Net.Search;
+using Lucene.Net.Store;
+using Lucene.Net.Util;
+using NUnit.Framework;
+
+namespace Lucene.Net.Tests.Queries
+{
+    public class ChainedFilterTest : LuceneTestCase
+    {
+        public const int Max = 500;
+
+        private Directory directory;
+        private IndexSearcher searcher;
+        private IndexReader reader;
+        private Query query;
+        // private DateFilter dateFilter;   DateFilter was deprecated and removed
+        private TermRangeFilter dateFilter;
+        private QueryWrapperFilter bobFilter;
+        private QueryWrapperFilter sueFilter;
+
+        [SetUp]
+        public override void SetUp()
+        {
+            base.SetUp();
+            directory = NewDirectory();
+            RandomIndexWriter writer = new RandomIndexWriter(Random(), directory);
+            // we use the default Locale/TZ since LuceneTestCase randomizes it
+            var cal = new DateTime(1970, 1, 1, 0, 0, 0, (int)TestUtil.NextLong(Random(),
0, long.MaxValue), new GregorianCalendar());
+
+            for (int i = 0; i < Max; i++)
+            {
+                Document doc = new Document();
+                doc.Add(NewStringField("key", "" + (i + 1), Field.Store.YES));
+                doc.Add(NewStringField("owner", (i < Max / 2) ? "bob" : "sue", Field.Store.YES));
+                doc.Add(NewStringField("date", cal.ToString(CultureInfo.InvariantCulture),
Field.Store.YES));
+                writer.AddDocument(doc);
+
+                cal = cal.AddDays(1);
+            }
+            reader = writer.Reader;
+            writer.Dispose();
+
+            searcher = NewSearcher(reader);
+
+            // query for everything to make life easier
+            BooleanQuery bq = new BooleanQuery();
+            bq.Add(new TermQuery(new Term("owner", "bob")), BooleanClause.Occur.SHOULD);
+            bq.Add(new TermQuery(new Term("owner", "sue")), BooleanClause.Occur.SHOULD);
+            query = bq;
+
+            // date filter matches everything too
+            //Date pastTheEnd = parseDate("2099 Jan 1");
+            // dateFilter = DateFilter.Before("date", pastTheEnd);
+            // just treat dates as strings and select the whole range for now...
+            dateFilter = TermRangeFilter.NewStringRange("date", "", "ZZZZ", true, true);
+
+            bobFilter = new QueryWrapperFilter(new TermQuery(new Term("owner", "bob")));
+            sueFilter = new QueryWrapperFilter(new TermQuery(new Term("owner", "sue")));
+        }
+
+        [TearDown]
+        public override void TearDown()
+        {
+            reader.Dispose();
+            directory.Dispose();
+            base.TearDown();
+        }
+
+        private ChainedFilter GetChainedFilter(Filter[] chain, int[] logic)
+        {
+            if (logic == null)
+            {
+                return new ChainedFilter(chain);
+            }
+            return new ChainedFilter(chain, logic);
+        }
+
+        private ChainedFilter GetChainedFilter(Filter[] chain, int logic)
+        {
+            return new ChainedFilter(chain, logic);
+        }
+
+        
+        [Test]
+        public virtual void TestSingleFilter()
+        {
+            ChainedFilter chain = GetChainedFilter(new Filter[] { dateFilter }, null);
+
+            int numHits = searcher.Search(query, chain, 1000).TotalHits;
+            assertEquals(Max, numHits);
+
+            chain = new ChainedFilter(new Filter[] { bobFilter });
+            numHits = searcher.Search(query, chain, 1000).TotalHits;
+            assertEquals(Max / 2, numHits);
+
+            chain = GetChainedFilter(new Filter[] { bobFilter }, new[] { ChainedFilter.AND
});
+            TopDocs hits = searcher.Search(query, chain, 1000);
+            numHits = hits.TotalHits;
+            assertEquals(Max / 2, numHits);
+            assertEquals("bob", searcher.Doc(hits.ScoreDocs[0].Doc).Get("owner"));
+
+            chain = GetChainedFilter(new Filter[] { bobFilter }, new[] { ChainedFilter.ANDNOT
});
+            hits = searcher.Search(query, chain, 1000);
+            numHits = hits.TotalHits;
+            assertEquals(Max / 2, numHits);
+            assertEquals("sue", searcher.Doc(hits.ScoreDocs[0].Doc).Get("owner"));
+        }
+        
+        [Test]
+        public virtual void TestOR()
+        {
+            ChainedFilter chain = GetChainedFilter(new Filter[] { sueFilter, bobFilter },
null);
+
+            int numHits = searcher.Search(query, chain, 1000).TotalHits;
+            assertEquals("OR matches all", Max, numHits);
+        }
+        
+        [Test]
+        public virtual void TestAND()
+        {
+            ChainedFilter chain = GetChainedFilter(new Filter[] { dateFilter, bobFilter },
ChainedFilter.AND);
+
+            TopDocs hits = searcher.Search(query, chain, 1000);
+            assertEquals("AND matches just bob", Max / 2, hits.TotalHits);
+            assertEquals("bob", searcher.Doc(hits.ScoreDocs[0].Doc).Get("owner"));
+        }
+        
+        [Test]
+        public virtual void TestXOR()
+        {
+            ChainedFilter chain = GetChainedFilter(new Filter[] { dateFilter, bobFilter },
ChainedFilter.XOR);
+
+            TopDocs hits = searcher.Search(query, chain, 1000);
+            assertEquals("XOR matches sue", Max / 2, hits.TotalHits);
+            assertEquals("sue", searcher.Doc(hits.ScoreDocs[0].Doc).Get("owner"));
+        }
+
+        [Test]
+        public virtual void TestANDNOT()
+        {
+            ChainedFilter chain = GetChainedFilter(new Filter[] { dateFilter, sueFilter },
new int[] { ChainedFilter.AND, ChainedFilter.ANDNOT });
+
+            TopDocs hits = searcher.Search(query, chain, 1000);
+            assertEquals("ANDNOT matches just bob", Max / 2, hits.TotalHits);
+            assertEquals("bob", searcher.Doc(hits.ScoreDocs[0].Doc).Get("owner"));
+
+            chain = GetChainedFilter(new Filter[] { bobFilter, bobFilter }, new int[] { ChainedFilter.ANDNOT,
ChainedFilter.ANDNOT });
+
+            hits = searcher.Search(query, chain, 1000);
+            assertEquals("ANDNOT bob ANDNOT bob matches all sues", Max / 2, hits.TotalHits);
+            assertEquals("sue", searcher.Doc(hits.ScoreDocs[0].Doc).Get("owner"));
+        }
+
+        [Test]
+        public virtual void TestWithCachingFilter()
+        {
+            Directory dir = NewDirectory();
+            RandomIndexWriter writer = new RandomIndexWriter(Random(), dir);
+            IndexReader reader = writer.Reader;
+            writer.Dispose();
+
+            IndexSearcher searcher = NewSearcher(reader);
+
+            Query query = new TermQuery(new Term("none", "none"));
+
+            QueryWrapperFilter queryFilter = new QueryWrapperFilter(query);
+            CachingWrapperFilter cachingFilter = new CachingWrapperFilter(queryFilter);
+
+            searcher.Search(query, cachingFilter, 1);
+
+            CachingWrapperFilter cachingFilter2 = new CachingWrapperFilter(queryFilter);
+            Filter[] chain = new Filter[2];
+            chain[0] = cachingFilter;
+            chain[1] = cachingFilter2;
+            ChainedFilter cf = new ChainedFilter(chain);
+
+            // throws java.lang.ClassCastException: org.apache.lucene.util.OpenBitSet cannot
be cast to java.util.BitSet
+            searcher.Search(new MatchAllDocsQuery(), cf, 1);
+            reader.Dispose();
+            dir.Dispose();
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/7b4987f7/src/Lucene.Net.Tests.Queries/Lucene.Net.Tests.Queries.csproj
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.Queries/Lucene.Net.Tests.Queries.csproj b/src/Lucene.Net.Tests.Queries/Lucene.Net.Tests.Queries.csproj
index 74afe8f..9264faa 100644
--- a/src/Lucene.Net.Tests.Queries/Lucene.Net.Tests.Queries.csproj
+++ b/src/Lucene.Net.Tests.Queries/Lucene.Net.Tests.Queries.csproj
@@ -43,6 +43,8 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="BooleanFilterTest.cs" />
+    <Compile Include="BoostingQueryTest.cs" />
+    <Compile Include="ChainedFilterTest.cs" />
     <Compile Include="CommonTermsQueryTest.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="TermFilterTest.cs" />


Mime
View raw message