Search in sources :

Example 1 with FilterQuery

use of org.alfresco.rest.api.search.model.FilterQuery in project alfresco-remote-api by Alfresco.

the class SearchMapperTests method fromFilterQuery.

@Test
public void fromFilterQuery() throws Exception {
    SearchParameters searchParameters = new SearchParameters();
    // Doesn't error
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery("hedgehog", null, null), new FilterQuery("king", null, null)));
    assertEquals(2, searchParameters.getFilterQueries().size());
    assertEquals("hedgehog", searchParameters.getFilterQueries().get(0));
    assertEquals("king", searchParameters.getFilterQueries().get(1));
    searchParameters = new SearchParameters();
    searchParameters.setLanguage(SearchService.LANGUAGE_CMIS_ALFRESCO);
    try {
        searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery("hedgehog", null, null)));
        fail();
    } catch (InvalidArgumentException iae) {
        // You can't specify FilterQuery when using the CMIS language
        assertNotNull(iae);
    }
    try {
        searchParameters.setLanguage(SearchService.LANGUAGE_SOLR_ALFRESCO);
        searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery(null, null, null)));
        fail();
    } catch (IllegalArgumentException iae) {
        // You can't specify FilterQuery when using the CMIS language
        assertNotNull(iae);
    }
    searchParameters = new SearchParameters();
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery("{!afts}description:xyz", Arrays.asList("desc1", "desc2"), null)));
    assertEquals("{!afts tag=desc1,desc2 }description:xyz", searchParameters.getFilterQueries().get(0));
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery("{!afts}description:xyz", Arrays.asList("desc1"), null)));
    assertEquals("{!afts tag=desc1 }description:xyz", searchParameters.getFilterQueries().get(1));
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery("description:xyz", Arrays.asList("desc1"), null)));
    assertEquals("{!afts tag=desc1 }description:xyz", searchParameters.getFilterQueries().get(2));
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery("{!afts} description:xyz", Arrays.asList("desc1"), null)));
    assertEquals("{!afts tag=desc1 } description:xyz", searchParameters.getFilterQueries().get(3));
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery(" {!afts cake} description:xyz", Arrays.asList("desc1"), null)));
    assertEquals("{!afts tag=desc1 cake} description:xyz", searchParameters.getFilterQueries().get(4));
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery("{!afts tag=desc1}description:xyz", Arrays.asList("desc1"), null)));
    assertEquals("{!afts tag=desc1}description:xyz", searchParameters.getFilterQueries().get(5));
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery("created:2011", Arrays.asList("desc1"), null)));
    assertEquals("{!afts tag=desc1 }created:2011", searchParameters.getFilterQueries().get(6));
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery("=cm:name:cabbage", Arrays.asList("desc1"), null)));
    assertEquals("{!afts tag=desc1 }=cm:name:cabbage", searchParameters.getFilterQueries().get(7));
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery("{http://www.alfresco.org/model/content/1.0}title:workflow", null, null)));
    assertEquals("{http://www.alfresco.org/model/content/1.0}title:workflow", searchParameters.getFilterQueries().get(8));
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery("{http://www.alfresco.org/model/content/1.0}title:workflow", Arrays.asList("desc1"), null)));
    assertEquals("{!afts tag=desc1 }{http://www.alfresco.org/model/content/1.0}title:workflow", searchParameters.getFilterQueries().get(9));
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery("{!afts} description:xyz", Arrays.asList("desc1", "desc2"), null)));
    assertEquals("{!afts tag=desc1,desc2 }description:xyz", searchParameters.getFilterQueries().get(0));
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery("{ !afts } description:xyz", Arrays.asList("desc1", "desc2"), null)));
    assertEquals("{!afts tag=desc1,desc2 }description:xyz", searchParameters.getFilterQueries().get(0));
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery(null, Arrays.asList("desc1"), Arrays.asList("cm:name:cabbage", "cm:creator:bob"))));
    assertEquals("{!afts tag=desc1 }cm:name:cabbage OR cm:creator:bob", searchParameters.getFilterQueries().get(12));
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery(null, null, Arrays.asList("cm:name:cabbage", "cm:creator:bob"))));
    assertEquals("cm:name:cabbage OR cm:creator:bob", searchParameters.getFilterQueries().get(13));
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery(null, null, Arrays.asList("cm:name:cabbage"))));
    assertEquals("cm:name:cabbage", searchParameters.getFilterQueries().get(14));
    searchMapper.fromFilterQuery(searchParameters, Arrays.asList(new FilterQuery("created:2011", null, Arrays.asList("cm:name:cabbage"))));
    assertEquals("Single query should take precident over multiple queries ORed together.", "created:2011", searchParameters.getFilterQueries().get(15));
}
Also used : SearchParameters(org.alfresco.service.cmr.search.SearchParameters) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) FilterQuery(org.alfresco.rest.api.search.model.FilterQuery) Test(org.junit.Test)

Example 2 with FilterQuery

use of org.alfresco.rest.api.search.model.FilterQuery in project alfresco-remote-api by Alfresco.

the class SearchMapper method fromFilterQuery.

/**
 ** SearchParameters from List<FilterQuery>
 * @param sp
 * @param filterQueries
 */
public void fromFilterQuery(SearchParameters sp, List<FilterQuery> filterQueries) {
    if (filterQueries != null && !filterQueries.isEmpty()) {
        if (LANGUAGE_CMIS_ALFRESCO.equals(sp.getLanguage())) {
            throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID, new Object[] { ": filterQueries {} not allowed with cmis language" });
        }
        for (FilterQuery fq : filterQueries) {
            String query = null;
            if (fq.getQuery() != null && !fq.getQuery().isEmpty()) {
                query = fq.getQuery().trim();
            }
            if (fq.getQueries() != null && !fq.getQueries().isEmpty() && query == null) {
                query = String.join(" OR ", fq.getQueries());
            }
            ParameterCheck.mandatoryString("filterQueries query", query);
            if (fq.getTags() == null || fq.getTags().isEmpty() || query.contains("afts tag")) {
                // If its already got tags then just let it through
                sp.addFilterQuery(query);
            } else {
                String tags = "tag=" + String.join(",", fq.getTags());
                Matcher matcher = LuceneQueryLanguageSPI.AFTS_QUERY.matcher(query);
                if (matcher.find()) {
                    query = "{!afts " + tags + " " + matcher.group(1).trim() + "}" + matcher.group(2);
                } else {
                    query = "{!afts " + tags + " }" + query;
                }
                sp.addFilterQuery(query);
            }
        }
    }
}
Also used : InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Matcher(java.util.regex.Matcher) FilterQuery(org.alfresco.rest.api.search.model.FilterQuery)

Aggregations

FilterQuery (org.alfresco.rest.api.search.model.FilterQuery)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 Matcher (java.util.regex.Matcher)1 SearchParameters (org.alfresco.service.cmr.search.SearchParameters)1 Test (org.junit.Test)1