Refactor of the SiteService Lucene query to support users explicitly searching for "*" in the site-finder UI.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29015 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Neil McErlean
2011-07-14 09:11:09 +00:00
parent 68956f2c48
commit 7c65a094d5
2 changed files with 38 additions and 3 deletions

View File

@@ -750,13 +750,17 @@ public class SiteServiceImpl extends AbstractLifecycleBean implements SiteServic
StringBuilder query = new StringBuilder(128); StringBuilder query = new StringBuilder(128);
query.append("+PARENT:\"").append(siteRoot.toString()).append('"'); query.append("+PARENT:\"").append(siteRoot.toString()).append('"');
final boolean nameFilterIsPresent = filter != null && filter.length() > 0; final boolean filterIsPresent = filter != null && filter.length() > 0;
// The filter string is only used in the Lucene query if it restricts results.
// A search for name/title/description = "*" does not need to be put into the Lucene query.
// This allows users to search for "*" in the site-finder.
final boolean filterIsPresentAndNecessary = filterIsPresent && !filter.equals("*");
final boolean sitePresetFilterIsPresent = sitePresetFilter != null && sitePresetFilter.length() > 0; final boolean sitePresetFilterIsPresent = sitePresetFilter != null && sitePresetFilter.length() > 0;
if (nameFilterIsPresent || sitePresetFilterIsPresent) if (filterIsPresentAndNecessary || sitePresetFilterIsPresent)
{ {
query.append(" +("); query.append(" +(");
if (nameFilterIsPresent) if (filterIsPresentAndNecessary)
{ {
String escNameFilter = AbstractLuceneQueryParser.escape(filter.replace('"', ' ')); String escNameFilter = AbstractLuceneQueryParser.escape(filter.replace('"', ' '));

View File

@@ -605,6 +605,37 @@ public class SiteServiceImplTest extends BaseAlfrescoSpringTest
} }
} }
/**
* This test method ensures that searches with wildcards work as they should
*/
public void testfindSitesWithWildcardTitles() throws Exception
{
// How many sites are there already in the repo?
List<SiteInfo> preexistingSites = this.siteService.findSites(null, null, 0);
final int preexistingSitesCount = preexistingSites.size();
// Create some test sites
//
// Note that the shortName can't contain an asterisk but the title can.
this.siteService.createSite(TEST_SITE_PRESET, "siteAlpha", "asterix", TEST_DESCRIPTION, SiteVisibility.PUBLIC);
this.siteService.createSite(TEST_SITE_PRESET, "siteBeta", "asterix*obelix", TEST_DESCRIPTION, SiteVisibility.PUBLIC);
// Get sites by matching title
List<SiteInfo> sites = this.siteService.findSites("asterix", null, 0);
assertNotNull(sites);
// As the name & description do not contain "asterix", this will become a search for sites whose titles match "asterix"
assertEquals("Matched wrong number of sites with title equal to 'asterix'", 2, sites.size());
// This means 'find all'
sites = this.siteService.findSites("*", null, 0);
assertNotNull(sites);
assertEquals("Matched wrong number of sites using '*'", preexistingSitesCount + 2, sites.size());
sites = this.siteService.findSites("as?erix", null, 0);
assertNotNull(sites);
assertEquals("Matched wrong number of sites using '?'", 2, sites.size());
}
public void testGetSite() public void testGetSite()
{ {
// Get a site that isn't there // Get a site that isn't there