package net.sourceforge.phpdt.core.tests.lucene; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.index.TermEnum; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; public class PHPSearcher { private IndexSearcher fSearcher; // private StandardAnalyzer analyzer; public PHPSearcher(String indexPath) { try { fSearcher = new IndexSearcher(indexPath); // analyzer = new StandardAnalyzer(); } catch (IOException e) { e.printStackTrace(); } } public Hits getClassInfo(String ident) { Hits hits = null; try { Query query = new TermQuery(new Term("c", ident)); hits = fSearcher.search(query); int hitCount = hits.length(); Document doc; for (int i = 0; (i < hitCount && i < 10); i++) { doc = hits.doc(i); for (int j = 0; j < doc.getValues("c").length; j++) { System.out.println(doc.getValues("c")[j]); } } } catch (IOException e) { e.printStackTrace(); } return hits; } public Hits getIncludeInfo(String ident) { Hits hits = null; try { // Query query = QueryParser.parse(ident, "f", new StandardAnalyzer()); Query query = new TermQuery(new Term("i", ident)); hits = fSearcher.search(query); int hitCount = hits.length(); Document doc; for (int i = 0; (i < hitCount && i < 10); i++) { doc = hits.doc(i); for (int j = 0; j < doc.getValues("i").length; j++) { System.out.println(doc.getValues("i")[j]); } } } catch (IOException e) { e.printStackTrace(); } return hits; } public Hits getFunctionInfo(String ident) { Hits hits = null; try { // Query query = QueryParser.parse(ident, "f", new StandardAnalyzer()); Query query = new TermQuery(new Term("f", ident)); hits = fSearcher.search(query); int hitCount = hits.length(); Document doc; for (int i = 0; (i < hitCount && i < 10); i++) { doc = hits.doc(i); for (int j = 0; j < doc.getValues("f").length; j++) { System.out.println(doc.getValues("f")[j]); } } } catch (IOException e) { e.printStackTrace(); } return hits; } public Hits getMethodInfo(String ident) { Hits hits = null; try { // Query query = QueryParser.parse(ident, "m", new StandardAnalyzer()); Query query = new TermQuery(new Term("m", ident)); hits = fSearcher.search(query); int hitCount = hits.length(); Document doc; for (int i = 0; (i < hitCount && i < 10); i++) { doc = hits.doc(i); for (int j = 0; j < doc.getValues("m").length; j++) { System.out.println(doc.getValues("m")[j]); } } } catch (IOException e) { e.printStackTrace(); } return hits; } public Hits getAttributeInfo(String ident) { Hits hits = null; try { // Query query = QueryParser.parse(ident, "m", new StandardAnalyzer()); Query query = new TermQuery(new Term("a", ident)); hits = fSearcher.search(query); int hitCount = hits.length(); Document doc; for (int i = 0; (i < hitCount && i < 10); i++) { doc = hits.doc(i); for (int j = 0; j < doc.getValues("a").length; j++) { System.out.println(doc.getValues("a")[j]); } } } catch (IOException e) { e.printStackTrace(); } return hits; } public List getAttributePrefix(String prefix) { Hits hits = null; ArrayList list = new ArrayList(); try { Query query = new PrefixQuery(new Term("a", prefix)); hits = fSearcher.search(query); int hitCount = hits.length(); int len = prefix.length(); Document doc; for (int i = 0; (i < hitCount && i < 10); i++) { doc = hits.doc(i); for (int j = 0; j < doc.getValues("a").length; j++) { if (prefix.equals(doc.getValues("a")[j].substring(0, len))) { list.add(doc.getValues("a")[j]); } } } } catch (IOException e) { e.printStackTrace(); } System.out.println(list.toString()); return list; } public List getPrefixes(String path, String fld, String prefix) { ArrayList list = new ArrayList(); try { IndexReader reader = IndexReader.open(path); Term target = new Term(fld, prefix); int len = prefix.length(); TermEnum tenum = reader.terms(target); Term term; do { term = tenum.term(); if (term == null || !term.field().equals(fld) || !prefix.equals(term.text().substring(0, len))) { break; } list.add(term.text()); } while (tenum.next()); } catch (IOException e) { e.printStackTrace(); } System.out.println(list.toString()); return list; } }