[opencms-dev] Solution to opencms search problem

GASSIMBE Hamidou gassimbe at yahoo.fr
Fri Apr 7 13:11:28 CEST 2006


to search docment on my web site with opencms search functionality, I've got some problem (when i try to search when i am not connect to opencsms, i get no result).
 I find a solution: i use lucence library to resolve problem, to try the code below, you must have lucene-1.4.3.jar on your $CATALINA_HOME/web-inf/lib (opencms 6.0.2 is installed whith this jar). If you have probleme download the laste version of lucene jar (lucene-core-1.9.1.jar) and put it to $CATALINA_HOME/web-inf/lib and restart tomcat.
 
 this a example of jsp page
 
                 <%@page contentType="text/html"%>
 
 <%@page import="org.apache.lucene.document.Document, org.apache.lucene.search.IndexSearcher, org.apache.lucene.search.Query" %>
 
 <%@page import="org.apache.lucene.search.Hits, org.apache.lucene.store.FSDirectory, org.apache.lucene.store.Directory" %>
 
 <%@page import="org.apache.lucene.queryParser.QueryParser, org.apache.lucene.queryParser.MultiFieldQueryParser, org.apache.lucene.analysis.standard.StandardAnalyzer, java.io.File" %>
 
 <%@page pageEncoding="ISO-8859-1"%>
 
 <%
 
 String queryExpression = "opencms"; //Your query expression
 
 //You must specify where is index folder
 String indexPath="$CATALINA_HOME/web-inf/index/siteTest"; 
 
 File indexDir = new File( indexPath);
 
 Directory fsDir = FSDirectory.getDirectory(indexDir, false);
 
 IndexSearcher is = new IndexSearcher(fsDir);
 
 java.util.StringTokenizer token = null;
 
 String [] fields = new String [3];
 
 fields [0]="content";
 
 fields [1]="title";
 
 fields [2]="description";
 
 
 
 Query query = MultiFieldQueryParser.parse(queryExpression, fields, new StandardAnalyzer());
 
 Hits hits = is.search(query);
 
 out.print("<h1>" + hits.length() + " document(s) has been found for [" + queryExpression + "]</h1>"); //Number of result
 
 for (int i = 0; i < hits.length(); i++) {
 
 Document doc = hits.doc(i);
 
 out.print("<h2>" + (i+1) + ". " + "<a href='" + doc.get("path") + "'>" + doc.get("path") + "</a></h2>"); //Show the path of the file
 
 
 //show the 10 first word of file as introuction
 
 token = new java.util.StringTokenizer(doc.get("content"));
 
 for (int j = 0; j < 10 ; j++ ) {
 
 if (token.hasMoreElements())
 
 out.print(token.nextToken()+" ");
 
 else
 
 break;
 
 }
 
 out.print("...<hr size=1>");
 
 }
 
 
 %>
 
 
 
 
		
---------------------------------
 Nouveau : téléphonez moins cher avec Yahoo! Messenger ! Découvez les tarifs exceptionnels pour appeler la France et l'international.Téléchargez la version beta.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20060407/b2d6a7c9/attachment.htm>


More information about the opencms-dev mailing list