Example usage for org.apache.solr.client.solrj SolrQuery addFacetField

List of usage examples for org.apache.solr.client.solrj SolrQuery addFacetField

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj SolrQuery addFacetField.

Prototype

public SolrQuery addFacetField(String... fields) 

Source Link

Document

Add field(s) for facet computation.

Usage

From source file:org.ofbiz.solr.SolrUtil.java

License:Apache License

public static Map<String, Object> categoriesAvailable(String catalogId, String categoryId, String productId,
        String facetPrefix, boolean displayproducts, int viewIndex, int viewSize) {
    // create the data model
    Map<String, Object> result = FastMap.newInstance();
    HttpSolrServer server = null;/*from   w w  w  .j  a v  a  2s  .  co  m*/
    QueryResponse returnMap = new QueryResponse();
    try {
        // do the basic query
        server = new HttpSolrServer(solrUrl);
        // create Query Object
        String query = "inStock[1 TO *]";
        if (categoryId != null)
            query += " +cat:" + categoryId;
        else if (productId != null)
            query += " +productId:" + productId;
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery(query);

        if (catalogId != null)
            solrQuery.setFilterQueries("catalog:" + catalogId);
        if (displayproducts) {
            if (viewSize > -1) {
                solrQuery.setRows(viewSize);
            } else
                solrQuery.setRows(50000);
            if (viewIndex > -1) {
                solrQuery.setStart(viewIndex);
            }
        } else {
            solrQuery.setFields("cat");
            solrQuery.setRows(0);
        }

        if (UtilValidate.isNotEmpty(facetPrefix)) {
            solrQuery.setFacetPrefix(facetPrefix);
        }

        solrQuery.setFacetMinCount(0);
        solrQuery.setFacet(true);
        solrQuery.addFacetField("cat");
        solrQuery.setFacetLimit(-1);
        Debug.logVerbose("solr: solrQuery: " + solrQuery, module);
        returnMap = server.query(solrQuery, METHOD.POST);
        result.put("rows", returnMap);
        result.put("numFound", returnMap.getResults().getNumFound());
    } catch (Exception e) {
        Debug.logError(e.getMessage(), module);
    }
    return result;
}

From source file:org.openbrr.search.solr.ObrrSolrServer.java

public static List<ProjectIndexerData> query(String _query) {

    List<ProjectIndexerData> results = new ArrayList<ProjectIndexerData>();

    SolrQuery sq = new SolrQuery();
    sq.setQuery(_query);//from   w  w w.jav a 2  s  .c  o m
    sq.setFacet(true);
    sq.addFacetField(IndexFields.ID);
    sq.addFacetField(IndexFields.NAME);
    sq.addFacetField(IndexFields.DESC);
    sq.addFacetField(IndexFields.URL);
    sq.addFacetField(IndexFields.LIC_IDS);
    sq.addFacetField(IndexFields.TOPIC_IDS);
    sq.addFacetField(IndexFields.OS_IDS);
    sq.addFacetField(IndexFields.AUD_IDS);
    sq.addFacetField(IndexFields.PL_IDS);
    sq.addFacetField(IndexFields.DS_IDS);

    //sq.setIncludeScore(true);

    try {
        QueryResponse qr = server.query(sq);

        SolrDocumentList sdl = qr.getResults();

        for (SolrDocument d : sdl) {
            ProjectIndexerData data = new ProjectIndexerData((Integer) d.getFieldValue(IndexFields.ID),
                    (String) d.getFieldValue(IndexFields.NAME), (String) d.getFieldValue(IndexFields.DESC));

            data.setLicenseIds(stringToList((String) (d.getFieldValue(IndexFields.LIC_IDS))));
            data.setOsIds(stringToList((String) (d.getFieldValue(IndexFields.OS_IDS))));
            data.setTopicIds(stringToList((String) (d.getFieldValue(IndexFields.TOPIC_IDS))));
            data.setAudienceIds(stringToList((String) (d.getFieldValue(IndexFields.AUD_IDS))));
            data.setProgLangIds(stringToList((String) (d.getFieldValue(IndexFields.PL_IDS))));
            data.setDataStoreIds(stringToList((String) (d.getFieldValue(IndexFields.DS_IDS))));

            results.add(data);
        }
    } catch (SolrServerException e) {
        e.printStackTrace();
    }

    return results;
}

From source file:org.opencastproject.workflow.impl.WorkflowServiceSolrIndex.java

License:Educational Community License

/**
 * {@inheritDoc}/*from ww  w  .  j ava 2  s. c  o  m*/
 * 
 * @see org.opencastproject.workflow.impl.WorkflowServiceIndex#getStatistics()
 */
@Override
public WorkflowStatistics getStatistics() throws WorkflowDatabaseException {

    long total = 0;
    long paused = 0;
    long failed = 0;
    long failing = 0;
    long instantiated = 0;
    long running = 0;
    long stopped = 0;
    long succeeded = 0;

    WorkflowStatistics stats = new WorkflowStatistics();

    // Get all definitions and then query for the numbers and the current operation per definition
    try {
        String orgId = securityService.getOrganization().getId();
        StringBuilder queryString = new StringBuilder().append(ORG_KEY).append(":")
                .append(escapeQueryChars(orgId));
        appendSolrAuthFragment(queryString, WRITE_PERMISSION);
        SolrQuery solrQuery = new SolrQuery(queryString.toString());
        solrQuery.addFacetField(WORKFLOW_DEFINITION_KEY);
        solrQuery.addFacetField(OPERATION_KEY);
        solrQuery.setFacetMinCount(0);
        solrQuery.setFacet(true);
        QueryResponse response = solrServer.query(solrQuery);

        FacetField templateFacet = response.getFacetField(WORKFLOW_DEFINITION_KEY);
        FacetField operationFacet = response.getFacetField(OPERATION_KEY);

        // For every template and every operation
        if (templateFacet != null && templateFacet.getValues() != null) {

            for (Count template : templateFacet.getValues()) {

                WorkflowDefinitionReport templateReport = new WorkflowDefinitionReport();
                templateReport.setId(template.getName());

                long templateTotal = 0;
                long templatePaused = 0;
                long templateFailed = 0;
                long templateFailing = 0;
                long templateInstantiated = 0;
                long templateRunning = 0;
                long templateStopped = 0;
                long templateSucceeded = 0;

                if (operationFacet != null && operationFacet.getValues() != null) {

                    for (Count operation : operationFacet.getValues()) {

                        OperationReport operationReport = new OperationReport();
                        operationReport.setId(operation.getName());

                        StringBuilder baseSolrQuery = new StringBuilder().append(ORG_KEY).append(":")
                                .append(escapeQueryChars(orgId));
                        appendSolrAuthFragment(baseSolrQuery, WRITE_PERMISSION);
                        solrQuery = new SolrQuery(baseSolrQuery.toString());
                        solrQuery.addFacetField(STATE_KEY);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.FAILED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.FAILING);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.INSTANTIATED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.PAUSED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.RUNNING);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.STOPPED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.SUCCEEDED);
                        solrQuery.addFilterQuery(WORKFLOW_DEFINITION_KEY + ":" + template.getName());
                        solrQuery.addFilterQuery(OPERATION_KEY + ":" + operation.getName());
                        solrQuery.setFacetMinCount(0);
                        solrQuery.setFacet(true);

                        response = solrServer.query(solrQuery);

                        // Add the states
                        FacetField stateFacet = response.getFacetField(STATE_KEY);
                        for (Count stateValue : stateFacet.getValues()) {
                            WorkflowState state = WorkflowState.valueOf(stateValue.getName().toUpperCase());
                            templateTotal += stateValue.getCount();
                            total += stateValue.getCount();
                            switch (state) {
                            case FAILED:
                                operationReport.setFailed(stateValue.getCount());
                                templateFailed += stateValue.getCount();
                                failed += stateValue.getCount();
                                break;
                            case FAILING:
                                operationReport.setFailing(stateValue.getCount());
                                templateFailing += stateValue.getCount();
                                failing += stateValue.getCount();
                                break;
                            case INSTANTIATED:
                                operationReport.setInstantiated(stateValue.getCount());
                                templateInstantiated += stateValue.getCount();
                                instantiated += stateValue.getCount();
                                break;
                            case PAUSED:
                                operationReport.setPaused(stateValue.getCount());
                                templatePaused += stateValue.getCount();
                                paused += stateValue.getCount();
                                break;
                            case RUNNING:
                                operationReport.setRunning(stateValue.getCount());
                                templateRunning += stateValue.getCount();
                                running += stateValue.getCount();
                                break;
                            case STOPPED:
                                operationReport.setStopped(stateValue.getCount());
                                templateStopped += stateValue.getCount();
                                stopped += stateValue.getCount();
                                break;
                            case SUCCEEDED:
                                operationReport.setFinished(stateValue.getCount());
                                templateSucceeded += stateValue.getCount();
                                succeeded += stateValue.getCount();
                                break;
                            default:
                                throw new IllegalStateException("State '" + state + "' is not handled");
                            }
                        }

                        templateReport.getOperations().add(operationReport);
                    }
                }

                // Update the template statistics
                templateReport.setTotal(templateTotal);
                templateReport.setFailed(templateFailed);
                templateReport.setFailing(templateFailing);
                templateReport.setInstantiated(templateInstantiated);
                templateReport.setPaused(templatePaused);
                templateReport.setRunning(templateRunning);
                templateReport.setStopped(templateStopped);
                templateReport.setFinished(templateSucceeded);

                // Add the definition report to the statistics
                stats.getDefinitions().add(templateReport);

            }

        }
    } catch (SolrServerException e) {
        throw new WorkflowDatabaseException(e);
    }

    stats.setTotal(total);
    stats.setFailed(failed);
    stats.setFailing(failing);
    stats.setInstantiated(instantiated);
    stats.setPaused(paused);
    stats.setRunning(running);
    stats.setStopped(stopped);
    stats.setFinished(succeeded);

    return stats;
}

From source file:org.opencb.opencga.storage.core.search.solr.SolrQueryParser.java

License:Apache License

/**
 * Create a SolrQuery object from Query and QueryOptions.
 *
 * @param query         Query/* w  w w.  java 2  s. co m*/
 * @param queryOptions  Query Options
 * @return              SolrQuery
 */
public SolrQuery parse(Query query, QueryOptions queryOptions) {
    List<String> filterList = new ArrayList<>();

    SolrQuery solrQuery = new SolrQuery();

    //-------------------------------------
    // QueryOptions processing
    //-------------------------------------
    if (queryOptions.containsKey(QueryOptions.INCLUDE)) {
        solrQuery.setFields(queryOptions.getAsStringList(QueryOptions.INCLUDE).toString());
    }

    if (queryOptions.containsKey(QueryOptions.LIMIT)) {
        solrQuery.setRows(queryOptions.getInt(QueryOptions.LIMIT));
    }

    if (queryOptions.containsKey(QueryOptions.SKIP)) {
        solrQuery.setStart(queryOptions.getInt(QueryOptions.SKIP));
    }

    if (queryOptions.containsKey(QueryOptions.SORT)) {
        solrQuery.addSort(queryOptions.getString(QueryOptions.SORT), getSortOrder(queryOptions));
    }

    //-------------------------------------
    // Query processing
    //-------------------------------------

    // OR conditions
    // create a list for xrefs (without genes), genes, regions and cts
    // the function classifyIds function differentiates xrefs from genes
    List<String> xrefs = new ArrayList<>();
    List<String> genes = new ArrayList<>();
    List<Region> regions = new ArrayList<>();
    List<String> consequenceTypes = new ArrayList<>();

    // xref
    classifyIds(VariantQueryParams.ANNOT_XREF.key(), query, xrefs, genes);
    classifyIds(VariantQueryParams.ID.key(), query, xrefs, genes);
    classifyIds(VariantQueryParams.GENE.key(), query, xrefs, genes);
    classifyIds(VariantQueryParams.ANNOT_CLINVAR.key(), query, xrefs, genes);
    classifyIds(VariantQueryParams.ANNOT_COSMIC.key(), query, xrefs, genes);
    //        classifyIds(VariantQueryParams.ANNOT_HPO.key(), query, xrefs, genes);

    // Convert region string to region objects
    if (query.containsKey(VariantQueryParams.REGION.key())) {
        regions = Region.parseRegions(query.getString(VariantQueryParams.REGION.key()));
    }

    // consequence types (cts)
    if (query.containsKey(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key())
            && StringUtils.isNotEmpty(query.getString(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key()))) {
        consequenceTypes = Arrays
                .asList(query.getString(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key()).split("[,;]"));
    }

    // goal: [((xrefs OR regions) AND cts) OR (genes AND cts)] AND ... AND ...
    if (consequenceTypes.size() > 0) {
        if (genes.size() > 0) {
            // consequence types and genes
            String or = buildXrefOrRegionAndConsequenceType(xrefs, regions, consequenceTypes);
            if (xrefs.size() == 0 && regions.size() == 0) {
                // no xrefs or regions: genes AND cts
                filterList.add(buildGeneAndCt(genes, consequenceTypes));
            } else {
                // otherwise: [((xrefs OR regions) AND cts) OR (genes AND cts)]
                filterList.add("(" + or + ") OR (" + buildGeneAndCt(genes, consequenceTypes) + ")");
            }
        } else {
            // consequence types but no genes: (xrefs OR regions) AND cts
            // in this case, the resulting string will never be null, because there are some consequence types!!
            filterList.add(buildXrefOrRegionAndConsequenceType(xrefs, regions, consequenceTypes));
        }
    } else {
        // no consequence types: (xrefs OR regions) but we must add "OR genes", i.e.: xrefs OR regions OR genes
        // no consequence types: (xrefs OR regions) but we must add "OR genMINes", i.e.: xrefs OR regions OR genes
        // we must make an OR with xrefs, genes and regions and add it to the "AND" filter list
        String orXrefs = buildXrefOrGeneOrRegion(xrefs, genes, regions);
        if (!orXrefs.isEmpty()) {
            filterList.add(orXrefs);
        }
    }

    // now we continue with the other AND conditions...
    // type (t)
    String key = VariantQueryParams.STUDIES.key();
    if (isValidParam(query, VariantQueryParams.STUDIES)) {
        try {
            String value = query.getString(key);
            VariantDBAdaptorUtils.QueryOperation op = checkOperator(value);
            Set<Integer> studyIds = new HashSet<>(
                    variantDBAdaptorUtils.getStudyIds(splitValue(value, op), queryOptions));
            List<String> studyNames = new ArrayList<>(studyIds.size());
            Map<String, Integer> map = variantDBAdaptorUtils.getStudyConfigurationManager().getStudies(null);
            if (map != null && map.size() > 1) {
                map.forEach((name, id) -> {
                    if (studyIds.contains(id)) {
                        String[] s = name.split(":");
                        studyNames.add(s[s.length - 1]);
                    }
                });

                if (op == null || op == VariantDBAdaptorUtils.QueryOperation.OR) {
                    filterList.add(parseCategoryTermValue("studies", StringUtils.join(studyNames, ",")));
                } else {
                    filterList.add(parseCategoryTermValue("studies", StringUtils.join(studyNames, ";")));
                }
            }
        } catch (NullPointerException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

    // type (t)
    key = VariantQueryParams.TYPE.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseCategoryTermValue("type", query.getString(key)));
    }

    // Gene biotype
    key = VariantQueryParams.ANNOT_BIOTYPE.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseCategoryTermValue("biotypes", query.getString(key)));
    }

    // protein-substitution
    key = VariantQueryParams.ANNOT_PROTEIN_SUBSTITUTION.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseScoreValue(query.getString(key)));
    }

    // conservation
    key = VariantQueryParams.ANNOT_CONSERVATION.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseScoreValue(query.getString(key)));
    }

    // cadd, functional score
    key = VariantQueryParams.ANNOT_FUNCTIONAL_SCORE.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseScoreValue(query.getString(key)));
    }

    // maf population frequency
    // in the model: "popFreq__1kG_phase3__CLM":0.005319148767739534
    key = VariantQueryParams.ANNOT_POPULATION_MINOR_ALLELE_FREQUENCY.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parsePopValue("popFreq", query.getString(key)));
    }

    // stats maf
    // in the model: "stats__1kg_phase3__ALL"=0.02
    key = VariantQueryParams.STATS_MAF.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parsePopValue("stats", query.getString(key)));
    }

    // GO
    key = VariantQueryParams.ANNOT_GO.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        List<String> gos = Arrays.asList(query.getString(key).split(","));
        Set genesByGo = variantDBAdaptorUtils.getGenesByGo(gos);
        if (genesByGo != null && genesByGo.size() > 0) {
            filterList.add(parseCategoryTermValue("xrefs", StringUtils.join(genesByGo, ",")));
        }
    }

    // hpo
    key = VariantQueryParams.ANNOT_HPO.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseCategoryTermValue("traits", query.getString(key)));
    }

    // clinvar
    key = VariantQueryParams.ANNOT_CLINVAR.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseCategoryTermValue("traits", query.getString(key)));
    }

    // traits
    key = VariantQueryParams.ANNOT_TRAITS.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseCategoryTermValue("traits", query.getString(key)));
    }

    //-------------------------------------
    // Facet processing
    //-------------------------------------

    if (query.containsKey("facet.field")) {
        solrQuery.addFacetField((query.get("facet.field").toString()));
    }

    if (query.containsKey("facet.fields")) {
        solrQuery.addFacetField((query.get("facet.fields").toString().split(",")));
    }

    if (query.containsKey("facet.query")) {
        solrQuery.addFacetQuery(query.get("facet.query").toString());
    }

    if (query.containsKey("facet.prefix")) {
        solrQuery.setFacetPrefix(query.get("facet.prefix").toString());
    }

    if (query.containsKey("facet.range")) {

        Map<String, Map<String, Number>> rangeFields = (Map<String, Map<String, Number>>) query
                .get("facet.range");

        for (String k : rangeFields.keySet()) {
            Number rangeStart = rangeFields.get(k).get("facet.range.start");
            Number rangeEnd = rangeFields.get(k).get("facet.range.end");
            Number rangeGap = rangeFields.get(k).get("facet.range.gap");
            solrQuery.addNumericRangeFacet(k, rangeStart, rangeEnd, rangeGap);
        }
    }

    logger.debug("query = {}\n", query.toJson());

    solrQuery.setQuery("*:*");
    filterList.forEach(filter -> {
        solrQuery.addFilterQuery(filter);
        logger.debug("Solr fq: {}\n", filter);
    });

    return solrQuery;
}

From source file:org.opencms.search.solr.TestSolrSearch.java

License:Open Source License

/**
 * @throws Throwable if something goes wrong
 *///from w  w w .  j  a  va2  s  .  co  m
public void testAdvancedFacetting() throws Throwable {

    echo("Testing facet query count");

    // creating the query: facet=true&facet.field=Title_exact&facet.mincount=1&facet.query=text:OpenCms&rows=0
    SolrQuery query = new CmsSolrQuery(getCmsObject(), null);
    // facet=true
    query.setFacet(true);
    // facet.field=Title_exact
    query.addFacetField("Title_exact");
    // facet.mincount=1
    query.add("facet.mincount", "1");
    // facet.query=text:OpenCms
    query.addFacetQuery("text:OpenCms");
    // facet.query=Title_prop:OpenCms
    query.addFacetQuery("Title_prop:OpenCms");
    // rows=0
    query.setRows(new Integer(0));

    CmsSolrIndex index = OpenCms.getSearchManager().getIndexSolr(AllTests.SOLR_ONLINE);
    CmsSolrResultList results = index.search(getCmsObject(), query);
    long facetTextCount = results.getFacetQuery().get("text:OpenCms").intValue();
    long facetTitleCount = results.getFacetQuery().get("Title_prop:OpenCms").intValue();
    echo("Found '" + results.getFacetField("Title_exact").getValueCount()
            + "' facets for the field \"Title_exact\" and '" + facetTextCount
            + "' of them containing the word: \"OpenCms\" in the field 'text' and '" + facetTitleCount
            + "' of them containing the word \"OpenCms\" in the field 'Title_prop!'");

    query = new CmsSolrQuery(getCmsObject(), CmsRequestUtil.createParameterMap("q=text:OpenCms"));
    results = index.search(getCmsObject(), query);
    long numExpected = results.getNumFound();

    assertEquals(numExpected, facetTextCount);
    echo("Great Solr works fine!");
}

From source file:org.opencommercesearch.AbstractSearchServer.java

License:Apache License

@Override
public SearchResponse browse(BrowseOptions options, SolrQuery query, Site site, Locale locale,
        FilterQuery... filterQueries) throws SearchServerException {

    boolean hasCategoryId = StringUtils.isNotBlank(options.getCategoryId());
    boolean hasCategoryPath = StringUtils.isNotBlank(options.getCategoryPath());
    boolean hasBrandId = StringUtils.isNotBlank(options.getBrandId());
    boolean addCategoryGraph = (options.isFetchCategoryGraph()
            || (hasBrandId && options.isFetchProducts() && !hasCategoryId)) && !options.isRuleBasedPage();

    String categoryPath = null;/*from  www .  j a  va 2  s  .  c om*/

    if (hasCategoryPath) {
        categoryPath = options.getCategoryPath();
    } else {
        categoryPath = options.getCatalogId() + ".";
    }

    if (options.isRuleBasedPage()) {
        //handle rule based pages
        String filter = rulesBuilder.buildRulesFilter(options.getCategoryId(), locale);
        query.addFilterQuery(filter);
        query.setParam("q", "*:*");

    } else {
        //handle brand, category or onsale pages                
        if (addCategoryGraph) {
            query.setFacetPrefix(CATEGORY_PATH, categoryPath);
            query.addFacetField(CATEGORY_PATH);
            query.set("f.categoryPath.facet.limit", options.getMaxCategoryResults());
        }

        if (!options.isFetchProducts()) {
            query.setRows(0);
        }

        List<String> queryAltParams = new ArrayList<String>();

        if (hasCategoryId) {
            queryAltParams.add(CATEGORY_PATH + ":" + categoryPath);
            query.setParam("q", "");
        }

        if (hasBrandId) {
            queryAltParams.add(BRAND_ID + ":" + options.getBrandId());
            query.setParam("q", "");
        }

        if (options.isOnSale()) {
            queryAltParams.add("onsale" + locale.getCountry() + ":true");
        }

        if (queryAltParams.size() > 0) {

            query.set(Q_ALT, "(" + StringUtils.join(queryAltParams, " AND ") + ")");
        }
    }

    RepositoryItem catalog = null;
    if (site != null) {
        catalog = (RepositoryItem) site.getPropertyValue("defaultCatalog");
    }

    SearchResponse response = null;
    if (options.isRuleBasedPage()) {
        response = doSearch(query, site, catalog, locale, false, true, categoryPath, options.isOnSale(),
                options.getBrandId(), filterQueries);
    } else if (hasCategoryPath) {
        response = doSearch(query, site, catalog, locale, false, false, categoryPath, options.isOnSale(),
                options.getBrandId(), filterQueries);
    } else {
        response = doSearch(query, site, catalog, locale, false, false, null, options.isOnSale(),
                options.getBrandId(), filterQueries);
    }

    if (addCategoryGraph) {
        response.setCategoryGraph(
                createCategoryGraph(response, options.getCategoryPath(), options.getCatalogId(),
                        options.getCategoryId(), options.getDepthLimit(), options.getSeparator()));
    }

    return response;
}

From source file:org.opencommercesearch.AbstractSearchServer.java

License:Apache License

@Override
public Facet getFacet(Site site, Locale locale, String fieldFacet, int facetLimit, int depthLimit,
        String separator, FilterQuery... filterQueries) throws SearchServerException {
    try {//from   ww  w .j  a  va 2s .  c  om
        SolrQuery query = new SolrQuery();
        query.setRows(0);
        query.setQuery("*:*");
        query.addFacetField(fieldFacet);
        query.setFacetLimit(facetLimit);
        query.setFacetMinCount(1);
        query.addFilterQuery("country:" + locale.getCountry());

        RepositoryItem catalog = (RepositoryItem) site.getPropertyValue("defaultCatalog");
        String catalogId = catalog.getRepositoryId();
        query.setFacetPrefix(CATEGORY_PATH, catalogId + ".");
        query.addFilterQuery(CATEGORY_PATH + ":" + catalogId);

        if (filterQueries != null) {
            for (FilterQuery filterQuery : filterQueries) {
                query.addFilterQuery(filterQuery.toString());
            }
        }

        QueryResponse queryResponse = getCatalogSolrServer(locale).query(query);
        Facet facet = null;
        if (queryResponse != null && queryResponse.getFacetFields() != null) {
            FacetField facetField = queryResponse.getFacetField(fieldFacet);
            if (facetField != null) {
                List<Count> values = facetField.getValues();
                if (values != null && !values.isEmpty()) {
                    facet = new Facet();
                    facet.setName(StringUtils.capitalize(fieldFacet));
                    List<Filter> filters = new ArrayList<Facet.Filter>();

                    boolean filterByDepth = depthLimit > 0 && StringUtils.isNotBlank(separator);
                    for (Count count : values) {

                        if (filterByDepth
                                && StringUtils.countMatches(count.getName(), separator) > depthLimit) {
                            continue;
                        }

                        Filter filter = new Filter();
                        filter.setName(count.getName());
                        filter.setCount(count.getCount());
                        filter.setFilterQuery(count.getAsFilterQuery());
                        filter.setFilterQueries(count.getAsFilterQuery());
                        filters.add(filter);
                    }
                    facet.setFilters(filters);
                }
            }
        }
        return facet;
    } catch (SolrServerException ex) {
        throw create(SEARCH_EXCEPTION, ex);
    } catch (SolrException ex) {
        throw create(SEARCH_EXCEPTION, ex);
    }
}

From source file:org.opencommercesearch.AbstractSearchServer.java

License:Apache License

private SearchResponse doSearch(SolrQuery query, Site site, RepositoryItem catalog, Locale locale,
        boolean isSearch, boolean isRuleBasedPage, String categoryPath, boolean isOutletPage, String brandId,
        FilterQuery... filterQueries) throws SearchServerException {
    if (site == null) {
        throw new IllegalArgumentException("Missing site");
    }/*from  w  w w  .  j a  v a  2 s  .co  m*/
    if (catalog == null) {
        throw new IllegalArgumentException("Missing catalog");
    }
    long startTime = System.currentTimeMillis();

    query.addFacetField("category");
    query.set("facet.mincount", 1);

    RuleManager ruleManager = new RuleManager(getSearchRepository(), getRulesBuilder(),
            getRulesSolrServer(locale));
    if ((query.getRows() != null && query.getRows() > 0)
            || (query.get("group") != null && query.getBool("group"))) {
        setGroupParams(query, locale);
        setFieldListParams(query, locale.getCountry(), catalog.getRepositoryId());
        try {
            ruleManager.setRuleParams(query, isSearch, isRuleBasedPage, categoryPath, filterQueries, catalog,
                    isOutletPage, brandId);

            if (ruleManager.getRules().containsKey(SearchRepositoryItemDescriptor.REDIRECT_RULE)) {
                Map<String, List<RepositoryItem>> rules = ruleManager.getRules();
                List<RepositoryItem> redirects = rules.get(SearchRepositoryItemDescriptor.REDIRECT_RULE);
                if (redirects != null) {
                    RepositoryItem redirect = redirects.get(0);
                    return new SearchResponse(query, null, null, null,
                            (String) redirect.getPropertyValue(RedirectRuleProperty.URL), null, true);
                }
            }

        } catch (RepositoryException ex) {
            if (isLoggingError()) {
                logError("Unable to load search rules: " + ex.getMessage());
            }
            throw create(SEARCH_EXCEPTION, ex);
        } catch (SolrServerException ex) {
            if (isLoggingError()) {
                logError("Unable to load search rules: " + ex.getMessage());
            }
            throw create(SEARCH_EXCEPTION, ex);
        } catch (SolrException ex) {
            if (isLoggingError()) {
                logError("Unable to load search rules: " + ex.getMessage());
            }
            throw create(SEARCH_EXCEPTION, ex);
        }
    } else {
        ruleManager.setFilterQueries(filterQueries, catalog.getRepositoryId(), query);
    }

    try {
        QueryResponse queryResponse = getCatalogSolrServer(locale).query(query);

        String correctedTerm = null;
        boolean matchesAll = true;

        //if no results, check for spelling errors
        if (query.getRows() > 0 && isEmptySearch(queryResponse.getGroupResponse())
                && StringUtils.isNotEmpty(query.getQuery())) {

            SpellCheckResponse spellCheckResponse = queryResponse.getSpellCheckResponse();
            //try to do searching for the corrected term matching all terms (q.op=AND)
            QueryResponse tentativeResponse = handleSpellCheck(spellCheckResponse, getCatalogSolrServer(locale),
                    query, "AND");
            if (tentativeResponse != null) {
                //if we got results, set the corrected term variable and proceed to return the results
                queryResponse = tentativeResponse;
                correctedTerm = spellCheckResponse.getCollatedResult();
            } else {
                //if we didn't got any response, try doing another search matching any term (q.op=OR)
                tentativeResponse = handleSpellCheck(spellCheckResponse, getCatalogSolrServer(locale), query,
                        "OR");
                if (tentativeResponse != null) {
                    //if we got results for the match any term scenario. Set similar results to true
                    //and set the corrected term.
                    queryResponse = tentativeResponse;
                    matchesAll = false;
                    correctedTerm = query.getQuery();
                }
            }

        }

        long searchTime = System.currentTimeMillis() - startTime;
        if (isLoggingDebug()) {
            logDebug("Search time is " + searchTime + ", search engine time is " + queryResponse.getQTime());
        }

        SearchResponse searchResponse = new SearchResponse(query, queryResponse, ruleManager, filterQueries,
                null, correctedTerm, matchesAll);
        searchResponse.setRuleQueryTime(ruleManager.getLoadRulesTime());
        return searchResponse;
    } catch (SolrServerException ex) {
        throw create(SEARCH_EXCEPTION, ex);
    } catch (SolrException ex) {
        throw create(SEARCH_EXCEPTION, ex);
    }

}

From source file:org.openmrs.module.chartsearch.solr.ChartSearchSearcher.java

License:Mozilla Public License

public List<ChartListItem> getDocumentList(Integer patientId, String searchText, Integer start, Integer length,
        List<String> selectedCategories) throws Exception {
    SolrServer solrServer = SolrSingleton.getInstance().getServer();
    SolrQuery query = new SolrQuery();
    List<ChartListItem> list = new ArrayList<ChartListItem>();
    ChartSearchNonFacetFiltering nonFaceting = new ChartSearchNonFacetFiltering();

    searchText = StringUtils.isNotBlank(searchText) ? searchText : "*";
    //check for existence of characters such as ", and : in the search text and submit as it is if so
    if (searchText.contains("\"")) {
        String searchWhole = "text:" + searchText;
        query.setQuery(searchWhole);/*from   w ww .j  ava  2  s.c o m*/
    } else if (searchText.contains(":")) {
        query.setQuery(searchText);
    } else if (searchText.equals("*")) {
        query.setQuery(String.format("text:(%s)", searchText));
    } else {
        ChartSearchSyntax searchSyntax = new ChartSearchSyntax(searchText);
        searchText = searchSyntax.getSearchQuery();

        if (StringUtils.isNumeric(searchText)) {
            //this allows 36 returning 36.* for numerics
            searchText = searchText + ".*" + " || " + searchText;
        }
        query.setQuery(String.format("text:(%s)", searchText));
    }

    query.addFilterQuery(String.format("person_id:%d", patientId));
    addSelectedFilterQueriesToQuery(query, selectedCategories);
    query.setStart(start);
    query.setRows(length);
    query.setHighlight(true).setHighlightSnippets(1).setHighlightSimplePre("<b>")
            .setHighlightSimplePost("</b>");
    query.setParam("hl.fl", "text");

    query.remove(FacetParams.FACET_FIELD);
    query.setFacet(true);
    //adding facet field for concept_class
    query.addFacetField("concept_class_name");

    nonFaceting.applyNonFacetingLogicWhileSearching(patientId, searchText, selectedCategories, solrServer,
            query, list);

    return list;
}

From source file:org.pentaho.di.trans.steps.solrinput.SolrInput.java

License:Apache License

/**
 * Once the transformation starts executing, the processRow() method is called repeatedly
 * by PDI for as long as it returns true. To indicate that a step has finished processing rows
 * this method must call setOutputDone() and return false;
 * //w w w .jav a  2 s  .co  m
 * Steps which process incoming rows typically call getRow() to read a single row from the
 * input stream, change or add row content, call putRow() to pass the changed row on 
 * and return true. If getRow() returns null, no more rows are expected to come in, 
 * and the processRow() implementation calls setOutputDone() and returns false to
 * indicate that it is done too.
 * 
 * Steps which generate rows typically construct a new row Object[] using a call to
 * RowDataUtil.allocateRowData(numberOfFields), add row content, and call putRow() to
 * pass the new row on. Above process may happen in a loop to generate multiple rows,
 * at the end of which processRow() would call setOutputDone() and return false;
 * 
 * @param smi the step meta interface containing the step settings
 * @param sdi the step data interface that should be used to store
 * 
 * @return true to indicate that the function should be called again, false if the step is done
 */
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {

    if (first) {
        first = false;
        // Create the output row meta-data
        data.outputRowMeta = new RowMeta();
        meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
        // For String to <type> conversions, we allocate a conversion meta data row as well...
        data.convertRowMeta = data.outputRowMeta.cloneToType(ValueMetaInterface.TYPE_STRING);

        // get real values
        boolean tryCursor = true;
        Integer startRecord = 0;
        Integer chunkRowSize = 20;
        String realURL = meta.getURL();
        String realQ = meta.getQ();
        String realSort = meta.getSort();
        String realCursor = meta.getCursor();
        String realFq = meta.getFq();
        String realFl = meta.getFl();
        String realFacetQuery = meta.getFacetQuery();
        String realFacetField = meta.getFacetField();
        /* Send and Get the report */
        SolrQuery query = new SolrQuery();
        query.set("rows", chunkRowSize);
        if (realQ != null && !realQ.equals("")) {
            query.set("q", realQ);
        }
        if (realSort != null && !realSort.equals("")) {
            query.set("sort", realSort);
        } else {
            tryCursor = false;
        }
        if (realCursor != null && !realCursor.equals("")) {
            if (realCursor.equals("No")) {
                tryCursor = false;
            }
        }
        if (realFl != null && !realFl.equals("")) {
            query.set("fl", realFl);
        }
        if (realFq != null && !realFq.equals("")) {
            query.set("fq", realFq);
        }
        if (realFacetField != null && !realFacetField.equals("")) {
            //TODO incorporate multiple facet fields at once
            //String[] facetFields = realFacetField.split("\\s*,\\s*");
            //for(int i =0; i < facetFields.length; i++){
            query.addFacetField(realFacetField);
            //}
            query.setFacet(true);
            query.setFacetLimit(-1);
            query.setFacetMinCount(0);
            query.setFacetSort("count");
            query.set("rows", 0);
            tryCursor = false;
            data.facetRequested = true;
        }
        if (realFacetQuery != null && !realFacetQuery.equals("")) {
            query.addFacetQuery(realFacetQuery);
        }
        // You can't use "TimeAllowed" with "CursorMark"
        // The documentation says "Values <= 0 mean 
        // no time restriction", so setting to 0.
        query.setTimeAllowed(0);
        HttpSolrServer solr = new HttpSolrServer(realURL);
        String cursorMark = CursorMarkParams.CURSOR_MARK_START;
        boolean done = false;
        QueryResponse rsp = null;
        while (!done) {
            if (tryCursor) {
                query.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
            } else {
                query.setStart(startRecord);
            }
            try {
                rsp = solr.query(query);
            } catch (SolrServerException e) {
                e.printStackTrace();
            }
            if (data.facetRequested) {
                data.facetCountName = rsp.getFacetFields().get(0).getName();
                data.facetCounts = rsp.getFacetFields().get(0).getValues();
                done = true;
            } else {
                SolrDocumentList theseDocs = rsp.getResults();
                for (SolrDocument doc : theseDocs) {
                    data.documentList.add(doc);
                }
            }
            if (tryCursor) {
                String nextCursorMark = rsp.getNextCursorMark();
                if (cursorMark.equals(nextCursorMark)) {
                    done = true;
                } else {
                    cursorMark = nextCursorMark;
                }
            } else {
                startRecord = startRecord + chunkRowSize;
                if (startRecord >= rsp.getResults().getNumFound()) {
                    done = true;
                }
            }
        }
    }

    Object[] outputRowData = null;

    try {
        if (data.facetRequested) {
            // get one row if we can
            if (data.facetCounts.size() - 1 < data.recordIndex) {
                setOutputDone();
                return false;
            }
            FacetField.Count facetRecord = data.facetCounts.get(data.recordIndex);
            outputRowData = prepareFacetRecord(facetRecord);
        } else {
            // get one row if we can
            if (data.documentList.size() - 1 < data.recordIndex) {
                setOutputDone();
                return false;
            }
            SolrDocument record = data.documentList.get(data.recordIndex);
            outputRowData = prepareRecord(record);
        }
        putRow(data.outputRowMeta, outputRowData); // copy row to output rowset(s);
        data.recordIndex++;
        return true;
    } catch (KettleException e) {
        boolean sendToErrorRow = false;
        String errorMessage = null;
        if (getStepMeta().isDoingErrorHandling()) {
            sendToErrorRow = true;
            errorMessage = e.toString();
        } else {
            logError(BaseMessages.getString(PKG, "SolrInput.log.Exception", e.getMessage()));
            logError(Const.getStackTracker(e));
            setErrors(1);
            stopAll();
            setOutputDone(); // signal end to receiver(s)
            return false;
        }
        if (sendToErrorRow) {
            // Simply add this row to the error row
            putError(getInputRowMeta(), outputRowData, 1, errorMessage, null, "SolrInput001");
        }
    }
    return true;
}