Example usage for org.apache.solr.common.params FacetParams FACET_ZEROS

List of usage examples for org.apache.solr.common.params FacetParams FACET_ZEROS

Introduction

In this page you can find the example usage for org.apache.solr.common.params FacetParams FACET_ZEROS.

Prototype

String FACET_ZEROS

To view the source code for org.apache.solr.common.params FacetParams FACET_ZEROS.

Click Source Link

Document

Boolean option indicating whether facet field counts of "0" should be included in the response.

Usage

From source file:org.jahia.services.search.facets.SimpleJahiaJcrFacets.java

License:Open Source License

/**
 * Returns a list of facet counts for each of the facet queries specified in the params
 * //from ww  w .jav a  2 s.com
 * @see FacetParams#FACET_QUERY
 */
public NamedList<Object> getFacetQueryCounts() throws IOException, ParseException {

    NamedList<Object> res = new SimpleOrderedMap<Object>();

    /* Ignore CommonParams.DF - could have init param facet.query assuming
     * the schema default with query param DF intented to only affect Q.
     * If user doesn't want schema default for facet.query, they should be
     * explicit.
     */
    // SolrQueryParser qp = searcher.getSchema().getSolrQueryParser(null);

    String[] facetQs = params.getParams(FacetParams.FACET_QUERY);
    if (null != facetQs && 0 != facetQs.length) {
        for (String q : facetQs) {
            try {
                Integer minCount = params.getFieldInt(q, FacetParams.FACET_MINCOUNT);
                if (minCount == null) {
                    Boolean zeros = params.getFieldBool(q, FacetParams.FACET_ZEROS);
                    // mincount = (zeros!=null && zeros) ? 0 : 1;
                    minCount = (zeros != null && !zeros) ? 1 : 0;
                    // current default is to include zeros.
                }
                for (String query : params.getFieldParams(q, "query")) {
                    parseParams(FacetParams.FACET_QUERY, query);

                    QueryParser qp = new JahiaQueryParser(FieldNames.FULLTEXT, new KeywordAnalyzer());
                    qp.setLowercaseExpandedTerms(false);
                    Query qobj = qp.parse(query);
                    long count = OpenBitSet.intersectionCount(getDocIdSet(qobj, ""), base);
                    if (count >= minCount) {
                        res.add(key, count);
                    }
                }
            } catch (Exception e) {
                String msg = "Exception during facet.query of " + q;
                logger.warn(msg, e);
                addException(msg, e);
            }
        }
    }

    return res;
}

From source file:org.jahia.services.search.facets.SimpleJahiaJcrFacets.java

License:Open Source License

public NamedList<Object> getTermCounts(String field, ExtendedPropertyDefinition epd, String fieldNameInIndex,
        String locale) throws IOException, RepositoryException {
    int offset = params.getFieldInt(field, FacetParams.FACET_OFFSET, 0);
    int limit = params.getFieldInt(field, FacetParams.FACET_LIMIT, 100);
    if (limit == 0)
        return new NamedList<Object>();
    Integer mincount = params.getFieldInt(field, FacetParams.FACET_MINCOUNT);
    if (mincount == null) {
        Boolean zeros = params.getFieldBool(field, FacetParams.FACET_ZEROS);
        // mincount = (zeros!=null && zeros) ? 0 : 1;
        mincount = (zeros != null && !zeros) ? 1 : 0;
        // current default is to include zeros.
    }//from w  ww .j  a  v a 2 s. c om
    boolean missing = params.getFieldBool(field, FacetParams.FACET_MISSING, false);
    // default to sorting if there is a limit.
    String sort = params.getFieldParam(field, FacetParams.FACET_SORT,
            limit > 0 ? FacetParams.FACET_SORT_COUNT : FacetParams.FACET_SORT_INDEX);
    String prefix = params.getFieldParam(field, FacetParams.FACET_PREFIX);

    NamedList<Object> counts;
    SchemaField sf = new SchemaField(fieldNameInIndex, getType(epd));
    FieldType ft = sf.getType();

    // determine what type of faceting method to use
    String method = params.getFieldParam(field, FacetParams.FACET_METHOD);
    boolean enumMethod = FacetParams.FACET_METHOD_enum.equals(method);
    if (method == null && ft instanceof BoolField) {
        // Always use filters for booleans... we know the number of values is very small.
        enumMethod = true;
    }
    boolean multiToken = epd.isMultiple();
    // --> added by jahia as we don't use the UnInvertedField class yet due to not using SolrIndexSearcher        
    enumMethod = enumMethod || multiToken;
    //        if (TrieField.getMainValuePrefix(ft) != null) {
    //          // A TrieField with multiple parts indexed per value... currently only
    //          // UnInvertedField can handle this case, so force it's use.
    //          enumMethod = false;
    //          multiToken = true;
    //        }

    // unless the enum method is explicitly specified, use a counting method.
    if (enumMethod) {
        // Always use filters for booleans... we know the number of values is very small.
        counts = getFacetTermEnumCounts(searcher, docs, field, fieldNameInIndex, offset, limit, mincount,
                missing, sort, prefix, epd.isInternationalized() ? (locale == null ? "" : locale) : null, epd);
    } else {
        //            if (multiToken) {
        //                UnInvertedField uif = UnInvertedField.getUnInvertedField(field, searcher);
        //                counts = uif.getCounts(searcher, base, offset, limit, mincount,missing,sort,prefix);
        //              } else {
        // TODO: future logic could use filters instead of the fieldcache if
        // the number of terms in the field is small enough.
        counts = getFieldCacheCounts(searcher, docs, fieldNameInIndex, offset, limit, mincount, missing, sort,
                prefix, epd.isInternationalized() ? (locale == null ? "" : locale) : null, epd);
        //              }
    }

    return counts;
}

From source file:org.jahia.services.search.facets.SimpleJahiaJcrFacets.java

License:Open Source License

/**
 * @deprecated Use getFacetRangeCounts which is more generalized
 *///from w w w.j a v  a2 s  . com
@Deprecated
public void getFacetDateCounts(String dateFacet, NamedList<Object> resOuter)
        throws IOException, ParseException, RepositoryException, JahiaException {

    parseParams(FacetParams.FACET_DATE, dateFacet);
    String f = facetValue;

    final NamedList<Object> resInner = new SimpleOrderedMap<Object>();
    String fieldName = StringUtils.substringBeforeLast(f, PROPNAME_INDEX_SEPARATOR);
    ExtendedPropertyDefinition epd = NodeTypeRegistry.getInstance()
            .getNodeType(params.get("f." + f + ".facet.nodetype")).getPropertyDefinition(fieldName);
    String fieldNameInIndex = getFieldNameInIndex(f, fieldName, epd, params.getFieldParam(f, "facet.locale"));
    String prefix = params.getFieldParam(f, FacetParams.FACET_PREFIX);
    DateField ft = StringUtils.isEmpty(prefix) ? JahiaQueryParser.DATE_TYPE : JahiaQueryParser.JR_DATE_TYPE;
    final SchemaField sf = new SchemaField(fieldNameInIndex, ft);

    // TODO: Should we use the key now ?
    //    resOuter.add(key, resInner);
    resOuter.add(fieldName + PROPNAME_INDEX_SEPARATOR + fieldNameInIndex, resInner);

    if (!(epd.getRequiredType() == PropertyType.DATE)) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "Can not date facet on a field which is not a DateField: " + f);
    }
    Integer minCount = params.getFieldInt(f, FacetParams.FACET_MINCOUNT);
    if (minCount == null) {
        Boolean zeros = params.getFieldBool(f, FacetParams.FACET_ZEROS);
        // mincount = (zeros!=null && zeros) ? 0 : 1;
        minCount = (zeros != null && !zeros) ? 1 : 0;
        // current default is to include zeros.
    }

    final String startS = required.getFieldParam(f, FacetParams.FACET_DATE_START);
    final Date start;
    try {
        start = JahiaQueryParser.DATE_TYPE.parseMath(NOW, startS);
    } catch (SolrException e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "date facet 'start' is not a valid Date string: " + startS, e);
    }
    final String endS = required.getFieldParam(f, FacetParams.FACET_DATE_END);
    Date end; // not final, hardend may change this
    try {
        end = JahiaQueryParser.DATE_TYPE.parseMath(NOW, endS);
    } catch (SolrException e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "date facet 'end' is not a valid Date string: " + endS, e);
    }

    if (end.before(start)) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "date facet 'end' comes before 'start': " + endS + " < " + startS);
    }

    final String gap = required.getFieldParam(f, FacetParams.FACET_DATE_GAP);
    final DateMathParser dmp = new DateMathParser(DateField.UTC, Locale.US);
    dmp.setNow(NOW);

    String[] iStrs = params.getFieldParams(f, FacetParams.FACET_DATE_INCLUDE);
    // Legacy support for default of [lower,upper,edge] for date faceting
    // this is not handled by FacetRangeInclude.parseParam because
    // range faceting has differnet defaults
    final EnumSet<FacetRangeInclude> include = (null == iStrs || 0 == iStrs.length)
            ? EnumSet.of(FacetRangeInclude.LOWER, FacetRangeInclude.UPPER, FacetRangeInclude.EDGE)
            : FacetRangeInclude.parseParam(iStrs);

    try {
        Date low = start;
        while (low.before(end)) {
            dmp.setNow(low);
            String label = JahiaQueryParser.DATE_TYPE.toExternal(low);

            Date high = dmp.parseMath(gap);
            if (end.before(high)) {
                if (params.getFieldBool(f, FacetParams.FACET_DATE_HARD_END, false)) {
                    high = end;
                } else {
                    end = high;
                }
            }
            if (high.before(low)) {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                        "date facet infinite loop (is gap negative?)");
            }
            final boolean includeLower = (include.contains(FacetRangeInclude.LOWER)
                    || (include.contains(FacetRangeInclude.EDGE) && low.equals(start)));
            final boolean includeUpper = (include.contains(FacetRangeInclude.UPPER)
                    || (include.contains(FacetRangeInclude.EDGE) && high.equals(end)));

            Query rangeQuery = getRangeQuery(ft, null, sf, prefix, low, high, includeLower, includeUpper);

            int count = rangeCount(rangeQuery);
            if (count >= minCount) {
                // TODO: Can we use just label here ?                  
                resInner.add(label + PROPNAME_INDEX_SEPARATOR + rangeQuery.toString(), count);
            }
            low = high;
        }
    } catch (java.text.ParseException e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "date facet 'gap' is not a valid Date Math string: " + gap, e);
    }

    // explicitly return the gap and end so all the counts are meaningful
    resInner.add("gap", gap);
    resInner.add("start", start);
    resInner.add("end", end);

    final String[] othersP = params.getFieldParams(f, FacetParams.FACET_DATE_OTHER);
    if (null != othersP && 0 < othersP.length) {
        final Set<FacetDateOther> others = EnumSet.noneOf(FacetDateOther.class);

        for (final String o : othersP) {
            others.add(FacetDateOther.get(o));
        }

        // no matter what other values are listed, we don't do
        // anything if "none" is specified.
        if (!others.contains(FacetDateOther.NONE)) {
            boolean all = others.contains(FacetDateOther.ALL);

            if (all || others.contains(FacetDateOther.BEFORE)) {
                Query rangeQuery = getRangeQuery(ft, null, sf, prefix, null, start, false,
                        (include.contains(FacetRangeInclude.OUTER)
                                || (!(include.contains(FacetRangeInclude.LOWER)
                                        || include.contains(FacetRangeInclude.EDGE)))));
                int count = rangeCount(rangeQuery);
                if (count >= minCount) {
                    resInner.add(
                            FacetDateOther.BEFORE.toString() + PROPNAME_INDEX_SEPARATOR + rangeQuery.toString(),
                            count);
                }
            }
            if (all || others.contains(FacetDateOther.AFTER)) {
                Query rangeQuery = getRangeQuery(ft, null, sf, prefix, end, null,
                        (include.contains(FacetRangeInclude.OUTER)
                                || (!(include.contains(FacetRangeInclude.UPPER)
                                        || include.contains(FacetRangeInclude.EDGE)))),
                        false);
                int count = rangeCount(rangeQuery);
                if (count >= minCount) {
                    resInner.add(
                            FacetDateOther.AFTER.toString() + PROPNAME_INDEX_SEPARATOR + rangeQuery.toString(),
                            count);
                }
            }
            if (all || others.contains(FacetDateOther.BETWEEN)) {
                Query rangeQuery = getRangeQuery(ft, null, sf, prefix, start, end,
                        (include.contains(FacetRangeInclude.LOWER) || include.contains(FacetRangeInclude.EDGE)),
                        (include.contains(FacetRangeInclude.UPPER)
                                || include.contains(FacetRangeInclude.EDGE)));
                int count = rangeCount(rangeQuery);
                if (count >= minCount) {
                    resInner.add(FacetDateOther.BETWEEN.toString() + PROPNAME_INDEX_SEPARATOR
                            + rangeQuery.toString(), count);
                }
            }
        }
    }
}

From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java

License:Apache License

@Test
public void testConstructSolrQueryWithCustomFieldFacetParameters() {
    FacetQuery query = new SimpleFacetQuery(new Criteria("field_1").is("value_1"));
    FieldWithFacetParameters fieldWithFacetParameters = new FieldWithFacetParameters("facet_2")
            .addFacetParameter(new FacetParameter(FacetParams.FACET_ZEROS, "on"));
    FacetOptions facetOptions = new FacetOptions(new SimpleField("facet_1"), fieldWithFacetParameters);
    query.setFacetOptions(facetOptions);

    SolrQuery solrQuery = queryParser.constructSolrQuery(query);
    Assert.assertEquals("on",
            solrQuery.getParams("f." + fieldWithFacetParameters.getName() + ".facet.zeros")[0]);
}