Search in sources :

Example 1 with FacetFieldContext

use of org.alfresco.rest.api.search.context.FacetFieldContext in project alfresco-remote-api by Alfresco.

the class ResultMapperTests method testIsNullContext.

@Test
public void testIsNullContext() throws Exception {
    assertTrue(mapper.isNullContext(new SearchContext(0l, null, null, null, null, null)));
    assertFalse(mapper.isNullContext(new SearchContext(1l, null, null, null, null, null)));
    assertFalse(mapper.isNullContext(new SearchContext(0l, null, null, null, new SpellCheckContext(null, null), null)));
    assertFalse(mapper.isNullContext(new SearchContext(0l, null, Arrays.asList(new FacetQueryContext(null, null, 0)), null, null, null)));
    assertFalse(mapper.isNullContext(new SearchContext(0l, null, null, Arrays.asList(new FacetFieldContext(null, null)), null, null)));
    assertFalse(mapper.isNullContext(new SearchContext(0l, Arrays.asList(new GenericFacetResponse(null, null, null)), null, null, null, null)));
}
Also used : FacetQueryContext(org.alfresco.rest.api.search.context.FacetQueryContext) FacetFieldContext(org.alfresco.rest.api.search.context.FacetFieldContext) GenericFacetResponse(org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericFacetResponse) SearchContext(org.alfresco.rest.api.search.context.SearchContext) SpellCheckContext(org.alfresco.rest.api.search.context.SpellCheckContext) Test(org.junit.Test)

Example 2 with FacetFieldContext

use of org.alfresco.rest.api.search.context.FacetFieldContext in project alfresco-remote-api by Alfresco.

the class ResultMapper method getFacetBucketsForFacetFields.

protected List<FacetFieldContext> getFacetBucketsForFacetFields(Map<String, List<Pair<String, Integer>>> facetFields, SearchQuery searchQuery) {
    if (facetFields != null && !facetFields.isEmpty()) {
        List<FacetFieldContext> ffcs = new ArrayList<>(facetFields.size());
        for (Entry<String, List<Pair<String, Integer>>> facet : facetFields.entrySet()) {
            if (facet.getValue() != null && !facet.getValue().isEmpty()) {
                List<Bucket> buckets = new ArrayList<>(facet.getValue().size());
                for (Pair<String, Integer> buck : facet.getValue()) {
                    Object display = null;
                    String filterQuery = null;
                    if (searchQuery != null && searchQuery.getFacetFields() != null && searchQuery.getFacetFields().getFacets() != null && !searchQuery.getFacetFields().getFacets().isEmpty()) {
                        Optional<FacetField> found = searchQuery.getFacetFields().getFacets().stream().filter(queryable -> facet.getKey().equals(queryable.getLabel() != null ? queryable.getLabel() : queryable.getField())).findFirst();
                        if (found.isPresent()) {
                            display = propertyLookup.lookup(found.get().getField(), buck.getFirst());
                            String fq = found.get().toFilterQuery(buck.getFirst());
                            if (fq != null) {
                                filterQuery = fq;
                            }
                        }
                    }
                    buckets.add(new Bucket(buck.getFirst(), filterQuery, buck.getSecond(), display));
                }
                ffcs.add(new FacetFieldContext(facet.getKey(), buckets));
            }
        }
        return ffcs;
    }
    return Collections.emptyList();
}
Also used : Arrays(java.util.Arrays) SearchContext(org.alfresco.rest.api.search.context.SearchContext) FacetQuery(org.alfresco.rest.api.search.model.FacetQuery) GenericBucket(org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericBucket) LIVE_NODES(org.alfresco.rest.api.search.impl.StoreMapper.LIVE_NODES) Paging(org.alfresco.rest.framework.resource.parameters.Paging) FacetFormat(org.alfresco.service.cmr.search.FacetFormat) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Params(org.alfresco.rest.framework.resource.parameters.Params) Node(org.alfresco.rest.api.model.Node) Map(java.util.Map) HISTORY(org.alfresco.rest.api.search.impl.StoreMapper.HISTORY) METRIC_TYPE(org.alfresco.repo.search.impl.solr.facet.facetsresponse.Metric.METRIC_TYPE) ResultSet(org.alfresco.service.cmr.search.ResultSet) TupleEntry(org.alfresco.rest.api.search.model.TupleEntry) DeletedNodes(org.alfresco.rest.api.DeletedNodes) FacetQueryContext(org.alfresco.rest.api.search.context.FacetQueryContext) Set(java.util.Set) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) List(java.util.List) Version2Model(org.alfresco.repo.version.Version2Model) IntervalSet(org.alfresco.service.cmr.search.IntervalSet) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) CollectionWithPagingInfo(org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo) SpellCheckContext(org.alfresco.rest.api.search.context.SpellCheckContext) Bucket(org.alfresco.rest.api.search.context.FacetFieldContext.Bucket) Entry(java.util.Map.Entry) Optional(java.util.Optional) Nodes(org.alfresco.rest.api.Nodes) LogFactory(org.apache.commons.logging.LogFactory) SearchSQLQuery(org.alfresco.rest.api.search.model.SearchSQLQuery) NodeVersionsRelation(org.alfresco.rest.api.nodes.NodeVersionsRelation) TupleList(org.alfresco.rest.api.search.model.TupleList) DELETED(org.alfresco.rest.api.search.impl.StoreMapper.DELETED) Metric(org.alfresco.repo.search.impl.solr.facet.facetsresponse.Metric) Interval(org.alfresco.service.cmr.search.Interval) SearchRequestContext(org.alfresco.rest.api.search.context.SearchRequestContext) SimpleMetric(org.alfresco.repo.search.impl.solr.facet.facetsresponse.SimpleMetric) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Version(org.alfresco.service.cmr.version.Version) UserInfo(org.alfresco.rest.api.model.UserInfo) HashMap(java.util.HashMap) SearchEntry(org.alfresco.rest.api.search.model.SearchEntry) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) FacetField(org.alfresco.rest.api.search.model.FacetField) QName(org.alfresco.service.namespace.QName) RangeResultMapper(org.alfresco.repo.search.impl.solr.facet.facetsresponse.RangeResultMapper) PropertyLookupRegistry(org.alfresco.rest.api.lookups.PropertyLookupRegistry) ServiceRegistry(org.alfresco.service.ServiceRegistry) Pair(org.alfresco.util.Pair) FilteringResultSet(org.alfresco.repo.security.permissions.impl.acegi.FilteringResultSet) HighlightEntry(org.alfresco.rest.api.search.model.HighlightEntry) FacetFieldContext(org.alfresco.rest.api.search.context.FacetFieldContext) FACET_TYPE(org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericFacetResponse.FACET_TYPE) SearchQuery(org.alfresco.rest.api.search.model.SearchQuery) VERSIONS(org.alfresco.rest.api.search.impl.StoreMapper.VERSIONS) ResultSetRow(org.alfresco.service.cmr.search.ResultSetRow) Log(org.apache.commons.logging.Log) Collections(java.util.Collections) SolrJSONResultSet(org.alfresco.repo.search.impl.lucene.SolrJSONResultSet) SpellCheckResult(org.alfresco.service.cmr.search.SpellCheckResult) JSONArray(org.json.JSONArray) GenericFacetResponse(org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericFacetResponse) FacetFieldContext(org.alfresco.rest.api.search.context.FacetFieldContext) ArrayList(java.util.ArrayList) FacetField(org.alfresco.rest.api.search.model.FacetField) GenericBucket(org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericBucket) Bucket(org.alfresco.rest.api.search.context.FacetFieldContext.Bucket) List(java.util.List) TupleList(org.alfresco.rest.api.search.model.TupleList) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject)

Example 3 with FacetFieldContext

use of org.alfresco.rest.api.search.context.FacetFieldContext in project alfresco-remote-api by Alfresco.

the class ResultMapper method toSearchContext.

/**
 * Uses the results from Solr to set the Search Context
 * @param SolrJSONResultSet
 * @param searchQuery
 * @return SearchContext
 */
public SearchContext toSearchContext(SolrJSONResultSet solrResultSet, SearchRequestContext searchRequestContext, SearchQuery searchQuery, int notFound) {
    SearchContext context = null;
    Map<String, Integer> facetQueries = solrResultSet.getFacetQueries();
    List<GenericFacetResponse> facets = new ArrayList<>();
    List<FacetQueryContext> facetResults = null;
    SpellCheckContext spellCheckContext = null;
    List<FacetFieldContext> ffcs = new ArrayList<FacetFieldContext>();
    if (searchQuery == null) {
        throw new IllegalArgumentException("searchQuery can't be null");
    }
    // Facet queries
    if (facetQueries != null && !facetQueries.isEmpty()) {
        // If group by field populated in query facet return bucketing into facet field.
        List<GenericFacetResponse> facetQueryForFields = getFacetBucketsFromFacetQueries(facetQueries, searchQuery);
        if (hasGroup(searchQuery) || FacetFormat.V2 == searchQuery.getFacetFormat()) {
            facets.addAll(facetQueryForFields);
        } else {
            // Return the old way facet query with no bucketing.
            facetResults = new ArrayList<>(facetQueries.size());
            for (Entry<String, Integer> fq : facetQueries.entrySet()) {
                String filterQuery = null;
                if (searchQuery.getFacetQueries() != null) {
                    Optional<FacetQuery> found = searchQuery.getFacetQueries().stream().filter(facetQuery -> fq.getKey().equals(facetQuery.getLabel())).findFirst();
                    filterQuery = found.isPresent() ? found.get().getQuery() : fq.getKey();
                }
                facetResults.add(new FacetQueryContext(fq.getKey(), filterQuery, fq.getValue()));
            }
        }
    }
    // Field Facets
    Map<String, List<Pair<String, Integer>>> facetFields = solrResultSet.getFieldFacets();
    if (FacetFormat.V2 == searchQuery.getFacetFormat()) {
        facets.addAll(getFacetBucketsForFacetFieldsAsFacets(facetFields, searchQuery));
    } else {
        ffcs.addAll(getFacetBucketsForFacetFields(facetFields, searchQuery));
    }
    Map<String, List<Pair<String, Integer>>> facetInterval = solrResultSet.getFacetIntervals();
    facets.addAll(getGenericFacetsForIntervals(facetInterval, searchQuery));
    Map<String, List<Map<String, String>>> facetRanges = solrResultSet.getFacetRanges();
    facets.addAll(RangeResultMapper.getGenericFacetsForRanges(facetRanges, searchQuery.getFacetRanges()));
    List<GenericFacetResponse> stats = getFieldStats(searchRequestContext, solrResultSet.getStats());
    List<GenericFacetResponse> pimped = getPivots(searchRequestContext, solrResultSet.getPivotFacets(), stats);
    facets.addAll(pimped);
    facets.addAll(stats);
    // Spelling
    SpellCheckResult spell = solrResultSet.getSpellCheckResult();
    if (spell != null && spell.getResultName() != null && !spell.getResults().isEmpty()) {
        spellCheckContext = new SpellCheckContext(spell.getResultName(), spell.getResults());
    }
    // Put it all together
    context = new SearchContext(solrResultSet.getLastIndexedTxId(), facets, facetResults, ffcs, spellCheckContext, searchRequestContext.includeRequest() ? searchQuery : null);
    return isNullContext(context) ? null : context;
}
Also used : Arrays(java.util.Arrays) SearchContext(org.alfresco.rest.api.search.context.SearchContext) FacetQuery(org.alfresco.rest.api.search.model.FacetQuery) GenericBucket(org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericBucket) LIVE_NODES(org.alfresco.rest.api.search.impl.StoreMapper.LIVE_NODES) Paging(org.alfresco.rest.framework.resource.parameters.Paging) FacetFormat(org.alfresco.service.cmr.search.FacetFormat) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Params(org.alfresco.rest.framework.resource.parameters.Params) Node(org.alfresco.rest.api.model.Node) Map(java.util.Map) HISTORY(org.alfresco.rest.api.search.impl.StoreMapper.HISTORY) METRIC_TYPE(org.alfresco.repo.search.impl.solr.facet.facetsresponse.Metric.METRIC_TYPE) ResultSet(org.alfresco.service.cmr.search.ResultSet) TupleEntry(org.alfresco.rest.api.search.model.TupleEntry) DeletedNodes(org.alfresco.rest.api.DeletedNodes) FacetQueryContext(org.alfresco.rest.api.search.context.FacetQueryContext) Set(java.util.Set) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) List(java.util.List) Version2Model(org.alfresco.repo.version.Version2Model) IntervalSet(org.alfresco.service.cmr.search.IntervalSet) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) CollectionWithPagingInfo(org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo) SpellCheckContext(org.alfresco.rest.api.search.context.SpellCheckContext) Bucket(org.alfresco.rest.api.search.context.FacetFieldContext.Bucket) Entry(java.util.Map.Entry) Optional(java.util.Optional) Nodes(org.alfresco.rest.api.Nodes) LogFactory(org.apache.commons.logging.LogFactory) SearchSQLQuery(org.alfresco.rest.api.search.model.SearchSQLQuery) NodeVersionsRelation(org.alfresco.rest.api.nodes.NodeVersionsRelation) TupleList(org.alfresco.rest.api.search.model.TupleList) DELETED(org.alfresco.rest.api.search.impl.StoreMapper.DELETED) Metric(org.alfresco.repo.search.impl.solr.facet.facetsresponse.Metric) Interval(org.alfresco.service.cmr.search.Interval) SearchRequestContext(org.alfresco.rest.api.search.context.SearchRequestContext) SimpleMetric(org.alfresco.repo.search.impl.solr.facet.facetsresponse.SimpleMetric) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Version(org.alfresco.service.cmr.version.Version) UserInfo(org.alfresco.rest.api.model.UserInfo) HashMap(java.util.HashMap) SearchEntry(org.alfresco.rest.api.search.model.SearchEntry) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) FacetField(org.alfresco.rest.api.search.model.FacetField) QName(org.alfresco.service.namespace.QName) RangeResultMapper(org.alfresco.repo.search.impl.solr.facet.facetsresponse.RangeResultMapper) PropertyLookupRegistry(org.alfresco.rest.api.lookups.PropertyLookupRegistry) ServiceRegistry(org.alfresco.service.ServiceRegistry) Pair(org.alfresco.util.Pair) FilteringResultSet(org.alfresco.repo.security.permissions.impl.acegi.FilteringResultSet) HighlightEntry(org.alfresco.rest.api.search.model.HighlightEntry) FacetFieldContext(org.alfresco.rest.api.search.context.FacetFieldContext) FACET_TYPE(org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericFacetResponse.FACET_TYPE) SearchQuery(org.alfresco.rest.api.search.model.SearchQuery) VERSIONS(org.alfresco.rest.api.search.impl.StoreMapper.VERSIONS) ResultSetRow(org.alfresco.service.cmr.search.ResultSetRow) Log(org.apache.commons.logging.Log) Collections(java.util.Collections) SolrJSONResultSet(org.alfresco.repo.search.impl.lucene.SolrJSONResultSet) SpellCheckResult(org.alfresco.service.cmr.search.SpellCheckResult) JSONArray(org.json.JSONArray) GenericFacetResponse(org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericFacetResponse) FacetQueryContext(org.alfresco.rest.api.search.context.FacetQueryContext) FacetFieldContext(org.alfresco.rest.api.search.context.FacetFieldContext) GenericFacetResponse(org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericFacetResponse) ArrayList(java.util.ArrayList) SearchContext(org.alfresco.rest.api.search.context.SearchContext) SpellCheckResult(org.alfresco.service.cmr.search.SpellCheckResult) FacetQuery(org.alfresco.rest.api.search.model.FacetQuery) List(java.util.List) TupleList(org.alfresco.rest.api.search.model.TupleList) ArrayList(java.util.ArrayList) SpellCheckContext(org.alfresco.rest.api.search.context.SpellCheckContext)

Example 4 with FacetFieldContext

use of org.alfresco.rest.api.search.context.FacetFieldContext in project alfresco-remote-api by Alfresco.

the class SearchQuerySerializerTests method testSerializeContext.

@Test
public void testSerializeContext() throws IOException {
    ExecutionResult exec1 = new ExecutionResult(new Farmer("180"), null);
    FacetFieldContext ffc = new FacetFieldContext("theLabel", Arrays.asList(new Bucket("b1", "name:b1", 23, "displayText1"), new Bucket("b2", null, 34, "displayText2")));
    SearchContext searchContext = new SearchContext(23l, null, Arrays.asList(new FacetQueryContext("f1", "creator:bob", 15), new FacetQueryContext("f2", null, 20)), Arrays.asList(ffc), new SpellCheckContext("aFlag", Arrays.asList("bish", "bash")), null);
    CollectionWithPagingInfo<ExecutionResult> coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1), false, 2, null, searchContext);
    String out = helper.writeResponse(coll);
    assertTrue("There must 'context' json output", out.contains("\"context\":{\"consistency\":{\"lastTxId\":23}"));
    assertTrue("There must 'facetQueries' json output", out.contains("\"facetQueries\":"));
    assertTrue("There must 'facetQueries f1' json output", out.contains("{\"label\":\"f1\",\"filterQuery\":\"creator:bob\",\"count\":15}"));
    assertTrue("There must 'facetQueries f2' json output", out.contains("{\"label\":\"f2\",\"count\":20}"));
    assertTrue("There must 'spellCheck' json output", out.contains("\"spellCheck\":{\"type\":\"aFlag\",\"suggestions\":[\"bish\",\"bash\"]}"));
    assertTrue("There must 'facetsFields' json output", out.contains("\"facetsFields\":[{\"label\":\"theLabel\",\"buckets\""));
    assertTrue("There must 'bucket1' json output", out.contains("{\"label\":\"b1\",\"filterQuery\":\"name:b1\",\"count\":23,\"display\":\"displayText1\"}"));
    assertTrue("There must 'bucket2' json output", out.contains("{\"label\":\"b2\",\"count\":34,\"display\":\"displayText2\"}"));
    searchContext = new SearchContext(-1, null, null, null, null, null);
    coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1), false, 2, null, searchContext);
    out = helper.writeResponse(coll);
    assertTrue("There must NOT BE a 'context' json output", out.contains("\"context\":{}"));
    assertFalse("There must NOT BE a 'facetsFields' json output", out.contains("\"facetsFields\":{}"));
}
Also used : FacetFieldContext(org.alfresco.rest.api.search.context.FacetFieldContext) FacetQueryContext(org.alfresco.rest.api.search.context.FacetQueryContext) Bucket(org.alfresco.rest.api.search.context.FacetFieldContext.Bucket) SearchContext(org.alfresco.rest.api.search.context.SearchContext) ExecutionResult(org.alfresco.rest.framework.jacksonextensions.ExecutionResult) Farmer(org.alfresco.rest.framework.tests.api.mocks.Farmer) SpellCheckContext(org.alfresco.rest.api.search.context.SpellCheckContext) Test(org.junit.Test)

Aggregations

FacetFieldContext (org.alfresco.rest.api.search.context.FacetFieldContext)4 FacetQueryContext (org.alfresco.rest.api.search.context.FacetQueryContext)4 SearchContext (org.alfresco.rest.api.search.context.SearchContext)4 SpellCheckContext (org.alfresco.rest.api.search.context.SpellCheckContext)4 GenericFacetResponse (org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericFacetResponse)3 Bucket (org.alfresco.rest.api.search.context.FacetFieldContext.Bucket)3 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 SolrJSONResultSet (org.alfresco.repo.search.impl.lucene.SolrJSONResultSet)2 GenericBucket (org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericBucket)2