Example usage for org.apache.solr.schema FieldType getTypeName

List of usage examples for org.apache.solr.schema FieldType getTypeName

Introduction

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

Prototype

public String getTypeName() 

Source Link

Document

The Name of this FieldType as specified in the schema file

Usage

From source file:com.sindicetech.siren.solr.qparser.SirenQParser.java

License:Open Source License

/**
 * Check if all fields are of type {@link com.sindicetech.siren.solr.schema.ExtendedJsonField}.
 *///from  w  ww  . ja  v  a2 s  .  co  m
private static void checkFieldTypes(final IndexSchema indexSchema, final Map<String, Float> queryFields) {
    for (final String fieldName : queryFields.keySet()) {
        final FieldType fieldType = indexSchema.getFieldType(fieldName);
        if (!(fieldType instanceof ExtendedJsonField)) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "FieldType: " + fieldName + " ("
                    + fieldType.getTypeName() + ") do not support SIREn's tree query");
        }
    }
}

From source file:lux.solr.SolrIndexConfig.java

License:Mozilla Public License

private void informField(FieldDefinition xmlField, SolrCore core) {
    Map<String, SchemaField> schemaFields = schema.getFields();
    Map<String, FieldType> fieldTypes = schema.getFieldTypes();
    String fieldName = xmlField.getName();
    if (schemaFields.containsKey(fieldName) && xmlField.getType() != Type.TOKENS) {
        // The Solr schema has a definition for this field, but it's not a TOKENS field:
        // We're only interested in TOKENS fields here; these need to install their own special field type since they wrap the
        // analyzer defined by the schema
        return;//w  w w  .j a va 2 s .co m
    }
    // look up the type of this field using the mapping in this class
    FieldType fieldType = getFieldType(xmlField);
    if (!fieldTypes.containsKey(fieldType.getTypeName())) {
        // The Solr schema does not define this field type, so add it
        logger.info("{} defining fieldType: {}", core.getName(), fieldType.getTypeName());
        fieldTypes.put(fieldType.getTypeName(), fieldType);
    } else {
        fieldType = fieldTypes.get(fieldType.getTypeName());
    }
    // Add the field to the schema
    logger.info(core.getName() + " defining field: {} of type {}", fieldName, fieldType.getTypeName());
    schemaFields.put(fieldName, new SchemaField(fieldName, fieldType, xmlField.getSolrFieldProperties(), ""));
}

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);
    }//  w w  w .j a  v  a  2 s .  com

    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;/*ww  w  . ja va 2  s  .c  o m*/
        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

@SuppressWarnings({ "static-access", "deprecation" })
private static String field2string(final FieldType type, final String value) {
    String typeName = type.getTypeName();
    if (typeName.equals(SolrType.bool.printName())) {
        return "F".equals(value) ? "false" : "true";
    } else if (typeName.equals(SolrType.date.printName())) {
        return TrieDateField.formatExternal(new Date(Long.parseLong(value)));
    }/*from ww  w .java2  s .c  om*/
    return value;
}

From source file:org.sindice.siren.solr.qparser.keyword.KeywordQParser.java

License:Apache License

/**
 * Initialize the "per-field" analyzer. Walk over the field boosts and
 * retrieve the associated query analyzer. <br>
 * For each field type, check if there is not an associated keyword field
 * type, i.e., a field type with an identical name with "-keyword" appended.
 *
 * @param boosts The field boosts/* w ww.  j a v  a2  s . c  o  m*/
 * @return The per-field analyzer wrapper.
 */
private Analyzer initAnalyzers(final Map<String, Float> boosts) {
    final Analyzer defaultAnalyzer = new WhitespaceAnalyzer(Version.LUCENE_31);
    final PerFieldAnalyzerWrapper analyzerWrapper = new PerFieldAnalyzerWrapper(defaultAnalyzer);

    // Add analyzers for each field type
    for (final String fieldName : boosts.keySet()) {
        final FieldType fieldType = req.getSchema().getFieldType(fieldName);
        // check if there is a MultiQueryAnalyzerWrapper
        // and extract the associated keyword query analzyer
        if (fieldType.getQueryAnalyzer() instanceof MultiQueryAnalyzerWrapper) {
            final MultiQueryAnalyzerWrapper wrapper = (MultiQueryAnalyzerWrapper) fieldType.getQueryAnalyzer();
            Analyzer keywordAnalyzer;
            if ((keywordAnalyzer = wrapper.getAnalyzer(fieldType.getTypeName() + "-keyword")) == null) {
                throw new SolrException(ErrorCode.SERVER_ERROR,
                        "Field type definition " + fieldType.getTypeName() + "-keyword not defined");
            }
            analyzerWrapper.addAnalyzer(fieldName, keywordAnalyzer);
        } else {
            analyzerWrapper.addAnalyzer(fieldName, fieldType.getQueryAnalyzer());
        }
    }
    return analyzerWrapper;
}

From source file:org.sindice.siren.solr.qparser.ntriple.NTripleQParser.java

License:Apache License

/**
 * Find the associated query analyzers for the given field type.
 * The query analyzers should be provided through the
 * {@link MultiQueryAnalyzerWrapper}.<br>
 * Derive the names of the field types from the name of the given field type.
 * There is three field types expected:/*from  w ww. ja v a  2s.  co  m*/
 * <ul>
 * <li> main: define the main NTriple query analyzer
 * <li> uri: define the analyzer for URIs
 * <li> literal: define the analyzer for literals
 * <ul>
 *
 * @param fieldType The field type of the ntriple fields to query
 */
private void initAnalyzers(final FieldType fieldType) {
    if (!(fieldType.getQueryAnalyzer() instanceof MultiQueryAnalyzerWrapper)) {
        throw new SolrException(ErrorCode.SERVER_ERROR,
                "MultiQueryAnalyzerWrapper" + " expected for field type " + fieldType.getTypeName());
    }
    final MultiQueryAnalyzerWrapper wrapper = (MultiQueryAnalyzerWrapper) fieldType.getQueryAnalyzer();
    if ((ntripleAnalyzer = wrapper.getAnalyzer(fieldType.getTypeName() + "-main")) == null) {
        throw new SolrException(ErrorCode.SERVER_ERROR,
                "Field type definition " + fieldType.getTypeName() + "-main not defined");
    }
    if ((uriAnalyzer = wrapper.getAnalyzer(fieldType.getTypeName() + "-uri")) == null) {
        throw new SolrException(ErrorCode.SERVER_ERROR,
                "Field type definition " + fieldType.getTypeName() + "-uri not defined");
    }
    if ((literalAnalyzer = wrapper.getAnalyzer(fieldType.getTypeName() + "-literal")) == null) {
        throw new SolrException(ErrorCode.SERVER_ERROR,
                "Field type definition " + fieldType.getTypeName() + "-literal not defined");
    }
}

From source file:org.sindice.siren.solr.qparser.SirenQParser.java

License:Apache License

/**
 * Check if all fields are of type {@link SirenField}.
 *//*from   w  w  w. ja va 2  s.  c  o m*/
private static void checkFieldTypes(final IndexSchema indexSchema, final Map<String, Float> queryFields) {
    for (final String fieldName : queryFields.keySet()) {
        final FieldType fieldType = indexSchema.getFieldType(fieldName);
        if (!(fieldType instanceof SirenField)) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "FieldType: " + fieldName + " ("
                    + fieldType.getTypeName() + ") do not support Siren's tree query");
        }
    }
}

From source file:org.sindice.siren.solr.qparser.tuple.TupleQParser.java

License:Open Source License

/**
 * Check if all fields are of type {@link SirenField}.
 *///from  w w  w  . j a  va  2s .  c o m
private void checkFieldTypes() {
    for (final String fieldName : boosts.keySet()) {
        final FieldType fieldType = req.getSchema().getFieldType(fieldName);
        if (!(fieldType instanceof SirenField)) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "FieldType: " + fieldName + " ("
                    + fieldType.getTypeName() + ") do not support NTriple Query");
        }
    }
}

From source file:solr2155.solr.search.function.GeoHashValueSource.java

License:Apache License

@SuppressWarnings({ "unchecked" })
GeoHashValueSource(String fieldName, SolrIndexSearcher searcher) throws IOException {
    log.info("Loading geohash field " + fieldName + " into memory.");
    this.fieldName = fieldName;

    //Get gridReferenceSystem
    final GridNode.GridReferenceSystem gridReferenceSystem;
    FieldType fieldType = searcher.getSchema().getField(fieldName).getType();
    if (fieldType instanceof GeoHashField) {
        gridReferenceSystem = ((GeoHashField) fieldType).getGridReferenceSystem();
    } else/*from  w w w  .j  a  va2s.  c  om*/
        throw new RuntimeException(
                "field " + fieldName + " should be a GeoHashField, not " + fieldType.getTypeName());

    //Traverse the index to load up doc2PointsCache
    IndexReader reader = searcher.getIndexReader();
    TermsEnumCompatibility termsEnum = new TermsEnumCompatibility(reader, fieldName);
    TermDocs termDocs = reader.termDocs(); //cached for termsEnum.docs() calls
    try {
        while (true) {
            final Term term = termsEnum.next();
            if (term == null)
                break;
            if (term.text().length() != gridReferenceSystem.getPrecision())
                continue;
            Point2D point = gridReferenceSystem.decodeXY(term.text());
            termDocs.seek(termsEnum.getTermEnum());
            while (termDocs.next()) {
                final int docId = termDocs.doc();
                if (docId == DocIdSetIterator.NO_MORE_DOCS)
                    break;
                if (doc2PointsCache == null)
                    doc2PointsCache = (List<Point2D>[]) new List[reader.maxDoc()];//java generics hack
                List<Point2D> points = doc2PointsCache[docId];
                if (points == null) {
                    points = new ArrayList<Point2D>(DEFAULT_ARRAY_CAPACITY);
                    doc2PointsCache[docId] = points;
                }
                points.add(point);
            }
        }
    } finally { // in Lucene 3 these should be closed (not in Lucene 4)
        termDocs.close();
        termsEnum.close();
    }

    //Log statistics
    if (log.isInfoEnabled()) {
        int min = Integer.MAX_VALUE, sum = 0, max = 0;
        int dlen = 0;
        if (doc2PointsCache != null) {
            dlen = doc2PointsCache.length;
            for (List<Point2D> point2Ds : doc2PointsCache) {
                int plen = (point2Ds == null ? 0 : point2Ds.size());
                min = Math.min(min, plen);
                max = Math.max(max, plen);
                sum += plen;
            }
        }
        if (min == Integer.MAX_VALUE)
            min = 0;
        float avg = (float) sum / dlen;
        log.info("field '" + fieldName + "' in RAM: loaded min/avg/max per doc #: (" + min + "," + avg + ","
                + max + ") #" + dlen);
    }
}