Example usage for org.apache.lucene.queries.function FunctionValues floatVal

List of usage examples for org.apache.lucene.queries.function FunctionValues floatVal

Introduction

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

Prototype

public float floatVal(int doc) throws IOException 

Source Link

Usage

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

License:Apache License

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues vals = source.getValues(context, readerContext);
    return new FunctionValues() {

        @Override/*from  w  w  w.j a va 2s. com*/
        public byte byteVal(int doc) {
            return vals.byteVal(doc);
        }

        @Override
        public short shortVal(int doc) {
            return vals.shortVal(doc);
        }

        @Override
        public float floatVal(int doc) {
            return vals.floatVal(doc);
        }

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

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

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

        @Override
        public String strVal(int doc) {
            return vals.strVal(doc);
        }

        @Override
        public Object objectVal(int doc) {
            return exists(doc) ? vals.objectVal(doc) : null;
        }

        @Override
        public boolean exists(int doc) {
            Object other = vals.objectVal(doc);
            return other != null && !missValue.equals(other);
        }

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

        @Override
        public ValueFiller getValueFiller() {
            return new ValueFiller() {
                private final ValueFiller delegateFiller = vals.getValueFiller();
                private final MutableValue mval = delegateFiller.getValue();

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

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

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

License:Apache License

public void testDocValues() throws IOException {
    assertU(adoc("id", "1"));
    commit();/*from   www  .  ja va  2s.  c  om*/
    SolrCore core = h.getCoreInc();
    try {
        final RefCounted<SolrIndexSearcher> searcherRef = core.openNewSearcher(true, true);
        final SolrIndexSearcher searcher = searcherRef.get();
        try {
            final AtomicReader reader = searcher.getAtomicReader();
            assertEquals(1, reader.numDocs());
            final FieldInfos infos = reader.getFieldInfos();
            assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("floatdv").getDocValuesType());
            assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("intdv").getDocValuesType());
            assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("doubledv").getDocValuesType());
            assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("longdv").getDocValuesType());
            assertEquals(DocValuesType.SORTED, infos.fieldInfo("stringdv").getDocValuesType());

            assertEquals((long) Float.floatToIntBits(1), reader.getNumericDocValues("floatdv").get(0));
            assertEquals(2L, reader.getNumericDocValues("intdv").get(0));
            assertEquals(Double.doubleToLongBits(3), reader.getNumericDocValues("doubledv").get(0));
            assertEquals(4L, reader.getNumericDocValues("longdv").get(0));

            final IndexSchema schema = core.getLatestSchema();
            final SchemaField floatDv = schema.getField("floatdv");
            final SchemaField intDv = schema.getField("intdv");
            final SchemaField doubleDv = schema.getField("doubledv");
            final SchemaField longDv = schema.getField("longdv");

            FunctionValues values = floatDv.getType().getValueSource(floatDv, null).getValues(null,
                    searcher.getAtomicReader().leaves().get(0));
            assertEquals(1f, values.floatVal(0), 0f);
            assertEquals(1f, values.objectVal(0));
            values = intDv.getType().getValueSource(intDv, null).getValues(null,
                    searcher.getAtomicReader().leaves().get(0));
            assertEquals(2, values.intVal(0));
            assertEquals(2, values.objectVal(0));
            values = doubleDv.getType().getValueSource(doubleDv, null).getValues(null,
                    searcher.getAtomicReader().leaves().get(0));
            assertEquals(3d, values.doubleVal(0), 0d);
            assertEquals(3d, values.objectVal(0));
            values = longDv.getType().getValueSource(longDv, null).getValues(null,
                    searcher.getAtomicReader().leaves().get(0));
            assertEquals(4L, values.longVal(0));
            assertEquals(4L, values.objectVal(0));
        } finally {
            searcherRef.decref();
        }
    } finally {
        core.close();
    }
}

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 w  ww . ja va  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.NvlValueSourceParser.java

License:Apache License

@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
    ValueSource source = fp.parseValueSource();
    final float nvl = fp.parseFloat();

    return new SimpleFloatFunction(source) {
        @Override//w  ww .  j  av a 2 s  . c  o m
        protected String name() {
            return "nvl";
        }

        @Override
        protected float func(int doc, FunctionValues vals) {
            float v = vals.floatVal(doc);
            if (v == nvlFloatValue) {
                return nvl;
            } else {
                return v;
            }
        }
    };
}

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

License:Apache License

@Test
public void testCachedBoosts() throws Exception {
    when(boostCache.get(boostId)).thenReturn(createBoosts());
    ValueSource vs = vsp.parse(fp);/*from w  ww . j  a va  2  s .  com*/

    verifyZeroInteractions(httpClient);

    FunctionValues values = vs.getValues(null, null);
    Assert.assertEquals(0.7f, values.floatVal(0), 0.0f);
    Assert.assertEquals(0.6f, values.floatVal(1), 0.0f);
    Assert.assertEquals(0.5f, values.floatVal(2), 0.0f);
    for (int i = 3; i <= 10; i++) {
        Assert.assertEquals(0.0f, values.floatVal(i), 0.0f);
    }
}

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

License:Apache License

@Test
public void testCachedBoostsWithTreatment() throws Exception {
    when(params.get(TREATMENT_ID)).thenReturn("b");
    when(boostCache.get(boostId + "_b")).thenReturn(createBoosts());
    ValueSource vs = vsp.parse(fp);//from w w  w.  j  a  va  2s. com

    verifyZeroInteractions(httpClient);

    FunctionValues values = vs.getValues(null, null);
    Assert.assertEquals(0.7f, values.floatVal(0), 0.0f);
    Assert.assertEquals(0.6f, values.floatVal(1), 0.0f);
    Assert.assertEquals(0.5f, values.floatVal(2), 0.0f);
    for (int i = 3; i <= 10; i++) {
        Assert.assertEquals(0.0f, values.floatVal(i), 0.0f);
    }
}

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

License:Apache License

@Test
public void testUncachedBoosts() throws Exception {
    mockHttpResponse(HttpStatus.SC_OK);//from  w  w w  .  ja v  a  2s .  co m

    ValueSource vs = vsp.parse(fp);
    FunctionValues values = vs.getValues(null, null);
    Assert.assertEquals(0.7f, values.floatVal(0), 0.0f);
    Assert.assertEquals(0.6f, values.floatVal(1), 0.0f);
    Assert.assertEquals(0.5f, values.floatVal(2), 0.0f);
    for (int i = 3; i <= 10; i++) {
        Assert.assertEquals(0.0f, values.floatVal(i), 0.0f);
    }
}

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

License:Apache License

@Test
public void testNoBoosts() throws Exception {
    mockHttpResponse(HttpStatus.SC_NOT_FOUND);
    ValueSource vs = vsp.parse(fp);//from w w w.  ja va 2  s. c o  m
    FunctionValues values = vs.getValues(null, null);
    for (int i = 0; i <= 10; i++) {
        Assert.assertEquals(0.0f, values.floatVal(i), 0.0f);
    }
}

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

License:Apache License

@Test
public void testApiError() throws Exception {
    mockHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    ValueSource vs = vsp.parse(fp);//from  ww w.ja v a2s .c o  m
    FunctionValues values = vs.getValues(null, null);
    for (int i = 0; i <= 10; i++) {
        Assert.assertEquals(0.0f, values.floatVal(i), 0.0f);
    }
}