You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ar...@apache.org on 2006/06/04 04:41:25 UTC
svn commit: r411501 [27/30] - in /incubator/lucene.net/trunk/C#/src: ./
Demo/DeleteFiles/ Demo/DemoLib/ Demo/DemoLib/HTML/ Demo/IndexFiles/
Demo/IndexHtml/ Demo/SearchFiles/ Lucene.Net/ Lucene.Net/Analysis/
Lucene.Net/Analysis/Standard/ Lucene.Net/Docu...
Modified: incubator/lucene.net/trunk/C#/src/Test/Search/Spans/TestBasics.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Search/Spans/TestBasics.cs?rev=411501&r1=411500&r2=411501&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Search/Spans/TestBasics.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Search/Spans/TestBasics.cs Sat Jun 3 19:41:13 2006
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
using System;
using NUnit.Framework;
using SimpleAnalyzer = Lucene.Net.Analysis.SimpleAnalyzer;
@@ -24,6 +25,7 @@
using Searchable = Lucene.Net.Search.Searchable;
using RAMDirectory = Lucene.Net.Store.RAMDirectory;
using English = Lucene.Net.Util.English;
+
namespace Lucene.Net.Search.Spans
{
@@ -41,20 +43,20 @@
/// <author> Doug Cutting
/// </author>
[TestFixture]
- public class TestBasics
+ public class TestBasics
{
private IndexSearcher searcher;
- [TestFixtureSetUp]
- public virtual void SetUp()
+ [TestFixtureSetUp]
+ public virtual void SetUp()
{
RAMDirectory directory = new RAMDirectory();
IndexWriter writer = new IndexWriter(directory, new SimpleAnalyzer(), true);
//writer.infoStream = System.out;
for (int i = 0; i < 1000; i++)
{
- Document doc = new Document();
- doc.Add(Field.Text("Field", English.IntToEnglish(i)));
+ Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
+ doc.Add(new Field("field", English.IntToEnglish(i), Field.Store.YES, Field.Index.TOKENIZED));
writer.AddDocument(doc);
}
@@ -63,61 +65,61 @@
searcher = new IndexSearcher(directory);
}
- [Test]
- public virtual void TestTerm()
+ [Test]
+ public virtual void TestTerm()
{
- Query query = new TermQuery(new Term("Field", "seventy"));
+ Query query = new TermQuery(new Term("field", "seventy"));
CheckHits(query, new int[]{70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979});
}
- [Test]
- public virtual void TestTerm2()
+ [Test]
+ public virtual void TestTerm2()
{
- Query query = new TermQuery(new Term("Field", "seventish"));
+ Query query = new TermQuery(new Term("field", "seventish"));
CheckHits(query, new int[]{});
}
- [Test]
- public virtual void TestPhrase()
+ [Test]
+ public virtual void TestPhrase()
{
PhraseQuery query = new PhraseQuery();
- query.Add(new Term("Field", "seventy"));
- query.Add(new Term("Field", "seven"));
+ query.Add(new Term("field", "seventy"));
+ query.Add(new Term("field", "seven"));
CheckHits(query, new int[]{77, 177, 277, 377, 477, 577, 677, 777, 877, 977});
}
- [Test]
- public virtual void TestPhrase2()
+ [Test]
+ public virtual void TestPhrase2()
{
PhraseQuery query = new PhraseQuery();
- query.Add(new Term("Field", "seventish"));
- query.Add(new Term("Field", "sevenon"));
+ query.Add(new Term("field", "seventish"));
+ query.Add(new Term("field", "sevenon"));
CheckHits(query, new int[]{});
}
- [Test]
- public virtual void TestBoolean()
+ [Test]
+ public virtual void TestBoolean()
{
BooleanQuery query = new BooleanQuery();
- query.Add(new TermQuery(new Term("Field", "seventy")), true, false);
- query.Add(new TermQuery(new Term("Field", "seven")), true, false);
+ query.Add(new TermQuery(new Term("field", "seventy")), BooleanClause.Occur.MUST);
+ query.Add(new TermQuery(new Term("field", "seven")), BooleanClause.Occur.MUST);
CheckHits(query, new int[]{77, 777, 177, 277, 377, 477, 577, 677, 770, 771, 772, 773, 774, 775, 776, 778, 779, 877, 977});
}
- [Test]
- public virtual void TestBoolean2()
+ [Test]
+ public virtual void TestBoolean2()
{
BooleanQuery query = new BooleanQuery();
- query.Add(new TermQuery(new Term("Field", "sevento")), true, false);
- query.Add(new TermQuery(new Term("Field", "sevenly")), true, false);
+ query.Add(new TermQuery(new Term("field", "sevento")), BooleanClause.Occur.MUST);
+ query.Add(new TermQuery(new Term("field", "sevenly")), BooleanClause.Occur.MUST);
CheckHits(query, new int[]{});
}
- [Test]
- public virtual void TestSpanNearExact()
+ [Test]
+ public virtual void TestSpanNearExact()
{
- SpanTermQuery term1 = new SpanTermQuery(new Term("Field", "seventy"));
- SpanTermQuery term2 = new SpanTermQuery(new Term("Field", "seven"));
+ SpanTermQuery term1 = new SpanTermQuery(new Term("field", "seventy"));
+ SpanTermQuery term2 = new SpanTermQuery(new Term("field", "seven"));
SpanNearQuery query = new SpanNearQuery(new SpanQuery[]{term1, term2}, 0, true);
CheckHits(query, new int[]{77, 177, 277, 377, 477, 577, 677, 777, 877, 977});
@@ -125,32 +127,32 @@
Assert.IsTrue(searcher.Explain(query, 977).GetValue() > 0.0f);
}
- [Test]
- public virtual void TestSpanNearUnordered()
+ [Test]
+ public virtual void TestSpanNearUnordered()
{
- SpanTermQuery term1 = new SpanTermQuery(new Term("Field", "nine"));
- SpanTermQuery term2 = new SpanTermQuery(new Term("Field", "six"));
+ SpanTermQuery term1 = new SpanTermQuery(new Term("field", "nine"));
+ SpanTermQuery term2 = new SpanTermQuery(new Term("field", "six"));
SpanNearQuery query = new SpanNearQuery(new SpanQuery[]{term1, term2}, 4, false);
CheckHits(query, new int[]{609, 629, 639, 649, 659, 669, 679, 689, 699, 906, 926, 936, 946, 956, 966, 976, 986, 996});
}
- [Test]
- public virtual void TestSpanNearOrdered()
+ [Test]
+ public virtual void TestSpanNearOrdered()
{
- SpanTermQuery term1 = new SpanTermQuery(new Term("Field", "nine"));
- SpanTermQuery term2 = new SpanTermQuery(new Term("Field", "six"));
+ SpanTermQuery term1 = new SpanTermQuery(new Term("field", "nine"));
+ SpanTermQuery term2 = new SpanTermQuery(new Term("field", "six"));
SpanNearQuery query = new SpanNearQuery(new SpanQuery[]{term1, term2}, 4, true);
CheckHits(query, new int[]{906, 926, 936, 946, 956, 966, 976, 986, 996});
}
- [Test]
- public virtual void TestSpanNot()
+ [Test]
+ public virtual void TestSpanNot()
{
- SpanTermQuery term1 = new SpanTermQuery(new Term("Field", "eight"));
- SpanTermQuery term2 = new SpanTermQuery(new Term("Field", "one"));
+ SpanTermQuery term1 = new SpanTermQuery(new Term("field", "eight"));
+ SpanTermQuery term2 = new SpanTermQuery(new Term("field", "one"));
SpanNearQuery near = new SpanNearQuery(new SpanQuery[]{term1, term2}, 4, true);
- SpanTermQuery term3 = new SpanTermQuery(new Term("Field", "forty"));
+ SpanTermQuery term3 = new SpanTermQuery(new Term("field", "forty"));
SpanNotQuery query = new SpanNotQuery(near, term3);
CheckHits(query, new int[]{801, 821, 831, 851, 861, 871, 881, 891});
@@ -159,10 +161,10 @@
Assert.IsTrue(searcher.Explain(query, 891).GetValue() > 0.0f);
}
- [Test]
- public virtual void TestSpanFirst()
+ [Test]
+ public virtual void TestSpanFirst()
{
- SpanTermQuery term1 = new SpanTermQuery(new Term("Field", "five"));
+ SpanTermQuery term1 = new SpanTermQuery(new Term("field", "five"));
SpanFirstQuery query = new SpanFirstQuery(term1, 1);
CheckHits(query, new int[]{5, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599});
@@ -171,14 +173,14 @@
Assert.IsTrue(searcher.Explain(query, 599).GetValue() > 0.0f);
}
- [Test]
- public virtual void TestSpanOr()
+ [Test]
+ public virtual void TestSpanOr()
{
- SpanTermQuery term1 = new SpanTermQuery(new Term("Field", "thirty"));
- SpanTermQuery term2 = new SpanTermQuery(new Term("Field", "three"));
+ SpanTermQuery term1 = new SpanTermQuery(new Term("field", "thirty"));
+ SpanTermQuery term2 = new SpanTermQuery(new Term("field", "three"));
SpanNearQuery near1 = new SpanNearQuery(new SpanQuery[]{term1, term2}, 0, true);
- SpanTermQuery term3 = new SpanTermQuery(new Term("Field", "forty"));
- SpanTermQuery term4 = new SpanTermQuery(new Term("Field", "seven"));
+ SpanTermQuery term3 = new SpanTermQuery(new Term("field", "forty"));
+ SpanTermQuery term4 = new SpanTermQuery(new Term("field", "seven"));
SpanNearQuery near2 = new SpanNearQuery(new SpanQuery[]{term3, term4}, 0, true);
SpanOrQuery query = new SpanOrQuery(new SpanQuery[]{near1, near2});
@@ -189,14 +191,14 @@
Assert.IsTrue(searcher.Explain(query, 947).GetValue() > 0.0f);
}
- [Test]
- public virtual void TestSpanExactNested()
+ [Test]
+ public virtual void TestSpanExactNested()
{
- SpanTermQuery term1 = new SpanTermQuery(new Term("Field", "three"));
- SpanTermQuery term2 = new SpanTermQuery(new Term("Field", "hundred"));
+ SpanTermQuery term1 = new SpanTermQuery(new Term("field", "three"));
+ SpanTermQuery term2 = new SpanTermQuery(new Term("field", "hundred"));
SpanNearQuery near1 = new SpanNearQuery(new SpanQuery[]{term1, term2}, 0, true);
- SpanTermQuery term3 = new SpanTermQuery(new Term("Field", "thirty"));
- SpanTermQuery term4 = new SpanTermQuery(new Term("Field", "three"));
+ SpanTermQuery term3 = new SpanTermQuery(new Term("field", "thirty"));
+ SpanTermQuery term4 = new SpanTermQuery(new Term("field", "three"));
SpanNearQuery near2 = new SpanNearQuery(new SpanQuery[]{term3, term4}, 0, true);
SpanNearQuery query = new SpanNearQuery(new SpanQuery[]{near1, near2}, 0, true);
@@ -206,15 +208,15 @@
Assert.IsTrue(searcher.Explain(query, 333).GetValue() > 0.0f);
}
- [Test]
- public virtual void TestSpanNearOr()
+ [Test]
+ public virtual void TestSpanNearOr()
{
- SpanTermQuery t1 = new SpanTermQuery(new Term("Field", "six"));
- SpanTermQuery t3 = new SpanTermQuery(new Term("Field", "seven"));
+ SpanTermQuery t1 = new SpanTermQuery(new Term("field", "six"));
+ SpanTermQuery t3 = new SpanTermQuery(new Term("field", "seven"));
- SpanTermQuery t5 = new SpanTermQuery(new Term("Field", "seven"));
- SpanTermQuery t6 = new SpanTermQuery(new Term("Field", "six"));
+ SpanTermQuery t5 = new SpanTermQuery(new Term("field", "seven"));
+ SpanTermQuery t6 = new SpanTermQuery(new Term("field", "six"));
SpanOrQuery to1 = new SpanOrQuery(new SpanQuery[]{t1, t3});
SpanOrQuery to2 = new SpanOrQuery(new SpanQuery[]{t5, t6});
@@ -224,20 +226,20 @@
CheckHits(query, new int[]{606, 607, 626, 627, 636, 637, 646, 647, 656, 657, 666, 667, 676, 677, 686, 687, 696, 697, 706, 707, 726, 727, 736, 737, 746, 747, 756, 757, 766, 767, 776, 777, 786, 787, 796, 797});
}
- [Test]
- public virtual void TestSpanComplex1()
+ [Test]
+ public virtual void TestSpanComplex1()
{
- SpanTermQuery t1 = new SpanTermQuery(new Term("Field", "six"));
- SpanTermQuery t2 = new SpanTermQuery(new Term("Field", "hundred"));
+ SpanTermQuery t1 = new SpanTermQuery(new Term("field", "six"));
+ SpanTermQuery t2 = new SpanTermQuery(new Term("field", "hundred"));
SpanNearQuery tt1 = new SpanNearQuery(new SpanQuery[]{t1, t2}, 0, true);
- SpanTermQuery t3 = new SpanTermQuery(new Term("Field", "seven"));
- SpanTermQuery t4 = new SpanTermQuery(new Term("Field", "hundred"));
+ SpanTermQuery t3 = new SpanTermQuery(new Term("field", "seven"));
+ SpanTermQuery t4 = new SpanTermQuery(new Term("field", "hundred"));
SpanNearQuery tt2 = new SpanNearQuery(new SpanQuery[]{t3, t4}, 0, true);
- SpanTermQuery t5 = new SpanTermQuery(new Term("Field", "seven"));
- SpanTermQuery t6 = new SpanTermQuery(new Term("Field", "six"));
+ SpanTermQuery t5 = new SpanTermQuery(new Term("field", "seven"));
+ SpanTermQuery t6 = new SpanTermQuery(new Term("field", "six"));
SpanOrQuery to1 = new SpanOrQuery(new SpanQuery[]{tt1, tt2});
SpanOrQuery to2 = new SpanOrQuery(new SpanQuery[]{t5, t6});
@@ -250,7 +252,7 @@
private void CheckHits(Query query, int[] results)
{
- Lucene.Net.Search.CheckHits.CheckHits_(query, "Field", searcher, results, null);
+ Lucene.Net.Search.CheckHits.CheckHits_Renamed_Method(query, "field", searcher, results);
}
}
}
Modified: incubator/lucene.net/trunk/C#/src/Test/Search/Spans/TestSpans.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Search/Spans/TestSpans.cs?rev=411501&r1=411500&r2=411501&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Search/Spans/TestSpans.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Search/Spans/TestSpans.cs Sat Jun 3 19:41:13 2006
@@ -1,5 +1,5 @@
/*
- * Copyright 2004 The Apache Software Foundation
+ * Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
using System;
using WhitespaceAnalyzer = Lucene.Net.Analysis.WhitespaceAnalyzer;
using Document = Lucene.Net.Documents.Document;
@@ -20,11 +21,11 @@
using IndexWriter = Lucene.Net.Index.IndexWriter;
using Term = Lucene.Net.Index.Term;
using CheckHits = Lucene.Net.Search.CheckHits;
-using Hits = Lucene.Net.Search.Hits;
using IndexSearcher = Lucene.Net.Search.IndexSearcher;
using Query = Lucene.Net.Search.Query;
using RAMDirectory = Lucene.Net.Store.RAMDirectory;
using NUnit.Framework;
+
namespace Lucene.Net.Search.Spans
{
[TestFixture]
@@ -32,74 +33,161 @@
{
private IndexSearcher searcher;
- public const System.String field = "Field";
+ public const System.String field = "field";
- [TestFixtureSetUp]
- public virtual void SetUp()
+ [TestFixtureSetUp]
+ public virtual void SetUp()
{
RAMDirectory directory = new RAMDirectory();
IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
- System.Text.StringBuilder buffer = new System.Text.StringBuilder();
for (int i = 0; i < docFields.Length; i++)
{
- Document doc = new Document();
- doc.Add(Field.Text(field, docFields[i]));
+ Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
+ doc.Add(new Field(field, docFields[i], Field.Store.YES, Field.Index.TOKENIZED));
writer.AddDocument(doc);
}
writer.Close();
searcher = new IndexSearcher(directory);
+ //System.out.println("set up " + getName());
}
- private System.String[] docFields = new System.String[]{"w1 w2 w3 w4 w5", "w1 w3 w2 w3", "w1 xx w2 yy w3", "w1 w3 xx w2 yy w3", ""};
+ private System.String[] docFields = new System.String[]{"w1 w2 w3 w4 w5", "w1 w3 w2 w3", "w1 xx w2 yy w3", "w1 w3 xx w2 yy w3", "u2 u2 u1", "u2 xx u2 u1", "u2 u2 xx u1", "u2 xx u2 yy u1", "u2 xx u1 u2", "u2 u1 xx u2", "u1 u2 xx u2", "t1 t2 t1 t3 t2 t3"};
- public virtual SpanTermQuery makeSpanTermQuery(System.String text)
+ public virtual SpanTermQuery MakeSpanTermQuery(System.String text)
{
return new SpanTermQuery(new Term(field, text));
}
private void CheckHits(Query query, int[] results)
{
- Lucene.Net.Search.CheckHits.CheckHits_(query, field, searcher, results, null);
+ Lucene.Net.Search.CheckHits.CheckHits_Renamed_Method(query, field, searcher, results);
}
- public virtual void OrderedSlopTest3(int slop, int[] expectedDocs)
+ private void OrderedSlopTest3SQ(SpanQuery q1, SpanQuery q2, SpanQuery q3, int slop, int[] expectedDocs)
{
- SpanTermQuery w1 = makeSpanTermQuery("w1");
- SpanTermQuery w2 = makeSpanTermQuery("w2");
- SpanTermQuery w3 = makeSpanTermQuery("w3");
bool ordered = true;
- SpanNearQuery snq = new SpanNearQuery(new SpanQuery[]{w1, w2, w3}, slop, ordered);
+ SpanNearQuery snq = new SpanNearQuery(new SpanQuery[]{q1, q2, q3}, slop, ordered);
CheckHits(snq, expectedDocs);
}
- [Test]
- public virtual void TestSpanNearOrdered01()
+ public virtual void OrderedSlopTest3(int slop, int[] expectedDocs)
+ {
+ OrderedSlopTest3SQ(MakeSpanTermQuery("w1"), MakeSpanTermQuery("w2"), MakeSpanTermQuery("w3"), slop, expectedDocs);
+ }
+
+ public virtual void OrderedSlopTest3Equal(int slop, int[] expectedDocs)
+ {
+ OrderedSlopTest3SQ(MakeSpanTermQuery("w1"), MakeSpanTermQuery("w3"), MakeSpanTermQuery("w3"), slop, expectedDocs);
+ }
+
+ public virtual void OrderedSlopTest1Equal(int slop, int[] expectedDocs)
+ {
+ OrderedSlopTest3SQ(MakeSpanTermQuery("u2"), MakeSpanTermQuery("u2"), MakeSpanTermQuery("u1"), slop, expectedDocs);
+ }
+
+ [Test]
+ public virtual void TestSpanNearOrdered01()
{
OrderedSlopTest3(0, new int[]{0});
}
- [Test]
- public virtual void TestSpanNearOrdered02()
+ [Test]
+ public virtual void TestSpanNearOrdered02()
{
OrderedSlopTest3(1, new int[]{0, 1});
}
- [Test]
- public virtual void TestSpanNearOrdered03()
+ [Test]
+ public virtual void TestSpanNearOrdered03()
{
OrderedSlopTest3(2, new int[]{0, 1, 2});
}
- [Test]
- public virtual void TestSpanNearOrdered04()
+ [Test]
+ public virtual void TestSpanNearOrdered04()
{
OrderedSlopTest3(3, new int[]{0, 1, 2, 3});
}
- [Test]
- public virtual void TestSpanNearOrdered05()
+ [Test]
+ public virtual void TestSpanNearOrdered05()
{
OrderedSlopTest3(4, new int[]{0, 1, 2, 3});
+ }
+
+ [Test]
+ public virtual void TestSpanNearOrderedEqual01()
+ {
+ OrderedSlopTest3Equal(0, new int[]{});
+ }
+
+ [Test]
+ public virtual void TestSpanNearOrderedEqual02()
+ {
+ OrderedSlopTest3Equal(1, new int[]{1});
+ }
+
+ [Test]
+ public virtual void TestSpanNearOrderedEqual03()
+ {
+ OrderedSlopTest3Equal(2, new int[]{1});
+ }
+
+ [Test]
+ public virtual void TestSpanNearOrderedEqual04()
+ {
+ OrderedSlopTest3Equal(3, new int[]{1, 3});
+ }
+
+ [Test]
+ public virtual void TestSpanNearOrderedEqual11()
+ {
+ OrderedSlopTest1Equal(0, new int[]{4});
+ }
+
+ [Test]
+ public virtual void TestSpanNearOrderedEqual12()
+ {
+ OrderedSlopTest1Equal(0, new int[]{4});
+ }
+
+ [Test]
+ public virtual void TestSpanNearOrderedEqual13()
+ {
+ OrderedSlopTest1Equal(1, new int[]{4, 5, 6});
+ }
+
+ [Test]
+ public virtual void TestSpanNearOrderedEqual14()
+ {
+ OrderedSlopTest1Equal(2, new int[]{4, 5, 6, 7});
+ }
+
+ [Test]
+ public virtual void TestSpanNearOrderedEqual15()
+ {
+ OrderedSlopTest1Equal(3, new int[]{4, 5, 6, 7});
+ }
+
+ [Test]
+ public virtual void TestSpanNearOrderedOverlap()
+ {
+ bool ordered = true;
+ int slop = 1;
+ SpanNearQuery snq = new SpanNearQuery(new SpanQuery[]{MakeSpanTermQuery("t1"), MakeSpanTermQuery("t2"), MakeSpanTermQuery("t3")}, slop, ordered);
+ Spans spans = snq.GetSpans(searcher.GetIndexReader());
+
+ Assert.IsTrue(spans.Next(), "first range");
+ Assert.AreEqual(11, spans.Doc(), "first doc");
+ Assert.AreEqual(0, spans.Start(), "first start");
+ Assert.AreEqual(4, spans.End(), "first end");
+
+ Assert.IsTrue(spans.Next(), "second range");
+ Assert.AreEqual(11, spans.Doc(), "second doc");
+ Assert.AreEqual(2, spans.Start(), "second start");
+ Assert.AreEqual(6, spans.End(), "second end");
+
+ Assert.IsFalse(spans.Next(), "third range");
}
}
}
Added: incubator/lucene.net/trunk/C#/src/Test/Search/Spans/TestSpansAdvanced.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Search/Spans/TestSpansAdvanced.cs?rev=411501&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Search/Spans/TestSpansAdvanced.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Test/Search/Spans/TestSpansAdvanced.cs Sat Jun 3 19:41:13 2006
@@ -0,0 +1,170 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+
+using System;
+using NUnit.Framework;
+using StandardAnalyzer = Lucene.Net.Analysis.Standard.StandardAnalyzer;
+using Document = Lucene.Net.Documents.Document;
+using Field = Lucene.Net.Documents.Field;
+using IndexWriter = Lucene.Net.Index.IndexWriter;
+using Term = Lucene.Net.Index.Term;
+using BooleanClause = Lucene.Net.Search.BooleanClause;
+using BooleanQuery = Lucene.Net.Search.BooleanQuery;
+using Hits = Lucene.Net.Search.Hits;
+using IndexSearcher = Lucene.Net.Search.IndexSearcher;
+using Query = Lucene.Net.Search.Query;
+using Directory = Lucene.Net.Store.Directory;
+using RAMDirectory = Lucene.Net.Store.RAMDirectory;
+
+namespace Lucene.Net.Search.Spans
+{
+
+ /// <summary>****************************************************************************
+ /// Tests the span query bug in Lucene. It demonstrates that SpanTermQuerys don't
+ /// work correctly in a BooleanQuery.
+ ///
+ /// </summary>
+ /// <author> Reece Wilton
+ /// </author>
+ [TestFixture]
+ public class TestSpansAdvanced
+ {
+
+ // location to the index
+ protected internal Directory mDirectory;
+
+ // field names in the index
+ private const System.String FIELD_ID = "ID";
+ protected internal const System.String FIELD_TEXT = "TEXT";
+
+ /// <summary> Initializes the tests by adding 4 identical documents to the index.</summary>
+ [TestFixtureSetUp]
+ public virtual void SetUp()
+ {
+
+ // create test index
+ mDirectory = new RAMDirectory();
+ IndexWriter writer = new IndexWriter(mDirectory, new StandardAnalyzer(), true);
+ AddDocument(writer, "1", "I think it should work.");
+ AddDocument(writer, "2", "I think it should work.");
+ AddDocument(writer, "3", "I think it should work.");
+ AddDocument(writer, "4", "I think it should work.");
+ writer.Close();
+ }
+
+ [TestFixtureTearDown]
+ public virtual void TearDown()
+ {
+
+ mDirectory.Close();
+ mDirectory = null;
+ }
+
+ /// <summary> Adds the document to the index.
+ ///
+ /// </summary>
+ /// <param name="writer">the Lucene index writer
+ /// </param>
+ /// <param name="id">the unique id of the document
+ /// </param>
+ /// <param name="text">the text of the document
+ /// </param>
+ /// <throws> IOException </throws>
+ protected internal virtual void AddDocument(IndexWriter writer, System.String id, System.String text)
+ {
+
+ Lucene.Net.Documents.Document document = new Lucene.Net.Documents.Document();
+ document.Add(new Field(FIELD_ID, id, Field.Store.YES, Field.Index.UN_TOKENIZED));
+ document.Add(new Field(FIELD_TEXT, text, Field.Store.YES, Field.Index.TOKENIZED));
+ writer.AddDocument(document);
+ }
+
+ /// <summary> Tests two span queries.
+ ///
+ /// </summary>
+ /// <throws> IOException </throws>
+ [Test]
+ public virtual void TestBooleanQueryWithSpanQueries()
+ {
+
+ DoTestBooleanQueryWithSpanQueries(0.3884282f);
+ }
+
+ /// <summary> Tests two span queries.
+ ///
+ /// </summary>
+ /// <throws> IOException </throws>
+ protected internal virtual void DoTestBooleanQueryWithSpanQueries(float expectedScore)
+ {
+
+ Query spanQuery = new SpanTermQuery(new Term(FIELD_TEXT, "work"));
+ BooleanQuery query = new BooleanQuery();
+ query.Add(spanQuery, BooleanClause.Occur.MUST);
+ query.Add(spanQuery, BooleanClause.Occur.MUST);
+ Hits hits = ExecuteQuery(query);
+ System.String[] expectedIds = new System.String[]{"1", "2", "3", "4"};
+ float[] expectedScores = new float[]{expectedScore, expectedScore, expectedScore, expectedScore};
+ AssertHits(hits, "two span queries", expectedIds, expectedScores);
+ }
+
+ /// <summary> Executes the query and throws an assertion if the results don't match the
+ /// expectedHits.
+ ///
+ /// </summary>
+ /// <param name="query">the query to execute
+ /// </param>
+ /// <throws> IOException </throws>
+ protected internal virtual Hits ExecuteQuery(Query query)
+ {
+
+ IndexSearcher searcher = new IndexSearcher(mDirectory);
+ Hits hits = searcher.Search(query);
+ searcher.Close();
+ return hits;
+ }
+
+ /// <summary> Checks to see if the hits are what we expected.
+ ///
+ /// </summary>
+ /// <param name="hits">the search results
+ /// </param>
+ /// <param name="description">the description of the search
+ /// </param>
+ /// <param name="expectedIds">the expected document ids of the hits
+ /// </param>
+ /// <param name="expectedScores">the expected scores of the hits
+ ///
+ /// </param>
+ /// <throws> IOException </throws>
+ protected internal virtual void AssertHits(Hits hits, System.String description, System.String[] expectedIds, float[] expectedScores)
+ {
+
+ // display the hits
+ /*System.out.println(hits.length() + " hits for search: \"" + description + '\"');
+ for (int i = 0; i < hits.length(); i++) {
+ System.out.println(" " + FIELD_ID + ':' + hits.doc(i).get(FIELD_ID) + " (score:" + hits.score(i) + ')');
+ }*/
+
+ // did we get the hits we expected
+ Assert.AreEqual(expectedIds.Length, hits.Length());
+ for (int i = 0; i < hits.Length(); i++)
+ {
+ Assert.IsTrue(expectedIds[i].Equals(hits.Doc(i).Get(FIELD_ID)));
+ Assert.AreEqual(expectedScores[i], hits.Score(i), 0);
+ }
+ }
+ }
+}
\ No newline at end of file
Added: incubator/lucene.net/trunk/C#/src/Test/Search/Spans/TestSpansAdvanced2.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Search/Spans/TestSpansAdvanced2.cs?rev=411501&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Search/Spans/TestSpansAdvanced2.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Test/Search/Spans/TestSpansAdvanced2.cs Sat Jun 3 19:41:13 2006
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+
+using System;
+using NUnit.Framework;
+using StandardAnalyzer = Lucene.Net.Analysis.Standard.StandardAnalyzer;
+using IndexReader = Lucene.Net.Index.IndexReader;
+using IndexWriter = Lucene.Net.Index.IndexWriter;
+using Term = Lucene.Net.Index.Term;
+using BooleanClause = Lucene.Net.Search.BooleanClause;
+using BooleanQuery = Lucene.Net.Search.BooleanQuery;
+using Hits = Lucene.Net.Search.Hits;
+using Query = Lucene.Net.Search.Query;
+
+namespace Lucene.Net.Search.Spans
+{
+
+ /// <summary>****************************************************************************
+ /// Some expanded tests to make sure my patch doesn't break other SpanTermQuery
+ /// functionality.
+ ///
+ /// </summary>
+ /// <author> Reece Wilton
+ /// </author>
+ public class TestSpansAdvanced2:TestSpansAdvanced
+ {
+
+ /// <summary> Initializes the tests by adding documents to the index.</summary>
+ [TestFixtureSetUp]
+ public virtual void SetUp()
+ {
+ base.SetUp();
+
+ // create test index
+ IndexWriter writer = new IndexWriter(mDirectory, new StandardAnalyzer(), false);
+ AddDocument(writer, "A", "Should we, could we, would we?");
+ AddDocument(writer, "B", "It should. Should it?");
+ AddDocument(writer, "C", "It shouldn't.");
+ AddDocument(writer, "D", "Should we, should we, should we.");
+ writer.Close();
+ }
+
+ /// <summary> Verifies that the index has the correct number of documents.
+ ///
+ /// </summary>
+ /// <throws> Exception </throws>
+ [Test]
+ public virtual void TestVerifyIndex()
+ {
+ IndexReader reader = IndexReader.Open(mDirectory);
+ Assert.AreEqual(8, reader.NumDocs());
+ reader.Close();
+ }
+
+ /// <summary> Tests a single span query that matches multiple documents.
+ ///
+ /// </summary>
+ /// <throws> IOException </throws>
+ [Test]
+ public virtual void TestSingleSpanQuery()
+ {
+
+ Query spanQuery = new SpanTermQuery(new Term(FIELD_TEXT, "should"));
+ Hits hits = ExecuteQuery(spanQuery);
+ System.String[] expectedIds = new System.String[]{"B", "D", "1", "2", "3", "4", "A"};
+ float[] expectedScores = new float[]{0.625f, 0.45927936f, 0.35355338f, 0.35355338f, 0.35355338f, 0.35355338f, 0.26516503f};
+ AssertHits(hits, "single span query", expectedIds, expectedScores);
+ }
+
+ /// <summary> Tests a single span query that matches multiple documents.
+ ///
+ /// </summary>
+ /// <throws> IOException </throws>
+ [Test]
+ public virtual void TestMultipleDifferentSpanQueries()
+ {
+
+ Query spanQuery1 = new SpanTermQuery(new Term(FIELD_TEXT, "should"));
+ Query spanQuery2 = new SpanTermQuery(new Term(FIELD_TEXT, "we"));
+ BooleanQuery query = new BooleanQuery();
+ query.Add(spanQuery1, BooleanClause.Occur.MUST);
+ query.Add(spanQuery2, BooleanClause.Occur.MUST);
+ Hits hits = ExecuteQuery(query);
+ System.String[] expectedIds = new System.String[]{"A", "D"};
+ float[] expectedScores = new float[]{0.93163157f, 0.20698164f};
+ AssertHits(hits, "multiple different span queries", expectedIds, expectedScores);
+ }
+
+ /// <summary> Tests two span queries.
+ ///
+ /// </summary>
+ /// <throws> IOException </throws>
+ [Test]
+ public override void TestBooleanQueryWithSpanQueries()
+ {
+
+ DoTestBooleanQueryWithSpanQueries(0.73500174f);
+ }
+ }
+}
\ No newline at end of file
Added: incubator/lucene.net/trunk/C#/src/Test/Search/TestBoolean2.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Search/TestBoolean2.cs?rev=411501&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Search/TestBoolean2.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Test/Search/TestBoolean2.cs Sat Jun 3 19:41:13 2006
@@ -0,0 +1,280 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+
+using System;
+using WhitespaceAnalyzer = Lucene.Net.Analysis.WhitespaceAnalyzer;
+using Document = Lucene.Net.Documents.Document;
+using Field = Lucene.Net.Documents.Field;
+using IndexWriter = Lucene.Net.Index.IndexWriter;
+using Term = Lucene.Net.Index.Term;
+using ParseException = Lucene.Net.QueryParsers.ParseException;
+using QueryParser = Lucene.Net.QueryParsers.QueryParser;
+using RAMDirectory = Lucene.Net.Store.RAMDirectory;
+using NUnit.Framework;
+
+namespace Lucene.Net.Search
+{
+
+
+ /// <summary>Test BooleanQuery2 against BooleanQuery by overriding the standard query parser.
+ /// This also tests the scoring order of BooleanQuery.
+ /// </summary>
+ [TestFixture]
+ public class TestBoolean2
+ {
+ [Serializable]
+ private class AnonymousClassDefaultSimilarity:DefaultSimilarity
+ {
+ public AnonymousClassDefaultSimilarity(TestBoolean2 enclosingInstance)
+ {
+ InitBlock(enclosingInstance);
+ }
+ private void InitBlock(TestBoolean2 enclosingInstance)
+ {
+ this.enclosingInstance = enclosingInstance;
+ }
+ private TestBoolean2 enclosingInstance;
+ public TestBoolean2 Enclosing_Instance
+ {
+ get
+ {
+ return enclosingInstance;
+ }
+
+ }
+ public override float Coord(int overlap, int maxOverlap)
+ {
+ return overlap / ((float) maxOverlap - 1);
+ }
+ }
+ private IndexSearcher searcher;
+
+ public const System.String field = "field";
+
+ [TestFixtureSetUp]
+ public virtual void SetUp()
+ {
+ RAMDirectory directory = new RAMDirectory();
+ IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
+ for (int i = 0; i < docFields.Length; i++)
+ {
+ Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
+ doc.Add(new Field(field, docFields[i], Field.Store.NO, Field.Index.TOKENIZED));
+ writer.AddDocument(doc);
+ }
+ writer.Close();
+ searcher = new IndexSearcher(directory);
+ }
+
+ private System.String[] docFields = new System.String[]{"w1 w2 w3 w4 w5", "w1 w3 w2 w3", "w1 xx w2 yy w3", "w1 w3 xx w2 yy w3"};
+
+ public virtual Query MakeQuery(System.String queryText)
+ {
+ return (new Lucene.Net.QueryParsers.QueryParser(field, new WhitespaceAnalyzer())).Parse(queryText);
+ }
+
+ public virtual void QueriesTest(System.String queryText, int[] expDocNrs)
+ {
+ //System.out.println();
+ //System.out.println("Query: " + queryText);
+ try
+ {
+ Query query1 = MakeQuery(queryText);
+ BooleanQuery.SetUseScorer14(true);
+ Hits hits1 = searcher.Search(query1);
+
+ Query query2 = MakeQuery(queryText); // there should be no need to parse again...
+ BooleanQuery.SetUseScorer14(false);
+ Hits hits2 = searcher.Search(query2);
+
+ CheckHits.CheckHitsQuery(query2, hits1, hits2, expDocNrs);
+ }
+ finally
+ {
+ // even when a test fails.
+ BooleanQuery.SetUseScorer14(false);
+ }
+ }
+
+ [Test]
+ public virtual void TestQueries01()
+ {
+ System.String queryText = "+w3 +xx";
+ int[] expDocNrs = new int[]{2, 3};
+ QueriesTest(queryText, expDocNrs);
+ }
+
+ [Test]
+ public virtual void TestQueries02()
+ {
+ System.String queryText = "+w3 xx";
+ int[] expDocNrs = new int[]{2, 3, 1, 0};
+ QueriesTest(queryText, expDocNrs);
+ }
+
+ [Test]
+ public virtual void TestQueries03()
+ {
+ System.String queryText = "w3 xx";
+ int[] expDocNrs = new int[]{2, 3, 1, 0};
+ QueriesTest(queryText, expDocNrs);
+ }
+
+ [Test]
+ public virtual void TestQueries04()
+ {
+ System.String queryText = "w3 -xx";
+ int[] expDocNrs = new int[]{1, 0};
+ QueriesTest(queryText, expDocNrs);
+ }
+
+ [Test]
+ public virtual void TestQueries05()
+ {
+ System.String queryText = "+w3 -xx";
+ int[] expDocNrs = new int[]{1, 0};
+ QueriesTest(queryText, expDocNrs);
+ }
+
+ [Test]
+ public virtual void TestQueries06()
+ {
+ System.String queryText = "+w3 -xx -w5";
+ int[] expDocNrs = new int[]{1};
+ QueriesTest(queryText, expDocNrs);
+ }
+
+ [Test]
+ public virtual void TestQueries07()
+ {
+ System.String queryText = "-w3 -xx -w5";
+ int[] expDocNrs = new int[]{};
+ QueriesTest(queryText, expDocNrs);
+ }
+
+ [Test]
+ public virtual void TestQueries08()
+ {
+ System.String queryText = "+w3 xx -w5";
+ int[] expDocNrs = new int[]{2, 3, 1};
+ QueriesTest(queryText, expDocNrs);
+ }
+
+ [Test]
+ public virtual void TestQueries09()
+ {
+ System.String queryText = "+w3 +xx +w2 zz";
+ int[] expDocNrs = new int[]{2, 3};
+ QueriesTest(queryText, expDocNrs);
+ }
+
+ [Test]
+ public virtual void TestQueries10()
+ {
+ System.String queryText = "+w3 +xx +w2 zz";
+ int[] expDocNrs = new int[]{2, 3};
+ searcher.SetSimilarity(new AnonymousClassDefaultSimilarity(this));
+ QueriesTest(queryText, expDocNrs);
+ }
+
+ [Test]
+ public virtual void TestRandomQueries()
+ {
+ System.Random rnd = new System.Random((System.Int32) 0);
+
+ System.String[] vals = new System.String[]{"w1", "w2", "w3", "w4", "w5", "xx", "yy", "zzz"};
+
+ int tot = 0;
+
+ try
+ {
+
+ // increase number of iterations for more complete testing
+ for (int i = 0; i < 1000; i++)
+ {
+ int level = rnd.Next(3);
+ BooleanQuery q1 = RandBoolQuery(new System.Random((System.Int32) i), level, field, vals, null);
+
+ // Can't sort by relevance since floating point numbers may not quite
+ // match up.
+ Sort sort = Sort.INDEXORDER;
+
+ BooleanQuery.SetUseScorer14(false);
+ Hits hits1 = searcher.Search(q1, sort);
+ if (hits1.Length() > 0)
+ hits1.Id(hits1.Length() - 1);
+
+ BooleanQuery.SetUseScorer14(true);
+ Hits hits2 = searcher.Search(q1, sort);
+ if (hits2.Length() > 0)
+ hits2.Id(hits1.Length() - 1);
+ tot += hits2.Length();
+ CheckHits.CheckEqual(q1, hits1, hits2);
+ }
+ }
+ finally
+ {
+ // even when a test fails.
+ BooleanQuery.SetUseScorer14(false);
+ }
+
+ // System.out.println("Total hits:"+tot);
+ }
+
+
+ // used to set properties or change every BooleanQuery
+ // generated from randBoolQuery.
+ public interface Callback
+ {
+ void PostCreate(BooleanQuery q);
+ }
+
+ // Random rnd is passed in so that the exact same random query may be created
+ // more than once.
+ public static BooleanQuery RandBoolQuery(System.Random rnd, int level, System.String field, System.String[] vals, TestBoolean2.Callback cb)
+ {
+ //UPGRADE_TODO: Method 'java.util.Random.nextInt' was converted to 'System.Random.Next' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
+ BooleanQuery current = new BooleanQuery(rnd.Next() < 0);
+ for (int i = 0; i < rnd.Next(vals.Length) + 1; i++)
+ {
+ int qType = 0; // term query
+ if (level > 0)
+ {
+ qType = rnd.Next(10);
+ }
+ Query q;
+ if (qType < 7)
+ q = new TermQuery(new Term(field, vals[rnd.Next(vals.Length)]));
+ else
+ q = RandBoolQuery(rnd, level - 1, field, vals, cb);
+
+ int r = rnd.Next(10);
+ BooleanClause.Occur occur;
+ if (r < 2)
+ occur = BooleanClause.Occur.MUST_NOT;
+ else if (r < 5)
+ occur = BooleanClause.Occur.MUST;
+ else
+ occur = BooleanClause.Occur.SHOULD;
+
+ current.Add(q, occur);
+ }
+ if (cb != null)
+ cb.PostCreate(current);
+ return current;
+ }
+ }
+}
\ No newline at end of file
Added: incubator/lucene.net/trunk/C#/src/Test/Search/TestBooleanMinShouldMatch.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Search/TestBooleanMinShouldMatch.cs?rev=411501&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Search/TestBooleanMinShouldMatch.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Test/Search/TestBooleanMinShouldMatch.cs Sat Jun 3 19:41:13 2006
@@ -0,0 +1,424 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+
+using System;
+using WhitespaceAnalyzer = Lucene.Net.Analysis.WhitespaceAnalyzer;
+using Document = Lucene.Net.Documents.Document;
+using Field = Lucene.Net.Documents.Field;
+using IndexReader = Lucene.Net.Index.IndexReader;
+using IndexWriter = Lucene.Net.Index.IndexWriter;
+using Term = Lucene.Net.Index.Term;
+using ParseException = Lucene.Net.QueryParsers.ParseException;
+using QueryParser = Lucene.Net.QueryParsers.QueryParser;
+using Directory = Lucene.Net.Store.Directory;
+using RAMDirectory = Lucene.Net.Store.RAMDirectory;
+using NUnit.Framework;
+
+namespace Lucene.Net.Search
+{
+
+
+ /// <summary>Test that BooleanQuery.setMinimumNumberShouldMatch works.</summary>
+ [TestFixture]
+ public class TestBooleanMinShouldMatch
+ {
+ private class AnonymousClassCallback : TestBoolean2.Callback
+ {
+ public AnonymousClassCallback(System.Random rnd, TestBooleanMinShouldMatch enclosingInstance)
+ {
+ InitBlock(rnd, enclosingInstance);
+ }
+ private void InitBlock(System.Random rnd, TestBooleanMinShouldMatch enclosingInstance)
+ {
+ this.rnd = rnd;
+ this.enclosingInstance = enclosingInstance;
+ }
+
+ private System.Random rnd;
+ private TestBooleanMinShouldMatch enclosingInstance;
+
+ public TestBooleanMinShouldMatch Enclosing_Instance
+ {
+ get
+ {
+ return enclosingInstance;
+ }
+
+ }
+ 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));
+ }
+ }
+
+
+ public Directory index;
+ public IndexReader r;
+ public IndexSearcher s;
+
+ [TestFixtureSetUp]
+ public virtual void SetUp()
+ {
+
+
+ System.String[] data = new System.String[]{"A 1 2 3 4 5 6", "Z 4 5 6", null, "B 2 4 5 6", "Y 3 5 6", null, "C 3 6", "X 4 5 6"};
+
+ index = new RAMDirectory();
+ IndexWriter writer = new IndexWriter(index, new WhitespaceAnalyzer(), true);
+
+ for (int i = 0; i < data.Length; i++)
+ {
+ Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
+ doc.Add(Field.Keyword("id", System.Convert.ToString(i)));
+ doc.Add(Field.Keyword("all", "all"));
+ if (null != data[i])
+ {
+ doc.Add(Field.Text("data", data[i]));
+ }
+ writer.AddDocument(doc);
+ }
+
+ writer.Optimize();
+ writer.Close();
+
+ r = IndexReader.Open(index);
+ s = new IndexSearcher(r);
+
+ //System.out.println("Set up " + getName());
+ }
+
+ public virtual void VerifyNrHits(Query q, int expected)
+ {
+ Hits h = s.Search(q);
+ if (expected != h.Length())
+ {
+ PrintHits("TestBooleanMinShouldMatch", h); // PrintHits(NUnit.Framework.TestCase.GetName(), h); // {{Aroush-1.9}} 'GetName()' gives us the name of the test in JUnit, how is it done in NUnit?
+ }
+ Assert.AreEqual(expected, h.Length(), "result count");
+ }
+
+ [Test]
+ public virtual void TestAllOptional()
+ {
+
+ BooleanQuery q = new BooleanQuery();
+ for (int i = 1; i <= 4; i++)
+ {
+ q.Add(new TermQuery(new Term("data", "" + i)), false, false);
+ }
+ q.SetMinimumNumberShouldMatch(2); // match at least two of 4
+ VerifyNrHits(q, 2);
+ }
+
+ [Test]
+ public virtual void TestOneReqAndSomeOptional()
+ {
+
+ /* one required, some optional */
+ BooleanQuery q = new BooleanQuery();
+ q.Add(new TermQuery(new Term("all", "all")), true, false);
+ q.Add(new TermQuery(new Term("data", "5")), false, false);
+ q.Add(new TermQuery(new Term("data", "4")), false, false);
+ q.Add(new TermQuery(new Term("data", "3")), false, false);
+
+ q.SetMinimumNumberShouldMatch(2); // 2 of 3 optional
+
+ VerifyNrHits(q, 5);
+ }
+
+ [Test]
+ public virtual void TestSomeReqAndSomeOptional()
+ {
+
+ /* two required, some optional */
+ BooleanQuery q = new BooleanQuery();
+ q.Add(new TermQuery(new Term("all", "all")), true, false);
+ q.Add(new TermQuery(new Term("data", "6")), true, false);
+ q.Add(new TermQuery(new Term("data", "5")), false, false);
+ q.Add(new TermQuery(new Term("data", "4")), false, false);
+ q.Add(new TermQuery(new Term("data", "3")), false, false);
+
+ q.SetMinimumNumberShouldMatch(2); // 2 of 3 optional
+
+ VerifyNrHits(q, 5);
+ }
+
+ [Test]
+ public virtual void TestOneProhibAndSomeOptional()
+ {
+
+ /* one prohibited, some optional */
+ BooleanQuery q = new BooleanQuery();
+ q.Add(new TermQuery(new Term("data", "1")), false, false);
+ q.Add(new TermQuery(new Term("data", "2")), false, false);
+ q.Add(new TermQuery(new Term("data", "3")), false, true);
+ q.Add(new TermQuery(new Term("data", "4")), false, false);
+
+ q.SetMinimumNumberShouldMatch(2); // 2 of 3 optional
+
+ VerifyNrHits(q, 1);
+ }
+
+ [Test]
+ public virtual void TestSomeProhibAndSomeOptional()
+ {
+
+ /* two prohibited, some optional */
+ BooleanQuery q = new BooleanQuery();
+ q.Add(new TermQuery(new Term("data", "1")), false, false);
+ q.Add(new TermQuery(new Term("data", "2")), false, false);
+ q.Add(new TermQuery(new Term("data", "3")), false, true);
+ q.Add(new TermQuery(new Term("data", "4")), false, false);
+ q.Add(new TermQuery(new Term("data", "C")), false, true);
+
+ q.SetMinimumNumberShouldMatch(2); // 2 of 3 optional
+
+ VerifyNrHits(q, 1);
+ }
+
+ [Test]
+ public virtual void TestOneReqOneProhibAndSomeOptional()
+ {
+
+ /* one required, one prohibited, some optional */
+ BooleanQuery q = new BooleanQuery();
+ q.Add(new TermQuery(new Term("data", "6")), true, false);
+ q.Add(new TermQuery(new Term("data", "5")), false, false);
+ q.Add(new TermQuery(new Term("data", "4")), false, false);
+ q.Add(new TermQuery(new Term("data", "3")), false, true);
+ q.Add(new TermQuery(new Term("data", "2")), false, false);
+ q.Add(new TermQuery(new Term("data", "1")), false, false);
+
+ q.SetMinimumNumberShouldMatch(3); // 3 of 4 optional
+
+ VerifyNrHits(q, 1);
+ }
+
+ [Test]
+ public virtual void TestSomeReqOneProhibAndSomeOptional()
+ {
+
+ /* two required, one prohibited, some optional */
+ BooleanQuery q = new BooleanQuery();
+ q.Add(new TermQuery(new Term("all", "all")), true, false);
+ q.Add(new TermQuery(new Term("data", "6")), true, false);
+ q.Add(new TermQuery(new Term("data", "5")), false, false);
+ q.Add(new TermQuery(new Term("data", "4")), false, false);
+ q.Add(new TermQuery(new Term("data", "3")), false, true);
+ q.Add(new TermQuery(new Term("data", "2")), false, false);
+ q.Add(new TermQuery(new Term("data", "1")), false, false);
+
+ q.SetMinimumNumberShouldMatch(3); // 3 of 4 optional
+
+ VerifyNrHits(q, 1);
+ }
+
+ [Test]
+ public virtual void TestOneReqSomeProhibAndSomeOptional()
+ {
+
+ /* one required, two prohibited, some optional */
+ BooleanQuery q = new BooleanQuery();
+ q.Add(new TermQuery(new Term("data", "6")), true, false);
+ q.Add(new TermQuery(new Term("data", "5")), false, false);
+ q.Add(new TermQuery(new Term("data", "4")), false, false);
+ q.Add(new TermQuery(new Term("data", "3")), false, true);
+ q.Add(new TermQuery(new Term("data", "2")), false, false);
+ q.Add(new TermQuery(new Term("data", "1")), false, false);
+ q.Add(new TermQuery(new Term("data", "C")), false, true);
+
+ q.SetMinimumNumberShouldMatch(3); // 3 of 4 optional
+
+ VerifyNrHits(q, 1);
+ }
+
+ [Test]
+ public virtual void TestSomeReqSomeProhibAndSomeOptional()
+ {
+
+ /* two required, two prohibited, some optional */
+ BooleanQuery q = new BooleanQuery();
+ q.Add(new TermQuery(new Term("all", "all")), true, false);
+ q.Add(new TermQuery(new Term("data", "6")), true, false);
+ q.Add(new TermQuery(new Term("data", "5")), false, false);
+ q.Add(new TermQuery(new Term("data", "4")), false, false);
+ q.Add(new TermQuery(new Term("data", "3")), false, true);
+ q.Add(new TermQuery(new Term("data", "2")), false, false);
+ q.Add(new TermQuery(new Term("data", "1")), false, false);
+ q.Add(new TermQuery(new Term("data", "C")), false, true);
+
+ q.SetMinimumNumberShouldMatch(3); // 3 of 4 optional
+
+ VerifyNrHits(q, 1);
+ }
+
+ [Test]
+ public virtual void TestMinHigherThenNumOptional()
+ {
+
+ /* two required, two prohibited, some optional */
+ BooleanQuery q = new BooleanQuery();
+ q.Add(new TermQuery(new Term("all", "all")), true, false);
+ q.Add(new TermQuery(new Term("data", "6")), true, false);
+ q.Add(new TermQuery(new Term("data", "5")), false, false);
+ q.Add(new TermQuery(new Term("data", "4")), false, false);
+ q.Add(new TermQuery(new Term("data", "3")), false, true);
+ q.Add(new TermQuery(new Term("data", "2")), false, false);
+ q.Add(new TermQuery(new Term("data", "1")), false, false);
+ q.Add(new TermQuery(new Term("data", "C")), false, true);
+
+ q.SetMinimumNumberShouldMatch(90); // 90 of 4 optional ?!?!?!
+
+ VerifyNrHits(q, 0);
+ }
+
+ [Test]
+ public virtual void TestMinEqualToNumOptional()
+ {
+
+ /* two required, two optional */
+ BooleanQuery q = new BooleanQuery();
+ q.Add(new TermQuery(new Term("all", "all")), false, false);
+ q.Add(new TermQuery(new Term("data", "6")), true, false);
+ q.Add(new TermQuery(new Term("data", "3")), true, false);
+ q.Add(new TermQuery(new Term("data", "2")), false, false);
+
+ q.SetMinimumNumberShouldMatch(2); // 2 of 2 optional
+
+ VerifyNrHits(q, 1);
+ }
+
+ [Test]
+ public virtual void TestOneOptionalEqualToMin()
+ {
+
+ /* two required, one optional */
+ BooleanQuery q = new BooleanQuery();
+ q.Add(new TermQuery(new Term("all", "all")), true, false);
+ q.Add(new TermQuery(new Term("data", "3")), false, false);
+ q.Add(new TermQuery(new Term("data", "2")), true, false);
+
+ q.SetMinimumNumberShouldMatch(1); // 1 of 1 optional
+
+ VerifyNrHits(q, 1);
+ }
+
+ [Test]
+ public virtual void TestNoOptionalButMin()
+ {
+
+ /* two required, no optional */
+ BooleanQuery q = new BooleanQuery();
+ q.Add(new TermQuery(new Term("all", "all")), true, false);
+ q.Add(new TermQuery(new Term("data", "2")), true, false);
+
+ q.SetMinimumNumberShouldMatch(1); // 1 of 0 optional
+
+ VerifyNrHits(q, 0);
+ }
+
+ [Test]
+ public virtual void TestRandomQueries()
+ {
+ System.Random rnd = new System.Random((System.Int32) 0);
+
+ System.String field = "data";
+ System.String[] vals = new System.String[]{"1", "2", "3", "4", "5", "6", "A", "Z", "B", "Y", "Z", "X", "foo"};
+ int maxLev = 4;
+
+ // callback object to set a random setMinimumNumberShouldMatch
+ TestBoolean2.Callback minNrCB = new AnonymousClassCallback(rnd, this);
+
+
+ int tot = 0;
+ // increase number of iterations for more complete testing
+ for (int i = 0; i < 1000; i++)
+ {
+ int lev = rnd.Next(maxLev);
+ BooleanQuery q1 = TestBoolean2.RandBoolQuery(new System.Random((System.Int32) i), lev, field, vals, null);
+ // BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(i), lev, field, vals, minNrCB);
+ BooleanQuery q2 = TestBoolean2.RandBoolQuery(new System.Random((System.Int32) i), lev, field, vals, null);
+ // only set minimumNumberShouldMatch on the top level query since setting
+ // at a lower level can change the score.
+ minNrCB.PostCreate(q2);
+
+ // Can't use Hits because normalized scores will mess things
+ // up. The non-sorting version of search() that returns TopDocs
+ // will not normalize scores.
+ TopDocs top1 = s.Search(q1, null, 100);
+ TopDocs top2 = s.Search(q2, null, 100);
+ tot += top2.totalHits;
+
+ // The constrained query
+ // should be a superset to the unconstrained query.
+ if (top2.totalHits > top1.totalHits)
+ {
+ TestCase.Fail("Constrained results not a subset:\n" + CheckHits.TopdocsString(top1, 0, 0) + CheckHits.TopdocsString(top2, 0, 0) + "for query:" + q2.ToString());
+ }
+
+ for (int hit = 0; hit < top2.totalHits; hit++)
+ {
+ int id = top2.scoreDocs[hit].doc;
+ float score = top2.scoreDocs[hit].score;
+ bool found = false;
+ // find this doc in other hits
+ for (int other = 0; other < top1.totalHits; other++)
+ {
+ if (top1.scoreDocs[other].doc == id)
+ {
+ found = true;
+ float otherScore = top1.scoreDocs[other].score;
+ // check if scores match
+ if (System.Math.Abs(otherScore - score) > 1.0e-6f)
+ {
+ TestCase.Fail("Doc " + id + " scores don't match\n" + CheckHits.TopdocsString(top1, 0, 0) + CheckHits.TopdocsString(top2, 0, 0) + "for query:" + q2.ToString());
+ }
+ }
+ }
+
+ // check if subset
+ if (!found)
+ {
+ TestCase.Fail("Doc " + id + " not found\n" + CheckHits.TopdocsString(top1, 0, 0) + CheckHits.TopdocsString(top2, 0, 0) + "for query:" + q2.ToString());
+ }
+ }
+ }
+ // System.out.println("Total hits:"+tot);
+ }
+
+
+
+ protected internal virtual void PrintHits(System.String test, Hits h)
+ {
+
+ System.Console.Error.WriteLine("------- " + test + " -------");
+
+ for (int i = 0; i < h.Length(); i++)
+ {
+ Lucene.Net.Documents.Document d = h.Doc(i);
+ float score = h.Score(i);
+ System.Console.Error.WriteLine("#" + i + ": {0.000000}" + score + " - " + d.Get("id") + " - " + d.Get("data"));
+ }
+ }
+ }
+}
\ No newline at end of file
Added: incubator/lucene.net/trunk/C#/src/Test/Search/TestBooleanOr.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Search/TestBooleanOr.cs?rev=411501&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Search/TestBooleanOr.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Test/Search/TestBooleanOr.cs Sat Jun 3 19:41:13 2006
@@ -0,0 +1,160 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+
+using System;
+using NUnit.Framework;
+using StandardAnalyzer = Lucene.Net.Analysis.Standard.StandardAnalyzer;
+using Document = Lucene.Net.Documents.Document;
+using Field = Lucene.Net.Documents.Field;
+using IndexWriter = Lucene.Net.Index.IndexWriter;
+using Term = Lucene.Net.Index.Term;
+using RAMDirectory = Lucene.Net.Store.RAMDirectory;
+
+namespace Lucene.Net.Search
+{
+
+ /// <summary> Created on 2005. 2. 9.
+ /// <br>Adapted to Lucene testcase by Paul Elschot.
+ /// </summary>
+ /// <author> appler@gmail.com
+ /// </author>
+ [TestFixture]
+ public class TestBooleanOr
+ {
+
+ private static System.String FIELD_T = "T";
+ private static System.String FIELD_C = "C";
+
+ private TermQuery t1 = new TermQuery(new Term(FIELD_T, "files"));
+ private TermQuery t2 = new TermQuery(new Term(FIELD_T, "deleting"));
+ private TermQuery c1 = new TermQuery(new Term(FIELD_C, "production"));
+ private TermQuery c2 = new TermQuery(new Term(FIELD_C, "optimize"));
+
+ private IndexSearcher searcher = null;
+
+ private int Search(Query q)
+ {
+ return searcher.Search(q).Length();
+ }
+
+ [Test]
+ public virtual void TestElements()
+ {
+ Assert.AreEqual(1, Search(t1));
+ Assert.AreEqual(1, Search(t2));
+ Assert.AreEqual(1, Search(c1));
+ Assert.AreEqual(1, Search(c2));
+ }
+
+ /// <summary> <code>T:files T:deleting C:production C:optimize </code>
+ /// it works.
+ ///
+ /// </summary>
+ /// <throws> IOException </throws>
+ [Test]
+ public virtual void TestFlat()
+ {
+ BooleanQuery q = new BooleanQuery();
+ q.Add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
+ q.Add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
+ q.Add(new BooleanClause(c1, BooleanClause.Occur.SHOULD));
+ q.Add(new BooleanClause(c2, BooleanClause.Occur.SHOULD));
+ Assert.AreEqual(1, Search(q));
+ }
+
+ /// <summary> <code>(T:files T:deleting) (+C:production +C:optimize)</code>
+ /// it works.
+ ///
+ /// </summary>
+ /// <throws> IOException </throws>
+ [Test]
+ public virtual void TestParenthesisMust()
+ {
+ BooleanQuery q3 = new BooleanQuery();
+ q3.Add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
+ q3.Add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
+ BooleanQuery q4 = new BooleanQuery();
+ q4.Add(new BooleanClause(c1, BooleanClause.Occur.MUST));
+ q4.Add(new BooleanClause(c2, BooleanClause.Occur.MUST));
+ BooleanQuery q2 = new BooleanQuery();
+ q2.Add(q3, BooleanClause.Occur.SHOULD);
+ q2.Add(q4, BooleanClause.Occur.SHOULD);
+ Assert.AreEqual(1, Search(q2));
+ }
+
+ /// <summary> <code>(T:files T:deleting) +(C:production C:optimize)</code>
+ /// not working. results NO HIT.
+ ///
+ /// </summary>
+ /// <throws> IOException </throws>
+ [Test]
+ public virtual void TestParenthesisMust2()
+ {
+ BooleanQuery q3 = new BooleanQuery();
+ q3.Add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
+ q3.Add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
+ BooleanQuery q4 = new BooleanQuery();
+ q4.Add(new BooleanClause(c1, BooleanClause.Occur.SHOULD));
+ q4.Add(new BooleanClause(c2, BooleanClause.Occur.SHOULD));
+ BooleanQuery q2 = new BooleanQuery();
+ q2.Add(q3, BooleanClause.Occur.SHOULD);
+ q2.Add(q4, BooleanClause.Occur.MUST);
+ Assert.AreEqual(1, Search(q2));
+ }
+
+ /// <summary> <code>(T:files T:deleting) (C:production C:optimize)</code>
+ /// not working. results NO HIT.
+ ///
+ /// </summary>
+ /// <throws> IOException </throws>
+ [Test]
+ public virtual void TestParenthesisShould()
+ {
+ BooleanQuery q3 = new BooleanQuery();
+ q3.Add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
+ q3.Add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
+ BooleanQuery q4 = new BooleanQuery();
+ q4.Add(new BooleanClause(c1, BooleanClause.Occur.SHOULD));
+ q4.Add(new BooleanClause(c2, BooleanClause.Occur.SHOULD));
+ BooleanQuery q2 = new BooleanQuery();
+ q2.Add(q3, BooleanClause.Occur.SHOULD);
+ q2.Add(q4, BooleanClause.Occur.SHOULD);
+ Assert.AreEqual(1, Search(q2));
+ }
+
+ [TestFixtureSetUp]
+ public virtual void SetUp()
+ {
+ //
+ RAMDirectory rd = new RAMDirectory();
+
+ //
+ IndexWriter writer = new IndexWriter(rd, new StandardAnalyzer(), true);
+
+ //
+ Lucene.Net.Documents.Document d = new Lucene.Net.Documents.Document();
+ d.Add(new Field(FIELD_T, "Optimize not deleting all files", Field.Store.YES, Field.Index.TOKENIZED));
+ d.Add(new Field(FIELD_C, "Deleted When I run an optimize in our production environment.", Field.Store.YES, Field.Index.TOKENIZED));
+
+ //
+ writer.AddDocument(d);
+ writer.Close();
+
+ //
+ searcher = new IndexSearcher(rd);
+ }
+ }
+}
\ No newline at end of file
Modified: incubator/lucene.net/trunk/C#/src/Test/Search/TestBooleanPrefixQuery.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Search/TestBooleanPrefixQuery.cs?rev=411501&r1=411500&r2=411501&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Search/TestBooleanPrefixQuery.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Search/TestBooleanPrefixQuery.cs Sat Jun 3 19:41:13 2006
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
using System;
using NUnit.Framework;
using WhitespaceAnalyzer = Lucene.Net.Analysis.WhitespaceAnalyzer;
@@ -22,12 +23,13 @@
using IndexWriter = Lucene.Net.Index.IndexWriter;
using Term = Lucene.Net.Index.Term;
using RAMDirectory = Lucene.Net.Store.RAMDirectory;
+
namespace Lucene.Net.Search
{
/// <author> schnee
/// </author>
- /// <version> $Id: TestBooleanPrefixQuery.java,v 1.2 2004/03/29 22:48:06 cutting Exp $
+ /// <version> $Id: TestBooleanPrefixQuery.java 150492 2004-09-06 22:01:49Z dnaber $
///
/// </version>
[TestFixture]
@@ -37,11 +39,18 @@
[STAThread]
public static void Main(System.String[] args)
{
- //TestRunner.run(Suite());
+ // NUnit.Core.TestRunner.Run(Suite()); // {{Aroush}} where is 'TestRunner' in NUnit?
}
- [Test]
- public virtual void TestMethod()
+ /* // {{Aroush}} Do we need this method?
+ public static NUnit.Framework.TestCase Suite()
+ {
+ return new NUnit.Core.TestSuite(typeof(TestBooleanPrefixQuery));
+ }
+ */
+
+ [Test]
+ public virtual void TestMethod()
{
RAMDirectory directory = new RAMDirectory();
@@ -54,8 +63,8 @@
IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
for (int i = 0; i < categories.Length; i++)
{
- Document doc = new Document();
- doc.Add(Field.Keyword("category", categories[i]));
+ Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
+ doc.Add(new Field("category", categories[i], Field.Store.YES, Field.Index.UN_TOKENIZED));
writer.AddDocument(doc);
}
writer.Close();
@@ -66,7 +75,7 @@
rw1 = query.Rewrite(reader);
BooleanQuery bq = new BooleanQuery();
- bq.Add(query, true, false);
+ bq.Add(query, BooleanClause.Occur.MUST);
rw2 = bq.Rewrite(reader);
}
Added: incubator/lucene.net/trunk/C#/src/Test/Search/TestBooleanQuery.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Search/TestBooleanQuery.cs?rev=411501&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Search/TestBooleanQuery.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Test/Search/TestBooleanQuery.cs Sat Jun 3 19:41:13 2006
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+
+using System;
+using NUnit.Framework;
+using Term = Lucene.Net.Index.Term;
+
+namespace Lucene.Net.Search
+{
+ [TestFixture]
+ public class TestBooleanQuery
+ {
+
+ [Test]
+ public virtual void TestEquality()
+ {
+ BooleanQuery bq1 = new BooleanQuery();
+ bq1.Add(new TermQuery(new Term("field", "value1")), BooleanClause.Occur.SHOULD);
+ bq1.Add(new TermQuery(new Term("field", "value2")), BooleanClause.Occur.SHOULD);
+ BooleanQuery nested1 = new BooleanQuery();
+ nested1.Add(new TermQuery(new Term("field", "nestedvalue1")), BooleanClause.Occur.SHOULD);
+ nested1.Add(new TermQuery(new Term("field", "nestedvalue2")), BooleanClause.Occur.SHOULD);
+ bq1.Add(nested1, BooleanClause.Occur.SHOULD);
+
+ BooleanQuery bq2 = new BooleanQuery();
+ bq2.Add(new TermQuery(new Term("field", "value1")), BooleanClause.Occur.SHOULD);
+ bq2.Add(new TermQuery(new Term("field", "value2")), BooleanClause.Occur.SHOULD);
+ BooleanQuery nested2 = new BooleanQuery();
+ nested2.Add(new TermQuery(new Term("field", "nestedvalue1")), BooleanClause.Occur.SHOULD);
+ nested2.Add(new TermQuery(new Term("field", "nestedvalue2")), BooleanClause.Occur.SHOULD);
+ bq2.Add(nested2, BooleanClause.Occur.SHOULD);
+
+ Assert.AreEqual(bq1, bq2);
+ }
+
+ [Test]
+ public virtual void TestException()
+ {
+ try
+ {
+ BooleanQuery.SetMaxClauseCount(0);
+ Assert.Fail();
+ }
+ catch (System.ArgumentException e)
+ {
+ // okay
+ }
+ }
+ }
+}
\ No newline at end of file
Added: incubator/lucene.net/trunk/C#/src/Test/Search/TestBooleanScorer.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Search/TestBooleanScorer.cs?rev=411501&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Search/TestBooleanScorer.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Test/Search/TestBooleanScorer.cs Sat Jun 3 19:41:13 2006
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+
+using System;
+using WhitespaceAnalyzer = Lucene.Net.Analysis.WhitespaceAnalyzer;
+using Document = Lucene.Net.Documents.Document;
+using Field = Lucene.Net.Documents.Field;
+using IndexWriter = Lucene.Net.Index.IndexWriter;
+using Term = Lucene.Net.Index.Term;
+using RAMDirectory = Lucene.Net.Store.RAMDirectory;
+using NUnit.Framework;
+
+namespace Lucene.Net.Search
+{
+
+ /// <author> Christoph Goller
+ /// </author>
+ /// <version> $rcs = ' $Id: TestBooleanScorer.java 150700 2004-12-10 19:36:40Z goller $ ' ;
+ /// </version>
+ [TestFixture]
+ public class TestBooleanScorer
+ {
+
+ private const System.String FIELD = "category";
+
+ [Test]
+ public virtual void TestMethod()
+ {
+ RAMDirectory directory = new RAMDirectory();
+
+ System.String[] values = new System.String[]{"1", "2", "3", "4"};
+
+ try
+ {
+ IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
+ for (int i = 0; i < values.Length; i++)
+ {
+ Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
+ doc.Add(new Field(FIELD, values[i], Field.Store.YES, Field.Index.UN_TOKENIZED));
+ writer.AddDocument(doc);
+ }
+ writer.Close();
+
+ BooleanQuery booleanQuery1 = new BooleanQuery();
+ booleanQuery1.Add(new TermQuery(new Term(FIELD, "1")), BooleanClause.Occur.SHOULD);
+ booleanQuery1.Add(new TermQuery(new Term(FIELD, "2")), BooleanClause.Occur.SHOULD);
+
+ BooleanQuery query = new BooleanQuery();
+ query.Add(booleanQuery1, BooleanClause.Occur.MUST);
+ query.Add(new TermQuery(new Term(FIELD, "9")), BooleanClause.Occur.MUST_NOT);
+
+ IndexSearcher indexSearcher = new IndexSearcher(directory);
+ Hits hits = indexSearcher.Search(query);
+ Assert.AreEqual(2, hits.Length(), "Number of matched documents");
+ }
+ catch (System.IO.IOException e)
+ {
+ Assert.Fail(e.Message);
+ }
+ }
+ }
+}
\ No newline at end of file
Modified: incubator/lucene.net/trunk/C#/src/Test/Search/TestCachingWrapperFilter.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Search/TestCachingWrapperFilter.cs?rev=411501&r1=411500&r2=411501&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Search/TestCachingWrapperFilter.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Search/TestCachingWrapperFilter.cs Sat Jun 3 19:41:13 2006
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
using System;
using NUnit.Framework;
using StandardAnalyzer = Lucene.Net.Analysis.Standard.StandardAnalyzer;
@@ -20,13 +21,14 @@
using IndexWriter = Lucene.Net.Index.IndexWriter;
using Directory = Lucene.Net.Store.Directory;
using RAMDirectory = Lucene.Net.Store.RAMDirectory;
+
namespace Lucene.Net.Search
{
[TestFixture]
public class TestCachingWrapperFilter
{
- [Test]
- public virtual void TestCachingWorks()
+ [Test]
+ public virtual void TestCachingWorks()
{
Directory dir = new RAMDirectory();
IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
Added: incubator/lucene.net/trunk/C#/src/Test/Search/TestConstantScoreRangeQuery.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Search/TestConstantScoreRangeQuery.cs?rev=411501&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Search/TestConstantScoreRangeQuery.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Test/Search/TestConstantScoreRangeQuery.cs Sat Jun 3 19:41:13 2006
@@ -0,0 +1,362 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+
+using System;
+using NUnit.Framework;
+using WhitespaceAnalyzer = Lucene.Net.Analysis.WhitespaceAnalyzer;
+using Document = Lucene.Net.Documents.Document;
+using Field = Lucene.Net.Documents.Field;
+using IndexReader = Lucene.Net.Index.IndexReader;
+using IndexWriter = Lucene.Net.Index.IndexWriter;
+using Term = Lucene.Net.Index.Term;
+using Directory = Lucene.Net.Store.Directory;
+using RAMDirectory = Lucene.Net.Store.RAMDirectory;
+
+namespace Lucene.Net.Search
+{
+
+ [TestFixture]
+ public class TestConstantScoreRangeQuery : BaseTestRangeFilter
+ {
+ private class AnonymousClassHitCollector : HitCollector
+ {
+ public AnonymousClassHitCollector(TestConstantScoreRangeQuery enclosingInstance)
+ {
+ InitBlock(enclosingInstance);
+ }
+ private void InitBlock(TestConstantScoreRangeQuery enclosingInstance)
+ {
+ this.enclosingInstance = enclosingInstance;
+ }
+ private TestConstantScoreRangeQuery enclosingInstance;
+ public TestConstantScoreRangeQuery Enclosing_Instance
+ {
+ get
+ {
+ return enclosingInstance;
+ }
+
+ }
+ public override void Collect(int doc, float score)
+ {
+ Enclosing_Instance.AssertEquals("score for doc " + doc + " was not correct", 1.0f, score);
+ }
+ }
+
+ /// <summary>threshold for comparing floats </summary>
+ public const float SCORE_COMP_THRESH = 1e-6f;
+
+ public TestConstantScoreRangeQuery(System.String name) : base(name)
+ {
+ }
+ public TestConstantScoreRangeQuery() : base()
+ {
+ }
+
+ internal Directory small;
+
+ internal virtual void AssertEquals(System.String m, float e, float a)
+ {
+ Assert.AreEqual(e, a, m, SCORE_COMP_THRESH);
+ }
+
+ [TestFixtureSetUp]
+ public virtual void SetUp()
+ {
+
+ System.String[] data = new System.String[]{"A 1 2 3 4 5 6", "Z 4 5 6", null, "B 2 4 5 6", "Y 3 5 6", null, "C 3 6", "X 4 5 6"};
+
+ small = new RAMDirectory();
+ IndexWriter writer = new IndexWriter(small, new WhitespaceAnalyzer(), true);
+
+ for (int i = 0; i < data.Length; i++)
+ {
+ Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
+ doc.Add(Field.Keyword("id", System.Convert.ToString(i)));
+ doc.Add(Field.Keyword("all", "all"));
+ if (null != data[i])
+ {
+ doc.Add(Field.Text("data", data[i]));
+ }
+ writer.AddDocument(doc);
+ }
+
+ writer.Optimize();
+ writer.Close();
+ }
+
+
+
+ /// <summary>macro for readability </summary>
+ public static Query Csrq(System.String f, System.String l, System.String h, bool il, bool ih)
+ {
+ return new ConstantScoreRangeQuery(f, l, h, il, ih);
+ }
+
+ [Test]
+ public virtual void TestBasics()
+ {
+ QueryUtils.Check(Csrq("data", "1", "6", T, T));
+ QueryUtils.Check(Csrq("data", "A", "Z", T, T));
+ QueryUtils.CheckUnequal(Csrq("data", "1", "6", T, T), Csrq("data", "A", "Z", T, T));
+ }
+
+ [Test]
+ public virtual void TestEqualScores()
+ {
+ // NOTE: uses index build in *this* SetUp
+
+ IndexReader reader = IndexReader.Open(small);
+ IndexSearcher search = new IndexSearcher(reader);
+
+ Hits result;
+
+ // some hits match more terms then others, score should be the same
+
+ result = search.Search(Csrq("data", "1", "6", T, T));
+ int numHits = result.Length();
+ Assert.AreEqual(6, numHits, "wrong number of results");
+ float score = result.Score(0);
+ for (int i = 1; i < numHits; i++)
+ {
+ AssertEquals("score for " + i + " was not the same", score, result.Score(i));
+ }
+ }
+
+ [Test]
+ public virtual void TestBoost()
+ {
+ // NOTE: uses index build in *this* SetUp
+
+ IndexReader reader = IndexReader.Open(small);
+ IndexSearcher search = new IndexSearcher(reader);
+
+ // test for correct application of query normalization
+ // must use a non score normalizing method for this.
+ Query q = Csrq("data", "1", "6", T, T);
+ q.SetBoost(100);
+ search.Search(q, null, new AnonymousClassHitCollector(this));
+
+
+ //
+ // Ensure that boosting works to score one clause of a query higher
+ // than another.
+ //
+ Query q1 = Csrq("data", "A", "A", T, T); // matches document #0
+ q1.SetBoost(.1f);
+ Query q2 = Csrq("data", "Z", "Z", T, T); // matches document #1
+ BooleanQuery bq = new BooleanQuery(true);
+ bq.Add(q1, BooleanClause.Occur.SHOULD);
+ bq.Add(q2, BooleanClause.Occur.SHOULD);
+
+ Hits hits = search.Search(bq);
+ Assert.AreEqual(1, hits.Id(0));
+ Assert.AreEqual(0, hits.Id(1));
+ Assert.IsTrue(hits.Score(0) > hits.Score(1));
+
+ q1 = Csrq("data", "A", "A", T, T); // matches document #0
+ q1.SetBoost(10f);
+ q2 = Csrq("data", "Z", "Z", T, T); // matches document #1
+ bq = new BooleanQuery(true);
+ bq.Add(q1, BooleanClause.Occur.SHOULD);
+ bq.Add(q2, BooleanClause.Occur.SHOULD);
+
+ hits = search.Search(bq);
+ Assert.AreEqual(0, hits.Id(0));
+ Assert.AreEqual(1, hits.Id(1));
+ Assert.IsTrue(hits.Score(0) > hits.Score(1));
+ }
+
+ [Test]
+ public virtual void TestBooleanOrderUnAffected()
+ {
+ // NOTE: uses index build in *this* SetUp
+
+ IndexReader reader = IndexReader.Open(small);
+ IndexSearcher search = new IndexSearcher(reader);
+
+ // first do a regular RangeQuery which uses term expansion so
+ // docs with more terms in range get higher scores
+
+ Query rq = new RangeQuery(new Term("data", "1"), new Term("data", "4"), T);
+
+ Hits expected = search.Search(rq);
+ int numHits = expected.Length();
+
+ // now do a boolean where which also contains a
+ // ConstantScoreRangeQuery and make sure hte order is the same
+
+ BooleanQuery q = new BooleanQuery();
+ q.Add(rq, T, F);
+ q.Add(Csrq("data", "1", "6", T, T), T, F);
+
+ Hits actual = search.Search(q);
+
+ Assert.AreEqual(numHits, actual.Length(), "wrong numebr of hits");
+ for (int i = 0; i < numHits; i++)
+ {
+ Assert.AreEqual(expected.Id(i), actual.Id(i), "mismatch in docid for hit#" + i);
+ }
+ }
+
+ [Test]
+ public virtual void TestRangeQueryId()
+ {
+ // NOTE: uses index build in *super* SetUp
+
+ IndexReader reader = IndexReader.Open(index);
+ IndexSearcher search = new IndexSearcher(reader);
+
+ int medId = ((maxId - minId) / 2);
+
+ System.String minIP = Pad(minId);
+ System.String maxIP = Pad(maxId);
+ System.String medIP = Pad(medId);
+
+ int numDocs = reader.NumDocs();
+
+ Assert.AreEqual(numDocs, 1 + maxId - minId, "num of docs");
+
+ Hits result;
+
+ // test id, bounded on both ends
+
+ result = search.Search(Csrq("id", minIP, maxIP, T, T));
+ Assert.AreEqual(numDocs, result.Length(), "find all");
+
+ result = search.Search(Csrq("id", minIP, maxIP, T, F));
+ Assert.AreEqual(numDocs - 1, result.Length(), "all but last");
+
+ result = search.Search(Csrq("id", minIP, maxIP, F, T));
+ Assert.AreEqual(numDocs - 1, result.Length(), "all but first");
+
+ result = search.Search(Csrq("id", minIP, maxIP, F, F));
+ Assert.AreEqual(numDocs - 2, result.Length(), "all but ends");
+
+ result = search.Search(Csrq("id", medIP, maxIP, T, T));
+ Assert.AreEqual(1 + maxId - medId, result.Length(), "med and up");
+
+ result = search.Search(Csrq("id", minIP, medIP, T, T));
+ Assert.AreEqual(1 + medId - minId, result.Length(), "up to med");
+
+ // unbounded id
+
+ result = search.Search(Csrq("id", minIP, null, T, F));
+ Assert.AreEqual(numDocs, result.Length(), "min and up");
+
+ result = search.Search(Csrq("id", null, maxIP, F, T));
+ Assert.AreEqual(numDocs, result.Length(), "max and down");
+
+ result = search.Search(Csrq("id", minIP, null, F, F));
+ Assert.AreEqual(numDocs - 1, result.Length(), "not min, but up");
+
+ result = search.Search(Csrq("id", null, maxIP, F, F));
+ Assert.AreEqual(numDocs - 1, result.Length(), "not max, but down");
+
+ result = search.Search(Csrq("id", medIP, maxIP, T, F));
+ Assert.AreEqual(maxId - medId, result.Length(), "med and up, not max");
+
+ result = search.Search(Csrq("id", minIP, medIP, F, T));
+ Assert.AreEqual(medId - minId, result.Length(), "not min, up to med");
+
+ // very small sets
+
+ result = search.Search(Csrq("id", minIP, minIP, F, F));
+ Assert.AreEqual(0, result.Length(), "min,min,F,F");
+ result = search.Search(Csrq("id", medIP, medIP, F, F));
+ Assert.AreEqual(0, result.Length(), "med,med,F,F");
+ result = search.Search(Csrq("id", maxIP, maxIP, F, F));
+ Assert.AreEqual(0, result.Length(), "max,max,F,F");
+
+ result = search.Search(Csrq("id", minIP, minIP, T, T));
+ Assert.AreEqual(1, result.Length(), "min,min,T,T");
+ result = search.Search(Csrq("id", null, minIP, F, T));
+ Assert.AreEqual(1, result.Length(), "nul,min,F,T");
+
+ result = search.Search(Csrq("id", maxIP, maxIP, T, T));
+ Assert.AreEqual(1, result.Length(), "max,max,T,T");
+ result = search.Search(Csrq("id", maxIP, null, T, F));
+ Assert.AreEqual(1, result.Length(), "max,nul,T,T");
+
+ result = search.Search(Csrq("id", medIP, medIP, T, T));
+ Assert.AreEqual(1, result.Length(), "med,med,T,T");
+ }
+
+ [Test]
+ public virtual void TestRangeQueryRand()
+ {
+ // NOTE: uses index build in *super* SetUp
+
+ IndexReader reader = IndexReader.Open(index);
+ IndexSearcher search = new IndexSearcher(reader);
+
+ System.String minRP = Pad(minR);
+ System.String maxRP = Pad(maxR);
+
+ int numDocs = reader.NumDocs();
+
+ Assert.AreEqual(numDocs, 1 + maxId - minId, "num of docs");
+
+ Hits result;
+ Query q = new TermQuery(new Term("body", "body"));
+
+ // test extremes, bounded on both ends
+
+ result = search.Search(Csrq("rand", minRP, maxRP, T, T));
+ Assert.AreEqual(numDocs, result.Length(), "find all");
+
+ result = search.Search(Csrq("rand", minRP, maxRP, T, F));
+ Assert.AreEqual(numDocs - 1, result.Length(), "all but biggest");
+
+ result = search.Search(Csrq("rand", minRP, maxRP, F, T));
+ Assert.AreEqual(numDocs - 1, result.Length(), "all but smallest");
+
+ result = search.Search(Csrq("rand", minRP, maxRP, F, F));
+ Assert.AreEqual(numDocs - 2, result.Length(), "all but extremes");
+
+ // unbounded
+
+ result = search.Search(Csrq("rand", minRP, null, T, F));
+ Assert.AreEqual(numDocs, result.Length(), "smallest and up");
+
+ result = search.Search(Csrq("rand", null, maxRP, F, T));
+ Assert.AreEqual(numDocs, result.Length(), "biggest and down");
+
+ result = search.Search(Csrq("rand", minRP, null, F, F));
+ Assert.AreEqual(numDocs - 1, result.Length(), "not smallest, but up");
+
+ result = search.Search(Csrq("rand", null, maxRP, F, F));
+ Assert.AreEqual(numDocs - 1, result.Length(), "not biggest, but down");
+
+ // very small sets
+
+ result = search.Search(Csrq("rand", minRP, minRP, F, F));
+ Assert.AreEqual(0, result.Length(), "min,min,F,F");
+ result = search.Search(Csrq("rand", maxRP, maxRP, F, F));
+ Assert.AreEqual(0, result.Length(), "max,max,F,F");
+
+ result = search.Search(Csrq("rand", minRP, minRP, T, T));
+ Assert.AreEqual(1, result.Length(), "min,min,T,T");
+ result = search.Search(Csrq("rand", null, minRP, F, T));
+ Assert.AreEqual(1, result.Length(), "nul,min,F,T");
+
+ result = search.Search(Csrq("rand", maxRP, maxRP, T, T));
+ Assert.AreEqual(1, result.Length(), "max,max,T,T");
+ result = search.Search(Csrq("rand", maxRP, null, T, F));
+ Assert.AreEqual(1, result.Length(), "max,nul,T,T");
+ }
+ }
+}
\ No newline at end of file