*/
public SearchResults searchForTerm( String principal, List<String> selectedRepos, String term, SearchResultLimits limits );
+ /**
+ * Search for a specific term from the previous search results.
+ *
+ * @param principal the user doing the search.
+ * @param selectedRepos the repositories to search from.
+ * @param term the term to search for.
+ * @param limits the limits to apply to the search results.
+ * @param previousSearchTerms the list of the previous search terms.
+ * @return the results
+ */
+ public SearchResults searchForTerm( String principal, List<String> selectedRepos, String term,
+ SearchResultLimits limits, List<String> previousSearchTerms );
+
/**
* Search for the specific bytecode across all repositories.
*
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.BooleanQuery;
+import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.MultiSearcher;
+import org.apache.lucene.search.QueryWrapperFilter;
import org.apache.lucene.search.Searchable;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.ConfigurationNames;
QueryParser parser = new MultiFieldQueryParser( new String[]{HashcodesKeys.MD5, HashcodesKeys.SHA1},
new HashcodesHandlers().getAnalyzer() );
LuceneQuery query = new LuceneQuery( parser.parse( checksum ) );
- SearchResults results = searchAll( query, limits, indexes );
+ SearchResults results = searchAll( query, limits, indexes, null );
results.getRepositories().addAll( this.localIndexedRepositories );
return results;
{
QueryParser parser = new BytecodeHandlers().getQueryParser();
LuceneQuery query = new LuceneQuery( parser.parse( term ) );
- SearchResults results = searchAll( query, limits, indexes );
+ SearchResults results = searchAll( query, limits, indexes, null );
results.getRepositories().addAll( this.localIndexedRepositories );
return results;
return new SearchResults();
}
+
public SearchResults searchForTerm( String principal, List<String> selectedRepos, String term, SearchResultLimits limits )
+ {
+ return searchForTerm( principal, selectedRepos, term, limits, null );
+ }
+
+ public SearchResults searchForTerm( String principal, List<String> selectedRepos, String term,
+ SearchResultLimits limits, List<String> previousSearchTerms )
{
List<RepositoryContentIndex> indexes = getFileContentIndexes( principal, selectedRepos );
List<RepositoryContentIndex> bytecodeIndices = getBytecodeIndexes( principal, selectedRepos );
try
{
QueryParser parser = new FileContentHandlers().getQueryParser();
- LuceneQuery query = new LuceneQuery( parser.parse( term ) );
- SearchResults results = searchAll( query, limits, indexes );
+ LuceneQuery query = null;
+ SearchResults results = null;
+ if( previousSearchTerms == null || previousSearchTerms.isEmpty() )
+ {
+ query = new LuceneQuery( parser.parse( term ) );
+ results = searchAll( query, limits, indexes, null );
+ }
+ else
+ {
+ // AND the previous search terms
+ BooleanQuery booleanQuery = new BooleanQuery();
+ for( String previousSearchTerm : previousSearchTerms )
+ {
+ booleanQuery.add( parser.parse( previousSearchTerm ), BooleanClause.Occur.MUST );
+ }
+
+ query = new LuceneQuery( booleanQuery );
+ Filter filter = new QueryWrapperFilter( parser.parse( term ) );
+ results = searchAll( query, limits, indexes, filter );
+ }
results.getRepositories().addAll( this.localIndexedRepositories );
return results;
}
// empty results.
- return new SearchResults();
+ return new SearchResults();
}
-
- private SearchResults searchAll( LuceneQuery luceneQuery, SearchResultLimits limits, List<RepositoryContentIndex> indexes )
+
+ private SearchResults searchAll( LuceneQuery luceneQuery, SearchResultLimits limits, List<RepositoryContentIndex> indexes, Filter filter )
{
org.apache.lucene.search.Query specificQuery = luceneQuery.getLuceneQuery();
searcher = new MultiSearcher( searchables );
// Perform the search.
- Hits hits = searcher.search( specificQuery );
+ Hits hits = null;
+ if( filter != null )
+ {
+ hits = searcher.search( specificQuery, filter );
+ }
+ else
+ {
+ hits = searcher.search( specificQuery );
+ }
int hitCount = hits.length();
"org","org2","org3","org4","org5","org6","org7"
};
- assertSearchResults( expectedRepos, expectedResults, search, "org" );
+ assertSearchResults( expectedRepos, expectedResults, search, "org", null );
}
public void testSearchTerm_Junit()
"junit","junit2","junit3"
};
- assertSearchResults( expectedRepos, expectedResults, search, "junit" );
+ assertSearchResults( expectedRepos, expectedResults, search, "junit", null );
}
public void testSearchInvalidTerm()
// Nothing.
};
- assertSearchResults( expectedRepos, expectedResults, search, "monosodium" );
+ assertSearchResults( expectedRepos, expectedResults, search, "monosodium", null );
}
- private void assertSearchResults( String expectedRepos[], String expectedResults[], CrossRepositorySearch search, String term )
+ public void testSearchWithinSearchResults()
+ throws Exception
+ {
+ CrossRepositorySearch search = lookupCrossRepositorySearch();
+
+ String expectedRepos[] = new String[] {
+ TEST_DEFAULT_REPO_ID
+ };
+
+ String expectedResults[] = new String[] {
+ "org","org2","org3","org4","org5","org6","org7"
+ };
+
+ // first search
+ assertSearchResults( expectedRepos, expectedResults, search, "org", null );
+
+ List<String> previousSearchTerms = new ArrayList<String>();
+ previousSearchTerms.add( "org" );
+ String secondSearchExpectedResults[] = new String[] {
+ "org.apache.maven.archiva.record", "org.apache.maven.archiva.record2",
+ "org.apache.maven.archiva.record3", "org.apache.maven.archiva.record4",
+ "org.apache.maven.archiva.record5", "org.apache.maven.archiva.record6",
+ "org.apache.maven.archiva.record7"
+ };
+
+ //second search
+ assertSearchResults( expectedRepos, secondSearchExpectedResults, search, "org.apache.maven.archiva.record",
+ previousSearchTerms );
+
+ previousSearchTerms.add( "org.apache.maven.archiva.record" );
+ String thirdSearchExpectedResults[] = new String[] {
+ "junit", "junit2", "junit3"
+ };
+
+ //third search
+ assertSearchResults( expectedRepos, thirdSearchExpectedResults, search, "junit", previousSearchTerms );
+ }
+
+ private void assertSearchResults( String expectedRepos[], String expectedResults[], CrossRepositorySearch search,
+ String term, List<String> previousSearchTerms )
throws Exception
{
SearchResultLimits limits = new SearchResultLimits( 0 );
List<String> selectedRepos = new ArrayList<String>();
selectedRepos.addAll( Arrays.asList( expectedRepos ) );
- SearchResults results = search.searchForTerm( "guest", selectedRepos, term, limits );
+ SearchResults results = null;
+ if( previousSearchTerms == null )
+ {
+ results = search.searchForTerm( "guest", selectedRepos, term, limits );
+ }
+ else
+ {
+ results = search.searchForTerm( "guest", selectedRepos, term, limits, previousSearchTerms );
+ }
assertNotNull( "Search Results should not be null.", results );
assertEquals( "Repository Hits", expectedRepos.length, results.getRepositories().size() );
*/
import java.net.MalformedURLException;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
private int currentPage = 0;
private int totalPages;
+
+ private boolean searchResultsOnly;
+
+ private String completeQueryString;
+
+ private static final String COMPLETE_QUERY_STRING_SEPARATOR = ";";
public String quickSearch()
throws MalformedURLException, RepositoryIndexException, RepositoryIndexSearchException
return GlobalResults.ACCESS_TO_NO_REPOS;
}
- results = crossRepoSearch.searchForTerm( getPrincipal(), selectedRepos, q, limits );
+ if( searchResultsOnly && !completeQueryString.equals( "" ) )
+ {
+ results = crossRepoSearch.searchForTerm( getPrincipal(), selectedRepos, q, limits, parseCompleteQueryString() );
+ }
+ else
+ {
+ completeQueryString = "";
+ results = crossRepoSearch.searchForTerm( getPrincipal(), selectedRepos, q, limits );
+ }
if ( results.isEmpty() )
{
* to result in a higher score.
* - Joakim
*/
-
+
+ if( !isEqualToPreviousSearchTerm( q ) )
+ {
+ buildCompleteQueryString( q );
+ }
+
return SUCCESS;
}
return Collections.emptyList();
}
+ private void buildCompleteQueryString( String searchTerm )
+ {
+ if( searchTerm.indexOf( COMPLETE_QUERY_STRING_SEPARATOR ) != -1 )
+ {
+ searchTerm = StringUtils.remove( searchTerm, COMPLETE_QUERY_STRING_SEPARATOR );
+ }
+
+ if( completeQueryString == null || "".equals( completeQueryString ) )
+ {
+ completeQueryString = searchTerm;
+ }
+ else
+ {
+ completeQueryString = completeQueryString + COMPLETE_QUERY_STRING_SEPARATOR + searchTerm;
+ }
+ }
+
+ private List<String> parseCompleteQueryString()
+ {
+ List<String> parsedCompleteQueryString = new ArrayList<String>();
+ String[] parsed = StringUtils.split( completeQueryString, COMPLETE_QUERY_STRING_SEPARATOR );
+ CollectionUtils.addAll( parsedCompleteQueryString, parsed );
+
+ return parsedCompleteQueryString;
+ }
+
+ private boolean isEqualToPreviousSearchTerm( String searchTerm )
+ {
+ if( !"".equals( completeQueryString ) )
+ {
+ String[] parsed = StringUtils.split( completeQueryString, COMPLETE_QUERY_STRING_SEPARATOR );
+ if( StringUtils.equalsIgnoreCase( searchTerm, parsed[ parsed.length - 1 ] ) )
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
public String getQ()
{
return q;
{
this.totalPages = totalPages;
}
+
+ public boolean isSearchResultsOnly()
+ {
+ return searchResultsOnly;
+ }
+
+ public void setSearchResultsOnly( boolean searchResultsOnly )
+ {
+ this.searchResultsOnly = searchResultsOnly;
+ }
+
+ public String getCompleteQueryString()
+ {
+ return completeQueryString;
+ }
+
+ public void setCompleteQueryString( String completeQueryString )
+ {
+ this.completeQueryString = completeQueryString;
+ }
}
<div id="searchBox">
<ww:form method="get" action="quickSearch" validate="true">
<ww:textfield label="Search for" size="50" name="q"/>
+ <ww:checkbox label="Search Results Only" name="searchResultsOnly"/>
+ <ww:hidden name="completeQueryString" value="${completeQueryString}"/>
<ww:submit label="Go!"/>
</ww:form>
<ww:url action="quickSearch" namespace="/">
<ww:param name="q" value="%{'${q}'}"/>
<ww:param name="currentPage" value="%{'${i}'}"/>
+ <ww:param name="searchResultsOnly" value="%{'${searchResultsOnly}'}"/>
+ <ww:param name="completeQueryString" value="%{'${completeQueryString}'}"/>
</ww:url>
</c:set>
<a href="${specificPageUrl}">${i + 1}</a>