lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r919558 - in /lucene/lucene.net/trunk/C#/contrib/Queries.Net: Queries.Net/BooleanFilter.cs Queries.Net/TermsFilter.cs Test/BooleanFilterTest.cs Test/TermsFilterTest.cs
Date Fri, 05 Mar 2010 19:35:55 GMT
Author: digy
Date: Fri Mar  5 19:35:55 2010
New Revision: 919558

URL: http://svn.apache.org/viewvc?rev=919558&view=rev
Log:
BooleanFilter & TermsFilter added to Queries.Net

Modified:
    lucene/lucene.net/trunk/C#/contrib/Queries.Net/Queries.Net/BooleanFilter.cs
    lucene/lucene.net/trunk/C#/contrib/Queries.Net/Queries.Net/TermsFilter.cs
    lucene/lucene.net/trunk/C#/contrib/Queries.Net/Test/BooleanFilterTest.cs
    lucene/lucene.net/trunk/C#/contrib/Queries.Net/Test/TermsFilterTest.cs

Modified: lucene/lucene.net/trunk/C#/contrib/Queries.Net/Queries.Net/BooleanFilter.cs
URL: http://svn.apache.org/viewvc/lucene/lucene.net/trunk/C%23/contrib/Queries.Net/Queries.Net/BooleanFilter.cs?rev=919558&r1=919557&r2=919558&view=diff
==============================================================================
--- lucene/lucene.net/trunk/C#/contrib/Queries.Net/Queries.Net/BooleanFilter.cs (original)
+++ lucene/lucene.net/trunk/C#/contrib/Queries.Net/Queries.Net/BooleanFilter.cs Fri Mar  5
19:35:55 2010
@@ -20,13 +20,301 @@
 using System.Linq;
 using System.Text;
 
+using Lucene.Net.Index;
+using Lucene.Net.Util;
+
 namespace Lucene.Net.Search
 {
-    class BooleanFilter
+    public class BooleanFilter : Filter
     {
-        public BooleanFilter()
+        /// <summary>
+        /// The filters that are optional clauses.
+        /// </summary>
+        private List<Filter> shouldFilters = null;
+
+        /// <summary>
+        /// The filters that are used for exclusion.
+        /// </summary>
+        private List<Filter> notFilters = null;
+
+        /// <summary>
+        /// The filters that must be met.
+        /// </summary>
+        private List<Filter> mustFilters = null;
+
+        /// <summary>
+        /// Get the iterator for a specific filter.
+        /// </summary>
+        /// <param name="filters">The list of filters</param>
+        /// <param name="index">The index of the iterator to get.</param>
+        /// <param name="reader">The reader for the index.</param>
+        /// <returns></returns>
+        private DocIdSetIterator GetDISI(List<Filter> filters, int index, IndexReader
reader)
+        {
+            return ((Filter)filters[index]).GetDocIdSet(reader).Iterator();
+        }
+
+        /// <summary>
+        /// Get the id set for the filter.
+        /// </summary>
+        /// <param name="reader">The reader.</param>
+        /// <returns>The filter set to use.</returns>
+        public override DocIdSet GetDocIdSet(IndexReader reader)
+        {
+            OpenBitSetDISI res = null;
+
+            if (shouldFilters != null)
+            {
+                for (int i = 0; i < shouldFilters.Count; i++)
+                {
+                    if (res == null)
+                    {
+                        res = new OpenBitSetDISI(GetDISI(shouldFilters, i, reader), reader.MaxDoc());
+                    }
+                    else
+                    {
+                        DocIdSet dis = ((Filter)shouldFilters[i]).GetDocIdSet(reader);
+                        if (dis is OpenBitSet)
+                        {
+                            // optimized case for OpenBitSets
+                            res.Or((OpenBitSet)dis);
+                        }
+                        else
+                        {
+                            res.InPlaceOr(GetDISI(shouldFilters, i, reader));
+                        }
+                    }
+                }
+            }
+
+            if (notFilters != null)
+            {
+                for (int i = 0; i < notFilters.Count; i++)
+                {
+                    if (res == null)
+                    {
+                        res = new OpenBitSetDISI(GetDISI(notFilters, i, reader), reader.MaxDoc());
+                        res.Flip(0, reader.MaxDoc()); // NOTE: may set bits on deleted docs
+                    }
+                    else
+                    {
+                        DocIdSet dis = ((Filter)notFilters[i]).GetDocIdSet(reader);
+                        if (dis is OpenBitSet)
+                        {
+                            // optimized case for OpenBitSets
+                            res.AndNot((OpenBitSet)dis);
+                        }
+                        else
+                        {
+                            res.InPlaceNot(GetDISI(notFilters, i, reader));
+                        }
+                    }
+                }
+            }
+
+            if (mustFilters != null)
+            {
+                for (int i = 0; i < mustFilters.Count; i++)
+                {
+                    if (res == null)
+                    {
+                        res = new OpenBitSetDISI(GetDISI(mustFilters, i, reader), reader.MaxDoc());
+                    }
+                    else
+                    {
+                        DocIdSet dis = ((Filter)mustFilters[i]).GetDocIdSet(reader);
+                        if (dis is OpenBitSet)
+                        {
+                            // optimized case for OpenBitSets
+                            res.And((OpenBitSet)dis);
+                        }
+                        else
+                        {
+                            res.InPlaceAnd(GetDISI(mustFilters, i, reader));
+                        }
+                    }
+                }
+            }
+
+            if (res != null)
+                return FinalResult(res, reader.MaxDoc());
+
+            else
+            {
+                //TODO: 2.- change return DocIdSet.EMPTY_DOCIDSET;
+                return null;
+            }
+        }
+
+        /// <summary>
+        /// Add a filter clause.
+        /// </summary>
+        /// <param name="filterClause">The clause to add.</param>
+        public void Add(BooleanFilterClause filterClause)
+        {
+            if (filterClause.Occur == BooleanClause.Occur.MUST)
+            {
+                if (mustFilters == null)
+                {
+                    mustFilters = new List<Filter>();
+                }
+                mustFilters.Add(filterClause.Filter);
+            }
+            if (filterClause.Occur == BooleanClause.Occur.SHOULD)
+            {
+                if (shouldFilters == null)
+                {
+                    shouldFilters = new List<Filter>();
+                }
+                shouldFilters.Add(filterClause.Filter);
+            }
+            if (filterClause.Occur == BooleanClause.Occur.MUST_NOT)
+            {
+                if (notFilters == null)
+                {
+                    notFilters = new List<Filter>();
+                }
+                notFilters.Add(filterClause.Filter);
+            }
+        }
+
+        // TODO: in 3.0, instead of removing this deprecated
+        // method, make it a no-op and mark it final
+        /** Provide a SortedVIntList when it is definitely smaller
+         * than an OpenBitSet.
+         * @deprecated Either use CachingWrapperFilter, or
+         * switch to a different DocIdSet implementation yourself. */
+        protected DocIdSet FinalResult(OpenBitSetDISI result, int maxDocs)
+        {
+            return (result.Cardinality() < (maxDocs / 9))
+              ? (DocIdSet)new SortedVIntList(result)
+              : (DocIdSet)result;
+        }
+
+        /// <summary>
+        /// Determine equality between two lists.
+        /// </summary>
+        /// <param name="filters1"></param>
+        /// <param name="filters2"></param>
+        /// <returns></returns>
+        private bool EqualFilters(List<Filter> filters1, List<Filter> filters2)
+        {
+            return (filters1 == filters2) ||
+                     ((filters1 != null) && filters1.Equals(filters2));
+        }
+
+        /// <summary>
+        /// Equality
+        /// </summary>
+        /// <param name="obj"></param>
+        /// <returns></returns>
+        public override bool Equals(Object obj)
+        {
+            if (this == obj)
+                return true;
+
+            if ((obj == null) || !(obj is BooleanFilter))
+                return false;
+
+            BooleanFilter other = (BooleanFilter)obj;
+            return EqualFilters(notFilters, other.notFilters)
+                && EqualFilters(mustFilters, other.mustFilters)
+                && EqualFilters(shouldFilters, other.shouldFilters);
+        }
+
+        /// <summary>
+        /// Hash code.
+        /// </summary>
+        /// <returns></returns>
+        public override int GetHashCode()
+        {
+            int hash = 7;
+            hash = 31 * (hash + this.ListHash(this.mustFilters)); 
+            hash = 31 * (hash + this.ListHash(this.notFilters)); 
+            hash = 31 * (hash + this.ListHash(this.shouldFilters));
+            return hash;
+        }
+
+
+        private int ListHash(List<Filter> filters)
+        {
+            int sum = 0;
+            if (filters != null && filters.Count > 0)
+            {
+                for (int i = 0; i < filters.Count; i++)
+                {
+                    sum += filters[i].GetHashCode();
+                }
+            }
+            return sum;
+        }
+
+        /// <summary>
+        /// String representation.
+        /// </summary>
+        /// <returns></returns>
+        public override String ToString()
+        {
+            StringBuilder buffer = new StringBuilder();
+            buffer.Append("BooleanFilter(");
+            AppendFilters(shouldFilters, "", buffer);
+            AppendFilters(mustFilters, "+", buffer);
+            AppendFilters(notFilters, "-", buffer);
+            buffer.Append(")");
+            return buffer.ToString();
+        }
+
+        /// <summary>
+        /// Append individual filters.
+        /// </summary>
+        /// <param name="filters"></param>
+        /// <param name="occurString"></param>
+        /// <param name="buffer"></param>
+        private void AppendFilters(List<Filter> filters, String occurString, StringBuilder
buffer)
+        {
+            if (filters != null)
+            {
+                for (int i = 0; i < filters.Count(); i++)
+                {
+                    buffer.Append(' ');
+                    buffer.Append(occurString);
+                    buffer.Append(filters[i].ToString());
+                }
+            }
+        }
+    }
+
+    /// <summary>
+    /// A spefic clause that makes up a part of the BooleanFilter
+    /// </summary>
+    public class BooleanFilterClause
+    {
+        /// <summary>
+        /// Create a new BooleanFilterClause
+        /// </summary>
+        /// <param name="filter">A Filter object</param>
+        /// <param name="occur">A parameter implementation indicating SHOULD, MUST
or MUST NOT</param>
+        public BooleanFilterClause(Filter filter, BooleanClause.Occur occur)
+        {
+            this.Occur = occur;
+            this.Filter = filter;
+        }
+
+        /// <summary>
+        /// The underlying filter for the clause.
+        /// </summary>
+        public Filter Filter
+        {
+            get;
+            private set;
+        }
+        /// <summary>
+        /// The occurrence of this clause.
+        /// </summary>
+        public BooleanClause.Occur Occur
         {
-            throw new NotImplementedException("Not implemented yet.");
+            get;
+            private set;
         }
     }
 }

Modified: lucene/lucene.net/trunk/C#/contrib/Queries.Net/Queries.Net/TermsFilter.cs
URL: http://svn.apache.org/viewvc/lucene/lucene.net/trunk/C%23/contrib/Queries.Net/Queries.Net/TermsFilter.cs?rev=919558&r1=919557&r2=919558&view=diff
==============================================================================
--- lucene/lucene.net/trunk/C#/contrib/Queries.Net/Queries.Net/TermsFilter.cs (original)
+++ lucene/lucene.net/trunk/C#/contrib/Queries.Net/Queries.Net/TermsFilter.cs Fri Mar  5 19:35:55
2010
@@ -20,13 +20,92 @@
 using System.Linq;
 using System.Text;
 
+using Lucene.Net.Index;
+using Lucene.Net.Util;
+
 namespace Lucene.Net.Search
 {
-    class TermsFilter
+    /// <summary>
+    /// A filter that contains multiple terms.
+    /// </summary>
+    public class TermsFilter : Filter
     {
-        public TermsFilter()
+        /// <summary>
+        /// The set of terms for this filter.
+        /// </summary>
+        protected HashSet<Term> terms = new HashSet<Term>();
+
+        /// <summary>
+        /// Add a term to the set.
+        /// </summary>
+        /// <param name="term">The term to add.</param>
+        public void AddTerm(Term term)
+        {
+            terms.Add(term);
+        }
+
+        /// <summary>
+        /// Get the DocIdSet.
+        /// </summary>
+        /// <param name="reader">Applcible reader.</param>
+        /// <returns>The set.</returns>
+        public override DocIdSet GetDocIdSet(IndexReader reader)
+        {
+            OpenBitSet result = new OpenBitSet(reader.MaxDoc());
+            TermDocs td = reader.TermDocs();
+            try
+            {
+                foreach (Term t in this.terms)
+                {
+                    td.Seek(t);
+                    while (td.Next())
+                    {
+                        result.Set(td.Doc());
+                    }
+                }
+            }
+            finally
+            {
+                td.Close();
+            }
+
+            return result;
+        }
+
+        public override bool Equals(Object obj)
+        {
+            if (this == obj)
+            {
+                return true;
+            }
+            if ((obj == null) || !(obj is TermsFilter))
+            {
+                return false;
+            }
+            TermsFilter test = (TermsFilter)obj;
+            return (terms == test.terms || (terms != null && terms.Equals(test.terms)));
+        }
+
+        public override int GetHashCode()
+        {
+            int hash = 9;
+            foreach (Term t in this.terms)
+            {
+                hash = 31 * hash + t.GetHashCode();
+            }
+            return hash;
+        }
+
+        public override string ToString()
         {
-            throw new NotImplementedException("Not implemented yet.");
+            StringBuilder sb = new StringBuilder();
+            sb.Append("(");
+            foreach (Term t in this.terms)
+            {
+                sb.AppendFormat(" {0}:{1}", t.Field(), t.Text());
+            }
+            sb.Append(" )");
+            return sb.ToString();
         }
     }
 }

Modified: lucene/lucene.net/trunk/C#/contrib/Queries.Net/Test/BooleanFilterTest.cs
URL: http://svn.apache.org/viewvc/lucene/lucene.net/trunk/C%23/contrib/Queries.Net/Test/BooleanFilterTest.cs?rev=919558&r1=919557&r2=919558&view=diff
==============================================================================
--- lucene/lucene.net/trunk/C#/contrib/Queries.Net/Test/BooleanFilterTest.cs (original)
+++ lucene/lucene.net/trunk/C#/contrib/Queries.Net/Test/BooleanFilterTest.cs Fri Mar  5 19:35:55
2010
@@ -16,15 +16,306 @@
  */
 
 using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 
+using Lucene.Net.Analysis;
+using Lucene.Net.Documents;
+using Lucene.Net.Index;
+using Lucene.Net.Store;
+
 using NUnit.Framework;
 
 namespace Lucene.Net.Search
 {
-    class BooleanFilterTest
+    /// <summary>
+    /// Summary description for UnitTest1
+    /// </summary>
+    [TestFixture]
+    public class BooleanFilterTest
+    {
+        public BooleanFilterTest() { }
+
+        private IndexReader reader;
+
+        #region setup/teardown methods
+
+        [SetUp]
+        public void MyTestInitialize()
+        {
+            RAMDirectory directory = new RAMDirectory();
+            IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true,
IndexWriter.MaxFieldLength.LIMITED);
+
+            //Add series of docs with filterable fields : acces rights, prices, dates and
"in-stock" flags
+            AddDoc(writer, "admin guest", "010", "20040101", "Y");
+            AddDoc(writer, "guest", "020", "20040101", "Y");
+            AddDoc(writer, "guest", "020", "20050101", "Y");
+            AddDoc(writer, "admin", "020", "20050101", "Maybe");
+            AddDoc(writer, "admin guest", "030", "20050101", "N");
+
+            writer.Close();
+            reader = IndexReader.Open(directory, true);
+        }
+        
+
+         [TearDown]
+         public void MyTestCleanup() 
+         {
+             if (this.reader != null)
+             {
+                 this.reader.Close();
+                 this.reader = null;
+             }
+         }
+        
+        #endregion
+
+        [Test]
+        public void Should_Test()
+        {
+            for (int i = 0; i < 2; i++)
+            {
+                bool old = (i == 0);
+
+                BooleanFilter booleanFilter = new BooleanFilter();
+                booleanFilter.Add(new BooleanFilterClause(GetTermsFilter("price", "030",
old), BooleanClause.Occur.SHOULD));
+                TstFilterCard("Should retrieves only 1 doc", 1, booleanFilter);
+            }
+        }
+
+        [Test]
+        public void Shoulds_Test()
+        {
+            for (int i = 0; i < 2; i++)
+            {
+                bool old = (i == 0);
+
+                BooleanFilter booleanFilter = new BooleanFilter();
+                booleanFilter.Add(new BooleanFilterClause(GetRangeFilter("price", "010",
"020", old), BooleanClause.Occur.SHOULD));
+                booleanFilter.Add(new BooleanFilterClause(GetRangeFilter("price", "020",
"030", old), BooleanClause.Occur.SHOULD));
+                TstFilterCard("Shoulds are Ored together", 5, booleanFilter);
+            }
+        }
+
+        [Test]
+        public void ShouldsAndMustNot_Test()
+        {
+            for (int i = 0; i < 2; i++)
+            {
+                bool old = (i == 0);
+
+                BooleanFilter booleanFilter = new BooleanFilter();
+                booleanFilter.Add(new BooleanFilterClause(GetRangeFilter("price", "010",
"020", old), BooleanClause.Occur.SHOULD));
+                booleanFilter.Add(new BooleanFilterClause(GetRangeFilter("price", "020",
"030", old), BooleanClause.Occur.SHOULD));
+                booleanFilter.Add(new BooleanFilterClause(GetTermsFilter("inStock", "N",
old), BooleanClause.Occur.MUST_NOT));
+                TstFilterCard("Shoulds Ored but AndNot", 4, booleanFilter);
+
+                booleanFilter.Add(new BooleanFilterClause(GetTermsFilter("inStock", "Maybe",
old), BooleanClause.Occur.MUST_NOT));
+                TstFilterCard("Shoulds Ored but AndNots", 3, booleanFilter);
+            }
+
+        }
+
+        [Test]
+        public void ShouldsAndMust_Test()
+        {
+            for (int i = 0; i < 2; i++)
+            {
+                bool old = (i == 0);
+
+                BooleanFilter booleanFilter = new BooleanFilter();
+                booleanFilter.Add(new BooleanFilterClause(GetRangeFilter("price", "010",
"020", old), BooleanClause.Occur.SHOULD));
+                booleanFilter.Add(new BooleanFilterClause(GetRangeFilter("price", "020",
"030", old), BooleanClause.Occur.SHOULD));
+                booleanFilter.Add(new BooleanFilterClause(GetTermsFilter("accessRights",
"admin", old), BooleanClause.Occur.MUST));
+                TstFilterCard("Shoulds Ored but MUST", 3, booleanFilter);
+            }
+        }
+
+        [Test]
+        public void ShouldsAndMusts_Test()
+        {
+            for (int i = 0; i < 2; i++)
+            {
+                bool old = (i == 0);
+
+                BooleanFilter booleanFilter = new BooleanFilter();
+                booleanFilter.Add(new BooleanFilterClause(GetRangeFilter("price", "010",
"020", old), BooleanClause.Occur.SHOULD));
+                booleanFilter.Add(new BooleanFilterClause(GetRangeFilter("price", "020",
"030", old), BooleanClause.Occur.SHOULD));
+                booleanFilter.Add(new BooleanFilterClause(GetTermsFilter("accessRights",
"admin", old), BooleanClause.Occur.MUST));
+                booleanFilter.Add(new BooleanFilterClause(GetRangeFilter("date", "20040101",
"20041231", old), BooleanClause.Occur.MUST));
+                TstFilterCard("Shoulds Ored but MUSTs ANDED", 1, booleanFilter);
+            }
+        }
+
+        [Test]
+        public void ShouldsAndMustsAndMustNot_Test()
+        {
+            for (int i = 0; i < 2; i++)
+            {
+                bool old = (i == 0);
+
+                BooleanFilter booleanFilter = new BooleanFilter();
+                booleanFilter.Add(new BooleanFilterClause(GetRangeFilter("price", "030",
"040", old), BooleanClause.Occur.SHOULD));
+                booleanFilter.Add(new BooleanFilterClause(GetTermsFilter("accessRights",
"admin", old), BooleanClause.Occur.MUST));
+                booleanFilter.Add(new BooleanFilterClause(GetRangeFilter("date", "20050101",
"20051231", old), BooleanClause.Occur.MUST));
+                booleanFilter.Add(new BooleanFilterClause(GetTermsFilter("inStock", "N",
old), BooleanClause.Occur.MUST_NOT));
+                TstFilterCard("Shoulds Ored but MUSTs ANDED and MustNot", 0, booleanFilter);
+            }
+        }
+
+        [Test]
+        public void JustMust_Test()
+        {
+            for (int i = 0; i < 2; i++)
+            {
+                bool old = (i == 0);
+
+                BooleanFilter booleanFilter = new BooleanFilter();
+                booleanFilter.Add(new BooleanFilterClause(GetTermsFilter("accessRights",
"admin", old), BooleanClause.Occur.MUST));
+                TstFilterCard("MUST", 3, booleanFilter);
+            }
+        }
+
+        [Test]
+        public void JustMustNot_Test()
+        {
+            for (int i = 0; i < 2; i++)
+            {
+                bool old = (i == 0);
+
+                BooleanFilter booleanFilter = new BooleanFilter();
+                booleanFilter.Add(new BooleanFilterClause(GetTermsFilter("inStock", "N",
old), BooleanClause.Occur.MUST_NOT));
+                TstFilterCard("MUST_NOT", 4, booleanFilter);
+            }
+        }
+
+        [Test]
+        public void MustAndMustNot_Test()
+        {
+            for (int i = 0; i < 2; i++)
+            {
+                bool old = (i == 0);
+
+                BooleanFilter booleanFilter = new BooleanFilter();
+                booleanFilter.Add(new BooleanFilterClause(GetTermsFilter("inStock", "N",
old), BooleanClause.Occur.MUST));
+                booleanFilter.Add(new BooleanFilterClause(GetTermsFilter("price", "030",
old), BooleanClause.Occur.MUST_NOT));
+                TstFilterCard("MUST_NOT wins over MUST for same docs", 0, booleanFilter);
+            }
+        }
+
+        [Test]
+        public void HashEquality()
+        {
+            BooleanFilter a = new BooleanFilter();
+            a.Add(new BooleanFilterClause(GetTermsFilter("inStock", "N", false), BooleanClause.Occur.MUST));
+            a.Add(new BooleanFilterClause(GetTermsFilter("price", "030", false), BooleanClause.Occur.MUST_NOT));
+
+            BooleanFilter b = new BooleanFilter();
+            b.Add(new BooleanFilterClause(GetTermsFilter("inStock", "N", false), BooleanClause.Occur.MUST));
+            b.Add(new BooleanFilterClause(GetTermsFilter("price", "030", false), BooleanClause.Occur.MUST_NOT));
+
+            Assert.AreEqual(a.GetHashCode(), b.GetHashCode(), "Hashes do not match");
+        }
+
+        [Test]
+        public void ToString_Test()
+        {
+            BooleanFilter b = new BooleanFilter();
+            b.Add(new BooleanFilterClause(GetTermsFilter("inStock", "N", false), BooleanClause.Occur.MUST));
+            b.Add(new BooleanFilterClause(GetTermsFilter("price", "030", false), BooleanClause.Occur.MUST_NOT));
+            b.Add(new BooleanFilterClause(GetRangeFilter("price", "030", "040", false), BooleanClause.Occur.SHOULD));
+
+            Assert.AreEqual("BooleanFilter( price:[030 TO 040] +( inStock:N ) -( price:030
))", b.ToString());
+        }
+
+        [Test]
+        [ExpectedException(typeof(NotSupportedException))]
+        public void ExOnBits_Test()
+        {
+            BooleanFilter b = new BooleanFilter();
+            b.Add(new BooleanFilterClause(GetTermsFilter("inStock", "N", false), BooleanClause.Occur.MUST));
+            b.Add(new BooleanFilterClause(GetTermsFilter("price", "030", false), BooleanClause.Occur.MUST_NOT));
+            b.Add(new BooleanFilterClause(GetRangeFilter("price", "030", "040", false), BooleanClause.Occur.SHOULD));
+
+            BitArray bits = b.Bits(this.reader);
+        }
+
+        #region helpers
+
+        private void AddDoc(IndexWriter writer, String accessRights, String price, String
date, String inStock)
+        {
+            Document doc = new Document();
+            doc.Add(new Field("accessRights", accessRights, Field.Store.YES, Field.Index.ANALYZED));
+            doc.Add(new Field("price", price, Field.Store.YES, Field.Index.ANALYZED));
+            doc.Add(new Field("date", date, Field.Store.YES, Field.Index.ANALYZED));
+            doc.Add(new Field("inStock", inStock, Field.Store.YES, Field.Index.ANALYZED));
+            writer.AddDocument(doc);
+        }
+
+        private Filter GetOldBitSetFilter(Filter filter)
+        {
+            return new MockBooleanFilter(filter);
+        }
+
+        private Filter GetRangeFilter(String field, String lowerPrice, String upperPrice,
bool old)
+        {
+            Filter f = new TermRangeFilter(field, lowerPrice, upperPrice, true, true);
+            if (old)
+            {
+                return GetOldBitSetFilter(f);
+            }
+
+            return f;
+        }
+
+        private Filter GetTermsFilter(String field, String text, bool old)
+        {
+            TermsFilter tf = new TermsFilter();
+            tf.AddTerm(new Term(field, text));
+            if (old)
+            {
+                return GetOldBitSetFilter(tf);
+            }
+
+            return tf;
+        }
+
+        private void TstFilterCard(String mes, int expected, Filter filt)
+        {
+            DocIdSetIterator disi = filt.GetDocIdSet(reader).Iterator();
+            int actual = 0;
+            while (disi.NextDoc() != DocIdSetIterator.NO_MORE_DOCS)
+            {
+                actual++;
+            }
+            Assert.AreEqual(expected, actual, mes);
+        }
+
+        #endregion
+    }
+
+    public class MockBooleanFilter : Filter
     {
+        private Filter filter;
+
+        public MockBooleanFilter(Filter f)
+        {
+            this.filter = f;
+        }
+
+        [Obsolete]
+        public override BitArray Bits(IndexReader reader)
+        {
+            BitArray bits = new BitArray(reader.MaxDoc());
+            DocIdSetIterator it = filter.GetDocIdSet(reader).Iterator();
+            int doc = DocIdSetIterator.NO_MORE_DOCS;
+            while ((doc = it.NextDoc()) != DocIdSetIterator.NO_MORE_DOCS)
+            {
+                bits.Set(doc, true);
+            }
+            return bits;
+        }
     }
 }

Modified: lucene/lucene.net/trunk/C#/contrib/Queries.Net/Test/TermsFilterTest.cs
URL: http://svn.apache.org/viewvc/lucene/lucene.net/trunk/C%23/contrib/Queries.Net/Test/TermsFilterTest.cs?rev=919558&r1=919557&r2=919558&view=diff
==============================================================================
--- lucene/lucene.net/trunk/C#/contrib/Queries.Net/Test/TermsFilterTest.cs (original)
+++ lucene/lucene.net/trunk/C#/contrib/Queries.Net/Test/TermsFilterTest.cs Fri Mar  5 19:35:55
2010
@@ -16,15 +16,97 @@
  */
 
 using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 
+using Lucene.Net.Analysis;
+using Lucene.Net.Documents;
+using Lucene.Net.Index;
+using Lucene.Net.Store;
+using Lucene.Net.Util;
+
 using NUnit.Framework;
 
 namespace Lucene.Net.Search
 {
-    class TermsFilterTest
+    /// <summary>
+    /// Summary description for TermsFilterTest
+    /// </summary>
+    [TestFixture]
+    public class TermsFilterTest
     {
+        public TermsFilterTest() { }
+
+
+        [Test]
+        public void Cachability_Test()
+        {
+            TermsFilter a = new TermsFilter();
+            a.AddTerm(new Term("field1", "a"));
+            a.AddTerm(new Term("field1", "b"));
+
+            // original test used placing filters in a HashSet to
+            // determine equality, where the FilterManager uses
+            // the hash code of the filters as the key, so
+            // it makes more sense to just test the equality of the 
+            // hash codes.
+
+            TermsFilter b = new TermsFilter();
+            b.AddTerm(new Term("field1", "a"));
+            b.AddTerm(new Term("field1", "b"));
+            Assert.AreEqual(a.GetHashCode(), b.GetHashCode(), "Hashes do not match");
+
+            b.AddTerm(new Term("field1", "a")); //duplicate term
+            Assert.AreEqual(a.GetHashCode(), b.GetHashCode(), "Hashes do not match");
+
+            b.AddTerm(new Term("field1", "c"));
+            Assert.AreNotEqual(a.GetHashCode(), b.GetHashCode(), "Hashes match");
+        }
+
+        [Test]
+        public void MissingTerms_Test()
+        {
+            string fieldName = "field1";
+            RAMDirectory rd = new RAMDirectory();
+            IndexWriter w = new IndexWriter(rd, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);
+            for (int i = 0; i < 100; i++)
+            {
+                Document doc = new Document();
+                int term = i * 10; //terms are units of 10;
+                doc.Add(new Field(fieldName, "" + term, Field.Store.YES, Field.Index.NOT_ANALYZED));
+                w.AddDocument(doc);
+            }
+            w.Close();
+            IndexReader reader = IndexReader.Open(rd, true);
+
+            TermsFilter tf = new TermsFilter();
+            tf.AddTerm(new Term(fieldName, "19"));
+            OpenBitSet bits = (OpenBitSet)tf.GetDocIdSet(reader);
+            Assert.AreEqual(0, bits.Cardinality(), "Must match nothing");
+
+            tf.AddTerm(new Term(fieldName, "20"));
+            bits = (OpenBitSet)tf.GetDocIdSet(reader);
+            Assert.AreEqual(1, bits.Cardinality(), "Must match 1");
+
+            tf.AddTerm(new Term(fieldName, "10"));
+            bits = (OpenBitSet)tf.GetDocIdSet(reader);
+            Assert.AreEqual(2, bits.Cardinality(), "Must match 2");
+
+            tf.AddTerm(new Term(fieldName, "00"));
+            bits = (OpenBitSet)tf.GetDocIdSet(reader);
+            Assert.AreEqual(2, bits.Cardinality(), "Must match 2");
+        }
+
+        [Test]
+        [ExpectedException(typeof(NotSupportedException))]
+        public void ExOnBits()
+        {
+            TermsFilter a = new TermsFilter();
+            a.AddTerm(new Term("field1", "a"));
+            a.AddTerm(new Term("field1", "b"));
+            BitArray b = a.Bits(null);
+        }
     }
 }



Mime
View raw message