From 8daf21e21f61d1fb06dd131f343bee1286c907b3 Mon Sep 17 00:00:00 2001 From: James William Dumay Date: Sat, 13 Dec 2008 06:18:37 +0000 Subject: [PATCH] Improved search results by filtering out duplicates and querying the database for versions on quicksearch git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/archiva-search-improvements@726173 13f79535-47bb-0310-9956-ffa450edef68 --- .../archiva-base/archiva-indexer/pom.xml | 4 +++ .../search/DefaultCrossRepositorySearch.java | 15 ++++++++-- .../indexer/search/SearchResultHit.java | 28 +++++++++++------- .../archiva/indexer/search/SearchResults.java | 18 +++++------- .../archiva/web/action/SearchAction.java | 17 +++++++++-- .../src/main/webapp/WEB-INF/jsp/results.jsp | 29 +++++++++---------- .../webapp/WEB-INF/tags/showArtifactLink.tag | 5 +--- pom.xml | 5 ++++ 8 files changed, 77 insertions(+), 44 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-indexer/pom.xml b/archiva-modules/archiva-base/archiva-indexer/pom.xml index 31f88e679..13b1b2b92 100644 --- a/archiva-modules/archiva-base/archiva-indexer/pom.xml +++ b/archiva-modules/archiva-base/archiva-indexer/pom.xml @@ -40,6 +40,10 @@ org.apache.lucene lucene-core + + org.apache.lucene + lucene-queries + org.codehaus.plexus plexus-spring diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearch.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearch.java index 624e4bc50..b9f35d7f2 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearch.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearch.java @@ -28,8 +28,11 @@ import org.apache.lucene.queryParser.MultiFieldQueryParser; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanFilter; import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.DuplicateFilter; import org.apache.lucene.search.Filter; +import org.apache.lucene.search.FilterClause; import org.apache.lucene.search.Hits; import org.apache.lucene.search.MultiSearcher; import org.apache.lucene.search.Query; @@ -46,6 +49,7 @@ import org.apache.maven.archiva.indexer.RepositoryIndexSearchException; import org.apache.maven.archiva.indexer.bytecode.BytecodeHandlers; import org.apache.maven.archiva.indexer.bytecode.BytecodeKeys; import org.apache.maven.archiva.indexer.filecontent.FileContentHandlers; +import org.apache.maven.archiva.indexer.filecontent.FileContentKeys; import org.apache.maven.archiva.indexer.hashcodes.HashcodesHandlers; import org.apache.maven.archiva.indexer.hashcodes.HashcodesKeys; import org.apache.maven.archiva.indexer.lucene.LuceneEntryConverter; @@ -269,15 +273,22 @@ public class DefaultCrossRepositorySearch // Create a multi-searcher for looking up the information. searcher = new MultiSearcher( searchables ); + BooleanFilter booleanFilter = new BooleanFilter(); + DuplicateFilter artifactIdDuplicateFilter = new DuplicateFilter(FileContentKeys.ARTIFACTID_EXACT); + booleanFilter.add(new FilterClause(artifactIdDuplicateFilter, BooleanClause.Occur.MUST)); + DuplicateFilter groupIdDuplicateFilter = new DuplicateFilter(FileContentKeys.GROUPID_EXACT); + booleanFilter.add(new FilterClause(groupIdDuplicateFilter, BooleanClause.Occur.MUST)); + // Perform the search. Hits hits = null; if ( filter != null ) { - hits = searcher.search( specificQuery, filter ); + booleanFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST)); + hits = searcher.search( specificQuery, booleanFilter ); } else { - hits = searcher.search( specificQuery ); + hits = searcher.search( specificQuery, booleanFilter ); } int hitCount = hits.length(); diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResultHit.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResultHit.java index 05c3a1aef..ba3bca500 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResultHit.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResultHit.java @@ -48,10 +48,9 @@ public class SearchResultHit private String repositoryId = ""; - // Advanced hit, if artifact, all versions of artifact - private List artifacts = new ArrayList(); + private List versions = new ArrayList(); - private List versions = new ArrayList(); + private ArchivaArtifact artifact; public String getContext() { @@ -88,11 +87,10 @@ public class SearchResultHit this.artifactId = artifactId; } - public void addArtifact( ArchivaArtifact artifact ) + public void setArtifact( ArchivaArtifact artifact ) { - this.artifacts.add( artifact ); - - String ver = artifact.getVersion(); + this.artifact = artifact; + final String ver = artifact.getVersion(); if ( !this.versions.contains( ver ) ) { @@ -115,9 +113,9 @@ public class SearchResultHit } } - public List getArtifacts() + public ArchivaArtifact getArtifact() { - return artifacts; + return artifact; } public String getGroupId() @@ -135,11 +133,21 @@ public class SearchResultHit return version; } - public List getVersions() + public void setVersion(String version) + { + this.version = version; + } + + public List getVersions() { return versions; } + public void setVersions(List versions) + { + this.versions = versions; + } + public String getRepositoryId() { return repositoryId; diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResults.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResults.java index 50ed79b28..ecb01fbcf 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResults.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResults.java @@ -40,7 +40,7 @@ public class SearchResults { private List repositories = new ArrayList(); - private Map hits = new HashMap(); + private Map hits = new HashMap(); private int totalHits; @@ -82,7 +82,7 @@ public class SearchResults } hit.setRepositoryId( bytecode.getRepositoryId() ); - hit.addArtifact( bytecode.getArtifact() ); + hit.setArtifact( bytecode.getArtifact() ); hit.setContext( null ); // TODO: provide context on why this is a valuable hit. this.hits.put( key, hit ); @@ -111,18 +111,16 @@ public class SearchResults hit = new SearchResultHit(); } - hit.addArtifact( hashcodes.getArtifact() ); + hit.setArtifact( hashcodes.getArtifact() ); hit.setContext( null ); // TODO: provide context on why this is a valuable hit. - this.hits.put( key, hit ); + hits.put( key, hit ); } public void addFileContentHit( FileContentRecord filecontent ) { - String key = filecontent.getPrimaryKey(); - - SearchResultHit hit = (SearchResultHit) this.hits.get( key ); - + final String key = filecontent.getPrimaryKey(); + SearchResultHit hit = hits.get( key ); if ( hit == null ) { // Only need to worry about this hit if it is truely new. @@ -135,7 +133,7 @@ public class SearchResults // Test for possible artifact reference ... if( filecontent.getArtifact() != null ) { - hit.addArtifact( filecontent.getArtifact() ); + hit.setArtifact( filecontent.getArtifact() ); } this.hits.put( key, hit ); @@ -147,7 +145,7 @@ public class SearchResults * * @return the list of {@link SearchResultHit} objects. */ - public List getHits() + public List getHits() { return new ArrayList( hits.values() ); } diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/SearchAction.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/SearchAction.java index 939dbc51a..4965ff45a 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/SearchAction.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/SearchAction.java @@ -46,6 +46,8 @@ import org.apache.maven.archiva.security.UserRepositories; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.Preparable; +import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint; +import org.apache.maven.archiva.indexer.search.SearchResultHit; /** * Search all indexed fields by the given criteria. @@ -127,8 +129,6 @@ public class SearchAction private boolean fromResultsPage; - private int num; - public boolean isFromResultsPage() { return fromResultsPage; @@ -274,6 +274,18 @@ public class SearchAction buildCompleteQueryString( q ); } + //Lets get the versions for the artifact we just found and display them + //Yes, this is in the lucene index but its more challenging to get them out when we are searching by project + for (SearchResultHit resultHit : results.getHits()) + { + final List versions = dao.query(new UniqueVersionConstraint(getObservableRepos(), resultHit.getGroupId(), resultHit.getArtifactId())); + if (versions != null && !versions.isEmpty()) + { + resultHit.setVersion(null); + resultHit.setVersions(versions); + } + } + return SUCCESS; } @@ -329,7 +341,6 @@ public class SearchAction catch ( AccessDeniedException e ) { getLogger().warn( e.getMessage(), e ); - // TODO: pass this onto the screen. } catch ( ArchivaSecurityException e ) { diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/results.jsp b/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/results.jsp index 57ba124db..971f00252 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/results.jsp +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/results.jsp @@ -20,7 +20,7 @@ <%@ taglib uri="/struts-tags" prefix="s" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> -<%@ taglib prefix="my" tagdir="/WEB-INF/tags" %> +<%@ taglib prefix="archiva" tagdir="/WEB-INF/tags" %> @@ -135,10 +135,10 @@ - + - - + + @@ -151,10 +151,10 @@ - + - - + + @@ -259,12 +259,12 @@

- + +

- +

@@ -292,12 +292,11 @@

- +

- +

diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/tags/showArtifactLink.tag b/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/tags/showArtifactLink.tag index a5327a1a3..3377bf1bb 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/tags/showArtifactLink.tag +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/tags/showArtifactLink.tag @@ -30,10 +30,7 @@ <%@ attribute name="repositoryId" %> - ${repositoryId} - : - - + diff --git a/pom.xml b/pom.xml index 77a8d7ca3..48f84c4cd 100644 --- a/pom.xml +++ b/pom.xml @@ -518,6 +518,11 @@ lucene-core 2.4.0
+ + org.apache.lucene + lucene-queries + 2.4.0 + javax.mail mail -- 2.39.5