Search in sources :

Example 1 with StatsRequestParameters

use of org.alfresco.service.cmr.search.StatsRequestParameters in project alfresco-remote-api by Alfresco.

the class SearchMapper method fromStats.

public void fromStats(SearchParameters sp, List<StatsRequestParameters> stats) {
    if (stats != null && !stats.isEmpty()) {
        for (StatsRequestParameters aStat : stats) {
            ParameterCheck.mandatory("stats field", aStat.getField());
            List<Float> perc = aStat.getPercentiles();
            if (perc != null && !perc.isEmpty()) {
                for (Float percentile : perc) {
                    if (percentile == null || percentile < 0 || percentile > 100) {
                        throw new IllegalArgumentException("Invalid percentile " + percentile);
                    }
                }
            }
            if (aStat.getCardinality() && (aStat.getCardinalityAccuracy() < 0 || aStat.getCardinalityAccuracy() > 1)) {
                throw new IllegalArgumentException("Invalid cardinality accuracy " + aStat.getCardinalityAccuracy() + " It must be between 0 and 1.");
            }
        }
        sp.setStats(stats);
    }
}
Also used : StatsRequestParameters(org.alfresco.service.cmr.search.StatsRequestParameters)

Example 2 with StatsRequestParameters

use of org.alfresco.service.cmr.search.StatsRequestParameters in project alfresco-remote-api by Alfresco.

the class SearchMapper method buildPivotKeys.

protected void buildPivotKeys(List<String> pivotKeys, Pivot aPivot, List<StatsRequestParameters> stats, FacetFields facetFields, List<RangeParameters> ranges, SearchRequestContext searchRequestContext) {
    if (aPivot == null)
        return;
    String pivotKey = null;
    ParameterCheck.mandatoryString("pivot key", aPivot.getKey());
    if (facetFields.getFacets() != null && !facetFields.getFacets().isEmpty()) {
        Optional<FacetField> found = facetFields.getFacets().stream().filter(queryable -> aPivot.getKey().equals(queryable.getLabel() != null ? queryable.getLabel() : queryable.getField())).findFirst();
        if (found.isPresent()) {
            pivotKey = aPivot.getKey();
            if (searchRequestContext.getPivotKeys().containsValue(pivotKey)) {
                throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID, new Object[] { ": Duplicate pivot parameter " + aPivot.getKey() + "" });
            }
            pivotKeys.add(found.get().getField());
            facetFields.getFacets().remove(found.get());
            searchRequestContext.getPivotKeys().put(found.get().getField(), pivotKey);
        }
    }
    if (pivotKey == null && ((aPivot.getPivots() == null) || aPivot.getPivots().isEmpty())) {
        // It is the last one so it can reference stats or range
        if (stats != null && !stats.isEmpty()) {
            Optional<StatsRequestParameters> foundStat = stats.stream().filter(stas -> aPivot.getKey().equals(stas.getLabel() != null ? stas.getLabel() : stas.getField())).findFirst();
            if (foundStat.isPresent()) {
                pivotKey = aPivot.getKey();
                if (pivotKeys.isEmpty()) {
                    throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID, new Object[] { ": Stats key " + pivotKey + " cannot be used here" });
                }
                pivotKeys.add(pivotKey);
                searchRequestContext.getPivotKeys().put(pivotKey, pivotKey);
            }
        }
        if (ranges != null && !ranges.isEmpty()) {
            for (RangeParameters aRange : ranges) {
                if (aPivot.getKey().equals(aRange.getLabel())) {
                    pivotKey = aPivot.getKey();
                    if (pivotKeys.isEmpty()) {
                        throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID, new Object[] { ": Range key " + pivotKey + " cannot be used here" });
                    }
                    pivotKeys.add(pivotKey);
                    searchRequestContext.getPivotKeys().put(pivotKey, pivotKey);
                }
            }
        }
    }
    if (pivotKey == null) {
        String invalidMessage = searchRequestContext.getPivotKeys().values().contains(aPivot.getKey()) ? " cannot be used more than once." : " does not reference a facet Field, range or stats.";
        throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID, new Object[] { ": Pivot parameter " + aPivot.getKey() + invalidMessage });
    }
    if (aPivot.getPivots() != null && !aPivot.getPivots().isEmpty() && aPivot.getPivots().size() > 1) {
        throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID, new Object[] { ": Currently only 1 nested pivot is supported, you have " + aPivot.getPivots().size() });
    }
    aPivot.getPivots().forEach(subPivot -> {
        buildPivotKeys(pivotKeys, subPivot, stats, facetFields, ranges, searchRequestContext);
    });
}
Also used : Arrays(java.util.Arrays) FacetQuery(org.alfresco.rest.api.search.model.FacetQuery) Pivot(org.alfresco.rest.api.search.model.Pivot) Paging(org.alfresco.rest.framework.resource.parameters.Paging) PARAM_INCLUDE_PROPERTIES(org.alfresco.rest.api.Nodes.PARAM_INCLUDE_PROPERTIES) RangeParameters(org.alfresco.service.cmr.search.RangeParameters) Template(org.alfresco.rest.api.search.model.Template) IntervalParameters(org.alfresco.service.cmr.search.IntervalParameters) Limits(org.alfresco.rest.api.search.model.Limits) Matcher(java.util.regex.Matcher) Params(org.alfresco.rest.framework.resource.parameters.Params) Locale(java.util.Locale) Map(java.util.Map) LANGUAGE_FTS_ALFRESCO(org.alfresco.service.cmr.search.SearchService.LANGUAGE_FTS_ALFRESCO) Scope(org.alfresco.rest.api.search.model.Scope) PARAM_INCLUDE_PATH(org.alfresco.rest.api.Nodes.PARAM_INCLUDE_PATH) Localization(org.alfresco.rest.api.search.model.Localization) TimeZone(java.util.TimeZone) Set(java.util.Set) FacetFields(org.alfresco.rest.api.search.model.FacetFields) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) IntervalSet(org.alfresco.service.cmr.search.IntervalSet) List(java.util.List) LuceneQueryLanguageSPI(org.alfresco.repo.search.impl.lucene.LuceneQueryLanguageSPI) PARAM_INCLUDE_ALLOWABLEOPERATIONS(org.alfresco.rest.api.Nodes.PARAM_INCLUDE_ALLOWABLEOPERATIONS) Optional(java.util.Optional) Default(org.alfresco.rest.api.search.model.Default) FilterQuery(org.alfresco.rest.api.search.model.FilterQuery) SortDef(org.alfresco.rest.api.search.model.SortDef) LimitBy(org.alfresco.service.cmr.search.LimitBy) PARAM_INCLUDE_ISLINK(org.alfresco.rest.api.Nodes.PARAM_INCLUDE_ISLINK) Interval(org.alfresco.service.cmr.search.Interval) FieldFacet(org.alfresco.service.cmr.search.SearchParameters.FieldFacet) SearchRequestContext(org.alfresco.rest.api.search.context.SearchRequestContext) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) GeneralHighlightParameters(org.alfresco.service.cmr.search.GeneralHighlightParameters) Function(java.util.function.Function) ArrayList(java.util.ArrayList) FieldFacetSort(org.alfresco.service.cmr.search.SearchParameters.FieldFacetSort) PARAM_INCLUDE_ASPECTNAMES(org.alfresco.rest.api.Nodes.PARAM_INCLUDE_ASPECTNAMES) ParameterCheck(org.alfresco.util.ParameterCheck) StatsRequestParameters(org.alfresco.service.cmr.search.StatsRequestParameters) FacetField(org.alfresco.rest.api.search.model.FacetField) LANGUAGE_LUCENE(org.alfresco.service.cmr.search.SearchService.LANGUAGE_LUCENE) SortType(org.alfresco.service.cmr.search.SearchParameters.SortDefinition.SortType) StoreRef(org.alfresco.service.cmr.repository.StoreRef) PARAM_INCLUDE_ASSOCIATION(org.alfresco.rest.api.Nodes.PARAM_INCLUDE_ASSOCIATION) FieldFacetMethod(org.alfresco.service.cmr.search.SearchParameters.FieldFacetMethod) LANGUAGE_CMIS_ALFRESCO(org.alfresco.service.cmr.search.SearchService.LANGUAGE_CMIS_ALFRESCO) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) Operator(org.alfresco.service.cmr.search.SearchParameters.Operator) Query(org.alfresco.rest.api.search.model.Query) Collectors.toList(java.util.stream.Collectors.toList) SearchQuery(org.alfresco.rest.api.search.model.SearchQuery) SortDefinition(org.alfresco.service.cmr.search.SearchParameters.SortDefinition) Spelling(org.alfresco.rest.api.search.model.Spelling) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) FacetField(org.alfresco.rest.api.search.model.FacetField) StatsRequestParameters(org.alfresco.service.cmr.search.StatsRequestParameters) RangeParameters(org.alfresco.service.cmr.search.RangeParameters)

Example 3 with StatsRequestParameters

use of org.alfresco.service.cmr.search.StatsRequestParameters in project alfresco-remote-api by Alfresco.

the class SearchMapperTests method fromStats.

@Test
public void fromStats() throws Exception {
    SearchParameters searchParameters = new SearchParameters();
    searchMapper.fromStats(searchParameters, null);
    List<StatsRequestParameters> statsRequestParameters = new ArrayList<>(1);
    statsRequestParameters.add(new StatsRequestParameters(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null));
    try {
        searchMapper.fromStats(searchParameters, statsRequestParameters);
    } catch (IllegalArgumentException iae) {
        assertTrue(iae.getLocalizedMessage().contains("field is a mandatory parameter"));
    }
    statsRequestParameters.clear();
    statsRequestParameters.add(new StatsRequestParameters("cm:content", "myLabel", null, null, null, null, null, null, null, null, null, null, null, null, null, null));
    searchMapper.fromStats(searchParameters, statsRequestParameters);
    assertEquals(1, searchParameters.getStats().size());
    statsRequestParameters.clear();
    statsRequestParameters.add(new StatsRequestParameters("cm:content", "myLabel", Arrays.asList(3.4f, 12f, 10f), null, null, null, null, null, null, null, null, null, null, null, null, null));
    searchMapper.fromStats(searchParameters, statsRequestParameters);
    assertEquals(1, searchParameters.getStats().size());
    statsRequestParameters.clear();
    statsRequestParameters.add(new StatsRequestParameters("cm:content", "myLabel", Arrays.asList(-3.4f), null, null, null, null, null, null, null, null, null, null, null, null, null));
    try {
        searchMapper.fromStats(searchParameters, statsRequestParameters);
    } catch (IllegalArgumentException iae) {
        assertTrue(iae.getLocalizedMessage().contains("Invalid percentile -3.4"));
    }
    statsRequestParameters.clear();
    statsRequestParameters.add(new StatsRequestParameters("cm:content", "myLabel", Arrays.asList(101f), null, null, null, null, null, null, null, null, null, null, null, null, null));
    try {
        searchMapper.fromStats(searchParameters, statsRequestParameters);
    } catch (IllegalArgumentException iae) {
        assertTrue(iae.getLocalizedMessage().contains("Invalid percentile 101"));
    }
    statsRequestParameters.clear();
    statsRequestParameters.add(new StatsRequestParameters("cm:content", "myLabel", null, null, null, null, null, null, null, null, null, null, null, true, 12f, null));
    try {
        searchMapper.fromStats(searchParameters, statsRequestParameters);
    } catch (IllegalArgumentException iae) {
        assertTrue(iae.getLocalizedMessage().contains("Invalid cardinality accuracy 12.0"));
    }
    statsRequestParameters.clear();
    statsRequestParameters.add(new StatsRequestParameters("cm:content", "myLabel", null, null, null, null, null, null, null, null, null, null, null, null, 12f, null));
    searchMapper.fromStats(searchParameters, statsRequestParameters);
    // cardinality is ignored if not true
    assertEquals(1, searchParameters.getStats().size());
}
Also used : SearchParameters(org.alfresco.service.cmr.search.SearchParameters) ArrayList(java.util.ArrayList) StatsRequestParameters(org.alfresco.service.cmr.search.StatsRequestParameters) Test(org.junit.Test)

Example 4 with StatsRequestParameters

use of org.alfresco.service.cmr.search.StatsRequestParameters in project alfresco-remote-api by Alfresco.

the class SearchMapperTests method fromPivot.

@Test
public void fromPivot() throws Exception {
    SearchParameters searchParameters = new SearchParameters();
    searchMapper.fromPivot(searchParameters, null, null, null, null, null);
    List<FacetField> facets = new ArrayList<>(1);
    facets.add(new FacetField("myfield", null, null, null, null, null, null, null, null, null, null));
    facets.add(new FacetField("yourfield", null, null, null, null, null, null, null, null, null, null));
    FacetFields ff = new FacetFields(facets);
    searchMapper.fromFacetFields(searchParameters, ff);
    searchMapper.fromPivot(searchParameters, null, ff, null, null, null);
    assertEquals(2, searchParameters.getFieldFacets().size());
    assertEquals(0, searchParameters.getPivots().size());
    // Handle unknown pivot.
    searchParameters = new SearchParameters();
    try {
        searchMapper.fromPivot(searchParameters, null, ff, null, Arrays.asList(new Pivot(null, null)), null);
        fail();
    } catch (IllegalArgumentException iae) {
        // "bob" doesn't refer to a field facet
        assertNotNull(iae);
    }
    try {
        searchMapper.fromPivot(searchParameters, null, ff, null, Arrays.asList(new Pivot("", null)), null);
        fail();
    } catch (IllegalArgumentException iae) {
        assertNotNull(iae);
    }
    SearchRequestContext searchRequestContext = SearchRequestContext.from(minimalQuery());
    // "bob" doesn't refer to a field facet but its the last one so needs to refer to a stat
    StatsRequestParameters bobf = new StatsRequestParameters("bob", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
    StatsRequestParameters bobL = new StatsRequestParameters("creator", "bob", null, null, null, null, null, null, null, null, null, null, null, null, null, null);
    try {
        searchMapper.fromPivot(searchParameters, Arrays.asList(bobf), ff, null, Arrays.asList(new Pivot("bob", null)), searchRequestContext);
        fail();
    } catch (InvalidArgumentException iae) {
        // "bob" refers to a stat but it can't be at the root pivot, it needs to be nested
        assertNotNull(iae);
    }
    searchMapper.fromPivot(searchParameters, Arrays.asList(bobf), ff, null, Arrays.asList(new Pivot("yourfield", Arrays.asList(new Pivot("bob", null)))), searchRequestContext);
    assertEquals(1, searchParameters.getPivots().size());
    try {
        searchMapper.fromPivot(searchParameters, null, ff, null, Arrays.asList(new Pivot("ken", null), new Pivot("bob", null)), searchRequestContext);
        fail();
    } catch (InvalidArgumentException iae) {
        // "ken" doesn't refer to a field facet and its not the last one
        assertNotNull(iae);
    }
    searchParameters = new SearchParameters();
    searchMapper.fromPivot(searchParameters, null, ff, null, Arrays.asList(new Pivot("myfield", null)), searchRequestContext);
    searchMapper.fromFacetFields(searchParameters, ff);
    // Moved from a field facet to a pivot
    assertEquals(0, searchParameters.getFieldFacets().size());
    assertEquals(1, searchParameters.getPivots().size());
    assertEquals("myfield", searchParameters.getPivots().get(0).get(0));
    searchParameters = new SearchParameters();
    try {
        searchMapper.fromPivot(searchParameters, Arrays.asList(bobf), ff, null, Arrays.asList(new Pivot("bob", Arrays.asList(new Pivot("hope", null)))), searchRequestContext);
        fail();
    } catch (InvalidArgumentException iae) {
        // "bob" doesn't refer to a field facet or stat
        assertNotNull(iae);
    }
    searchRequestContext = SearchRequestContext.from(minimalQuery());
    facets = new ArrayList<>(1);
    facets.add(new FacetField("king", null, null, null, null, null, null, null, null, null, null));
    ff = new FacetFields(facets);
    searchMapper.fromPivot(searchParameters, Arrays.asList(bobf), ff, null, Arrays.asList(new Pivot("king", Arrays.asList(new Pivot("bob", null)))), searchRequestContext);
    assertEquals(1, searchParameters.getPivots().size());
    assertEquals(2, searchParameters.getPivots().get(0).size());
    assertEquals("king", searchParameters.getPivots().get(0).get(0));
    assertEquals("bob", searchParameters.getPivots().get(0).get(1));
    searchRequestContext = SearchRequestContext.from(minimalQuery());
    searchParameters = new SearchParameters();
    facets = new ArrayList<>(1);
    facets.add(new FacetField("king", null, null, null, null, null, null, null, null, null, null));
    facets.add(new FacetField("kong", null, null, null, null, null, null, null, null, null, null));
    facets.add(new FacetField("kang", null, null, null, null, null, null, null, null, null, null));
    ff = new FacetFields(facets);
    searchMapper.fromPivot(searchParameters, Arrays.asList(bobf), ff, null, Arrays.asList(new Pivot("king", Arrays.asList(new Pivot("bob", null))), new Pivot("kong", null)), searchRequestContext);
    assertEquals(2, searchParameters.getPivots().size());
    assertEquals(2, searchParameters.getPivots().get(0).size());
    assertEquals("king", searchParameters.getPivots().get(0).get(0));
    assertEquals("bob", searchParameters.getPivots().get(0).get(1));
    assertEquals("kong", searchParameters.getPivots().get(1).get(0));
    searchRequestContext = SearchRequestContext.from(minimalQuery());
    searchParameters = new SearchParameters();
    List<RangeParameters> rangeParams = new ArrayList<RangeParameters>();
    facets = new ArrayList<>(2);
    facets.add(new FacetField("king", null, null, null, null, null, null, null, null, null, null));
    facets.add(new FacetField("kong", null, null, null, null, null, null, null, null, null, null));
    ff = new FacetFields(facets);
    rangeParams.add(new RangeParameters("content.size", "0", "100000", "1000", true, null, null, "hope", null));
    searchMapper.fromPivot(searchParameters, Arrays.asList(bobf), ff, rangeParams, Arrays.asList(new Pivot("king", Arrays.asList(new Pivot("bob", null))), new Pivot("kong", Arrays.asList(new Pivot("hope", null)))), searchRequestContext);
    assertEquals(2, searchParameters.getPivots().size());
    assertEquals(2, searchParameters.getPivots().get(0).size());
    assertEquals("king", searchParameters.getPivots().get(0).get(0));
    assertEquals("bob", searchParameters.getPivots().get(0).get(1));
    assertEquals("kong", searchParameters.getPivots().get(1).get(0));
    assertEquals("hope", searchParameters.getPivots().get(1).get(1));
}
Also used : ArrayList(java.util.ArrayList) FacetField(org.alfresco.rest.api.search.model.FacetField) StatsRequestParameters(org.alfresco.service.cmr.search.StatsRequestParameters) SearchRequestContext(org.alfresco.rest.api.search.context.SearchRequestContext) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) FacetFields(org.alfresco.rest.api.search.model.FacetFields) Pivot(org.alfresco.rest.api.search.model.Pivot) RangeParameters(org.alfresco.service.cmr.search.RangeParameters) Test(org.junit.Test)

Aggregations

StatsRequestParameters (org.alfresco.service.cmr.search.StatsRequestParameters)4 ArrayList (java.util.ArrayList)3 SearchParameters (org.alfresco.service.cmr.search.SearchParameters)3 SearchRequestContext (org.alfresco.rest.api.search.context.SearchRequestContext)2 FacetField (org.alfresco.rest.api.search.model.FacetField)2 FacetFields (org.alfresco.rest.api.search.model.FacetFields)2 Pivot (org.alfresco.rest.api.search.model.Pivot)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 RangeParameters (org.alfresco.service.cmr.search.RangeParameters)2 Test (org.junit.Test)2 ZoneId (java.time.ZoneId)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 TimeZone (java.util.TimeZone)1 Function (java.util.function.Function)1 Matcher (java.util.regex.Matcher)1