From 19f99bba2e9abb44018015d63b0871e4ed6cf5b7 Mon Sep 17 00:00:00 2001 From: axelcl Date: Wed, 7 Sep 2005 17:25:32 +0000 Subject: [PATCH] More Lucene tests --- .../phpdt/core/tests/lucene/LuceneTest.java | 3 + .../phpdt/core/tests/lucene/PHPSearcher.java | 71 +++++++++++++++++--- 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpdt/core/tests/lucene/LuceneTest.java b/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpdt/core/tests/lucene/LuceneTest.java index fd388d3..7f824bc 100644 --- a/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpdt/core/tests/lucene/LuceneTest.java +++ b/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpdt/core/tests/lucene/LuceneTest.java @@ -91,6 +91,9 @@ public class LuceneTest extends AbstractCompilerTest { indexSearcher.getClassInfo("Overlib"); indexSearcher.getAttributeInfo("$ol_closetext"); indexSearcher.getMethodInfo("set"); + indexSearcher.getAttributePrefix("$ol_p"); + indexSearcher.getPrefixes(indexPath,"a", "$ol_p"); + } diff --git a/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpdt/core/tests/lucene/PHPSearcher.java b/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpdt/core/tests/lucene/PHPSearcher.java index 05ce6bb..d71e440 100644 --- a/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpdt/core/tests/lucene/PHPSearcher.java +++ b/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpdt/core/tests/lucene/PHPSearcher.java @@ -1,25 +1,28 @@ package net.sourceforge.phpdt.core.tests.lucene; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; -import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; -import org.apache.lucene.queryParser.ParseException; -import org.apache.lucene.queryParser.QueryParser; +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; + + // private StandardAnalyzer analyzer; public PHPSearcher(String indexPath) { try { fSearcher = new IndexSearcher(indexPath); - analyzer = new StandardAnalyzer(); + // analyzer = new StandardAnalyzer(); } catch (IOException e) { e.printStackTrace(); } @@ -28,7 +31,7 @@ public class PHPSearcher { public Hits getClassInfo(String ident) { Hits hits = null; try { - Query query = new TermQuery(new Term("c", ident)); + Query query = new TermQuery(new Term("c", ident)); hits = fSearcher.search(query); int hitCount = hits.length(); Document doc; @@ -47,7 +50,7 @@ public class PHPSearcher { public Hits getIncludeInfo(String ident) { Hits hits = null; try { -// Query query = QueryParser.parse(ident, "f", new StandardAnalyzer()); + // Query query = QueryParser.parse(ident, "f", new StandardAnalyzer()); Query query = new TermQuery(new Term("i", ident)); hits = fSearcher.search(query); int hitCount = hits.length(); @@ -67,7 +70,7 @@ public class PHPSearcher { public Hits getFunctionInfo(String ident) { Hits hits = null; try { -// Query query = QueryParser.parse(ident, "f", new StandardAnalyzer()); + // Query query = QueryParser.parse(ident, "f", new StandardAnalyzer()); Query query = new TermQuery(new Term("f", ident)); hits = fSearcher.search(query); int hitCount = hits.length(); @@ -87,7 +90,7 @@ public class PHPSearcher { public Hits getMethodInfo(String ident) { Hits hits = null; try { -// Query query = QueryParser.parse(ident, "m", new StandardAnalyzer()); + // Query query = QueryParser.parse(ident, "m", new StandardAnalyzer()); Query query = new TermQuery(new Term("m", ident)); hits = fSearcher.search(query); int hitCount = hits.length(); @@ -103,10 +106,11 @@ public class PHPSearcher { } return hits; } + public Hits getAttributeInfo(String ident) { Hits hits = null; try { -// Query query = QueryParser.parse(ident, "m", new StandardAnalyzer()); + // Query query = QueryParser.parse(ident, "m", new StandardAnalyzer()); Query query = new TermQuery(new Term("a", ident)); hits = fSearcher.search(query); int hitCount = hits.length(); @@ -122,4 +126,51 @@ public class PHPSearcher { } 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; + } } -- 1.7.1