BaseCmisSearchQueryBuilder implements CMISSearchQueryBuilder { @Override public String buildQuery(SearchContext searchContext, Query query) throws SearchException { StringBundler sb = new StringBundler(); sb.append("SELECT cmis:objectId"); QueryConfig queryConfig = searchContext.getQueryConfig(); if (queryConfig.isScoreEnabled()) { sb.append(", SCORE() AS HITS"); } sb.append(" FROM cmis:document"); CMISDisjunction cmisDisjunction = new CMISDisjunction(); if (_log.isDebugEnabled()) { _log.debug( "Repository query support " + queryConfig.getAttribute("capabilityQuery")); } if (!isSupportsOnlyFullText(queryConfig)) { traversePropertiesQuery(cmisDisjunction, query, queryConfig); } if (isSupportsFullText(queryConfig)) { CMISContainsExpression cmisContainsExpression = new CMISContainsExpression(); traverseContentQuery(cmisContainsExpression, query, queryConfig); if (!cmisContainsExpression.isEmpty()) { cmisDisjunction.add(cmisContainsExpression); } } if (!cmisDisjunction.isEmpty()) { sb.append(" WHERE "); sb.append(cmisDisjunction.toQueryFragment()); } Sort[] sorts = searchContext.getSorts(); if (queryConfig.isScoreEnabled() || ArrayUtil.isNotEmpty(sorts)) { sb.append(" ORDER BY "); } if (ArrayUtil.isNotEmpty(sorts)) { int i = 0; for (Sort sort : sorts) { String fieldName = sort.getFieldName(); if (!isSupportedField(fieldName)) { continue; } if (i > 0) { sb.append(", "); } sb.append(getCmisField(fieldName)); if (sort.isReverse()) { sb.append(" DESC"); } else { sb.append(" ASC"); } i++; } } else if (queryConfig.isScoreEnabled()) { sb.append("HITS DESC"); } if (_log.isDebugEnabled()) { _log.debug("CMIS query " + sb.toString()); } return sb.toString(); } BaseCmisSearchQueryBuilder(); protected  BaseCmisSearchQueryBuilder(
		RepositoryEntryLocalService repositoryEntryLocalService,
		UserLocalService userLocalService); @Override String buildQuery(SearchContext searchContext, Query query);  }
@Test public void testBooleanQuery() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("+test* -test.doc"); String cmisQuery = buildQuery(searchContext); assertQueryEquals( "((cmis:name LIKE 'test%' AND NOT(cmis:name = 'test.doc')) OR " + "(cmis:createdBy LIKE 'test%' AND NOT(cmis:createdBy = " + "'test.doc')))", cmisQuery); }
@Test public void testContainsCombinedSupportedQuery() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("test"); QueryConfig queryConfig = searchContext.getQueryConfig(); queryConfig.setAttribute( "capabilityQuery", CapabilityQuery.BOTHCOMBINED.value()); String cmisQuery = buildQuery(searchContext); assertQueryEquals( "((cmis:name = 'test' OR cmis:createdBy = 'test') OR " + "CONTAINS('test'))", cmisQuery); }
@Test public void testContainsCombinedSupportedWildcardQuery() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("test*.jpg"); QueryConfig queryConfig = searchContext.getQueryConfig(); queryConfig.setAttribute( "capabilityQuery", CapabilityQuery.BOTHCOMBINED.value()); String cmisQuery = buildQuery(searchContext); assertQueryEquals( "((cmis:name LIKE 'test%.jpg' OR cmis:createdBy LIKE " + "'test%.jpg') OR CONTAINS('(test AND .jpg)'))", cmisQuery); }
@Test public void testContainsOnlySupportedQuery() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("test"); QueryConfig queryConfig = searchContext.getQueryConfig(); queryConfig.setAttribute( "capabilityQuery", CapabilityQuery.FULLTEXTONLY.value()); String cmisQuery = buildQuery(searchContext); assertQueryEquals("CONTAINS('test')", cmisQuery); }
@Test public void testContainsOnlySupportedQueryMultipleKeywords() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("test multiple"); QueryConfig queryConfig = searchContext.getQueryConfig(); queryConfig.setAttribute( "capabilityQuery", CapabilityQuery.FULLTEXTONLY.value()); String cmisQuery = buildQuery(searchContext); assertQueryEquals("CONTAINS('(test OR multiple)')", cmisQuery); }
@Test public void testContainsOnlySupportedQueryWithConjunction() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("+test +multiple"); QueryConfig queryConfig = searchContext.getQueryConfig(); queryConfig.setAttribute( "capabilityQuery", CapabilityQuery.FULLTEXTONLY.value()); String cmisQuery = buildQuery(searchContext); assertQueryEquals("CONTAINS('(test multiple)')", cmisQuery); }
@Test public void testContainsOnlySupportedQueryWithNegation() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("test -multiple"); QueryConfig queryConfig = searchContext.getQueryConfig(); queryConfig.setAttribute( "capabilityQuery", CapabilityQuery.FULLTEXTONLY.value()); String cmisQuery = buildQuery(searchContext); assertQueryEquals("CONTAINS('(-multiple OR test)')", cmisQuery); }
@Test public void testContainsOnlySupportedQueryWithNegationPhrase() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("test -\"multiple words\""); QueryConfig queryConfig = searchContext.getQueryConfig(); queryConfig.setAttribute( "capabilityQuery", CapabilityQuery.FULLTEXTONLY.value()); String cmisQuery = buildQuery(searchContext); assertQueryEquals( "CONTAINS('(-\\'multiple words\\' OR test)')", cmisQuery); }
@Test public void testContainsOnlySupportedWithApostrophe() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("test's"); QueryConfig queryConfig = searchContext.getQueryConfig(); queryConfig.setAttribute( "capabilityQuery", CapabilityQuery.FULLTEXTONLY.value()); String cmisQuery = buildQuery(searchContext); assertQueryEquals("CONTAINS('test\\'s')", cmisQuery); }
@Test public void testExactFileNameQuery() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("test.jpg"); String cmisQuery = buildQuery(searchContext); assertQueryEquals( "(cmis:name = 'test.jpg' OR cmis:createdBy = 'test.jpg')", cmisQuery); }
@Test public void testFuzzyQuery() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("test~"); String cmisQuery = buildQuery(searchContext); assertQueryEquals( "(cmis:name LIKE 'test%' OR cmis:createdBy LIKE 'test%')", cmisQuery); }
@Test public void testPhraseQuery() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("\"My test document.jpg\""); String cmisQuery = buildQuery(searchContext); assertQueryEquals( "(cmis:name = 'My test document.jpg' OR cmis:createdBy = 'My " + "test document.jpg')", cmisQuery); }
@Test public void testPrefixQuery() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("Test*"); String cmisQuery = buildQuery(searchContext); assertQueryEquals( "(cmis:name LIKE 'Test%' OR cmis:createdBy LIKE 'Test%')", cmisQuery); }
@Test public void testProximityQuery() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("\"test document\"~10"); String cmisQuery = buildQuery(searchContext); assertQueryEquals( "(cmis:name = 'test document' OR cmis:createdBy = 'test document')", cmisQuery); }
@Test public void testRangeQuery() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords( "createDate:[20091011000000 TO 20091110235959]"); String cmisQuery = buildQuery(searchContext); assertQueryEquals( "cmis:creationDate >= 2009-10-11T00:00:00.000Z AND " + "cmis:creationDate <= 2009-11-10T23:59:59.000Z", cmisQuery); }
@Test public void testWildcardFieldQuery() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("+title:test*.jpg +userName:bar*"); String cmisQuery = buildQuery(searchContext); assertQueryEquals( "(cmis:name LIKE 'test%.jpg' AND cmis:createdBy LIKE 'bar%')", cmisQuery); }
@Test public void testWildcardQuery() throws Exception { SearchContext searchContext = getSearchContext(); searchContext.setKeywords("test*.jpg"); String cmisQuery = buildQuery(searchContext); assertQueryEquals( "(cmis:name LIKE 'test%.jpg' OR cmis:createdBy LIKE 'test%.jpg')", cmisQuery); }