* @see ESContentletIndexAPI
*/
@Test
public void testSearch () throws Exception {
SiteSearchAPI siteSearchAPI = APILocator.getSiteSearchAPI();
//*****************************************************************
//Verify if we already have and site search index, if not lets create one...
IndiciesAPI.IndiciesInfo indiciesInfo = APILocator.getIndiciesAPI().loadIndicies();
String currentSiteSearchIndex = indiciesInfo.site_search;
String indexName = currentSiteSearchIndex;
if ( currentSiteSearchIndex == null ) {
indexName = SiteSearchAPI.ES_SITE_SEARCH_NAME + "_" + ESContentletIndexAPI.timestampFormatter.format( new Date() );
APILocator.getSiteSearchAPI().createSiteSearchIndex( indexName, null, 1 );
APILocator.getSiteSearchAPI().activateIndex( indexName );
}
//*****************************************************************
//Creating a test structure
Structure testStructure = loadTestStructure();
//Creating a test contentlet
Contentlet testContentlet = loadTestContentlet( testStructure );
//Creating a test html page
HTMLPage testHtmlPage = loadHtmlPage( testContentlet );
//*****************************************************************
//Build a site search result in order to add it to the index
VersionInfo versionInfo = APILocator.getVersionableAPI().getVersionInfo( testHtmlPage.getIdentifier() );
String docId = testHtmlPage.getIdentifier() + "_" + defaultLanguage.getId();
SiteSearchResult res = new SiteSearchResult( testHtmlPage.getMap() );
res.setLanguage( defaultLanguage.getId() );
res.setFileName( testHtmlPage.getFriendlyName() );
res.setModified( versionInfo.getVersionTs() );
res.setHost( defaultHost.getIdentifier() );
res.setMimeType( "text/html" );
res.setContentLength( 1 );//Just sending something different than 0
res.setContent( stemmerText );
res.setUri( testHtmlPage.getURI() );
res.setUrl( testHtmlPage.getPageUrl() );
res.setId( docId );
//Adding it to the index
siteSearchAPI.putToIndex( indexName, res );
isDocIndexed( docId );
try {
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++STEMMERS+++++++++++++++++++++++++
/*
NOTE: THE CONTENT TEXT DOES NOT CONTAIN THE ROOT WORDS, THIS IS JUST THE REFERENCE TEXT SHOWING HOW SHOULD WORKS!!
A stemmer for English, for example, should identify the string 'cats' (and possibly 'catlike', 'catty' etc.) as based on the root 'cat',
and 'stemmer', 'stemming', 'stemmed' as based on 'stem'.
A stemming algorithm reduces the words 'fishing', 'fished', 'fish', and 'fisher' to the
root word, 'fish'.
On the other hand, 'argue', 'argued', 'argues', 'arguing', and 'argus' reduce to the stem 'argu' (illustrating the case where the stem is
not itself a word or root) but 'argument' and 'arguments' reduce to the stem 'argument'.
NOTE: THE CONTENT TEXT DOES NOT CONTAIN THE ROOT WORDS, THIS IS JUST THE REFERENCE TEXT SHOWING HOW SHOULD WORKS!!
*/
//Testing the stemer
SiteSearchResults siteSearchResults = siteSearchAPI.search( indexName, "argu", 0, 100 );
//Validations
assertTrue( siteSearchResults.getError() == null || siteSearchResults.getError().isEmpty() );
assertTrue( siteSearchResults.getTotalResults() > 0 );
String highLights = siteSearchResults.getResults().get( 0 ).getHighLights()[0];
assertTrue( highLights.contains( "<em>argue</em>" ) );
assertTrue( highLights.contains( "<em>argued</em>" ) );
assertTrue( highLights.contains( "<em>argues</em>" ) );
assertTrue( highLights.contains( "<em>arguing</em>" ) );
//assertTrue( highLights.contains( "<em>argus</em>" ) );//Not found..., verify this....
//Testing the stemer
siteSearchResults = siteSearchAPI.search( indexName, "cats", 0, 100 );
//Validations
assertTrue( siteSearchResults.getError() == null || siteSearchResults.getError().isEmpty() );
assertTrue( siteSearchResults.getTotalResults() > 0 );
highLights = siteSearchResults.getResults().get( 0 ).getHighLights()[0];
assertTrue( highLights.contains( "<em>cat</em>" ) );
//Testing the stemer
siteSearchResults = siteSearchAPI.search( indexName, "stem", 0, 100 );
//Validations
assertTrue( siteSearchResults.getError() == null || siteSearchResults.getError().isEmpty() );
assertTrue( siteSearchResults.getTotalResults() > 0 );
highLights = siteSearchResults.getResults().get( 0 ).getHighLights()[0];
//assertTrue( highLights.contains( "<em>stemmer</em>" ) );//Not found..., verify this....
assertTrue( highLights.contains( "<em>stemming</em>" ) );
assertTrue( highLights.contains( "<em>stemmed</em>" ) );
//Testing the stemer
siteSearchResults = siteSearchAPI.search( indexName, "argument", 0, 100 );
//Validations
assertTrue( siteSearchResults.getError() == null || siteSearchResults.getError().isEmpty() );
assertTrue( siteSearchResults.getTotalResults() > 0 );
highLights = siteSearchResults.getResults().get( 0 ).getHighLights()[0];
assertTrue( highLights.contains( "<em>arguments</em>" ) );
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++NGrams++++++++++++++++++++++++++
//Testing the search with words that are in each extreme of the text
siteSearchResults = siteSearchAPI.search( indexName, "english illustrating", 0, 100 );
//Validations
assertTrue( siteSearchResults.getError() == null || siteSearchResults.getError().isEmpty() );
assertTrue( siteSearchResults.getTotalResults() > 0 );
//Testing the search with words that are not in find order
siteSearchResults = siteSearchAPI.search( indexName, "arguments algorithm", 0, 100 );
//Validations
assertTrue( siteSearchResults.getError() == null || siteSearchResults.getError().isEmpty() );
assertTrue( siteSearchResults.getTotalResults() > 0 );
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++OTHER TESTS+++++++++++++++++++++++
//Testing the search
siteSearchResults = siteSearchAPI.search( indexName, "engli*", 0, 100 );
//Validations
assertTrue( siteSearchResults.getError() == null || siteSearchResults.getError().isEmpty() );
assertTrue( siteSearchResults.getTotalResults() > 0 );
//Testing the search
siteSearchResults = siteSearchAPI.search( indexName, "*engli", 0, 100 );
//Validations
assertTrue( siteSearchResults.getError() == null || siteSearchResults.getError().isEmpty() );
assertEquals( siteSearchResults.getTotalResults(), 0 );
//Testing the search
siteSearchResults = siteSearchAPI.search( indexName, "e", 0, 100 );
//Validations
assertTrue( siteSearchResults.getError() == null || siteSearchResults.getError().isEmpty() );
assertEquals( siteSearchResults.getTotalResults(), 0 );
//Testing the search with a non existing word
siteSearchResults = siteSearchAPI.search( indexName, "weird", 0, 100 );
//Validations
assertTrue( siteSearchResults.getError() == null || siteSearchResults.getError().isEmpty() );
assertEquals( siteSearchResults.getTotalResults(), 0 );
} finally {
//And finally remove the index
siteSearchAPI.deleteFromIndex( indexName, docId );
}
}