public virtual void  TestOutOfOrderCollection()
        {
            Directory   dir    = new RAMDirectory();
            IndexWriter writer = new IndexWriter(dir, null, MaxFieldLength.UNLIMITED);

            for (int i = 0; i < 10; i++)
            {
                writer.AddDocument(new Document());
            }
            writer.Commit();
            writer.Close();

            bool[]          inOrder         = new bool[] { false, true };
            System.String[] actualTSDCClass = new System.String[] { "OutOfOrderTopScoreDocCollector", "InOrderTopScoreDocCollector" };

            // Save the original value to set later.
            bool origVal = BooleanQuery.GetAllowDocsOutOfOrder();

            BooleanQuery.SetAllowDocsOutOfOrder(true);

            BooleanQuery bq = new BooleanQuery();

            // Add a Query with SHOULD, since bw.scorer() returns BooleanScorer2
            // which delegates to BS if there are no mandatory clauses.
            bq.Add(new MatchAllDocsQuery(), Occur.SHOULD);
            // Set minNrShouldMatch to 1 so that BQ will not optimize rewrite to return
            // the clause instead of BQ.
            bq.SetMinimumNumberShouldMatch(1);
            try
            {
                IndexSearcher searcher = new IndexSearcher(dir);
                for (int i = 0; i < inOrder.Length; i++)
                {
                    TopDocsCollector tdc = TopScoreDocCollector.create(3, inOrder[i]);
                    Assert.AreEqual("Lucene.Net.Search.TopScoreDocCollector+" + actualTSDCClass[i], tdc.GetType().FullName);

                    searcher.Search(new MatchAllDocsQuery(), tdc);

                    ScoreDoc[] sd = tdc.TopDocs().ScoreDocs;
                    Assert.AreEqual(3, sd.Length);
                    for (int j = 0; j < sd.Length; j++)
                    {
                        Assert.AreEqual(j, sd[j].doc, "expected doc Id " + j + " found " + sd[j].doc);
                    }
                }
            }
            finally
            {
                // Whatever happens, reset BooleanQuery.allowDocsOutOfOrder to the
                // original value. Don't set it to false in case the implementation in BQ
                // will change some day.
                BooleanQuery.SetAllowDocsOutOfOrder(origVal);
            }
        }
Beispiel #2
0
        public virtual void  TestNoOptionalButMin2()
        {
            /* one required, no optional */
            BooleanQuery q = new BooleanQuery();

            q.Add(new TermQuery(new Term("all", "all")), BooleanClause.Occur.MUST); //true,  false);

            q.SetMinimumNumberShouldMatch(1);                                       // 1 of 0 optional

            VerifyNrHits(q, 0);
        }
			public virtual void  PostCreate(BooleanQuery q)
			{
				BooleanClause[] c = q.GetClauses();
				int opt = 0;
				for (int i = 0; i < c.Length; i++)
				{
					if (c[i].GetOccur() == BooleanClause.Occur.SHOULD)
						opt++;
				}
				q.SetMinimumNumberShouldMatch(rnd.Next(opt + 2));
			}
Beispiel #4
0
        public virtual void  TestAllOptional()
        {
            BooleanQuery q = new BooleanQuery();

            for (int i = 1; i <= 4; i++)
            {
                q.Add(new TermQuery(new Term("data", "" + i)), BooleanClause.Occur.SHOULD); //false, false);
            }
            q.SetMinimumNumberShouldMatch(2);                                               // match at least two of 4
            VerifyNrHits(q, 2);
        }
		public virtual void  TestOutOfOrderCollection()
		{
			
			Directory dir = new RAMDirectory();
			IndexWriter writer = new IndexWriter(dir, null, MaxFieldLength.UNLIMITED);
			for (int i = 0; i < 10; i++)
			{
				writer.AddDocument(new Document());
			}
			writer.Commit();
			writer.Close();
			
			bool[] inOrder = new bool[]{false, true};
			System.String[] actualTSDCClass = new System.String[]{"OutOfOrderTopScoreDocCollector", "InOrderTopScoreDocCollector"};
			
			// Save the original value to set later.
			bool origVal = BooleanQuery.GetAllowDocsOutOfOrder();
			
			BooleanQuery.SetAllowDocsOutOfOrder(true);
			
			BooleanQuery bq = new BooleanQuery();
			// Add a Query with SHOULD, since bw.scorer() returns BooleanScorer2
			// which delegates to BS if there are no mandatory clauses.
			bq.Add(new MatchAllDocsQuery(), Occur.SHOULD);
			// Set minNrShouldMatch to 1 so that BQ will not optimize rewrite to return
			// the clause instead of BQ.
			bq.SetMinimumNumberShouldMatch(1);
			try
			{
				
				IndexSearcher searcher = new IndexSearcher(dir);
				for (int i = 0; i < inOrder.Length; i++)
				{
					TopDocsCollector tdc = TopScoreDocCollector.create(3, inOrder[i]);
					Assert.AreEqual("Lucene.Net.Search.TopScoreDocCollector+" + actualTSDCClass[i], tdc.GetType().FullName);
					
					searcher.Search(new MatchAllDocsQuery(), tdc);
					
					ScoreDoc[] sd = tdc.TopDocs().scoreDocs;
					Assert.AreEqual(3, sd.Length);
					for (int j = 0; j < sd.Length; j++)
					{
						Assert.AreEqual(j, sd[j].doc, "expected doc Id " + j + " found " + sd[j].doc);
					}
				}
			}
			finally
			{
				// Whatever happens, reset BooleanQuery.allowDocsOutOfOrder to the
				// original value. Don't set it to false in case the implementation in BQ
				// will change some day.
				BooleanQuery.SetAllowDocsOutOfOrder(origVal);
			}
		}
        public virtual void  TestBQ20()
        {
            BooleanQuery q = new BooleanQuery();

            q.SetMinimumNumberShouldMatch(2);
            q.Add(qp.Parse("QQQQQ"), BooleanClause.Occur.SHOULD);
            q.Add(qp.Parse("yy"), BooleanClause.Occur.SHOULD);
            q.Add(qp.Parse("zz"), BooleanClause.Occur.SHOULD);
            q.Add(qp.Parse("w5"), BooleanClause.Occur.SHOULD);
            q.Add(qp.Parse("w4"), BooleanClause.Occur.SHOULD);

            Qtest(q, new int[] { 0, 3 });
        }
Beispiel #7
0
        public virtual void  TestOneOptionalEqualToMin()
        {
            /* two required, one optional */
            BooleanQuery q = new BooleanQuery();

            q.Add(new TermQuery(new Term("all", "all")), BooleanClause.Occur.MUST);  //true,  false);
            q.Add(new TermQuery(new Term("data", "3")), BooleanClause.Occur.SHOULD); //false, false);
            q.Add(new TermQuery(new Term("data", "2")), BooleanClause.Occur.MUST);   //true,  false);

            q.SetMinimumNumberShouldMatch(1);                                        // 1 of 1 optional

            VerifyNrHits(q, 1);
        }
Beispiel #8
0
            public virtual void  PostCreate(BooleanQuery q)
            {
                BooleanClause[] c   = q.GetClauses();
                int             opt = 0;

                for (int i = 0; i < c.Length; i++)
                {
                    if (c[i].GetOccur() == BooleanClause.Occur.SHOULD)
                    {
                        opt++;
                    }
                }
                q.SetMinimumNumberShouldMatch(rnd.Next(opt + 2));
            }
Beispiel #9
0
        public virtual void  TestOneProhibAndSomeOptional()
        {
            /* one prohibited, some optional */
            BooleanQuery q = new BooleanQuery();

            q.Add(new TermQuery(new Term("data", "1")), BooleanClause.Occur.SHOULD);   //false, false);
            q.Add(new TermQuery(new Term("data", "2")), BooleanClause.Occur.SHOULD);   //false, false);
            q.Add(new TermQuery(new Term("data", "3")), BooleanClause.Occur.MUST_NOT); //false, true );
            q.Add(new TermQuery(new Term("data", "4")), BooleanClause.Occur.SHOULD);   //false, false);

            q.SetMinimumNumberShouldMatch(2);                                          // 2 of 3 optional

            VerifyNrHits(q, 1);
        }
Beispiel #10
0
        public virtual void  TestOneReqAndSomeOptional()
        {
            /* one required, some optional */
            BooleanQuery q = new BooleanQuery();

            q.Add(new TermQuery(new Term("all", "all")), BooleanClause.Occur.MUST);  //true,  false);
            q.Add(new TermQuery(new Term("data", "5")), BooleanClause.Occur.SHOULD); //false, false);
            q.Add(new TermQuery(new Term("data", "4")), BooleanClause.Occur.SHOULD); //false, false);
            q.Add(new TermQuery(new Term("data", "3")), BooleanClause.Occur.SHOULD); //false, false);

            q.SetMinimumNumberShouldMatch(2);                                        // 2 of 3 optional

            VerifyNrHits(q, 5);
        }
Beispiel #11
0
        public virtual void  TestSomeReqOneProhibAndSomeOptional()
        {
            /* two required, one prohibited, some optional */
            BooleanQuery q = new BooleanQuery();

            q.Add(new TermQuery(new Term("all", "all")), BooleanClause.Occur.MUST);    //true,  false);
            q.Add(new TermQuery(new Term("data", "6")), BooleanClause.Occur.MUST);     //true,  false);
            q.Add(new TermQuery(new Term("data", "5")), BooleanClause.Occur.SHOULD);   //false, false);
            q.Add(new TermQuery(new Term("data", "4")), BooleanClause.Occur.SHOULD);   //false, false);
            q.Add(new TermQuery(new Term("data", "3")), BooleanClause.Occur.MUST_NOT); //false, true );
            q.Add(new TermQuery(new Term("data", "2")), BooleanClause.Occur.SHOULD);   //false, false);
            q.Add(new TermQuery(new Term("data", "1")), BooleanClause.Occur.SHOULD);   //false, false);

            q.SetMinimumNumberShouldMatch(3);                                          // 3 of 4 optional

            VerifyNrHits(q, 1);
        }
        public virtual void  Test2()
        {
            BooleanQuery q = new BooleanQuery();

            q.Add(qp.Parse("\"w1 w2\"~1"), Occur.MUST);
            q.Add(Snear(St("w2"), Sor("w5", "zz"), 4, true), Occur.SHOULD);
            q.Add(Snear(Sf("w3", 2), St("w2"), St("w3"), 5, true), Occur.SHOULD);

            Query t = new FilteredQuery(qp.Parse("xx"), new ItemizedFilter(new int[] { 1, 3 }));

            t.SetBoost(1000);
            q.Add(t, Occur.SHOULD);

            t = new ConstantScoreQuery(new ItemizedFilter(new int[] { 0, 2 }));
            t.SetBoost(-20.0f);
            q.Add(t, Occur.SHOULD);

            DisjunctionMaxQuery dm = new DisjunctionMaxQuery(0.2f);

            dm.Add(Snear(St("w2"), Sor("w5", "zz"), 4, true));
            dm.Add(qp.Parse("QQ"));
            dm.Add(qp.Parse("xx yy -zz"));
            dm.Add(qp.Parse("-xx -w1"));

            DisjunctionMaxQuery dm2 = new DisjunctionMaxQuery(0.5f);

            dm2.Add(qp.Parse("w1"));
            dm2.Add(qp.Parse("w2"));
            dm2.Add(qp.Parse("w3"));
            dm.Add(dm2);

            q.Add(dm, Occur.SHOULD);

            BooleanQuery b = new BooleanQuery();

            b.SetMinimumNumberShouldMatch(2);
            b.Add(Snear("w1", "w2", 1, true), Occur.SHOULD);
            b.Add(Snear("w2", "w3", 1, true), Occur.SHOULD);
            b.Add(Snear("w1", "w3", 3, true), Occur.SHOULD);
            b.SetBoost(0.0f);

            q.Add(b, Occur.SHOULD);

            Qtest(q, new int[] { 0, 1, 2 });
        }
Beispiel #13
0
        public virtual void  TestMinHigherThenNumOptional()
        {
            /* two required, two prohibited, some optional */
            BooleanQuery q = new BooleanQuery();

            q.Add(new TermQuery(new Term("all", "all")), BooleanClause.Occur.MUST);    //true,  false);
            q.Add(new TermQuery(new Term("data", "6")), BooleanClause.Occur.MUST);     //true,  false);
            q.Add(new TermQuery(new Term("data", "5")), BooleanClause.Occur.SHOULD);   //false, false);
            q.Add(new TermQuery(new Term("data", "4")), BooleanClause.Occur.SHOULD);   //false, false);
            q.Add(new TermQuery(new Term("data", "3")), BooleanClause.Occur.MUST_NOT); //false, true );
            q.Add(new TermQuery(new Term("data", "2")), BooleanClause.Occur.SHOULD);   //false, false);
            q.Add(new TermQuery(new Term("data", "1")), BooleanClause.Occur.SHOULD);   //false, false);
            q.Add(new TermQuery(new Term("data", "C")), BooleanClause.Occur.MUST_NOT); //false, true );

            q.SetMinimumNumberShouldMatch(90);                                         // 90 of 4 optional ?!?!?!

            VerifyNrHits(q, 0);
        }
		public virtual void  Test1()
		{
			
			BooleanQuery q = new BooleanQuery();
			
			q.Add(qp.Parse("\"w1 w2\"~1"), Occur.MUST);
			q.Add(Snear(St("w2"), Sor("w5", "zz"), 4, true), Occur.SHOULD);
			q.Add(Snear(Sf("w3", 2), St("w2"), St("w3"), 5, true), Occur.SHOULD);
			
			Query t = new FilteredQuery(qp.Parse("xx"), new ItemizedFilter(new int[]{1, 3}));
			t.SetBoost(1000);
			q.Add(t, Occur.SHOULD);
			
			t = new ConstantScoreQuery(new ItemizedFilter(new int[]{0, 2}));
			t.SetBoost(30);
			q.Add(t, Occur.SHOULD);
			
			DisjunctionMaxQuery dm = new DisjunctionMaxQuery(0.2f);
			dm.Add(Snear(St("w2"), Sor("w5", "zz"), 4, true));
			dm.Add(qp.Parse("QQ"));
			dm.Add(qp.Parse("xx yy -zz"));
			dm.Add(qp.Parse("-xx -w1"));
			
			DisjunctionMaxQuery dm2 = new DisjunctionMaxQuery(0.5f);
			dm2.Add(qp.Parse("w1"));
			dm2.Add(qp.Parse("w2"));
			dm2.Add(qp.Parse("w3"));
			dm.Add(dm2);
			
			q.Add(dm, Occur.SHOULD);
			
			BooleanQuery b = new BooleanQuery();
			b.SetMinimumNumberShouldMatch(2);
			b.Add(Snear("w1", "w2", 1, true), Occur.SHOULD);
			b.Add(Snear("w2", "w3", 1, true), Occur.SHOULD);
			b.Add(Snear("w1", "w3", 3, true), Occur.SHOULD);
			
			q.Add(b, Occur.SHOULD);
			
			Qtest(q, new int[]{0, 1, 2});
		}
		public virtual void  TestOneOptionalEqualToMin()
		{
			
			/* two required, one optional */
			BooleanQuery q = new BooleanQuery();
			q.Add(new TermQuery(new Term("all", "all")), BooleanClause.Occur.MUST); //true,  false);
			q.Add(new TermQuery(new Term("data", "3")), BooleanClause.Occur.SHOULD); //false, false);
			q.Add(new TermQuery(new Term("data", "2")), BooleanClause.Occur.MUST); //true,  false);
			
			q.SetMinimumNumberShouldMatch(1); // 1 of 1 optional 
			
			VerifyNrHits(q, 1);
		}
		public virtual void  TestNoOptionalButMin2()
		{
			
			/* one required, no optional */
			BooleanQuery q = new BooleanQuery();
			q.Add(new TermQuery(new Term("all", "all")), BooleanClause.Occur.MUST); //true,  false);
			
			q.SetMinimumNumberShouldMatch(1); // 1 of 0 optional 
			
			VerifyNrHits(q, 0);
		}
		public virtual void  TestSomeReqSomeProhibAndSomeOptional()
		{
			
			/* two required, two prohibited, some optional */
			BooleanQuery q = new BooleanQuery();
			q.Add(new TermQuery(new Term("all", "all")), BooleanClause.Occur.MUST); //true,  false);
			q.Add(new TermQuery(new Term("data", "6")), BooleanClause.Occur.MUST); //true,  false);
			q.Add(new TermQuery(new Term("data", "5")), BooleanClause.Occur.SHOULD); //false, false);
			q.Add(new TermQuery(new Term("data", "4")), BooleanClause.Occur.SHOULD); //false, false);
			q.Add(new TermQuery(new Term("data", "3")), BooleanClause.Occur.MUST_NOT); //false, true );
			q.Add(new TermQuery(new Term("data", "2")), BooleanClause.Occur.SHOULD); //false, false);
			q.Add(new TermQuery(new Term("data", "1")), BooleanClause.Occur.SHOULD); //false, false);
			q.Add(new TermQuery(new Term("data", "C")), BooleanClause.Occur.MUST_NOT); //false, true );
			
			q.SetMinimumNumberShouldMatch(3); // 3 of 4 optional 
			
			VerifyNrHits(q, 1);
		}
		public virtual void  TestMinHigherThenNumOptional()
		{
			
			/* two required, two prohibited, some optional */
			BooleanQuery q = new BooleanQuery();
			q.Add(new TermQuery(new Term("all", "all")), BooleanClause.Occur.MUST); //true,  false);
			q.Add(new TermQuery(new Term("data", "6")), BooleanClause.Occur.MUST); //true,  false);
			q.Add(new TermQuery(new Term("data", "5")), BooleanClause.Occur.SHOULD); //false, false);
			q.Add(new TermQuery(new Term("data", "4")), BooleanClause.Occur.SHOULD); //false, false);
			q.Add(new TermQuery(new Term("data", "3")), BooleanClause.Occur.MUST_NOT); //false, true );
			q.Add(new TermQuery(new Term("data", "2")), BooleanClause.Occur.SHOULD); //false, false);
			q.Add(new TermQuery(new Term("data", "1")), BooleanClause.Occur.SHOULD); //false, false);
			q.Add(new TermQuery(new Term("data", "C")), BooleanClause.Occur.MUST_NOT); //false, true );
			
			q.SetMinimumNumberShouldMatch(90); // 90 of 4 optional ?!?!?!
			
			VerifyNrHits(q, 0);
		}
		public virtual void  TestOneProhibAndSomeOptional()
		{
			
			/* one prohibited, some optional */
			BooleanQuery q = new BooleanQuery();
			q.Add(new TermQuery(new Term("data", "1")), BooleanClause.Occur.SHOULD); //false, false);
			q.Add(new TermQuery(new Term("data", "2")), BooleanClause.Occur.SHOULD); //false, false);
			q.Add(new TermQuery(new Term("data", "3")), BooleanClause.Occur.MUST_NOT); //false, true );
			q.Add(new TermQuery(new Term("data", "4")), BooleanClause.Occur.SHOULD); //false, false);
			
			q.SetMinimumNumberShouldMatch(2); // 2 of 3 optional 
			
			VerifyNrHits(q, 1);
		}
		public virtual void  TestSomeReqAndSomeOptional()
		{
			
			/* two required, some optional */
			BooleanQuery q = new BooleanQuery();
			q.Add(new TermQuery(new Term("all", "all")), BooleanClause.Occur.MUST); //true,  false);
			q.Add(new TermQuery(new Term("data", "6")), BooleanClause.Occur.MUST); //true,  false);
			q.Add(new TermQuery(new Term("data", "5")), BooleanClause.Occur.SHOULD); //false, false);
			q.Add(new TermQuery(new Term("data", "4")), BooleanClause.Occur.SHOULD); //false, false);
			q.Add(new TermQuery(new Term("data", "3")), BooleanClause.Occur.SHOULD); //false, false);
			
			q.SetMinimumNumberShouldMatch(2); // 2 of 3 optional 
			
			VerifyNrHits(q, 5);
		}
		public virtual void  TestAllOptional()
		{
			
			BooleanQuery q = new BooleanQuery();
			for (int i = 1; i <= 4; i++)
			{
				q.Add(new TermQuery(new Term("data", "" + i)), BooleanClause.Occur.SHOULD); //false, false);
			}
			q.SetMinimumNumberShouldMatch(2); // match at least two of 4
			VerifyNrHits(q, 2);
		}
Beispiel #22
0
		public virtual void  TestOutOfOrderDocsScoringSort()
		{
			
			// Two Sort criteria to instantiate the multi/single comparators.
			Sort[] sort = new Sort[]{new Sort(SortField.FIELD_DOC), new Sort()};
			bool[][] tfcOptions = new bool[][]{new bool[]{false, false, false}, new bool[]{false, false, true}, new bool[]{false, true, false}, new bool[]{false, true, true}, new bool[]{true, false, false}, new bool[]{true, false, true}, new bool[]{true, true, false}, new bool[]{true, true, true}};
			System.String[] actualTFCClasses = new System.String[]{"OutOfOrderOneComparatorNonScoringCollector", "OutOfOrderOneComparatorScoringMaxScoreCollector", "OutOfOrderOneComparatorScoringNoMaxScoreCollector", "OutOfOrderOneComparatorScoringMaxScoreCollector", "OutOfOrderOneComparatorNonScoringCollector", "OutOfOrderOneComparatorScoringMaxScoreCollector", "OutOfOrderOneComparatorScoringNoMaxScoreCollector", "OutOfOrderOneComparatorScoringMaxScoreCollector"};
			
			// Save the original value to set later.
			bool origVal = BooleanQuery.GetAllowDocsOutOfOrder();
			
			BooleanQuery.SetAllowDocsOutOfOrder(true);
			
			BooleanQuery bq = new BooleanQuery();
			// Add a Query with SHOULD, since bw.scorer() returns BooleanScorer2
			// which delegates to BS if there are no mandatory clauses.
			bq.Add(new MatchAllDocsQuery(), Occur.SHOULD);
			// Set minNrShouldMatch to 1 so that BQ will not optimize rewrite to return
			// the clause instead of BQ.
			bq.SetMinimumNumberShouldMatch(1);
			try
			{
				for (int i = 0; i < sort.Length; i++)
				{
					for (int j = 0; j < tfcOptions.Length; j++)
					{
                        TopFieldCollector tdc = TopFieldCollector.Create(sort[i], 10, tfcOptions[j][0], tfcOptions[j][1], tfcOptions[j][2], false);
						
						Assert.IsTrue(tdc.GetType().FullName.EndsWith("+" + actualTFCClasses[j]));
						
						full.Search(bq, tdc);
						
						TopDocs td = tdc.TopDocs();
						ScoreDoc[] sd = td.ScoreDocs;
						Assert.AreEqual(10, sd.Length);
					}
				}
			}
			finally
			{
				// Whatever happens, reset BooleanQuery.allowDocsOutOfOrder to the
				// original value. Don't set it to false in case the implementation in BQ
				// will change some day.
				BooleanQuery.SetAllowDocsOutOfOrder(origVal);
			}
		}
		public virtual void  TestBQ20()
		{
			BooleanQuery q = new BooleanQuery();
			q.SetMinimumNumberShouldMatch(2);
			q.Add(qp.Parse("QQQQQ"), BooleanClause.Occur.SHOULD);
			q.Add(qp.Parse("yy"), BooleanClause.Occur.SHOULD);
			q.Add(qp.Parse("zz"), BooleanClause.Occur.SHOULD);
			q.Add(qp.Parse("w5"), BooleanClause.Occur.SHOULD);
			q.Add(qp.Parse("w4"), BooleanClause.Occur.SHOULD);
			
			Qtest(q, new int[]{0, 3});
		}