package com.telnext.search; import java.io.IOException; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Vector; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.queryParser.*; import org.apache.lucene.search.*; public class SearchLucene extends com.telnext.bean.BaseBean { private String q; private String o = ""; //private StringMatrix res; private Searcher searcher; private static String luceneIndexPathArt = "/home/www/www.webup.telnext.com/htdocs/Lucene/Articoli"; private static String luceneIndexPathSource = "/home/www/www.webup.telnext.com/htdocs/Lucene/Schede"; public SearchLucene() throws Exception{ clear(); } public void clear()throws Exception { } public void add() throws Exception{} public void load() throws Exception{} public void edt() throws Exception{} public void del() throws Exception {} public void cpy()throws Exception {} public void ord()throws Exception {} public void mov()throws Exception {} public static void main(String argv[]) throws Exception { SearchLucene search = new SearchLucene(); //String lang = "1"; search.setQ("HD"); if (argv.length != 0) { search.setQ(argv[0]); } search.fnd(1, "source"); } public Hits fnd(int idlingua, String type) throws Exception { Hits hits = null; if(type.equalsIgnoreCase("ART")){ this.searcher = new IndexSearcher(luceneIndexPathArt); }else{ System.out.println("Utilizzo l'indice: " + luceneIndexPathSource + " con query: " + q); this.searcher = new IndexSearcher(luceneIndexPathSource); } try { if (!this.q.equals("")) { hits = this.getLuceneRes(type); } return hits; } catch (Exception ex) {throw ex;} } public void closeIndex(){ try { searcher.close(); } catch (IOException ex) { Logger.getLogger(SearchLucene.class.getName()).log(Level.SEVERE, null, ex); } } public Vector getIndexFields(String type) throws Exception{ Vector campi = new Vector(); if(type.equalsIgnoreCase("ART")){ this.searcher = new IndexSearcher(luceneIndexPathArt); }else{ this.searcher = new IndexSearcher(luceneIndexPathSource); } Document tmp = searcher.doc(0); Field fld = null; Iterator itr = tmp.getFields().iterator(); while(itr.hasNext()){ fld = (Field) itr.next(); campi.add(fld.name()); } Collections.sort(campi); return campi; } /*public StringMatrix fndAll() throws Exception { return this.fndAll(1); } public StringMatrix fndAll(int idlingua) throws Exception { this.searcher = new IndexSearcher(luceneIndexPath); try { if(!this.q.equals("")) { Hits hits = this.getLuceneRes(); for (int i = 0; i < hits.length(); i++) res.addRow(new String[] { hits.doc(i).get("idrub"), hits.doc(i).get("idtypobj"), hits.doc(i).get("idrubs"), hits.doc(i).get("data1"), hits.doc(i).get("tit1"), hits.doc(i).get("sottotit1"), String.valueOf(hits.score(i)) }); } return res; } catch (Exception ex) {throw ex;} finally {searcher.close();} }*/ /*public Hits getSingleLuceneRes() throws Exception { Searcher searcher = new IndexSearcher(indexPath); QueryParser queryParser = new QueryParser("des", new StandardAnalyzer()); Query query = queryParser.parse(q); Hits hits = searcher.search(query); for (int i = 0; i < hits.length(); i++) { System.out.println(i+") "+hits.doc(i).get("idrub") + "; Title: " + hits.doc(i).get("tit1") + "; Score: " + hits.score(i)); } searcher.close(); return hits; }*/ public Hits getLuceneRes(String type) throws Exception { String[] fields; if(type.equalsIgnoreCase("ART")){ fields = new String[] {"Codice Articolo", "Descrizione articolo"}; }else{ fields = new String[] {"Nome Scheda", "Source"}; } Analyzer analyzer = new StandardAnalyzer(); System.out.println("QUERY: " + q); MultiFieldQueryParser queryParser = new MultiFieldQueryParser(fields, new StandardAnalyzer()); queryParser.setDefaultOperator(QueryParser.OR_OPERATOR); //queryParser.setDefaultOperator(QueryParser.Operator.AND); Query query = queryParser.parse(q); Hits hits = null; if(this.o.equalsIgnoreCase("")){ hits = searcher.search(query); }else{ hits = searcher.search(query, new Sort(fields[Integer.parseInt(o)])); } //per la suddivisione in pagine dei risultati int n = hits.length(); System.out.println("Numero risultati: " + n); Iterator itr = hits.iterator(); while(itr.hasNext()){ Hit lucene = itr.next(); lucene.getScore(); /*System.out.println("Codice: " + "" + lucene.getDocument().getField("Codice Articolo").stringValue() + " " + "Descrizione: " + lucene.getDocument().getField("Descrizione articolo").stringValue() + "" + " score: " + lucene.getScore() + "%");*/ } return hits; } public int getCountPagtot() { int n = 0; if(n!=0) ; // this.pagtot=(int)((double)this.n / this.rowperpag+0.99999); return n; } public String getQ() { return q; } public void setQ(String q) { this.q = q; } public String getO() { return o; } public void setO(String o) { this.o = o; } }