/*
* Copyright 2002-2007 the original author or authors.
*
* 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.
*/
package org.springmodules.lucene.search.object;
import java.util.List;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.springmodules.lucene.AbstractLuceneTestCase;
import org.springmodules.lucene.search.factory.SearcherFactory;
import org.springmodules.lucene.search.factory.SimpleSearcherFactory;
/**
* @author Thierry Templier
*/
public class SimpleLuceneSearchQueryTests extends AbstractLuceneTestCase {
final public void testSearch() {
//Initialization of the searcher
SearcherFactory searcherFactory = new SimpleSearcherFactory(directory);
//Initialization of the query
LuceneSearchQuery query = new SimpleLuceneSearchQuery(searcherFactory,null) {
protected Query constructSearchQuery(String textToSearch) throws ParseException {
return new TermQuery(new Term("field", textToSearch));
}
protected Object extractResultHit(int id, Document document, float score) {
return document.get("field");
}
};
//First search
List results=query.search("lucene");
assertEquals(results.size(),1);
assertEquals((String)results.get(0), "a Lucene support sample");
//Second search
results=query.search("sample");
assertEquals(results.size(),3);
assertEquals((String)results.get(0), "a sample");
assertEquals((String)results.get(1), "a Lucene support sample");
assertEquals((String)results.get(2), "a different sample");
}
}