package org.apache.fulcrum.search;
/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Turbine" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * "Apache Turbine", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.Hits; import org.apache.fulcrum.Service; import java.util.List; /** * SearchService interface. * @author <a href="mailto:[EMAIL PROTECTED]">Hugh Brien</a> */ public interface SearchService extends Service { public static final String SERVICE_NAME = "SearchService"; public static final String INDEX_DEFAULT_LOCATION = "."; /** * Directory for location of index files produced by Lucene */ public static final String INDEX_OUTPUT_DIRECTORY = "index.directory"; public static final String SEARCH_PREFIX= "search.engine"; /** * Object that may be added to the index. * Not implemented yet */ static String OBJECT_CLASS_NAMES = "database.objects"; /** * Hits returned from a search. Query is generated by * Query Parser. See the Lucene javadocs for * the Hits and Query interfaces. */ public Hits search(Query query); /** * Returns an implementation of a Query based on the * search text and a field */ public Query getQuery(String searchText, String fieldName); /** * Method for indexing data. This provides a simple way * to create an index. Maps are used as containers for * name-value pairs of that are pushed into the index. * Either a List of Map objects or List of Document objects * may be passed into the this method. The method will * check for an instance of a Map or Document. */ public void indexData(List listofMaps); /** * Returns the directory of the index * @return String */ public String getIndexLocation(); } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ package org.apache.fulcrum.search; /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Turbine" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * "Apache Turbine", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.DateField; import java.util.Map; import java.util.Iterator; import java.util.Date; /** * BaseDocument interface. * @author <a href="mailto:[EMAIL PROTECTED]">Hugh Brien</a> */ public class BaseDocument { private BaseDocument(){} /** * Creates Lucene Document Object. The Document object is * the basic unit of searchable data. It is divided into * fields that are user defined. See Lucene Javadoc for details. * @return Document */ public static Document Document(Map docData) throws Exception { Document doc = new Document(); if (docData != null) { Iterator iterator = docData.keySet().iterator(); while(iterator.hasNext()) { String key = (String)iterator.next(); String value = (String)docData.get(key); doc.add(Field.Text(key, value)); } doc.add(Field.Text("date",new Date().toString())); } return doc; } } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++ package org.apache.fulcrum.search; /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Turbine" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * "Apache Turbine", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ import org.apache.fulcrum.TurbineServices; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.StopAnalyzer; import org.apache.lucene.search.Searcher; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.TermQuery; import org.apache.lucene.index.Term; import org.apache.lucene.search.Hits; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.index.IndexWriter; import java.util.Date; import java.util.Vector; import java.util.Map; import java.util.Hashtable; import java.util.List; import java.util.Iterator; import java.util.Set; import java.io.Writer; import java.io.PrintWriter; import java.io.StringWriter; public class TestSearch { public TestSearch() { } public void testIndexStuff() { TurbineSearchService search = new TurbineSearchService(); search.indexOutputDirectory = "H:/index/test"; List mapList = search.createListOfMaps(); search.indexData(mapList); } public void testSearch() { TurbineSearchService search = new TurbineSearchService(); search.indexOutputDirectory = "H:/index/test"; Query query = search.getQuery("keyword", "keywords"); Hits hits = search.search(query); System.out.println(hits.length()); query = search.getQuery("test", "keywords"); hits = search.search(query); System.out.println(hits.length()); } public void testPortalSearch() { file://TurbineConfig tc = new TurbineConfig("c:\\jakarta-tomcat-3.2.3\\applications\\jakarta-turbine-2\\co nf","TurbineResources.properties"); file://org.apache.turbine.util.TurbineConfig tc = new org.apache.turbine.util.TurbineConfig("j:\\agassi\\novus-portal","WEB-INF\\c onf\\TestTurbineResources.properties"); file://tc.init(); SearchService search = (SearchService)TurbineServices.getInstance().getService(SearchService.SERVIC E_NAME); Query query = search.getQuery("Symmetrix", "keywords"); Hits hits = search.search(query); if (hits != null) { System.out.println("found "); System.out.println(hits.length()); } } /** * */ static public void main(String[] args) { TestSearch test = new TestSearch(); file://test.testIndexStuff(); test.testSearch(); } } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++ package org.apache.fulcrum.search; /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Turbine" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * "Apache Turbine", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Hits; import org.apache.lucene.search.Searcher; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.document.Document; import org.apache.lucene.analysis.StopAnalyzer; import org.apache.lucene.analysis.Analyzer; import org.apache.fulcrum.BaseService; import java.util.List; import java.util.*; import java.io.*; /** * Title: NovusSearchService * Description: SearchService Implementation * Copyright: Copyright (c) 2001 * Company: Novus Consulting Group * @author Hugh P. Brien * @version 1.0 */ public class TurbineSearchService extends BaseService implements SearchService { String indexOutputDirectory; public TurbineSearchService() {} public Query getQuery(String searchText, String fieldName) { Analyzer analyzer = new StopAnalyzer(); Query baseQuery = null; try { baseQuery = QueryParser.parse(searchText, fieldName, analyzer); } catch (Exception ex) { file://Log.error("Error parsing Search Text in Search Service. Search Text is " + searchText + " for field " + fieldName); } return baseQuery; } public Hits search(Query query) { Searcher searcher = null; Hits searchHits = null; try { searcher = new IndexSearcher(indexOutputDirectory); searchHits = searcher.search(query); } catch (java.io.IOException ioe) { file://Log.error("Problem with find file system indexes. File not found, File not readable"); ioe.printStackTrace(); } return searchHits; } public void indexData(List listofMaps) { try { IndexWriter writer = new IndexWriter(this.indexOutputDirectory, new StopAnalyzer(), true); Iterator iterator = listofMaps.iterator(); while(iterator.hasNext()) { Map docMap = (Map)iterator.next(); Document document = BaseDocument.Document(docMap); writer.addDocument(document); } writer.close(); } catch (java.io.IOException ioe) { file://Log.error("IO Exception when attempting to run indexing service against the following directory" + this.indexOutputDirectory); } catch (Exception ex) { ex.printStackTrace(); } } /** * Create a List of Maps for test purposes */ public List createListOfMaps() { List mapList = new Vector(); Hashtable defaultMap = new Hashtable(); defaultMap.put("id", ""); defaultMap.put("name", ""); defaultMap.put("keywords", ""); defaultMap.put("content", ""); for (int i = 0; i < 1000; i++) { int modValue = i % 5; Map newMap = (Map)defaultMap.clone(); newMap.put("id", "id fields" + String.valueOf(i)); newMap.put("name", "name fields" + String.valueOf(i)); newMap.put("keywords", "keyword" + String.valueOf(i) + "test" + String.valueOf(modValue)); newMap.put("content", "content field" + String.valueOf(i)); mapList.add(newMap); } return mapList; } /** * */ public String getIndexLocation() { return this.indexOutputDirectory; } /** * Initializes the service. * * @throws InitializationException if initialization fails. */ public void init() { file://Log.info("SearchService: Init Method Started"); System.out.println("SearchService: Init Method Started"); file://this.indexOutputDirectory = getProperties().getProperty(SearchService.INDEX_OUTPUT_DIRECTORY); this.indexOutputDirectory = "."; file://Log.info(this.indexOutputDirectory); setInit(true); file://Log.info("SearchService: Init Method Ended"); System.out.println("SearchService: Init Method Ended"); } } -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
