|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.script; |
| 11 | + |
| 12 | +import org.apache.lucene.index.BinaryDocValues; |
| 13 | +import org.apache.lucene.index.DocValues; |
| 14 | +import org.apache.lucene.index.LeafReaderContext; |
| 15 | +import org.apache.lucene.util.BytesRef; |
| 16 | +import org.elasticsearch.index.mapper.OnScriptError; |
| 17 | +import org.elasticsearch.search.lookup.SearchLookup; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +public class BinaryDocValuesStringFieldScript extends StringFieldScript { |
| 23 | + private final BinaryDocValues binaryDocValues; |
| 24 | + |
| 25 | + boolean hasValue = false; |
| 26 | + |
| 27 | + public BinaryDocValuesStringFieldScript(String fieldName, SearchLookup searchLookup, LeafReaderContext ctx) { |
| 28 | + super(fieldName, Map.of(), searchLookup, OnScriptError.FAIL, ctx); |
| 29 | + try { |
| 30 | + binaryDocValues = DocValues.getBinary(ctx.reader(), fieldName); |
| 31 | + } catch (IOException e) { |
| 32 | + throw new IllegalStateException("Cannot load doc values", e); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void setDocument(int docID) { |
| 38 | + try { |
| 39 | + hasValue = binaryDocValues.advanceExact(docID); |
| 40 | + } catch (IOException e) { |
| 41 | + throw new IllegalStateException("Cannot load doc values", e); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void execute() { |
| 47 | + try { |
| 48 | + if (hasValue) { |
| 49 | + BytesRef bytesRef = binaryDocValues.binaryValue(); |
| 50 | + emit(bytesRef.utf8ToString()); |
| 51 | + } |
| 52 | + } catch (IOException e) { |
| 53 | + throw new IllegalStateException("Cannot load doc values", e); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments