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

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

Introduction

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

Prototype

public FieldType getType() 

Source Link

Usage

From source file:alba.solr.core.DocValuesDynamicValueSource.java

License:Apache License

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    SchemaField field = this.functionQueryParser.getReq().getSchema().getField("id");

    return field.getType().getValueSource(field, this.functionQueryParser).getValues(context, readerContext);

    /*//from w ww. jav  a  2s.c  o  m
    final NumericDocValues arr = DocValues.getNumeric(readerContext.reader(), "id");
    final Bits valid = DocValues.getDocsWithField(readerContext.reader(), "id");
            
    return new LongDocValues(this) {
       @Override
       public long longVal(int doc) {
    return arr.get(doc);
               
       }
               
       @Override
       public double doubleVal(int doc) {
    return arr.get(doc);
       }
               
            
       @Override
       public boolean exists(int doc) {
    return arr.get(doc) != 0 || valid.get(doc);
       }
            
       @Override
       public Object objectVal(int doc) {
    return valid.get(doc) ? longToObject(arr.get(doc)) : null;
       }
            
       @Override
       public String strVal(int doc) {
    return valid.get(doc) ? longToString(arr.get(doc)) : null;
       }
            
       @Override
       protected long externalToLong(String extVal) {
    return DocValuesDynamicValueSource.this.externalToLong(extVal);
       }
            
       @Override
       public ValueFiller getValueFiller() {
    return new ValueFiller() {
       private final MutableValueLong mval = newMutableValueLong();
            
       @Override
       public MutableValue getValue() {
          return mval;
       }
            
       @Override
       public void fillValue(int doc) {
          mval.value = arr.get(doc);
          mval.exists = mval.value != 0 || valid.get(doc);
       }
    };
       }
            
    }; */
}

From source file:alba.solr.core.DynamicValueSourceParser.java

License:Apache License

@SuppressWarnings("unchecked")
public ValueSource parse(FunctionQParser fp) throws SyntaxError {

    String functionName;/*from   www  . jav  a 2 s. com*/

    List<ValueSource> valueSourceList = new ArrayList<ValueSource>();

    functions = (Map<String, CallableFunction>) fp.getReq().getContext().get(Loader.FUNCTIONS);

    Map<String, ValueSource> values = new HashMap<String, ValueSource>();

    int i = 0;
    while (fp.hasMoreArguments()) {
        rawargs[i++] = fp.parseArg();
    }

    functionName = rawargs[0];

    CallableFunction function = functions.get(functionName);

    //still need this?

    // FunctionExecutionContext cachedEC = (FunctionExecutionContext)fp.getReq().getContext().get(fp.getString() );
    /* if (cachedEC != null) {
       logger.error("reusing executor from cache!");
       return cachedEC.getFunctionExecutor();
    } */

    for (int k = 1; k < i; k++) {
        String parts[] = rawargs[k].split("=");
        String name = parts[0];
        String value = parts[1];

        args.put(parts[0], parts[1]);

        if (value.startsWith("\"") && value.endsWith("\"")) {
            //probably quite ineffcient..
            String v = value.replaceAll("^\"", "").replaceAll("\"$", "");
            LiteralValueSource l = new LiteralValueSource(v);
            values.put(name, l);
            valueSourceList.add(l);
        } else if (NumberUtils.isNumber(value)) {
            ConstValueSource cvs = new ConstValueSource(Float.parseFloat(value));
            values.put(name, cvs);
            valueSourceList.add(cvs);
        } else {
            SchemaField f = fp.getReq().getSchema().getField(value);
            ValueSource vs = f.getType().getValueSource(f, fp);
            values.put(name, vs);
            valueSourceList.add(vs);
        }

    }

    FunctionExecutor executor = new FunctionExecutor(values, valueSourceList, fp, this);

    executor.setFunction(functions.get(functionName));

    // still need this????
    FunctionExecutionContext ec = new FunctionExecutionContext(fp.getString(), values, function, executor);
    fp.getReq().getContext().put(fp.getString(), ec);

    return executor;

}

From source file:alba.solr.docvalues.DynamicDocValuesHelper.java

License:Apache License

public Object eval(int doc) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    // TODO Auto-generated method stub

    /*if (doc < 0 || doc > this.readerContext.reader().maxDoc()) {
       return null;//  w  ww  . j a va2s .c  om
    }*/

    Map<String, Object> params = new HashMap<String, Object>();

    for (String s : args.keySet()) {
        if (args.get(s).startsWith("\"")) {
            params.put(s, args.get(s));
        } else if (NumberUtils.isNumber(args.get(s))) {
            Object objVal;

            try {
                objVal = Long.parseLong(args.get(s));
            } catch (NumberFormatException nfe1) {
                try {
                    objVal = Float.parseFloat(args.get(s));
                } catch (NumberFormatException nfe2) {
                    objVal = s;
                }

            }

            if (objVal != null) {
                params.put(s, objVal);
            } else {
                params.put(s, "N/A");
            }

        } else if ("false".equals(args.get(s).toLowerCase())) {
            params.put(s, false);
        } else if ("true".equals(args.get(s).toLowerCase())) {
            params.put(s, true);
        } else {
            SchemaField f = fp.getReq().getSchema().getField(args.get(s));

            ValueSource vs = f.getType().getValueSource(f, fp);

            Object objVal = null;

            try {
                objVal = vs.getValues(this.context, this.readerContext).longVal(doc);
                params.put(s, objVal);
            } catch (IOException | UnsupportedOperationException e) {
                // TODO Auto-generated catch block
                // TODO Log properly

                try {
                    objVal = vs.getValues(this.context, this.readerContext).floatVal(doc);
                } catch (IOException | UnsupportedOperationException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();

                    try {
                        objVal = vs.getValues(this.context, this.readerContext).strVal(doc);
                    } catch (IOException | UnsupportedOperationException e2) {
                        // TODO Auto-generated catch block
                        e2.printStackTrace();
                    }
                }

                logger.error("error converting values ", e);
            }

            if (objVal != null) {
                params.put(s, objVal);
            } else {
                params.put(s, "N/A");
            }

        }

    }

    CallableFunction cf = functions.get(this.functionName);

    if (cf == null) {
        logger.error("unable to get function " + this.functionName);
    }

    if (cf != null) {
        List<Object> fnParams = new ArrayList<Object>();
        Parameter[] methodParameters = cf.getMethod().getParameters();

        //TODO spostare quanto pi codice possibile in fase di inizializzazione
        for (Parameter p : methodParameters) {
            if (p.isAnnotationPresent(Param.class)) {
                Param paramAnnotation = p.getAnnotation(Param.class);
                fnParams.add(params.get(paramAnnotation.name()));
            }
        }

        return cf.getMethod().invoke(cf.getInstance(), fnParams.toArray());
    } else {
        return null;
    }

}

From source file:alba.solr.docvalues.FloatFunction.java

License:Apache License

@Override
public float floatVal(int doc) {
    // TODO Auto-generated method stub
    Map<String, Object> params = new HashMap<String, Object>();

    for (String s : args.keySet()) {
        if (args.get(s).startsWith("\"")) {
            params.put(s, args.get(s));/*from ww w.j  a  v a  2s  . c  o  m*/
        } else if (NumberUtils.isNumber(args.get(s))) {
            Object objVal;

            try {
                objVal = Long.parseLong(args.get(s));
            } catch (NumberFormatException nfe1) {
                try {
                    objVal = Float.parseFloat(args.get(s));
                } catch (NumberFormatException nfe2) {
                    objVal = s;
                }

            }

            if (objVal != null) {
                params.put(s, objVal);
            } else {
                params.put(s, "N/A");
            }

        } else if ("false".equals(args.get(s).toLowerCase())) {
            params.put(s, false);
        } else if ("true".equals(args.get(s).toLowerCase())) {
            params.put(s, true);
        } else {
            SchemaField f = fp.getReq().getSchema().getField(args.get(s));

            ValueSource vs = f.getType().getValueSource(f, fp);

            Object objVal = null;

            try {
                objVal = vs.getValues(this.context, this.readerContext).longVal(doc);
                params.put(s, objVal);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                // TODO Log properly

                try {
                    objVal = vs.getValues(this.context, this.readerContext).floatVal(doc);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();

                    try {
                        objVal = vs.getValues(this.context, this.readerContext).strVal(doc);
                    } catch (IOException e2) {
                        // TODO Auto-generated catch block
                        e2.printStackTrace();
                    }
                }

                e.printStackTrace();
            }

            if (objVal != null) {
                params.put(s, objVal);
            } else {
                params.put(s, "N/A");
            }

        }

    }

    CallableFunction cf = functions.get(this.functionName);

    if (cf == null) {
        logger.error("unable to get function " + this.functionName);
    }

    if (cf != null) {
        List<Object> fnParams = new ArrayList<Object>();
        Parameter[] methodParameters = cf.getMethod().getParameters();

        //TODO spostare quanto pi codice possibile in fase di inizializzazione
        for (Parameter p : methodParameters) {
            if (p.isAnnotationPresent(Param.class)) {
                Param paramAnnotation = p.getAnnotation(Param.class);
                fnParams.add(params.get(paramAnnotation.name()));
            }
        }

        try {
            return (float) cf.getMethod().invoke(cf.getInstance(), fnParams.toArray());
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            // TODO Auto-generated catch block
            logger.error("errore mentre chiamavo " + cf.getMethod().getName(), e);
            for (Object o : fnParams) {
                logger.error("p " + o.toString());
            }
        }
    }

    return -1f;

}

From source file:com.alipay.tiansuan.solrplugin.SelectInLocalFileQParserPlugin.java

License:Apache License

@Override
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
    return new QParser(qstr, localParams, params, req) {
        @Override//from  w  ww .  ja va  2 s.  co  m
        public Query parse() throws ParseException {
            try {
                String file = localParams.get(QueryParsing.V);
                String field = localParams.get(QueryParsing.F);

                SchemaField sf = req.getSchema().getField(field);
                FieldType ft = sf.getType();
                if (ft instanceof StrField) {
                    return new StringSelectInQuery<String>(file, field, new HdfsToSet.TransString());
                }
                if (ft instanceof TrieField) {
                    TrieField tft = (TrieField) ft;
                    TrieTypes cdt = tft.getType();
                    if (cdt.equals(TrieTypes.INTEGER)) {
                        return new NumericSelectInQuery<Integer>(field, tft.getPrecisionStep(), DataType.INT,
                                new HdfsToSet.TransInt(), file);
                    }
                    if (cdt.equals(TrieTypes.LONG)) {
                        return new NumericSelectInQuery<Long>(field, tft.getPrecisionStep(), DataType.LONG,
                                new HdfsToSet.TransLong(), file);
                    }

                    if (cdt.equals(TrieTypes.FLOAT)) {
                        return new NumericSelectInQuery<Float>(field, tft.getPrecisionStep(), DataType.FLOAT,
                                new HdfsToSet.TransFloat(), file);
                    }

                    if (cdt.equals(TrieTypes.DOUBLE)) {
                        return new NumericSelectInQuery<Double>(field, tft.getPrecisionStep(), DataType.DOUBLE,
                                new HdfsToSet.TransDouble(), file);
                    }

                    if (cdt.equals(TrieTypes.DATE)) {
                        return new NumericSelectInQuery<Long>(field, tft.getPrecisionStep(), DataType.LONG,
                                new HdfsToSet.TransDate(), file);
                    }
                }

                throw new ParseException("file type error");

            } catch (Exception e) {
                throw new ParseException(e.toString());
            }
        }
    };
}

From source file:com.gogobot.DistanceParser.java

License:Apache License

private MultiValueSource parseSfield(FunctionQParser fp) throws SyntaxError {
    String sfield = fp.getParam(SpatialParams.FIELD);
    if (sfield == null)
        return null;
    SchemaField sf = fp.getReq().getSchema().getField(sfield);
    FieldType type = sf.getType();
    if (type instanceof AbstractSpatialFieldType) {
        AbstractSpatialFieldType asft = (AbstractSpatialFieldType) type;
        return new SpatialStrategyMultiValueSource(asft.getStrategy(sfield));
    }//from   w  w  w. j ava  2 s  .c o m
    ValueSource vs = type.getValueSource(sf, fp);
    if (vs instanceof MultiValueSource) {
        return (MultiValueSource) vs;
    }
    throw new SyntaxError(
            "Spatial field must implement MultiValueSource or extend AbstractSpatialFieldType:" + sf);
}

From source file:com.gu.solr.MergeUtils.java

License:Apache License

private static void addFields(Document luceneDoc, SolrDocument solrDoc, IndexSchema schema) {
    for (Fieldable f : (List<Fieldable>) luceneDoc.getFields()) {
        SchemaField sf = schema.getField(f.name());
        if (!schema.isCopyFieldTarget(sf)) {
            Object externalVal = sf.getType().toObject(f);
            solrDoc.addField(f.name(), externalVal);
        }/*from   w ww .ja v a  2  s. c  om*/
    }
}

From source file:com.indoqa.solr.spatial.corridor.query.route.RouteQueryParser.java

License:Apache License

private ValueSource parseLocationValueSource() {
    SchemaField locationField = this.req.getSchema().getField(this.getParam("field"));
    ValueSource locationValueSource = locationField.getType().getValueSource(locationField, this);
    return locationValueSource;
}

From source file:com.mwired.grid.commons.commons.solr.custom.NewsDateInfluenceCustomeScoreQParsePlugin.java

@Override
public QParser createParser(String query, SolrParams sp, SolrParams sp1, SolrQueryRequest sqr) {
    return new QParser(query, sp, sp1, sqr) {
        @Override//from   w w  w .  j  av  a  2 s. c  o m
        public Query parse() throws SyntaxError {
            QParser parser = getParser(this.qstr, "lucene", this.req);
            Query inner = parser.parse();
            SchemaField createDate = getReq().getSchema().getField(PostPropAndColMap.CREATE_DATE);
            SchemaField influence = getReq().getSchema().getField(PostPropAndColMap.INFLUENCE_SCORE);
            ValueSource influence_source = influence.getType().getValueSource(influence, parser);
            ValueSource createDate_source = createDate.getType().getValueSource(createDate, parser);

            return new NewsDateInfluenceCustomQuery(inner, new FunctionQuery(influence_source),
                    new FunctionQuery(createDate_source));
        }
    };
}

From source file:com.mysoft.b2b.solr.B258DynamicSourceParser.java

License:Open Source License

public ValueSource getValueSource(FunctionQParser fp, String field) {
    if (field == null)
        return null;
    SchemaField f = fp.getReq().getSchema().getField(field);
    if (f.getType().getClass() == IntField.class) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "Can't use ms() function on non-numeric legacy date field " + field);
    }//from w w  w.ja va 2  s  .c o  m
    return f.getType().getValueSource(f, fp);
}