Search in sources :

Example 6 with FacetField

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

the class SearchMapperTests method fromFacetFields.

@Test
public void fromFacetFields() throws Exception {
    SearchParameters searchParameters = new SearchParameters();
    // Doesn't error
    searchMapper.fromFacetFields(searchParameters, null);
    try {
        searchMapper.fromFacetFields(searchParameters, new FacetFields(null));
        fail();
    } catch (IllegalArgumentException iae) {
        assertTrue(iae.getLocalizedMessage().contains("facetFields facets is a mandatory parameter"));
    }
    try {
        searchMapper.fromFacetFields(searchParameters, new FacetFields(Arrays.asList(new FacetField(null, null, null, null, null, null, null, null, null, null, null))));
        fail();
    } catch (IllegalArgumentException iae) {
        assertTrue(iae.getLocalizedMessage().contains("facetFields facet field is a mandatory parameter"));
    }
    searchMapper.fromFacetFields(searchParameters, new FacetFields(Arrays.asList(new FacetField("myfield", null, null, null, null, null, null, null, null, null, null))));
    assertEquals(1, searchParameters.getFieldFacets().size());
    FieldFacet ff = searchParameters.getFieldFacets().get(0);
    // Check defaults
    // assertEquals(true, ff.getMissing());
    assertNull(ff.getLimitOrNull());
    assertEquals(0, ff.getOffset());
    assertEquals(1, ff.getMinCount());
    assertFalse(ff.isCountDocsMissingFacetField());
    assertEquals(0, ff.getEnumMethodCacheMinDF());
    // assertEquals("{key='myfield'}myfield" ,ff.getField());
    searchParameters = new SearchParameters();
    searchMapper.fromFacetFields(searchParameters, new FacetFields(Arrays.asList(new FacetField("myfield", "mylabel", "myprefix", null, null, null, null, null, null, null, null))));
    ff = searchParameters.getFieldFacets().get(0);
    // assertEquals("{key='mylabel'}myfield" ,ff.getField());
    assertEquals("myprefix", ff.getPrefix());
    try {
        searchMapper.fromFacetFields(searchParameters, new FacetFields(Arrays.asList(new FacetField("myfield", null, null, "badsort", null, null, null, null, null, null, null))));
        fail();
    } catch (InvalidArgumentException iae) {
        // Sort is an enum
        assertNotNull(iae);
    }
    try {
        searchMapper.fromFacetFields(searchParameters, new FacetFields(Arrays.asList(new FacetField("myfield", null, null, null, "badmethod", null, null, null, null, null, null))));
        fail();
    } catch (InvalidArgumentException iae) {
        // Method is an enum
        assertNotNull(iae);
    }
    searchParameters = new SearchParameters();
    searchMapper.fromFacetFields(searchParameters, new FacetFields(Arrays.asList(new FacetField("myfield", null, null, "INDEX", "ENUM", null, null, null, null, null, null))));
    ff = searchParameters.getFieldFacets().get(0);
    assertEquals("INDEX", ff.getSort().toString());
    assertEquals("ENUM", ff.getMethod().toString());
}
Also used : SearchParameters(org.alfresco.service.cmr.search.SearchParameters) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) FacetFields(org.alfresco.rest.api.search.model.FacetFields) FieldFacet(org.alfresco.service.cmr.search.SearchParameters.FieldFacet) FacetField(org.alfresco.rest.api.search.model.FacetField) Test(org.junit.Test)

Example 7 with FacetField

use of org.alfresco.rest.api.search.model.FacetField 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

FacetField (org.alfresco.rest.api.search.model.FacetField)7 ArrayList (java.util.ArrayList)4 SearchRequestContext (org.alfresco.rest.api.search.context.SearchRequestContext)4 SearchQuery (org.alfresco.rest.api.search.model.SearchQuery)4 Interval (org.alfresco.service.cmr.search.Interval)4 IntervalSet (org.alfresco.service.cmr.search.IntervalSet)4 Arrays (java.util.Arrays)3 List (java.util.List)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 FacetFields (org.alfresco.rest.api.search.model.FacetFields)3 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)3 SearchParameters (org.alfresco.service.cmr.search.SearchParameters)3 Serializable (java.io.Serializable)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Entry (java.util.Map.Entry)2