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

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

Introduction

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

Prototype

public FloatDocValues(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  ww  w  .j  a  v a 2 s .  co  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.ConstDateSource.java

License:Apache License

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    return new FloatDocValues(this) {
        @Override/*  w w  w. j  a v a 2s .  c o m*/
        public float floatVal(int doc) {
            return getFloat();
        }

        @Override
        public int intVal(int doc) {
            return getInt();
        }

        @Override
        public long longVal(int doc) {
            return getLong();
        }

        @Override
        public double doubleVal(int doc) {
            return getDouble();
        }

        @Override
        public String toString(int doc) {
            return description();
        }

        @Override
        public Object objectVal(int doc) {
            return new Date(longVal(doc));
        }

        @SuppressWarnings("deprecation")
        @Override
        public String strVal(int doc) {
            return TrieDateField.formatExternal(new Date(longVal(doc)));
        }

        @Override
        public boolean boolVal(int doc) {
            return getFloat() != 0.0f;
        }

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

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

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

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

License:Apache License

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

    return new SortedSetFieldSource(f.getName(), choice) {
        @Override//from   w w w. jav a2  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 FloatDocValues(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()) {
                        return docID == view.advance(docID);
                    } else {
                        return docID == view.docID();
                    }
                }

                @Override
                public float floatVal(int doc) throws IOException {
                    if (setDoc(doc)) {
                        BytesRef bytes = view.binaryValue();
                        assert bytes.length > 0;
                        return NumericUtils.sortableIntToFloat(LegacyNumericUtils.prefixCodedToInt(bytes));
                    } else {
                        return 0F;
                    }
                }

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

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

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

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

From source file:org.apache.solr.search.FloatPayloadValueSource.java

License:Apache License

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

    Fields fields = readerContext.reader().fields();
    final Terms terms = fields.terms(indexedField);

    FunctionValues defaultValues = defaultValueSource.getValues(context, readerContext);

    // copied the bulk of this from TFValueSource - TODO: this is a very repeated pattern - base-class this advance logic stuff?
    return new FloatDocValues(this) {
        PostingsEnum docs;//from  www.j  av  a 2 s . com
        int atDoc;
        int lastDocRequested = -1;

        {
            reset();
        }

        public void reset() throws IOException {
            // no one should call us for deleted docs?

            if (terms != null) {
                final TermsEnum termsEnum = terms.iterator();
                if (termsEnum.seekExact(indexedBytes)) {
                    docs = termsEnum.postings(null, PostingsEnum.ALL);
                } else {
                    docs = null;
                }
            } else {
                docs = null;
            }

            if (docs == null) {
                // dummy PostingsEnum so floatVal() can work
                // when would this be called?  if field/val did not match?  this is called for every doc?  create once and cache?
                docs = new PostingsEnum() {
                    @Override
                    public int freq() {
                        return 0;
                    }

                    @Override
                    public int nextPosition() throws IOException {
                        return -1;
                    }

                    @Override
                    public int startOffset() throws IOException {
                        return -1;
                    }

                    @Override
                    public int endOffset() throws IOException {
                        return -1;
                    }

                    @Override
                    public BytesRef getPayload() throws IOException {
                        return null;
                    }

                    @Override
                    public int docID() {
                        return DocIdSetIterator.NO_MORE_DOCS;
                    }

                    @Override
                    public int nextDoc() {
                        return DocIdSetIterator.NO_MORE_DOCS;
                    }

                    @Override
                    public int advance(int target) {
                        return DocIdSetIterator.NO_MORE_DOCS;
                    }

                    @Override
                    public long cost() {
                        return 0;
                    }
                };
            }
            atDoc = -1;
        }

        @Override
        public float floatVal(int doc) {
            try {
                if (doc < lastDocRequested) {
                    // out-of-order access.... reset
                    reset();
                }
                lastDocRequested = doc;

                if (atDoc < doc) {
                    atDoc = docs.advance(doc);
                }

                if (atDoc > doc) {
                    // term doesn't match this document... either because we hit the
                    // end, or because the next doc is after this doc.
                    return defaultValues.floatVal(doc);
                }

                // a match!
                int freq = docs.freq();
                int numPayloadsSeen = 0;
                float currentScore = 0;
                for (int i = 0; i < freq; i++) {
                    docs.nextPosition();
                    BytesRef payload = docs.getPayload();
                    if (payload != null) {
                        float payloadVal = decoder.decode(atDoc, docs.startOffset(), docs.endOffset(), payload);

                        // payloadFunction = null represents "first"
                        if (payloadFunction == null)
                            return payloadVal;

                        currentScore = payloadFunction.currentScore(doc, indexedField, docs.startOffset(),
                                docs.endOffset(), numPayloadsSeen, currentScore, payloadVal);
                        numPayloadsSeen++;

                    }
                }

                return (numPayloadsSeen > 0)
                        ? payloadFunction.docScore(doc, indexedField, numPayloadsSeen, currentScore)
                        : defaultValues.floatVal(doc);
            } catch (IOException e) {
                throw new RuntimeException("caught exception in function " + description() + " : doc=" + doc,
                        e);
            }
        }
    };
}

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

License:Apache License

@Override
public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException {
    final FunctionValues str1DV = str1.getValues(context, readerContext);
    final FunctionValues str2DV = str2.getValues(context, readerContext);
    return new FloatDocValues(this) {

        @Override/*from w  w w.  j a va2s  . c  om*/
        public float floatVal(int doc) {
            return dist.getDistance(str1DV.strVal(doc), str2DV.strVal(doc));
        }

        @Override
        public String toString(int doc) {
            StringBuilder sb = new StringBuilder();
            sb.append("strdist").append('(');
            sb.append(str1DV.toString(doc)).append(',').append(str2DV.toString(doc)).append(", dist=")
                    .append(dist.getClass().getName());
            sb.append(')');
            return sb.toString();
        }
    };
}

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

License:Apache License

@Override
public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException {
    final int off = readerContext.docBase;
    IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(readerContext);

    final float[] arr = getCachedFloats(topLevelContext.reader());
    return new FloatDocValues(this) {
        @Override//from   ww  w.  ja  v  a 2s . c om
        public float floatVal(int doc) {
            return arr[doc + off];
        }

        @Override
        public Object objectVal(int doc) {
            return floatVal(doc); // TODO: keep track of missing values
        }
    };
}

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

License:Apache License

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

    return new FloatDocValues(this) {
        @Override// w w w . ja v a2  s . c o m
        public float floatVal(int doc) {
            float boost = 0.0f;

            String value = vals.strVal(doc);
            Float b = boosts.get(value);
            if (b != null) {
                boost = b;
            }
            return boost;
        }
    };
}