Example usage for org.apache.lucene.index BinaryDocValues BinaryDocValues

List of usage examples for org.apache.lucene.index BinaryDocValues BinaryDocValues

Introduction

In this page you can find the example usage for org.apache.lucene.index BinaryDocValues BinaryDocValues.

Prototype

protected BinaryDocValues() 

Source Link

Document

Sole constructor.

Usage

From source file:lucene.security.index.SecureAtomicReader.java

License:Apache License

@Override
public BinaryDocValues getBinaryDocValues(String field) throws IOException {
    final BinaryDocValues binaryDocValues = in.getBinaryDocValues(field);
    if (binaryDocValues == null) {
        return null;
    }/* w w w  .j  av a  2  s  .  co m*/
    return new BinaryDocValues() {

        @Override
        public void get(int docID, BytesRef result) {
            try {
                if (_accessControl.hasAccess(ReadType.BINARY_DOC_VALUE, docID)) {
                    binaryDocValues.get(docID, result);
                    return;
                }
                // Default missing value.
                result.bytes = MISSING;
                result.length = 0;
                result.offset = 0;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };
}

From source file:org.codelibs.elasticsearch.search.MultiValueMode.java

License:Apache License

/**
 * Return a {BinaryDocValues} instance that can be used to sort documents
 * with this mode and the provided values. When a document has no value,
 * <code>missingValue</code> is returned.
 *
 * Allowed Modes: MIN, MAX//from ww w . j a  va2  s  .c  o  m
 */
public BinaryDocValues select(final SortedBinaryDocValues values, final BytesRef missingValue) {
    final BinaryDocValues singleton = FieldData.unwrapSingleton(values);
    if (singleton != null) {
        final Bits docsWithField = FieldData.unwrapSingletonBits(values);
        if (docsWithField == null) {
            return singleton;
        } else {
            return new BinaryDocValues() {
                @Override
                public BytesRef get(int docID) {
                    final BytesRef value = singleton.get(docID);
                    if (value.length == 0 && docsWithField.get(docID) == false) {
                        return missingValue;
                    }
                    return value;
                }
            };
        }
    } else {
        return new BinaryDocValues() {
            @Override
            public BytesRef get(int docID) {
                return pick(values, missingValue, docID);
            }
        };
    }
}

From source file:org.codelibs.elasticsearch.search.MultiValueMode.java

License:Apache License

/**
 * Return a {BinaryDocValues} instance that can be used to sort root documents
 * with this mode, the provided values and filters for root/inner documents.
 *
 * For every root document, the values of its inner documents will be aggregated.
 * If none of the inner documents has a value, then <code>missingValue</code> is returned.
 *
 * Allowed Modes: MIN, MAX/*from w  w  w  .  j  a  v a  2  s.c  om*/
 *
 * NOTE: Calling the returned instance on docs that are not root docs is illegal
 *       The returned instance can only be evaluate the current and upcoming docs
 */
public BinaryDocValues select(final SortedBinaryDocValues values, final BytesRef missingValue,
        final BitSet rootDocs, final DocIdSetIterator innerDocs, int maxDoc) throws IOException {
    if (rootDocs == null || innerDocs == null) {
        return select(FieldData.emptySortedBinary(maxDoc), missingValue);
    }
    final BinaryDocValues selectedValues = select(values, null);

    return new BinaryDocValues() {

        final BytesRefBuilder builder = new BytesRefBuilder();

        int lastSeenRootDoc = 0;
        BytesRef lastEmittedValue = missingValue;

        @Override
        public BytesRef get(int rootDoc) {
            assert rootDocs.get(rootDoc) : "can only sort root documents";
            assert rootDoc >= lastSeenRootDoc : "can only evaluate current and upcoming root docs";
            if (rootDoc == lastSeenRootDoc) {
                return lastEmittedValue;
            }

            try {
                final int prevRootDoc = rootDocs.prevSetBit(rootDoc - 1);
                final int firstNestedDoc;
                if (innerDocs.docID() > prevRootDoc) {
                    firstNestedDoc = innerDocs.docID();
                } else {
                    firstNestedDoc = innerDocs.advance(prevRootDoc + 1);
                }

                lastSeenRootDoc = rootDoc;
                lastEmittedValue = pick(selectedValues, builder, innerDocs, firstNestedDoc, rootDoc);
                if (lastEmittedValue == null) {
                    lastEmittedValue = missingValue;
                }
                return lastEmittedValue;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };
}

From source file:org.elasticsearch.search.MultiValueModeTests.java

License:Apache License

public void testSingleValuedStrings() throws Exception {
    final int numDocs = scaledRandomIntBetween(1, 100);
    final BytesRef[] array = new BytesRef[numDocs];
    final FixedBitSet docsWithValue = randomBoolean() ? null : new FixedBitSet(numDocs);
    for (int i = 0; i < array.length; ++i) {
        if (randomBoolean()) {
            array[i] = new BytesRef(RandomStrings.randomAsciiOfLength(getRandom(), 8));
            if (docsWithValue != null) {
                docsWithValue.set(i);/*from   w  ww. j  av  a 2  s. c o m*/
            }
        } else {
            array[i] = new BytesRef();
            if (docsWithValue != null && randomBoolean()) {
                docsWithValue.set(i);
            }
        }
    }
    final BinaryDocValues singleValues = new BinaryDocValues() {
        @Override
        public BytesRef get(int docID) {
            return BytesRef.deepCopyOf(array[docID]);
        }
    };
    final SortedBinaryDocValues multiValues = FieldData.singleton(singleValues, docsWithValue);
    verify(multiValues, numDocs);
    final FixedBitSet rootDocs = randomRootDocs(numDocs);
    final FixedBitSet innerDocs = randomInnerDocs(rootDocs);
    verify(multiValues, numDocs, rootDocs, innerDocs);
}

From source file:org.elasticsearch.search.sort.ScriptSortBuilder.java

License:Apache License

@Override
public SortFieldAndFormat build(QueryShardContext context) throws IOException {
    final SearchScript searchScript = context.getScriptService().search(context.lookup(), script,
            ScriptContext.Standard.SEARCH, Collections.emptyMap());

    MultiValueMode valueMode = null;//from   www.ja  va2 s.c o m
    if (sortMode != null) {
        valueMode = MultiValueMode.fromString(sortMode.toString());
    }
    boolean reverse = (order == SortOrder.DESC);
    if (valueMode == null) {
        valueMode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN;
    }

    final Nested nested = resolveNested(context, nestedPath, nestedFilter);
    final IndexFieldData.XFieldComparatorSource fieldComparatorSource;
    switch (type) {
    case STRING:
        fieldComparatorSource = new BytesRefFieldComparatorSource(null, null, valueMode, nested) {
            LeafSearchScript leafScript;

            @Override
            protected SortedBinaryDocValues getValues(LeafReaderContext context) throws IOException {
                leafScript = searchScript.getLeafSearchScript(context);
                final BinaryDocValues values = new BinaryDocValues() {
                    final BytesRefBuilder spare = new BytesRefBuilder();

                    @Override
                    public BytesRef get(int docID) {
                        leafScript.setDocument(docID);
                        spare.copyChars(leafScript.run().toString());
                        return spare.get();
                    }
                };
                return FieldData.singleton(values, null);
            }

            @Override
            protected void setScorer(Scorer scorer) {
                leafScript.setScorer(scorer);
            }
        };
        break;
    case NUMBER:
        fieldComparatorSource = new DoubleValuesComparatorSource(null, Double.MAX_VALUE, valueMode, nested) {
            LeafSearchScript leafScript;

            @Override
            protected SortedNumericDoubleValues getValues(LeafReaderContext context) throws IOException {
                leafScript = searchScript.getLeafSearchScript(context);
                final NumericDoubleValues values = new NumericDoubleValues() {
                    @Override
                    public double get(int docID) {
                        leafScript.setDocument(docID);
                        return leafScript.runAsDouble();
                    }
                };
                return FieldData.singleton(values, null);
            }

            @Override
            protected void setScorer(Scorer scorer) {
                leafScript.setScorer(scorer);
            }
        };
        break;
    default:
        throw new QueryShardException(context, "custom script sort type [" + type + "] not supported");
    }

    return new SortFieldAndFormat(new SortField("_script", fieldComparatorSource, reverse), DocValueFormat.RAW);
}