List of usage examples for org.apache.solr.common.params FacetParams FACET_PREFIX
String FACET_PREFIX
To view the source code for org.apache.solr.common.params FacetParams FACET_PREFIX.
Click Source Link
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 ww w . jav 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
public String getFieldNameInIndex(String field, String propertyFieldName, ExtendedPropertyDefinition epd, String langCode) {// w ww .jav a 2 s . c o m String fieldName = propertyFieldName; try { fieldName = resolver.getJCRName(NameFactoryImpl.getInstance() .create(session.getNamespaceURI(epd.getPrefix()), epd.getLocalName())); int idx = fieldName.indexOf(':'); if (epd != null && epd.isFacetable()) { fieldName = fieldName.substring(0, idx + 1) + JahiaNodeIndexer.FACET_PREFIX + fieldName.substring(idx + 1); } else if ((epd == null || !epd.isFacetable())) { String prefix = params.getFieldParam(field, FacetParams.FACET_PREFIX); if (params instanceof MapSolrParams) { ((MapSolrParams) params).getMap().put("f." + field + '.' + FacetParams.FACET_PREFIX, fieldName + "[" + (prefix != null ? prefix : "")); } else if (params instanceof MultiMapSolrParams) { ((MultiMapSolrParams) params).getMap().put("f." + field + '.' + FacetParams.FACET_PREFIX, new String[] { fieldName + "[" + (prefix != null ? prefix : "") }); } fieldName = FieldNames.PROPERTIES; } else { fieldName = fieldName.substring(0, idx + 1) + FieldNames.FULLTEXT_PREFIX + fieldName.substring(idx + 1); } } catch (RepositoryException e) { // will never happen <- false: this actually happens quite often as epd.getPrefix is sometimes null // todo: what should be done here when the exception happens? Or rather, what should be done to prevent it? } return fieldName; }
From source file:org.jahia.services.search.facets.SimpleJahiaJcrFacets.java
License:Open Source License
/** * @deprecated Use getFacetRangeCounts which is more generalized *///from ww w . jav a2s . c om @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.jahia.services.search.facets.SimpleJahiaJcrFacets.java
License:Open Source License
private <T extends Comparable<T>> NamedList<Object> getFacetRangeCounts(final SchemaField sf, final String f, final RangeEndpointCalculator<T> calc) throws IOException { String prefix = params.getFieldParam(f, FacetParams.FACET_PREFIX); final NamedList<Object> res = new SimpleOrderedMap<Object>(); final NamedList<Object> counts = new NamedList<Object>(); res.add("counts", counts); final T start = calc.getValue(required.getFieldParam(f, FacetParams.FACET_RANGE_START)); // not final, hardend may change this T end = calc.getValue(required.getFieldParam(f, FacetParams.FACET_RANGE_END)); if (end.compareTo(start) < 0) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "range facet 'end' comes before 'start': " + end + " < " + start); }/*from w w w. j a v a 2 s .c o m*/ final String gap = required.getFieldParam(f, FacetParams.FACET_RANGE_GAP); // explicitly return the gap. compute this early so we are more // likely to catch parse errors before attempting math res.add("gap", calc.getGap(gap)); final int minCount = params.getFieldInt(f, FacetParams.FACET_MINCOUNT, 0); final EnumSet<FacetRangeInclude> include = FacetRangeInclude .parseParam(params.getFieldParams(f, FacetParams.FACET_RANGE_INCLUDE)); T low = start; while (low.compareTo(end) < 0) { T high = calc.addGap(low, gap); if (end.compareTo(high) < 0) { if (params.getFieldBool(f, FacetParams.FACET_RANGE_HARD_END, false)) { high = end; } else { end = high; } } if (high.compareTo(low) < 0) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "range facet infinite loop (is gap negative? did the math overflow?)"); } final boolean includeLower = (include.contains(FacetRangeInclude.LOWER) || (include.contains(FacetRangeInclude.EDGE) && 0 == low.compareTo(start))); final boolean includeUpper = (include.contains(FacetRangeInclude.UPPER) || (include.contains(FacetRangeInclude.EDGE) && 0 == high.compareTo(end))); final String lowS = calc.formatValue(low); final String highS = calc.formatValue(high); Query rangeQ = getRangeQuery(sf.getType(), null, sf, prefix, lowS, highS, includeLower, includeUpper); final int count = rangeCount(rangeQ); if (count >= minCount) { counts.add(lowS + PROPNAME_INDEX_SEPARATOR + rangeQ.toString(), count); } low = high; } // explicitly return the start and end so all the counts // (including before/after/between) are meaningful - even if mincount // has removed the neighboring ranges res.add("start", start); res.add("end", end); final String[] othersP = params.getFieldParams(f, FacetParams.FACET_RANGE_OTHER); if (null != othersP && 0 < othersP.length) { Set<FacetRangeOther> others = EnumSet.noneOf(FacetRangeOther.class); for (final String o : othersP) { others.add(FacetRangeOther.get(o)); } // no matter what other values are listed, we don't do // anything if "none" is specified. if (!others.contains(FacetRangeOther.NONE)) { boolean all = others.contains(FacetRangeOther.ALL); final String startS = calc.formatValue(start); final String endS = calc.formatValue(end); if (all || others.contains(FacetRangeOther.BEFORE)) { // include upper bound if "outer" or if first gap doesn't already include it Query rangeQ = getRangeQuery(sf.getType(), null, sf, prefix, null, startS, false, (include.contains(FacetRangeInclude.OUTER) || (!(include.contains(FacetRangeInclude.LOWER) || include.contains(FacetRangeInclude.EDGE))))); int count = rangeCount(rangeQ); if (count >= minCount) { res.add(FacetRangeOther.BEFORE.toString(), count); counts.add(FacetRangeOther.BEFORE.toString() + PROPNAME_INDEX_SEPARATOR + rangeQ.toString(), count); } } if (all || others.contains(FacetRangeOther.AFTER)) { // include lower bound if "outer" or if last gap doesn't already include it Query rangeQ = getRangeQuery(sf.getType(), null, sf, prefix, endS, null, (include.contains(FacetRangeInclude.OUTER) || (!(include.contains(FacetRangeInclude.UPPER) || include.contains(FacetRangeInclude.EDGE)))), false); int count = rangeCount(rangeQ); if (count >= minCount) { res.add(FacetRangeOther.AFTER.toString(), count); counts.add(FacetRangeOther.AFTER.toString() + PROPNAME_INDEX_SEPARATOR + rangeQ.toString(), count); } } if (all || others.contains(FacetRangeOther.BETWEEN)) { Query rangeQ = getRangeQuery(sf.getType(), null, sf, prefix, startS, endS, (include.contains(FacetRangeInclude.LOWER) || include.contains(FacetRangeInclude.EDGE)), (include.contains(FacetRangeInclude.UPPER) || include.contains(FacetRangeInclude.EDGE))); int count = rangeCount(rangeQ); if (count >= minCount) { res.add(FacetRangeOther.BETWEEN.toString(), count); counts.add( FacetRangeOther.BETWEEN.toString() + PROPNAME_INDEX_SEPARATOR + rangeQ.toString(), count); } } } } return res; }
From source file:org.opencommercesearch.SearchResponse.java
License:Apache License
public List<Facet> getFacets() { Map<String, Facet> facetMap = new HashMap<String, Facet>(); FacetManager manager = getRuleManager().getFacetManager(); for (FacetField facetField : queryResponse.getFacetFields()) { Facet facet = new Facet(); facet.setName(manager.getFacetName(facetField)); facet.setMinBuckets(manager.getFacetMinBuckets(facetField)); facet.setIsMultiSelect(manager.isMultiSelectFacet(facetField)); facet.setIsMixedSorting(manager.isMixedSorting(facetField)); setMetadata(manager, facetField.getName(), facet); List<Filter> filters = new ArrayList<Filter>(facetField.getValueCount()); String prefix = query.getFieldParam(facet.getName(), FacetParams.FACET_PREFIX); Set<String> facetBlackList = getFacetBlacklist(facetField.getName()); for (Count count : facetField.getValues()) { String filterName = manager.getCountName(count, prefix); if (facetBlackList.contains(filterName)) { continue; }/* w w w. j ava2s . com*/ Filter filter = new Filter(); filter.setName(filterName); filter.setCount(count.getCount()); filter.setFilterQueries(manager.getCountPath(count, getFilterQueries())); filter.setFilterQuery(count.getAsFilterQuery()); filter.setSelected(count.getFacetField().getName(), filterName, filterQueries); filters.add(filter); } facet.setFilters(filters); facetMap.put(facetField.getName(), facet); } getRangeFacets(facetMap); getQueryFacets(facetMap); List<Facet> sortedFacets = new ArrayList<Facet>(facetMap.size()); for (String fieldName : new String[] { "category", "categoryPath" }) { Facet facet = facetMap.get(fieldName); if (facet != null) { sortedFacets.add(facet); } } for (String fieldName : manager.facetFieldNames()) { Facet facet = facetMap.get(fieldName); if (facet != null) { sortedFacets.add(facet); } } return sortedFacets; }