Example usage for org.apache.solr.schema SchemaField SchemaField

List of usage examples for org.apache.solr.schema SchemaField SchemaField

Introduction

In this page you can find the example usage for org.apache.solr.schema SchemaField SchemaField.

Prototype

public SchemaField(SchemaField prototype, String name) 

Source Link

Document

Create a new SchemaField from an existing one by using all of the properties of the prototype except the field name.

Usage

From source file:net.yacy.cora.federate.solr.responsewriter.EnhancedXMLResponseWriter.java

License:Open Source License

private static final void writeDoc(final Writer writer, final IndexSchema schema, final String name,
        final List<IndexableField> fields, final float score, final boolean includeScore) throws IOException {
    startTagOpen(writer, "doc", name);

    if (includeScore) {
        writeTag(writer, "float", "score", Float.toString(score), false);
    }//from   ww w. j  av  a2 s  .  co  m

    int sz = fields.size();
    int fidx1 = 0, fidx2 = 0;
    while (fidx1 < sz) {
        IndexableField value = fields.get(fidx1);
        String fieldName = value.name();
        fidx2 = fidx1 + 1;
        while (fidx2 < sz && fieldName.equals(fields.get(fidx2).name())) {
            fidx2++;
        }
        SchemaField sf = schema == null ? null : schema.getFieldOrNull(fieldName);
        if (sf == null) {
            sf = new SchemaField(fieldName, new TextField());
        }
        FieldType type = sf.getType();
        if (fidx1 + 1 == fidx2) {
            if (sf.multiValued()) {
                startTagOpen(writer, "arr", fieldName);
                writer.write(lb);
                String sv = value.stringValue();
                writeField(writer, type.getTypeName(), null, sv); //sf.write(this, null, f1);
                writer.write("</arr>");
            } else {
                writeField(writer, type.getTypeName(), value.name(), value.stringValue()); //sf.write(this, f1.name(), f1);
            }
        } else {
            startTagOpen(writer, "arr", fieldName);
            writer.write(lb);
            for (int i = fidx1; i < fidx2; i++) {
                String sv = fields.get(i).stringValue();
                writeField(writer, type.getTypeName(), null, sv); //sf.write(this, null, (Fieldable)this.tlst.get(i));
            }
            writer.write("</arr>");
            writer.write(lb);
        }
        fidx1 = fidx2;
    }
    writer.write("</doc>");
    writer.write(lb);
}

From source file:net.yacy.cora.federate.solr.responsewriter.FlatJSONResponseWriter.java

License:Open Source License

private static final void writeDoc(final Writer writer, final IndexSchema schema, final String name,
        final List<IndexableField> fields, final float score, final boolean includeScore) throws IOException {
    JSONObject json = new JSONObject(true);

    int sz = fields.size();
    int fidx1 = 0, fidx2 = 0;
    while (fidx1 < sz) {
        IndexableField value = fields.get(fidx1);
        String fieldName = value.name();
        fidx2 = fidx1 + 1;/*w ww . ja va 2 s . com*/
        while (fidx2 < sz && fieldName.equals(fields.get(fidx2).name())) {
            fidx2++;
        }
        SchemaField sf = schema == null ? null : schema.getFieldOrNull(fieldName);
        if (sf == null) {
            sf = new SchemaField(fieldName, new TextField());
        }
        FieldType type = sf.getType();
        if (fidx1 + 1 == fidx2) {
            if (sf.multiValued()) {
                JSONArray a = new JSONArray();
                json.put(fieldName, a);
                JSONObject j = new JSONObject();
                String sv = value.stringValue();
                setValue(j, type.getTypeName(), "x", sv); //sf.write(this, null, f1);
                a.add(j.get("x"));
            } else {
                setValue(json, type.getTypeName(), value.name(), value.stringValue());
            }
        } else {
            JSONArray a = new JSONArray();
            json.put(fieldName, a);
            for (int i = fidx1; i < fidx2; i++) {
                String sv = fields.get(i).stringValue();
                JSONObject j = new JSONObject();
                setValue(j, type.getTypeName(), "x", sv); //sf.write(this, null, f1);
                a.add(j.get("x"));
            }
        }
        fidx1 = fidx2;
    }
    writer.write(json.toString());
    writer.write(lb);
}

From source file:net.yacy.cora.federate.solr.responsewriter.HTMLResponseWriter.java

License:Open Source License

public static final LinkedHashMap<String, String> translateDoc(final IndexSchema schema, final Document doc) {
    List<IndexableField> fields = doc.getFields();
    int sz = fields.size();
    int fidx1 = 0, fidx2 = 0;
    LinkedHashMap<String, String> kv = new LinkedHashMap<String, String>();
    while (fidx1 < sz) {
        IndexableField value = fields.get(fidx1);
        String fieldName = value.name();
        fidx2 = fidx1 + 1;/*  w w  w  .  j  a v  a 2  s .  co  m*/
        while (fidx2 < sz && fieldName.equals(fields.get(fidx2).name())) {
            fidx2++;
        }
        SchemaField sf = schema.getFieldOrNull(fieldName);
        if (sf == null)
            sf = new SchemaField(fieldName, new TextField());
        FieldType type = sf.getType();

        if (fidx1 + 1 == fidx2) {
            if (sf.multiValued()) {
                String sv = value.stringValue();
                kv.put(fieldName, field2string(type, sv));
            } else {
                kv.put(fieldName, field2string(type, value.stringValue()));
            }
        } else {
            int c = 0;
            for (int i = fidx1; i < fidx2; i++) {
                String sv = fields.get(i).stringValue();
                kv.put(fieldName + "_" + c++, field2string(type, sv));
            }
        }

        fidx1 = fidx2;
    }
    return kv;
}

From source file:org.apache.jackrabbit.core.query.lucene.JahiaLuceneQueryFactoryImpl.java

License:Open Source License

private Query resolveSingleMixedInclusiveExclusiveRangeQuery(String expression) {
    Query qobj = null;//from   w  w  w.  j  a v a 2  s . c o m
    boolean inclusiveEndRange = expression.endsWith("]");
    boolean exclusiveEndRange = expression.endsWith("}");
    int inclusiveBeginRangeCount = StringUtils.countMatches(expression, "[");
    int exclusiveBeginRangeCount = StringUtils.countMatches(expression, "{");
    if (((inclusiveEndRange && exclusiveBeginRangeCount == 1 && inclusiveBeginRangeCount == 0)
            || (exclusiveEndRange && inclusiveBeginRangeCount == 1 && exclusiveBeginRangeCount == 0))) {
        String fieldName = (inclusiveEndRange || exclusiveEndRange)
                ? StringUtils.substringBefore(expression, inclusiveEndRange ? ":{" : ":[")
                : "";
        if (fieldName.indexOf(' ') == -1) {
            fieldName = fieldName.replace("\\:", ":");
            String rangeExpression = StringUtils.substringBetween(expression, inclusiveEndRange ? "{" : "[",
                    inclusiveEndRange ? "]" : "}");
            String part1 = StringUtils.substringBefore(rangeExpression, " TO");
            String part2 = StringUtils.substringAfter(rangeExpression, "TO ");
            SchemaField sf = new SchemaField(fieldName, JahiaQueryParser.STRING_TYPE);
            qobj = JahiaQueryParser.STRING_TYPE.getRangeQuery(null, sf, part1.equals("*") ? null : part1,
                    part2.equals("*") ? null : part2, !inclusiveEndRange, inclusiveEndRange);
        }
    }
    return qobj;
}

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 www  .  j a  v  a2s .co  m
    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 ww  w .  j ava2 s . 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

void getFacetRangeCounts(String facetRange, NamedList<Object> resOuter)
        throws IOException, ParseException, RepositoryException {

    parseParams(FacetParams.FACET_RANGE, facetRange);
    String f = facetValue;//from   ww  w .  j a  v  a  2 s  .  co  m

    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"));
    SchemaField sf = new SchemaField(fieldNameInIndex, getType(epd));
    final FieldType ft = sf.getType();

    RangeEndpointCalculator<? extends Comparable> calc = null;

    if (ft instanceof TrieField) {
        final TrieField trie = (TrieField) ft;

        switch (trie.getType()) {
        case FLOAT:
            calc = new FloatRangeEndpointCalculator(sf);
            break;
        case DOUBLE:
            calc = new DoubleRangeEndpointCalculator(sf);
            break;
        case INTEGER:
            calc = new IntegerRangeEndpointCalculator(sf);
            break;
        case LONG:
            calc = new LongRangeEndpointCalculator(sf);
            break;
        default:
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                    "Unable to range facet on tried field of unexpected type:" + f);
        }
    } else if (ft instanceof DateField) {
        calc = new DateRangeEndpointCalculator(sf, NOW);
    } else if (ft instanceof SortableIntField) {
        calc = new IntegerRangeEndpointCalculator(sf);
    } else if (ft instanceof SortableLongField) {
        calc = new LongRangeEndpointCalculator(sf);
    } else if (ft instanceof SortableFloatField) {
        calc = new FloatRangeEndpointCalculator(sf);
    } else if (ft instanceof SortableDoubleField) {
        calc = new DoubleRangeEndpointCalculator(sf);
    } else {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unable to range facet on field:" + sf);
    }

    resOuter.add(key, getFacetRangeCounts(sf, f, calc));
}

From source file:org.opencommercesearch.lucene.queries.function.valuesource.BoostValueSourceParserTest.java

License:Apache License

@Before
public void setup() throws Exception {
    initMocks(this);

    schemaField = new SchemaField("productId", fieldType);

    vsp.defaultClient = httpClient;//from w w w  .j a  va 2s .c om

    when(fp.getReq()).thenReturn(request);
    when(fp.parseArg()).thenReturn("productId");
    when(request.getParams()).thenReturn(params);
    when(params.get(RuleManagerParams.CATALOG_ID)).thenReturn("myCatalog");
    when(params.get(BOOST_ID)).thenReturn(boostId);
    when(params.get(CommonParams.ROWS)).thenReturn("10");
    when(request.getSchema()).thenReturn(schema);
    when(schema.getField("productId")).thenReturn(schemaField);
    when(fieldType.getValueSource(schemaField, fp)).thenReturn(productIdValueSource);
    when(request.getSearcher()).thenReturn(searcher);
    when(searcher.getCache("boostCache")).thenReturn(boostCache);

    // product id function values
    when(productIdValueSource.getValues(any(Map.class), any(AtomicReaderContext.class)))
            .thenReturn(productIdFunctionValues);
    for (int i = 0; i <= 10; i++) {
        when(productIdFunctionValues.strVal(i)).thenReturn("prod" + i);
    }
}