diff options
author | adrianocrestani <adrianocrestani@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-10 01:40:02 +0000 |
---|---|---|
committer | adrianocrestani <adrianocrestani@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-10 01:40:02 +0000 |
commit | 8e7db232ee169c8d8513c8e8a84921d905e4af2d (patch) | |
tree | 0cc43203723c019a13f23a1b46b466639ed37da0 /branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchImpl.java | |
parent | 1dff5319f17ee5bec9588675d43a84e836627a5d (diff) |
applying tuscany_2552_domain_search_phillipe_ramalho_08_08_2009.patch and tuscany_2552_domain_manager_phillipe_ramalho_08_08_2009.patch submitted to TUSCANY-2552
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@802637 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchImpl.java')
-rw-r--r-- | branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchImpl.java | 410 |
1 files changed, 300 insertions, 110 deletions
diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchImpl.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchImpl.java index 332bd249e1..8ac7b4054d 100644 --- a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchImpl.java +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchImpl.java @@ -1,189 +1,357 @@ package org.apache.tuscany.sca.domain.search.impl; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; -import java.util.List; +import java.util.HashSet; import org.apache.lucene.analysis.Analyzer; -import org.apache.lucene.document.Field; import org.apache.lucene.index.CorruptIndexException; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.queryParser.MultiFieldQueryParser; import org.apache.lucene.queryParser.ParseException; -import org.apache.lucene.queryParser.QueryParser; -import org.apache.lucene.search.HitCollector; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; +import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; -import org.apache.lucene.store.LockObtainFailedException; +import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.RAMDirectory; +import org.apache.lucene.store.SimpleFSLockFactory; import org.apache.tuscany.sca.contribution.Contribution; -import org.apache.tuscany.sca.contribution.service.ContributionRepository; import org.apache.tuscany.sca.domain.search.DocumentMap; import org.apache.tuscany.sca.domain.search.DomainSearch; import org.apache.tuscany.sca.domain.search.Result; import org.osoa.sca.annotations.AllowsPassByReference; +import org.osoa.sca.annotations.Property; import org.osoa.sca.annotations.Scope; @Scope("COMPOSITE") public class DomainSearchImpl implements DomainSearch { - private Directory dir = new RAMDirectory(); + @Property + public String indexDirectoryPath; + + private Directory dir; private Analyzer analyzer = new DomainSearchAnalyzer(); + private MultiFieldQueryParser qp = new MultiFieldQueryParser(new String[] { + SearchFields.ARTIFACT_FIELD, SearchFields.BINDING_FIELD, + SearchFields.COMPONENT_FIELD, SearchFields.COMPOSITE_FIELD, + SearchFields.CONTRIBUTION_FIELD, SearchFields.EXPORTEDBY_FIELD, + SearchFields.FILE_CONTENT_FIELD, SearchFields.IMPLEMENTS_FIELD, + SearchFields.IMPORTEDBY_FIELD, SearchFields.INCLUDEDBY_FIELD, + SearchFields.PROPERTY_KEY_FIELD, SearchFields.REFERENCE_FIELD, + SearchFields.REFERENCE_INTERFACE_CALLBACK_FIELD, + SearchFields.REFERENCE_INTERFACE_FIELD, + SearchFields.REFERENCE_NAME_FIELD, SearchFields.SERVICE_FIELD, + SearchFields.SERVICE_INTERFACE_CALLBACK_FIELD, + SearchFields.SERVICE_INTERFACE_FIELD, + SearchFields.SERVICE_NAME_FIELD, SearchFields.TYPE_FIELD, + SearchFields.VALUE_FIELD }, this.analyzer); + public DomainSearchImpl() { - // empty constructor + this.qp.setAllowLeadingWildcard(true); + + } + + private Directory getIndexDirectory() throws IOException { + + if (this.dir == null) { + + if (this.indexDirectoryPath == null + || this.indexDirectoryPath.length() == 0) { + this.dir = new RAMDirectory(); + + } else { + + try { + this.dir = new FSDirectory( + new File(this.indexDirectoryPath), + new SimpleFSLockFactory(this.indexDirectoryPath)); + + } catch (IOException e) { + System.err.println("Could not open index at " + + this.indexDirectoryPath); + + throw e; + + } + + } + + } + + return this.dir; + } @AllowsPassByReference - public void contributionAdded(ContributionRepository repository, - Contribution contribution) { - System.out.println("contributionAdded:"); - System.out.println(repository); - System.out.println(contribution); + public void contributionAdded(Contribution contribution) { + + IndexWriter indexWriter = null; try { - IndexWriter indexWriter = new IndexWriter(dir, analyzer, + indexWriter = new IndexWriter(getIndexDirectory(), this.analyzer, IndexWriter.MaxFieldLength.UNLIMITED); - try { - DomainSearchDocumentProcessorsMap docProcessors = new DomainSearchDocumentProcessorsMap(); - DocumentMap docs = new DocumentMap(); + contributionAdded(contribution, indexWriter); + + indexWriter.commit(); + + } catch (Exception e) { + + if (indexWriter != null) { + + try { + indexWriter.rollback(); + + } catch (Exception e1) { + // ignore exception + } + + } + + throw new RuntimeException("Problem while indexing!", e); + + } finally { + + if (indexWriter != null) { try { - docProcessors.process(docProcessors, docs, contribution, null, ""); + indexWriter.close(); + } catch (Exception e) { - e.printStackTrace(); + // ignore exception } - for (Document doc : docs.values()) { - indexWriter.addDocument(doc.createLuceneDocument()); + } + + } + + } + + @AllowsPassByReference + public void contributionRemoved(Contribution contribution) { + + IndexWriter indexWriter = null; + + try { + indexWriter = new IndexWriter(getIndexDirectory(), this.analyzer, + IndexWriter.MaxFieldLength.UNLIMITED); + + contributionRemoved(contribution, indexWriter); + + indexWriter.commit(); + + } catch (Exception e) { + + if (indexWriter != null) { + + try { + indexWriter.rollback(); + + } catch (Exception e1) { + // ignore exception + } + + } + + throw new RuntimeException("Problem while indexing!", e); + + } finally { + + if (indexWriter != null) { + + try { + indexWriter.close(); + + } catch (Exception e) { + // ignore exception } - } finally { - indexWriter.close(); } - // BufferedReader consoleReader = new BufferedReader( - // new InputStreamReader(System.in)); - // - // while (true) { - // System.out.print("query: "); - // String queryString = consoleReader.readLine(); - // - // if (queryString.equals("exit")) { - // break; - // } - // - // parseAndSearch(queryString, false); - // - // } - - } catch (CorruptIndexException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (LockObtainFailedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } } - public void contributionRemoved(ContributionRepository repository, - Contribution contribution) { - System.out.println("contributionRemoved:"); - System.out.println(repository); - System.out.println(contribution); + private void contributionRemoved(Contribution contribution, + IndexWriter indexWriter) throws CorruptIndexException, IOException { + + String contributionURI = contribution.getURI(); + StringBuilder sb = new StringBuilder(SearchFields.PARENT_FIELD); + sb.append(":\""); + sb.append(DomainPathAnalyzer.PATH_START); + sb.append(contributionURI); + sb.append("\" OR "); + sb.append(SearchFields.CONTRIBUTION_FIELD); + sb.append(":\""); + sb.append(contributionURI); + sb.append('"'); + + try { + Query query = this.qp.parse(sb.toString()); + indexWriter.deleteDocuments(query); + + } catch (ParseException e) { + throw new RuntimeException("Could not parse query: " + + sb.toString(), e); + } } - public void contributionUpdated(ContributionRepository repository, - Contribution oldContribution, Contribution contribution) { + private void contributionAdded(Contribution contribution, + IndexWriter indexWriter) throws CorruptIndexException, IOException { - System.out.println("contributionUpdated:"); - System.out.println(repository); - System.out.println(oldContribution); - System.out.println(contribution); + DomainSearchDocumentProcessorsMap docProcessors = new DomainSearchDocumentProcessorsMap(); + DocumentMap docs = new DocumentMap(); + + try { + docProcessors.process(docProcessors, docs, contribution, null, ""); + + } catch (Exception e) { + e.printStackTrace(); + } + + FileWriter writer = new FileWriter("indexed.txt"); + for (Document doc : docs.values()) { + org.apache.lucene.document.Document luceneDoc = doc + .createLuceneDocument(); + writer.write(luceneDoc.toString()); + writer.write('\n'); + writer.write('\n'); + indexWriter.addDocument(luceneDoc); + + } + + writer.close(); + + // BufferedReader consoleReader = new BufferedReader( + // new InputStreamReader(System.in)); + // + // while (true) { + // System.out.print("query: "); + // String queryString = consoleReader.readLine(); + // + // if (queryString.equals("exit")) { + // break; + // } + // + // parseAndSearch(queryString, false); + // + // } } - @SuppressWarnings("deprecation") - public Result[] parseAndSearch(String searchQuery, boolean highlight) { + @AllowsPassByReference + public void contributionUpdated(Contribution oldContribution, + Contribution contribution) { + + IndexWriter indexWriter = null; try { - final IndexSearcher searcher = new IndexSearcher(dir); - final DomainSearchResultProcessor resultProcessor = new DomainSearchResultProcessor( - new DomainSearchResultFactory()); + indexWriter = new IndexWriter(getIndexDirectory(), this.analyzer, + IndexWriter.MaxFieldLength.UNLIMITED); - QueryParser qp = new QueryParser("", analyzer); + contributionRemoved(oldContribution, indexWriter); + contributionAdded(contribution, indexWriter); - qp.setAllowLeadingWildcard(true); + indexWriter.commit(); - try { - final Query query = qp.parse(searchQuery); + } catch (Exception e) { - searcher.search(query, new HitCollector() { + if (indexWriter != null) { - @Override - public void collect(int doc, float score) { - try { - org.apache.lucene.document.Document document = searcher - .doc(doc); + try { + indexWriter.rollback(); - resultProcessor.process(document, query, null); + } catch (Exception e1) { + // ignore exception + } - List<?> fields = document.getFields(); - System.out.println("---------"); - System.out.println("doc = " + doc); + } - for (Object obj : fields) { - Field field = (Field) obj; - String[] values = document.getValues(field - .name()); - System.out.println(field); + throw new RuntimeException("Problem while indexing!", e); - for (String value : values) { - System.out.println("\t" + value); - } + } finally { - } + if (indexWriter != null) { - } catch (CorruptIndexException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } + try { + indexWriter.close(); - }); + } catch (Exception e) { + // ignore exception + } - } catch (ParseException e) { - System.out.println(e.getMessage()); } - Result[] results = resultProcessor.getResultRoots(); + } + + } + + public Result[] parseAndSearch(String searchQuery, final boolean highlight) + throws Exception { - // for (Result result : results) { - // System.out.println(result); - // } + final IndexSearcher searcher = new IndexSearcher(getIndexDirectory()); - System.out.println(); + DomainSearchResultProcessor resultProcessor = new DomainSearchResultProcessor( + new DomainSearchResultFactory()); - return results; + final Query query = qp.parse(searchQuery); + System.out.println("query: " + searchQuery); + + TopDocs topDocs = searcher.search(query, 1000); + + int indexed = 0; + HashSet<String> set = new HashSet<String>(); + for (ScoreDoc scoreDoc : topDocs.scoreDocs) { + org.apache.lucene.document.Document luceneDocument = searcher + .doc(scoreDoc.doc); + + resultProcessor.process(luceneDocument, null); + + indexed++; + set.add(luceneDocument.toString()); + + System.out.println(luceneDocument); - } catch (CorruptIndexException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } catch (IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); } - return null; + /* + * searcher.search(query, new HitCollector() { + * + * @Override public void collect(int doc, float score) { try { + * org.apache.lucene.document.Document document = searcher .doc(doc); + * + * luceneDocuments.put(doc, document); + * + * System.out.println(doc); + * + * } catch (CorruptIndexException e) { // TODO Auto-generated catch + * block e.printStackTrace(); } catch (IOException e) { // TODO + * Auto-generated catch block e.printStackTrace(); } } + * + * }); + */ + + System.out.println("indexed = " + indexed); + System.out.println("set.size() = " + set.size()); + + Result[] results = resultProcessor.createResultRoots(); + + if (highlight) { + + for (Result result : results) { + HighlightingUtil.highlightResult(result, query); + } + + } + + return results; } @@ -192,4 +360,26 @@ public class DomainSearchImpl implements DomainSearch { return null; } + public String highlight(String field, String text, String searchQuery) + throws Exception { + final Query query = qp.parse(searchQuery); + + return HighlightingUtil.highlight(field, query, text); + + } + + public boolean indexExists() { + + if ((this.indexDirectoryPath == null || this.indexDirectoryPath + .length() == 0) + && this.dir == null) { + + return false; + + } else { + return this.dir != null || IndexReader.indexExists(new File(this.indexDirectoryPath)); + } + + } + } |