Example usage for org.apache.lucene.queries.function.docvalues StrDocValues StrDocValues

List of usage examples for org.apache.lucene.queries.function.docvalues StrDocValues StrDocValues

Introduction

In this page you can find the example usage for org.apache.lucene.queries.function.docvalues StrDocValues StrDocValues.

Prototype

public StrDocValues(ValueSource vs) 

Source Link

Usage

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

License:Apache License

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final Map<String, FunctionValues> valsMap = this.valsMap(sources, context, readerContext);

    final CallableFunction fn = this.function;

    FunctionExecutor host = this;

    if ((this.function.getReturnType() == Boolean.class) || (this.function.getReturnType() == boolean.class)) {
        logger.error("calling boolean function!");
        return new BoolDocValues(this.sources.get(0)) {

            @Override/*from w  w  w.ja  v a  2 s.c o m*/
            public boolean boolVal(int doc) {
                // TODO Auto-generated method stub
                Object[] objParams = host.populateObjParams(valsMap, doc);
                return host.getBooleanResult(fn, objParams);
            }
        };
    } else if (this.function.getReturnType() == Double.class) {
        return new DoubleDocValues(this.sources.get(0)) {

            @Override
            public double doubleVal(int doc) {
                // TODO Auto-generated method stub
                return 12.0d;
            }
        };
    } else if (this.function.getReturnType() == String.class) {
        return new StrDocValues(this.vs) {
            @Override
            public String strVal(int doc) {
                Object[] objParams = host.populateObjParams(valsMap, doc);
                return host.getStringResult(fn, objParams);
            }
        };
    } else if (this.function.getReturnType() == Float.class) {
        return new FloatDocValues(this.sources.get(0)) {

            @Override
            public float floatVal(int doc) {
                // TODO Auto-generated method stub
                Object[] objParams = host.populateObjParams(valsMap, doc);
                return host.getFloatResult(fn, objParams, doc);
            }
        };
    } else if ((this.function.getReturnType() == Integer.class)) {
        // with this snippet of code we could avoid to instantiate a new object of type IntDocValues
        // simply by using the existing instance of WrappedIntDocValues.
        // but.. this cause a crash :(
        // wrappedIntDocValues.setFunction(fn);
        // wrappedIntDocValues.setExpectedParams(this.expectedParams);
        // return wrappedIntDocValues;

        // so for now, just go on with the good old new instance of IntDocValues

        return new IntDocValues(this.sources.get(0)) {

            @Override
            public int intVal(int doc) {
                // TODO Auto-generated method stub

                Object[] objParams = host.populateObjParams(valsMap, doc);
                return host.getIntegerResult(fn, objParams, doc);

            }

        };
    }

    //apparently we dind't find an appropriate DocValues for this function.
    //it could be a function which should generate two or more additional fields in the resulting docs
    //but HOW can we do that??? 
    //for now, just return a null, so no field will be added.
    host.ping2caller();

    logger.error("I don't know how to deal with class " + this.function.getReturnType()
            + ", check FunctionExecutor.java");

    /*
     * instanziare e restituire x forza un DocValues altrimenti non valorizza i parametri!!
     * poi in doctransformer togliere questo campo (capire come!)
     */
    IntDocValues dd = new IntDocValues(this.vs) {

        @Override
        public int intVal(int doc) {
            // TODO Auto-generated method stub

            Object[] objParams = host.populateObjParams(valsMap, doc);
            try {
                host.function.getMethod().invoke(fn.getInstance(), objParams);
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                // TODO Auto-generated catch block
                logger.error("argggg", e);
            }
            return 0; //we're not going to use this value

        }

    };
    return dd; // new StrFunctionValue(null);

}

From source file:org.apache.solr.analytics.util.valuesource.MultiStringFunction.java

License:Apache License

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues[] valsArr = new FunctionValues[sources.length];
    for (int i = 0; i < sources.length; i++) {
        valsArr[i] = sources[i].getValues(context, readerContext);
    }//from   w w w  .  ja va 2 s . c  o  m

    return new StrDocValues(this) {
        @Override
        public String strVal(int doc) {
            CharSequence cs = func(doc, valsArr);
            return cs != null ? cs.toString() : null;
        }

        @Override
        public boolean exists(int doc) {
            boolean exists = true;
            for (FunctionValues val : valsArr) {
                exists = exists & val.exists(doc);
            }
            return exists;
        }

        @Override
        public boolean bytesVal(int doc, BytesRefBuilder bytes) {
            bytes.clear();
            CharSequence cs = func(doc, valsArr);
            if (cs != null) {
                bytes.copyChars(func(doc, valsArr));
                return true;
            } else {
                return false;
            }
        }

        @Override
        public String toString(int doc) {
            StringBuilder sb = new StringBuilder();
            sb.append(name()).append('(');
            boolean firstTime = true;
            for (FunctionValues vals : valsArr) {
                if (firstTime) {
                    firstTime = false;
                } else {
                    sb.append(',');
                }
                sb.append(vals.toString(doc));
            }
            sb.append(')');
            return sb.toString();
        }

        @Override
        public ValueFiller getValueFiller() {
            return new ValueFiller() {
                private final MutableValueStr mval = new MutableValueStr();

                @Override
                public MutableValue getValue() {
                    return mval;
                }

                @Override
                public void fillValue(int doc) {
                    mval.exists = bytesVal(doc, mval.value);
                }
            };
        }
    };
}

From source file:org.apache.solr.analytics.util.valuesource.SingleStringFunction.java

License:Apache License

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues vals = source.getValues(context, readerContext);
    return new StrDocValues(this) {
        @Override/*from   w ww. ja  v a  2s.  c  o  m*/
        public String strVal(int doc) {
            CharSequence cs = func(doc, vals);
            return cs != null ? cs.toString() : null;
        }

        @Override
        public boolean bytesVal(int doc, BytesRefBuilder bytes) {
            CharSequence cs = func(doc, vals);
            if (cs != null) {
                bytes.copyChars(func(doc, vals));
                return true;
            } else {
                bytes.clear();
                return false;
            }
        }

        @Override
        public Object objectVal(int doc) {
            return strVal(doc);
        }

        @Override
        public boolean exists(int doc) {
            return vals.exists(doc);
        }

        @Override
        public String toString(int doc) {
            return name() + '(' + strVal(doc) + ')';
        }

        @Override
        public ValueFiller getValueFiller() {
            return new ValueFiller() {
                private final MutableValueStr mval = new MutableValueStr();

                @Override
                public MutableValue getValue() {
                    return mval;
                }

                @Override
                public void fillValue(int doc) {
                    mval.exists = bytesVal(doc, mval.value);
                }
            };
        }
    };
}

From source file:org.apache.solr.search.function.MultiStringFunction.java

License:Apache License

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues[] valsArr = new FunctionValues[sources.length];
    for (int i = 0; i < sources.length; i++) {
        valsArr[i] = sources[i].getValues(context, readerContext);
    }//from  ww  w.  java  2  s  .  c  om

    return new StrDocValues(this) {
        @Override
        public String strVal(int doc) throws IOException {
            CharSequence cs = func(doc, valsArr);
            return cs != null ? cs.toString() : null;
        }

        @Override
        public boolean exists(int doc) throws IOException {
            boolean exists = true;
            for (FunctionValues val : valsArr) {
                exists = exists & val.exists(doc);
            }
            return exists;
        }

        @Override
        public boolean bytesVal(int doc, BytesRefBuilder bytes) throws IOException {
            bytes.clear();
            CharSequence cs = func(doc, valsArr);
            if (cs != null) {
                bytes.copyChars(func(doc, valsArr));
                return true;
            } else {
                return false;
            }
        }

        @Override
        public String toString(int doc) throws IOException {
            StringBuilder sb = new StringBuilder();
            sb.append(name()).append('(');
            boolean firstTime = true;
            for (FunctionValues vals : valsArr) {
                if (firstTime) {
                    firstTime = false;
                } else {
                    sb.append(',');
                }
                sb.append(vals.toString(doc));
            }
            sb.append(')');
            return sb.toString();
        }

        @Override
        public ValueFiller getValueFiller() {
            return new ValueFiller() {
                private final MutableValueStr mval = new MutableValueStr();

                @Override
                public MutableValue getValue() {
                    return mval;
                }

                @Override
                public void fillValue(int doc) throws IOException {
                    mval.exists = bytesVal(doc, mval.value);
                }
            };
        }
    };
}