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

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

Introduction

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

Prototype

public DoubleDocValues(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 www .  j  a  va 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.ConstDoubleSource.java

License:Apache License

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    return new DoubleDocValues(this) {
        @Override/* w ww  . j  a v  a2  s  .co  m*/
        public double doubleVal(int doc) {
            return constant;
        }

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

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

License:Apache License

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues aVals = a.getValues(context, readerContext);
    final FunctionValues bVals = b.getValues(context, readerContext);
    return new DoubleDocValues(this) {
        @Override/*from   w ww.j a v  a  2 s .  co m*/
        public double doubleVal(int doc) {
            return func(doc, aVals, bVals);
        }

        @Override
        public boolean exists(int doc) {
            return aVals.exists(doc) & bVals.exists(doc);
        }

        @Override
        public String toString(int doc) {
            return name() + '(' + aVals.toString(doc) + ',' + bVals.toString(doc) + ')';
        }
    };
}

From source file:org.apache.solr.analytics.util.valuesource.MultiDoubleFunction.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 ww .ja v a  2s  .  c  o  m

    return new DoubleDocValues(this) {
        @Override
        public double doubleVal(int doc) {
            return func(doc, valsArr);
        }

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

        @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();
        }
    };
}

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

License:Apache License

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues vals = source.getValues(context, readerContext);
    return new DoubleDocValues(this) {
        @Override/*w w  w  .j  a  va2s .c om*/
        public double doubleVal(int doc) {
            return func(doc, vals);
        }

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

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

From source file:org.apache.solr.schema.TrieDoubleField.java

License:Apache License

@Override
protected ValueSource getSingleValueSource(SortedSetSelector.Type choice, SchemaField f) {

    return new SortedSetFieldSource(f.getName(), choice) {
        @Override/*  w  w  w  .ja va  2  s  .  co  m*/
        public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
            SortedSetFieldSource thisAsSortedSetFieldSource = this; // needed for nested anon class ref

            SortedSetDocValues sortedSet = DocValues.getSortedSet(readerContext.reader(), field);
            SortedDocValues view = SortedSetSelector.wrap(sortedSet, selector);

            return new DoubleDocValues(thisAsSortedSetFieldSource) {
                private int lastDocID;

                private boolean setDoc(int docID) throws IOException {
                    if (docID < lastDocID) {
                        throw new IllegalArgumentException(
                                "docs out of order: lastDocID=" + lastDocID + " docID=" + docID);
                    }
                    if (docID > view.docID()) {
                        lastDocID = docID;
                        return docID == view.advance(docID);
                    } else {
                        return docID == view.docID();
                    }
                }

                @Override
                public double doubleVal(int doc) throws IOException {
                    if (setDoc(doc)) {
                        BytesRef bytes = view.binaryValue();
                        assert bytes.length > 0;
                        return NumericUtils.sortableLongToDouble(LegacyNumericUtils.prefixCodedToLong(bytes));
                    } else {
                        return 0D;
                    }
                }

                @Override
                public boolean exists(int doc) throws IOException {
                    return setDoc(doc);
                }

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

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

                        @Override
                        public void fillValue(int doc) throws IOException {
                            if (setDoc(doc)) {
                                mval.exists = true;
                                mval.value = NumericUtils.sortableLongToDouble(
                                        LegacyNumericUtils.prefixCodedToLong(view.binaryValue()));
                            } else {
                                mval.exists = false;
                                mval.value = 0D;
                            }
                        }
                    };
                }
            };
        }
    };
}

From source file:org.apache.solr.search.function.distance.GeohashHaversineFunction.java

License:Apache License

@Override
public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException {
    final FunctionValues gh1DV = geoHash1.getValues(context, readerContext);
    final FunctionValues gh2DV = geoHash2.getValues(context, readerContext);

    return new DoubleDocValues(this) {
        @Override/*from  w  w  w.j av  a  2  s  .c  o  m*/
        public double doubleVal(int doc) {
            return distance(doc, gh1DV, gh2DV);
        }

        @Override
        public String toString(int doc) {
            StringBuilder sb = new StringBuilder();
            sb.append(name()).append('(');
            sb.append(gh1DV.toString(doc)).append(',').append(gh2DV.toString(doc));
            sb.append(')');
            return sb.toString();
        }
    };
}

From source file:org.apache.solr.search.function.distance.HaversineConstFunction.java

License:Apache License

@Override
public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException {
    final FunctionValues latVals = latSource.getValues(context, readerContext);
    final FunctionValues lonVals = lonSource.getValues(context, readerContext);
    final double latCenterRad = this.latCenter * DEGREES_TO_RADIANS;
    final double lonCenterRad = this.lonCenter * DEGREES_TO_RADIANS;
    final double latCenterRad_cos = this.latCenterRad_cos;

    return new DoubleDocValues(this) {
        @Override//from www.j a  v  a 2 s.  c  o m
        public double doubleVal(int doc) {
            double latRad = latVals.doubleVal(doc) * DEGREES_TO_RADIANS;
            double lonRad = lonVals.doubleVal(doc) * DEGREES_TO_RADIANS;
            double diffX = latCenterRad - latRad;
            double diffY = lonCenterRad - lonRad;
            double hsinX = Math.sin(diffX * 0.5);
            double hsinY = Math.sin(diffY * 0.5);
            double h = hsinX * hsinX + (latCenterRad_cos * Math.cos(latRad) * hsinY * hsinY);
            return (EARTH_MEAN_DIAMETER * Math.atan2(Math.sqrt(h), Math.sqrt(1 - h)));
        }

        @Override
        public String toString(int doc) {
            return name() + '(' + latVals.toString(doc) + ',' + lonVals.toString(doc) + ',' + latCenter + ','
                    + lonCenter + ')';
        }
    };
}

From source file:org.apache.solr.search.function.distance.HaversineFunction.java

License:Apache License

@Override
public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException {
    final FunctionValues vals1 = p1.getValues(context, readerContext);

    final FunctionValues vals2 = p2.getValues(context, readerContext);
    return new DoubleDocValues(this) {
        @Override/*w  w  w  . j  a v a2 s  .c  om*/
        public double doubleVal(int doc) {
            return distance(doc, vals1, vals2);
        }

        @Override
        public String toString(int doc) {
            StringBuilder sb = new StringBuilder();
            sb.append(name()).append('(');
            sb.append(vals1.toString(doc)).append(',').append(vals2.toString(doc));
            sb.append(')');
            return sb.toString();
        }
    };
}

From source file:org.apache.solr.search.function.distance.VectorDistanceFunction.java

License:Apache License

@Override
public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException {

    final FunctionValues vals1 = source1.getValues(context, readerContext);

    final FunctionValues vals2 = source2.getValues(context, readerContext);

    return new DoubleDocValues(this) {

        @Override/*from  www .ja v  a 2  s .  c o m*/
        public double doubleVal(int doc) {
            return distance(doc, vals1, vals2);
        }

        @Override
        public String toString(int doc) {
            StringBuilder sb = new StringBuilder();
            sb.append(name()).append('(').append(power).append(',');
            boolean firstTime = true;
            sb.append(vals1.toString(doc)).append(',');
            sb.append(vals2.toString(doc));
            sb.append(')');
            return sb.toString();
        }
    };
}