Search in sources :

Example 1 with SolrFacetHelper

use of org.alfresco.repo.search.impl.solr.facet.SolrFacetHelper in project alfresco-repository by Alfresco.

the class ScriptSearchTest method mockServiceRegistry.

private ServiceRegistry mockServiceRegistry() {
    SearchService searchService = mock(SearchService.class);
    ResultSet results = mock(ResultSet.class);
    List<Pair<String, Integer>> fieldFacets1 = new ArrayList<>();
    fieldFacets1.add(new Pair<>(mimetype1, 1));
    fieldFacets1.add(new Pair<>(mimetype2, 2));
    List<Pair<String, Integer>> fieldFacets2 = new ArrayList<>();
    fieldFacets2.add(new Pair<>(modifier, 1));
    when(results.getFieldFacet(fieldFacet1)).thenReturn(fieldFacets1);
    when(results.getFieldFacet(fieldFacet2)).thenReturn(fieldFacets2);
    when(results.getFacetQueries()).thenReturn(new HashMap<>());
    when(searchService.query((SearchParameters) any())).thenReturn(results);
    FacetLabelDisplayHandlerRegistry displayHandlerRegistry = mock(FacetLabelDisplayHandlerRegistry.class);
    ServiceRegistry services = mock(ServiceRegistry.class);
    when(services.getSearchService()).thenReturn(searchService);
    when(displayHandlerRegistry.getDisplayHandler(fieldFacet1)).thenReturn(new MimetypeOrderDisplayHandler());
    when(displayHandlerRegistry.getDisplayHandler(fieldFacet2)).thenReturn(null);
    SolrFacetHelper solrFacetHelper = mock(SolrFacetHelper.class);
    when(solrFacetHelper.getBucketedFieldFacets()).thenReturn(new HashSet<>());
    when(services.getSolrFacetHelper()).thenReturn(solrFacetHelper);
    when(services.getFacetLabelDisplayHandlerRegistry()).thenReturn(displayHandlerRegistry);
    return services;
}
Also used : SearchService(org.alfresco.service.cmr.search.SearchService) ResultSet(org.alfresco.service.cmr.search.ResultSet) FacetLabelDisplayHandlerRegistry(org.alfresco.repo.search.impl.solr.facet.handler.FacetLabelDisplayHandlerRegistry) ServiceRegistry(org.alfresco.service.ServiceRegistry) Pair(org.alfresco.util.Pair) SolrFacetHelper(org.alfresco.repo.search.impl.solr.facet.SolrFacetHelper)

Example 2 with SolrFacetHelper

use of org.alfresco.repo.search.impl.solr.facet.SolrFacetHelper in project alfresco-repository by Alfresco.

the class Search method queryResultSet.

@SuppressWarnings("unchecked")
public Scriptable queryResultSet(Object search) {
    Object[] results = null;
    Map<String, Object> meta = null;
    // convert values from JS to Java - may contain native JS object such as ConsString etc.
    search = new ValueConverter().convertValueForJava(search);
    if (search instanceof Serializable) {
        Serializable obj = new ValueConverter().convertValueForRepo((Serializable) search);
        if (obj instanceof Map) {
            Map<Serializable, Serializable> def = (Map<Serializable, Serializable>) obj;
            // test for mandatory values
            String query = (String) def.get("query");
            if (query == null || query.length() == 0) {
                throw new AlfrescoRuntimeException("Failed to search: Missing mandatory 'query' value.");
            }
            // collect optional values
            String store = (String) def.get("store");
            String language = (String) def.get("language");
            List<Map<Serializable, Serializable>> sort = (List<Map<Serializable, Serializable>>) def.get("sort");
            Map<Serializable, Serializable> page = (Map<Serializable, Serializable>) def.get("page");
            List<String> facets = (List<String>) def.get("fieldFacets");
            List<String> filterQueries = (List<String>) def.get("filterQueries");
            String namespace = (String) def.get("namespace");
            String onerror = (String) def.get("onerror");
            String defaultField = (String) def.get("defaultField");
            String defaultOperator = (String) def.get("defaultOperator");
            String searchTerm = (String) def.get("searchTerm");
            boolean spellCheck = Boolean.TRUE.equals(def.get("spellCheck"));
            // extract supplied values
            // sorting columns
            SortColumn[] sortColumns = null;
            if (sort != null) {
                sortColumns = new SortColumn[sort.size()];
                int index = 0;
                for (Map<Serializable, Serializable> column : sort) {
                    String strCol = (String) column.get("column");
                    if (strCol == null || strCol.length() == 0) {
                        throw new AlfrescoRuntimeException("Failed to search: Missing mandatory 'sort: column' value.");
                    }
                    Boolean boolAsc = (Boolean) column.get("ascending");
                    boolean ascending = (boolAsc != null ? boolAsc.booleanValue() : false);
                    sortColumns[index++] = new SortColumn(strCol, ascending);
                }
            }
            // paging settings
            int maxResults = -1;
            int skipResults = 0;
            if (page != null) {
                if (page.get("maxItems") != null) {
                    Object maxItems = page.get("maxItems");
                    if (maxItems instanceof Number) {
                        maxResults = ((Number) maxItems).intValue();
                    } else if (maxItems instanceof String) {
                        // try and convert to int (which it what it should be!)
                        maxResults = Integer.parseInt((String) maxItems);
                    }
                }
                if (page.get("skipCount") != null) {
                    Object skipCount = page.get("skipCount");
                    if (skipCount instanceof Number) {
                        skipResults = ((Number) page.get("skipCount")).intValue();
                    } else if (skipCount instanceof String) {
                        skipResults = Integer.parseInt((String) skipCount);
                    }
                }
            }
            // query templates
            Map<String, String> queryTemplates = null;
            List<Map<Serializable, Serializable>> templates = (List<Map<Serializable, Serializable>>) def.get("templates");
            if (templates != null) {
                queryTemplates = new HashMap<String, String>(templates.size(), 1.0f);
                for (Map<Serializable, Serializable> template : templates) {
                    String field = (String) template.get("field");
                    if (field == null || field.length() == 0) {
                        throw new AlfrescoRuntimeException("Failed to search: Missing mandatory 'template: field' value.");
                    }
                    String t = (String) template.get("template");
                    if (t == null || t.length() == 0) {
                        throw new AlfrescoRuntimeException("Failed to search: Missing mandatory 'template: template' value.");
                    }
                    queryTemplates.put(field, t);
                }
            }
            SearchParameters sp = new SearchParameters();
            sp.addStore(store != null ? new StoreRef(store) : this.storeRef);
            sp.setLanguage(language != null ? language : SearchService.LANGUAGE_LUCENE);
            sp.setQuery(query);
            sp.setSearchTerm(searchTerm);
            sp.setSpellCheck(spellCheck);
            if (defaultField != null) {
                sp.setDefaultFieldName(defaultField);
            }
            if (defaultOperator != null && defaultOperator.length() != 0) {
                try {
                    sp.setDefaultOperator(Operator.valueOf(defaultOperator.toUpperCase()));
                } catch (IllegalArgumentException e) {
                // ignore invalid Operator and the default value will be used
                }
            }
            if (namespace != null) {
                sp.setNamespace(namespace);
            }
            if (maxResults > 0) {
                sp.setLimit(maxResults);
                sp.setLimitBy(LimitBy.FINAL_SIZE);
            }
            if (skipResults > 0) {
                sp.setSkipCount(skipResults);
            }
            if (sort != null) {
                for (SortColumn sd : sortColumns) {
                    sp.addSort(sd.column, sd.asc);
                }
            }
            if (queryTemplates != null) {
                for (String field : queryTemplates.keySet()) {
                    sp.addQueryTemplate(field, queryTemplates.get(field));
                }
            }
            if (facets != null) {
                SolrFacetHelper solrFacetHelper = services.getSolrFacetHelper();
                for (String field : facets) {
                    if (field.isEmpty()) {
                        continue;
                    }
                    final String modifiedField = "@" + field;
                    if (solrFacetHelper.hasFacetQueries(modifiedField)) {
                        List<String> facetQueries = solrFacetHelper.getFacetQueries(modifiedField);
                        addFacetQuery(sp, field, facetQueries, query);
                    } else {
                        final FieldFacet fieldFacet;
                        if (solrFacetHelper.isSpecialFacetId(field)) {
                            fieldFacet = new FieldFacet(field);
                        } else {
                            fieldFacet = new FieldFacet(modifiedField);
                        }
                        sp.addFieldFacet(fieldFacet);
                    }
                }
            }
            if (filterQueries != null) {
                for (String filter : filterQueries) {
                    sp.addFilterQuery(filter);
                }
            }
            Map<Serializable, Serializable> highlighting = (Map<Serializable, Serializable>) def.get("highlight");
            if (highlighting != null) {
                int snippetCount = this.getIntegerValue("snippetCount", 20, highlighting);
                int fragmentSize = this.getIntegerValue("fragmentSize", 50, highlighting);
                // see SEARCH-284
                Integer maxAnalyzedChars = null;
                boolean usePhraseHighlighter = this.getBooleanValue("usePhraseHighlighter", true, highlighting);
                boolean mergeContiguous = this.getBooleanValue("mergeContiguous", true, highlighting);
                String prefix = (String) highlighting.get("prefix");
                if (prefix == null) {
                    prefix = "<mark>";
                }
                String postfix = (String) highlighting.get("postfix");
                if (postfix == null) {
                    postfix = "</mark>";
                }
                List<FieldHighlightParameters> fieldHighlightParameters = new ArrayList<FieldHighlightParameters>();
                List<Map<Serializable, Serializable>> fields = (List<Map<Serializable, Serializable>>) highlighting.get("fields");
                if (fields != null) {
                    for (Map<Serializable, Serializable> field : fields) {
                        String propertyName = (String) field.get("field");
                        if (propertyName != null) {
                            fieldHighlightParameters.add(new FieldHighlightParameters(propertyName, snippetCount, fragmentSize, mergeContiguous, prefix, postfix));
                        }
                    }
                }
                GeneralHighlightParameters ghp = new GeneralHighlightParameters(snippetCount, fragmentSize, mergeContiguous, prefix, postfix, maxAnalyzedChars, usePhraseHighlighter, fieldHighlightParameters);
                sp.setHighlight(ghp);
            }
            // error handling opions
            boolean exceptionOnError = true;
            if (onerror != null) {
                if (onerror.equals("exception")) {
                // default value, do nothing
                } else if (onerror.equals("no-results")) {
                    exceptionOnError = false;
                } else {
                    throw new AlfrescoRuntimeException("Failed to search: Unknown value supplied for 'onerror': " + onerror);
                }
            }
            // execute search based on search definition
            Pair<Object[], Map<String, Object>> r = queryResultMeta(sp, exceptionOnError);
            results = r.getFirst();
            meta = r.getSecond();
        }
    }
    if (results == null) {
        results = new Object[0];
    }
    // construct a JS return object
    // {
    // nodes: [],                // Array of ScriptNode results
    // meta: {
    // numberFound: long,     // total number found in index, or -1 if not known or not supported by this resultset
    // facets: {              // facets are returned for each field as requested in the SearchParameters fieldfacets
    // field: {            // each field contains a map of facet to value
    // facet: value,
    // ...
    // },
    // ...
    // }
    // }
    // }
    Scriptable scope = getScope();
    Scriptable res = Context.getCurrentContext().newObject(scope);
    res.put("nodes", res, Context.getCurrentContext().newArray(scope, results));
    res.put("meta", res, meta);
    return res;
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) ArrayList(java.util.ArrayList) List(java.util.List) FieldHighlightParameters(org.alfresco.service.cmr.search.FieldHighlightParameters) StoreRef(org.alfresco.service.cmr.repository.StoreRef) Scriptable(org.mozilla.javascript.Scriptable) SolrFacetHelper(org.alfresco.repo.search.impl.solr.facet.SolrFacetHelper) FieldFacet(org.alfresco.service.cmr.search.SearchParameters.FieldFacet) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) HashMap(java.util.HashMap) Map(java.util.Map) GeneralHighlightParameters(org.alfresco.service.cmr.search.GeneralHighlightParameters)

Aggregations

SolrFacetHelper (org.alfresco.repo.search.impl.solr.facet.SolrFacetHelper)2 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 FacetLabelDisplayHandlerRegistry (org.alfresco.repo.search.impl.solr.facet.handler.FacetLabelDisplayHandlerRegistry)1 ServiceRegistry (org.alfresco.service.ServiceRegistry)1 StoreRef (org.alfresco.service.cmr.repository.StoreRef)1 FieldHighlightParameters (org.alfresco.service.cmr.search.FieldHighlightParameters)1 GeneralHighlightParameters (org.alfresco.service.cmr.search.GeneralHighlightParameters)1 ResultSet (org.alfresco.service.cmr.search.ResultSet)1 SearchParameters (org.alfresco.service.cmr.search.SearchParameters)1 FieldFacet (org.alfresco.service.cmr.search.SearchParameters.FieldFacet)1 SearchService (org.alfresco.service.cmr.search.SearchService)1 Pair (org.alfresco.util.Pair)1 Scriptable (org.mozilla.javascript.Scriptable)1