/*
* 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 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 ParseException = Lucene.Net.QueryParsers.ParseException;
using QueryParser = Lucene.Net.QueryParsers.QueryParser;
using Directory = Lucene.Net.Store.Directory;
using RAMDirectory = Lucene.Net.Store.RAMDirectory;
namespace Lucene.Net.Search
{
/// Tests {@link MultiSearcher} ranking, i.e. makes sure this bug is fixed:
/// http://issues.apache.org/bugzilla/show_bug.cgi?id=31841
///
///
/// $Id: TestMultiSearcher.java 150492 2004-09-06 22:01:49Z dnaber $
///
[TestFixture]
public class TestMultiSearcherRanking
{
private bool verbose = false; // set to true to output hits
private System.String FIELD_NAME = "body";
private Searcher multiSearcher;
private Searcher singleSearcher;
[Test]
public virtual void TestOneTermQuery()
{
CheckQuery("three");
}
[Test]
public virtual void TestTwoTermQuery()
{
CheckQuery("three foo");
}
[Test]
public virtual void TestPrefixQuery()
{
CheckQuery("multi*");
}
[Test]
public virtual void TestFuzzyQuery()
{
CheckQuery("multiThree~");
}
[Test]
public virtual void TestRangeQuery()
{
CheckQuery("{multiA TO multiP}");
}
[Test]
public virtual void TestMultiPhraseQuery()
{
CheckQuery("\"blueberry pi*\"");
}
[Test]
public virtual void TestNoMatchQuery()
{
CheckQuery("+three +nomatch");
}
/*
public void testTermRepeatedQuery() throws IOException, ParseException {
// TODO: this corner case yields different results.
checkQuery("multi* multi* foo");
}
*/
/// checks if a query yields the same result when executed on
/// a single IndexSearcher containing all documents and on a
/// MultiSearcher aggregating sub-searchers
///
/// the query to check.
///
/// IOException
/// ParseException
private void CheckQuery(System.String queryStr)
{
// check result hit ranking
if (verbose)
System.Console.Out.WriteLine("Query: " + queryStr);
Query query = Lucene.Net.QueryParsers.QueryParser.Parse(queryStr, FIELD_NAME, new StandardAnalyzer());
Hits multiSearcherHits = multiSearcher.Search(query);
Hits singleSearcherHits = singleSearcher.Search(query);
Assert.AreEqual(multiSearcherHits.Length(), singleSearcherHits.Length());
for (int i = 0; i < multiSearcherHits.Length(); i++)
{
Lucene.Net.Documents.Document docMulti = multiSearcherHits.Doc(i);
Lucene.Net.Documents.Document docSingle = singleSearcherHits.Doc(i);
if (verbose)
System.Console.Out.WriteLine("Multi: " + docMulti.Get(FIELD_NAME) + " score=" + multiSearcherHits.Score(i));
if (verbose)
System.Console.Out.WriteLine("Single: " + docSingle.Get(FIELD_NAME) + " score=" + singleSearcherHits.Score(i));
Assert.AreEqual(multiSearcherHits.Score(i), singleSearcherHits.Score(i), 0.001f);
Assert.AreEqual(docMulti.Get(FIELD_NAME), docSingle.Get(FIELD_NAME));
}
if (verbose)
System.Console.Out.WriteLine();
}
/// initializes multiSearcher and singleSearcher with the same document set
[SetUp]
public virtual void SetUp()
{
// create MultiSearcher from two seperate searchers
Directory d1 = new RAMDirectory();
IndexWriter iw1 = new IndexWriter(d1, new StandardAnalyzer(), true);
AddCollection1(iw1);
iw1.Close();
Directory d2 = new RAMDirectory();
IndexWriter iw2 = new IndexWriter(d2, new StandardAnalyzer(), true);
AddCollection2(iw2);
iw2.Close();
Lucene.Net.Search.Searchable[] s = new Lucene.Net.Search.Searchable[2];
s[0] = new IndexSearcher(d1);
s[1] = new IndexSearcher(d2);
multiSearcher = new MultiSearcher(s);
// create IndexSearcher which contains all documents
Directory d = new RAMDirectory();
IndexWriter iw = new IndexWriter(d, new StandardAnalyzer(), true);
AddCollection1(iw);
AddCollection2(iw);
iw.Close();
singleSearcher = new IndexSearcher(d);
}
private void AddCollection1(IndexWriter iw)
{
Add("one blah three", iw);
Add("one foo three multiOne", iw);
Add("one foobar three multiThree", iw);
Add("blueberry pie", iw);
Add("blueberry strudel", iw);
Add("blueberry pizza", iw);
}
private void AddCollection2(IndexWriter iw)
{
Add("two blah three", iw);
Add("two foo xxx multiTwo", iw);
Add("two foobar xxx multiThreee", iw);
Add("blueberry chewing gum", iw);
Add("bluebird pizza", iw);
Add("bluebird foobar pizza", iw);
Add("piccadilly circus", iw);
}
private void Add(System.String value_Renamed, IndexWriter iw)
{
Lucene.Net.Documents.Document d = new Lucene.Net.Documents.Document();
d.Add(new Field(FIELD_NAME, value_Renamed, Field.Store.YES, Field.Index.TOKENIZED));
iw.AddDocument(d);
}
}
}