package at.ac.univie.mminf.luceneSKOS.analysis;

import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.Version;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import at.ac.univie.mminf.luceneSKOS.analysis.SKOSAnalyzer.ExpansionType;
import at.ac.univie.mminf.luceneSKOS.util.AnalyzerUtils;
import at.ac.univie.mminf.luceneSKOS.util.TestUtil;

/**
 * Testing the SKOS Label Filter
 * 
 * @author Bernhard Haslhofer <bernhard.haslhofer@univie.ac.at>
 * @author Martin Kysel <martin.kysel@univie.ac.at>
 * 
 */
public class SKOSLabelFilterTest extends AbstractFilterTest {
  
  @Before
  @Override
  public void setUp() throws Exception {
    
    super.setUp();
    
    skosAnalyzer = new SKOSAnalyzer(Version.LUCENE_40, skosEngine,
        ExpansionType.LABEL);
    
    writer = new IndexWriter(directory, new IndexWriterConfig(
        Version.LUCENE_40, skosAnalyzer));
    
  }
  
  @Test
  public void termQuerySearch() throws Exception {
    
    Document doc = new Document();
    doc.add(new Field("content", "The quick brown fox jumps over the lazy dog",
        TextField.TYPE_STORED));
    
    writer.addDocument(doc);
    
    searcher = new IndexSearcher(DirectoryReader.open(writer, false));
    
    TermQuery tq = new TermQuery(new Term("content", "hops"));
    
    Assert.assertEquals(1, TestUtil.hitCount(searcher, tq));
    
  }
  
  @Test
  public void phraseQuerySearch() throws Exception {
    
    Document doc = new Document();
    doc.add(new Field("content", "The quick brown fox jumps over the lazy dog",
        TextField.TYPE_STORED));
    
    writer.addDocument(doc);
    
    searcher = new IndexSearcher(DirectoryReader.open(writer, false));
    
    PhraseQuery pq = new PhraseQuery();
    pq.add(new Term("content", "fox"));
    pq.add(new Term("content", "hops"));
    
    Assert.assertEquals(1, TestUtil.hitCount(searcher, pq));
    
  }
  
  @Test
  public void queryParserSearch() throws Exception {
    
    Document doc = new Document();
    doc.add(new Field("content", "The quick brown fox jumps over the lazy dog",
        TextField.TYPE_STORED));
    
    writer.addDocument(doc);
    
    searcher = new IndexSearcher(DirectoryReader.open(writer, false));
    
    Query query = new QueryParser(Version.LUCENE_40, "content", skosAnalyzer)
        .parse("\"fox jumps\"");
    
    Assert.assertEquals(1, TestUtil.hitCount(searcher, query));
    
    Assert.assertEquals("content:\"fox (jumps hops leaps)\"", query.toString());
    Assert.assertEquals("org.apache.lucene.search.MultiPhraseQuery", query
        .getClass().getName());
    
    query = new QueryParser(Version.LUCENE_40, "content", new StandardAnalyzer(
        Version.LUCENE_40)).parse("\"fox jumps\"");
    Assert.assertEquals(1, TestUtil.hitCount(searcher, query));
    
    Assert.assertEquals("content:\"fox jumps\"", query.toString());
    Assert.assertEquals("org.apache.lucene.search.PhraseQuery", query
        .getClass().getName());
    
  }
  
  @Test
  public void testTermQuery() throws Exception {
    
    Document doc = new Document();
    doc.add(new Field("content", "I work for the united nations",
        TextField.TYPE_STORED));
    
    writer.addDocument(doc);
    
    searcher = new IndexSearcher(DirectoryReader.open(writer, false));
    
    QueryParser parser = new QueryParser(Version.LUCENE_40, "content",
        new SimpleAnalyzer(Version.LUCENE_40));
    
    Query query = parser.parse("united nations");
    
    Assert.assertEquals(1, TestUtil.hitCount(searcher, query));
    
  }
  
  // @Test
  public void displayTokensWithLabelExpansion() throws Exception {
    
    String text = "The quick brown fox jumps over the lazy dog";
    
    AnalyzerUtils.displayTokensWithFullDetails(skosAnalyzer, text);
    // AnalyzerUtils.displayTokensWithPositions(synonymAnalyzer, text);
    
  }
  
}

@@ -508,16 +508,34 @@
 ser.
-classic.
+flexible.standard.Standard
 Quer
@@ -2895,32 +2895,40 @@
 ery query = new 
+Standard
 QueryParser(Vers
@@ -2927,38 +2927,8 @@
 ser(
-Version.LUCENE_40, %22content%22, 
 skos
@@ -2936,25 +2936,16 @@
 nalyzer)
-%0A        
 .parse(%22
@@ -2954,24 +2954,43 @@
 fox jumps%5C%22%22
+,%0A        %22content%22
 );%0A    %0A    
@@ -3259,24 +3259,32 @@
 query = new 
+Standard
 QueryParser(
@@ -3287,38 +3287,8 @@
 ser(
-Version.LUCENE_40, %22content%22, 
 new 
@@ -3304,25 +3304,16 @@
 nalyzer(
-%0A        
 Version.
@@ -3323,16 +3323,25 @@
 ENE_40))
+%0A        
 .parse(%22
@@ -3354,16 +3354,27 @@
 jumps%5C%22%22
+, %22content%22
 );%0A    A
@@ -3930,24 +3930,32 @@
 );%0A    %0A    
+Standard
 QueryParser 
@@ -3967,16 +3967,24 @@
 r = new 
+Standard
 QueryPar
@@ -3991,46 +3991,8 @@
 ser(
-Version.LUCENE_40, %22content%22,%0A        
 new 
@@ -3998,32 +3998,41 @@
  SimpleAnalyzer(
+%0A        
 Version.LUCENE_4
@@ -4088,16 +4088,27 @@
 nations%22
+, %22content%22
 );%0A    %0A

Move to StandardQueryParser.