FileDocCategorySizeDatePackage
TestQueryTemplateManager.javaAPI DocApache Lucene 2.1.06246Wed Feb 14 10:46:18 GMT 2007org.apache.lucene.xmlparser

TestQueryTemplateManager

public class TestQueryTemplateManager extends TestCase
This class illustrates how form input (such as from a web page or Swing gui) can be turned into Lucene queries using a choice of XSL templates for different styles of queries.
author
maharwood

Fields Summary
CoreParser
builder
Analyzer
analyzer
private IndexSearcher
searcher
String[]
docFieldValues
String[]
queryForms
Constructors Summary
Methods Summary
org.apache.lucene.document.DocumentgetDocumentFromString(java.lang.String nameValuePairs)

		org.apache.lucene.document.Document result=new org.apache.lucene.document.Document();
		StringTokenizer st=new StringTokenizer(nameValuePairs,"\t=");
		while(st.hasMoreTokens())
		{
			String name=st.nextToken().trim();
			if(st.hasMoreTokens())
			{
				String value=st.nextToken().trim();
				result.add(new Field(name,value,Field.Store.YES,Field.Index.TOKENIZED));
			}
		}
		return result;
	
java.util.PropertiesgetPropsFromString(java.lang.String nameValuePairs)

		Properties result=new Properties();
		StringTokenizer st=new StringTokenizer(nameValuePairs,"\t=");
		while(st.hasMoreTokens())
		{
			String name=st.nextToken().trim();
			if(st.hasMoreTokens())
			{
				String value=st.nextToken().trim();
				result.setProperty(name,value);
			}
		}
		return result;
	
protected voidsetUp()

		super.setUp();
		
		
		//Create an index
		RAMDirectory dir=new RAMDirectory();
		IndexWriter w=new IndexWriter(dir,analyzer,true);
		for (int i = 0; i < docFieldValues.length; i++)
		{
			w.addDocument(getDocumentFromString(docFieldValues[i]));
		}
		w.optimize();
		w.close();
		searcher=new IndexSearcher(dir);
		
		//initialize the parser
		builder=new CorePlusExtensionsParser(analyzer,new QueryParser("artist", analyzer));
		
	
protected voidtearDown()

		searcher.close();
	
public voidtestFormTransforms()

	
	
	         
	
		//Cache all the query templates we will be referring to.
		QueryTemplateManager qtm=new QueryTemplateManager();
		qtm.addQueryTemplate("albumBooleanQuery", getClass().getResourceAsStream("albumBooleanQuery.xsl"));
		qtm.addQueryTemplate("albumFilteredQuery", getClass().getResourceAsStream("albumFilteredQuery.xsl"));
		qtm.addQueryTemplate("albumLuceneClassicQuery", getClass().getResourceAsStream("albumLuceneClassicQuery.xsl"));
		//Run all of our test queries
		for (int i = 0; i < queryForms.length; i++)
		{
			Properties queryFormProperties=getPropsFromString(queryForms[i]);
			
			//Get the required query XSL template for this test
//			Templates template=getTemplate(queryFormProperties.getProperty("template"));
			
			//Transform the queryFormProperties into a Lucene XML query
			Document doc=qtm.getQueryAsDOM(queryFormProperties,queryFormProperties.getProperty("template"));
			
			//Parse the XML query using the XML parser
			Query q=builder.getQuery(doc.getDocumentElement());
			
			//Run the query
			Hits h=searcher.search(q);
			
			//Check we have the expected number of results
			int expectedHits=Integer.parseInt(queryFormProperties.getProperty("expectedMatches"));
			assertEquals("Number of results should match for query "+queryForms[i],expectedHits,h.length());
			
		}