/*
 * 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 StopAnalyzer = Lucene.Net.Analysis.StopAnalyzer;
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;*/
namespace clucene.net
{
	
	/// <summary> Tests {@link PhraseQuery}.
	/// 
	/// </summary>
	/// <seealso cref="TestPositionIncrement">
	/// </seealso>
	/// <author>  Erik Hatcher
	/// </author>
	[TestFixture]
    public class TestPhraseQuery
	{
		private IndexSearcher searcher;
		private PhraseQuery query;
		private RAMDirectory directory;
		
        [TestFixtureSetUp]
		public virtual void  SetUp()
		{
			directory = new RAMDirectory();
			IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
			
			Document doc = new Document();
			doc.add(Field.Text("Field", "one two three four five"));
			writer.addDocument(doc);
			
			writer.optimize();
			writer.close();
			
			searcher = new IndexSearcher(directory);
			query = new PhraseQuery();
		}
		
        [TestFixtureTearDown]
		public virtual void  TearDown()
		{
			searcher.close();
			directory.close();
		}
		
        [Test]
		public virtual void  TestNotCloseEnough()
		{
			query.setSlop(2);
			query.add(new Term("Field", "one"));
			query.add(new Term("Field", "five"));
			Hits hits = searcher.search(query);
			Assert.AreEqual(0, hits.length());
		}
		
        [Test]
		public virtual void  TestBarelyCloseEnough()
		{
			query.setSlop(3);
			query.add(new Term("Field", "one"));
			query.add(new Term("Field", "five"));
			Hits hits = searcher.search(query);
			Assert.AreEqual(1, hits.length());
		}
		
		/// <summary> Ensures slop of 0 works for exact matches, but not reversed</summary>
		[Test]
		public virtual void  TestExact()
		{
			// slop is zero by default
			query.add(new Term("Field", "four"));
			query.add(new Term("Field", "five"));
			Hits hits = searcher.search(query);
			Assert.AreEqual(1, hits.length(), "exact match");
			
			query = new PhraseQuery();
			query.add(new Term("Field", "two"));
			query.add(new Term("Field", "one"));
			hits = searcher.search(query);
			Assert.AreEqual(0, hits.length(), "reverse not exact");
		}
		
        [Test]
		public virtual void  TestSlop1()
		{
            SetUp();

			// Ensures slop of 1 works with terms in order.
			query.setSlop(1);
			query.add(new Term("Field", "one"));
			query.add(new Term("Field", "two"));
			Hits hits = searcher.search(query);
			Assert.AreEqual(1, hits.length(), "in order");
			
			// Ensures slop of 1 does not work for phrases out of order;
			// must be at least 2.
			query = new PhraseQuery();
			query.setSlop(1);
			query.add(new Term("Field", "two"));
			query.add(new Term("Field", "one"));
			hits = searcher.search(query);
			Assert.AreEqual(0, hits.length(), "reversed, slop not 2 or more");
		}
		
		/// <summary> As long as slop is at least 2, terms can be reversed</summary>
		[Test]
        public virtual void  TestOrderDoesntMatter()
		{
            SetUp();

			query.setSlop(2); // must be at least two for reverse order match
			query.add(new Term("Field", "two"));
			query.add(new Term("Field", "one"));
			Hits hits = searcher.search(query);
			Assert.AreEqual(1, hits.length(), "just sloppy enough");
			
			query = new PhraseQuery();
			query.setSlop(2);
			query.add(new Term("Field", "three"));
			query.add(new Term("Field", "one"));
			hits = searcher.search(query);
			Assert.AreEqual(0, hits.length(), "not sloppy enough");
		}
		
		/// <summary> slop is the total number of positional moves allowed
		/// to line up a phrase
		/// </summary>
		[Test]
        public virtual void  TestMulipleTerms()
		{
            SetUp();

			query.setSlop(2);
			query.add(new Term("Field", "one"));
			query.add(new Term("Field", "three"));
			query.add(new Term("Field", "five"));
			Hits hits = searcher.search(query);
			Assert.AreEqual(1, hits.length(), "two total moves");
			
			query = new PhraseQuery();
			query.setSlop(5); // it takes six moves to match this phrase
			query.add(new Term("Field", "five"));
			query.add(new Term("Field", "three"));
			query.add(new Term("Field", "one"));
			hits = searcher.search(query);
			Assert.AreEqual(0, hits.length(), "slop of 5 not close enough");
			
			query.setSlop(6);
			hits = searcher.search(query);
			Assert.AreEqual(1, hits.length(), "slop of 6 just right");
		}
		
        [Test]
		public virtual void  TestPhraseQueryWithStopAnalyzer()
		{
			RAMDirectory directory = new RAMDirectory();
			StopAnalyzer stopAnalyzer = new StopAnalyzer();
			IndexWriter writer = new IndexWriter(directory, stopAnalyzer, true);
			Document doc = new Document();
			doc.add(Field.Text("Field", "the stop words are here"));
			writer.addDocument(doc);
			writer.close();
			
			IndexSearcher searcher = new IndexSearcher(directory);
			
			// valid exact phrase query
			PhraseQuery query = new PhraseQuery();
			query.add(new Term("Field", "stop"));
			query.add(new Term("Field", "words"));
			Hits hits = searcher.search(query);
			Assert.AreEqual(1, hits.length());
			
			// currently StopAnalyzer does not leave "holes", so this matches.
			query = new PhraseQuery();
			query.add(new Term("Field", "words"));
			query.add(new Term("Field", "here"));
			hits = searcher.search(query);
			Assert.AreEqual(1, hits.length());
			
			searcher.close();
		}
		
        [Test]
		public virtual void  TestPhraseQueryInConjunctionScorer()
		{
			RAMDirectory directory = new RAMDirectory();
			IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
			
			Document doc = new Document();
			doc.add(new Field("source", "marketing info", true, true, true));
			writer.addDocument(doc);
			
			doc = new Document();
			doc.add(new Field("contents", "foobar", true, true, true));
			doc.add(new Field("source", "marketing info", true, true, true));
			writer.addDocument(doc);
			
			writer.optimize();
			writer.close();
			
			IndexSearcher searcher = new IndexSearcher(directory);
			
			PhraseQuery phraseQuery = new PhraseQuery();
			phraseQuery.add(new Term("source", "marketing"));
			phraseQuery.add(new Term("source", "info"));
			Hits hits = searcher.search(phraseQuery);
			Assert.AreEqual(2, hits.length());
			
			TermQuery termQuery = new TermQuery(new Term("contents", "foobar"));
			BooleanQuery booleanQuery = new BooleanQuery();
			booleanQuery.add(termQuery, true, false);
			booleanQuery.add(phraseQuery, true, false);
			hits = searcher.search(booleanQuery);
			Assert.AreEqual(1, hits.length());
			
			searcher.close();
			
			writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
			doc = new Document();
			doc.add(new Field("contents", "map entry woo", true, true, true));
			writer.addDocument(doc);
			
			doc = new Document();
			doc.add(new Field("contents", "woo map entry", true, true, true));
			writer.addDocument(doc);
			
			doc = new Document();
			doc.add(new Field("contents", "map foobarword entry woo", true, true, true));
			writer.addDocument(doc);
			
			writer.optimize();
			writer.close();
			
			searcher = new IndexSearcher(directory);
			
			termQuery = new TermQuery(new Term("contents", "woo"));
			phraseQuery = new PhraseQuery();
			phraseQuery.add(new Term("contents", "map"));
			phraseQuery.add(new Term("contents", "entry"));
			
			hits = searcher.search(termQuery);
			Assert.AreEqual(3, hits.length());
			hits = searcher.search(phraseQuery);
			Assert.AreEqual(2, hits.length());
			
			booleanQuery = new BooleanQuery();
			booleanQuery.add(termQuery, true, false);
			booleanQuery.add(phraseQuery, true, false);
			hits = searcher.search(booleanQuery);
			Assert.AreEqual(2, hits.length());
			
			booleanQuery = new BooleanQuery();
			booleanQuery.add(phraseQuery, true, false);
			booleanQuery.add(termQuery, true, false);
			hits = searcher.search(booleanQuery);
			Assert.AreEqual(2, hits.length());
			
			searcher.close();
			directory.close();
		}
	}
}