Sorry, something ate the attachments.

Four classes follow.

You can find easily the websphinx and lucene packages easily on the net.

------------------------------------
package foo.common;

import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.lucene.document.DateField;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

import websphinx.DownloadParameters;
import websphinx.Element;
import websphinx.Link;
import websphinx.Page;

public class Crawler extends websphinx.Crawler {
        private IndexWriter writer;

        public Crawler(IndexWriter writer, String docroot) {
                super();

                try {
                        this.setRoot(new Link(docroot));
                } catch (MalformedURLException e) {
                        this.setRoot(null);
                }

                this.writer = writer;
                this.setSynchronous(true);
                this.setDomain(Crawler.SERVER);

                DownloadParameters dp = new DownloadParameters();
                dp.changeMaxThreads(1);

                this.setDownloadParameters(dp);
        }

        public void visit(Page p) {
                boolean index = false;
                index(p);
        }

        public void index(Page p) {
                StringBuffer contents = new StringBuffer();
                Document doc = new Document();

                doc.add(Field.Text("url", p.getURL().toString()));
                doc.add(
                        Field.Keyword(
                                "modified",
        
DateField.timeToString(p.getLastModified())));

                if (p.getTitle() != null) {
                        doc.add(Field.Text("title", p.getTitle()));
                }

                Element[] elements = p.getElements();
                for (int i = 0; i < elements.length; i++) {
                        if
(elements[i].getTagName().equalsIgnoreCase("meta")) {
                                String name =
elements[i].getHTMLAttribute("name", "");
                                String content =
elements[i].getHTMLAttribute("content", "");
                                if (!name.equals("")) {
                                        doc.add(Field.Text(name,
content));
                                }
                        }
                }

                try {
                        writer.addDocument(doc);
                } catch (IOException e) {
                        throw new RuntimeException(e.toString());
                }
        }

        public void noindex(Page p) {
                System.out.println("    Skipping...");
        }

        public boolean shouldVisit(Link l) {
                if(l.getURL().toString().indexOf("setLanguage") > -1)
                        return false;
                        
                return true;
        }
}
------------------------------------
package foo.common;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;

public class Indexer {

        public static void index() throws IOException {
                index("en");
                index("pt");
        }

        public static void index(String languageId) throws IOException {
                IndexWriter writer =
                        new IndexWriter(
                                new File("temp/" + languageId),
                                new StandardAnalyzer(),
                                true);

                Crawler crawler =
                        new Crawler(
                                writer,
        
"http://zanserver:8080/eloja-tomeu/catalog/welcome.do?hasFlash=no&langua
geId=" + languageId);
                
                crawler.run();

                writer.close();
        }
}
------------------------------------
package foo.catalog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import pt.co.zan.eloja.common.LocaleHolder;
import pt.co.zan.struts.eloja.common.BaseAction;

public class SearchProductAction extends BaseAction {

        public ActionForward execute(
                ActionMapping mapping,
                ActionForm form,
                HttpServletRequest request,
                HttpServletResponse response)
                throws Exception {

                super.execute(mapping, form, request, response);
                
                String languageId =
LocaleHolder.instance().getLocale().getLanguage();

                Directory fsDir = FSDirectory.getDirectory("temp/" +
languageId, false);
                IndexSearcher is = new IndexSearcher(fsDir);

                Query query =
                        QueryParser.parse(
                                request.getParameter("queryString"),
                                "Description",
                                new StandardAnalyzer());

                Hits hits = is.search(query);
                List results = new ArrayList();

                for (int i = 0; i < hits.length(); i++) {
                        Document doc = hits.doc(i);
                        Map dto = new HashMap();

                        dto.put("title", doc.get("title"));
                        dto.put("url", doc.get("url"));
                        dto.put("description", doc.get("Description"));

                        results.add(dto);
                }

                request.setAttribute("resultsBean", results);

                return mapping.findForward("success");
        }
}
------------------------------------
package foo.catalog.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import pt.co.zan.struts.eloja.common.BaseAction;
import pt.co.zan.struts.eloja.common.Indexer;

public class IndexingAction extends BaseAction {

        public ActionForward execute(
                ActionMapping mapping,
                ActionForm form,
                HttpServletRequest request,
                HttpServletResponse response)
                throws Exception {

                super.execute(mapping, form, request, response);

                Indexer.index();

                return null;
        }

}
------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to