Skip to content

Commit

Permalink
Support retrieving doc values of unsigned long field (opensearch-proj…
Browse files Browse the repository at this point in the history
…ect#16543)

* Support retrieving doc values of unsigned long field

Signed-off-by: panguixin <panguixin@bytedance.com>

* add test

Signed-off-by: panguixin <panguixin@bytedance.com>

* changelog

Signed-off-by: panguixin <panguixin@bytedance.com>

* randomize test

Signed-off-by: panguixin <panguixin@bytedance.com>

---------

Signed-off-by: panguixin <panguixin@bytedance.com>
  • Loading branch information
bugmakerrrrrr authored Nov 1, 2024
1 parent 0363aa7 commit a2a01f8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Revert changes to upload remote state manifest using minimum codec version([#16403](https://github.com/opensearch-project/OpenSearch/pull/16403))
- Ensure index templates are not applied to system indices ([#16418](https://github.com/opensearch-project/OpenSearch/pull/16418))
- Remove resource usages object from search response headers ([#16532](https://github.com/opensearch-project/OpenSearch/pull/16532))
- Support retrieving doc values of unsigned long field ([#16543](https://github.com/opensearch-project/OpenSearch/pull/16543))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.lucene.sandbox.document.HalfFloatPoint;
import org.apache.lucene.util.Accountable;
import org.apache.lucene.util.NumericUtils;
import org.opensearch.common.Numbers;
import org.opensearch.common.time.DateUtils;
import org.opensearch.core.indices.breaker.CircuitBreakerService;
import org.opensearch.index.fielddata.FieldData;
Expand Down Expand Up @@ -573,6 +574,28 @@ public final SortedBinaryDocValues getBytesValues() {
return FieldData.toUnsignedString(getLongValues());
}

@Override
public DocValueFetcher.Leaf getLeafValueFetcher(DocValueFormat format) {
SortedNumericDocValues values = getLongValues();
return new DocValueFetcher.Leaf() {
@Override
public boolean advanceExact(int docId) throws IOException {
return values.advanceExact(docId);
}

@Override
public int docValueCount() {
return values.docValueCount();
}

@Override
public Object nextValue() throws IOException {
final BigInteger value = Numbers.toUnsignedBigInteger(values.nextValue());
return format.format(value);
}
};
}

@Override
public long ramBytesUsed() {
return 0L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType;
import org.opensearch.index.mapper.NumberFieldMapper.NumberType;
import org.opensearch.index.query.QueryShardContext;
import org.opensearch.search.DocValueFormat;
import org.opensearch.search.MultiValueMode;
import org.opensearch.search.query.BitmapDocValuesQuery;
import org.junit.Before;
Expand Down Expand Up @@ -981,4 +982,28 @@ public void testBitmapQuery() throws IOException {
NumberFieldType finalFt = ft;
assertThrows(IllegalArgumentException.class, () -> finalFt.bitmapQuery(bitmap));
}

public void testFetchUnsignedLongDocValues() throws IOException {
Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
Document doc = new Document();
final BigInteger expectedValue = randomUnsignedLong();
doc.add(new SortedNumericDocValuesField("ul", expectedValue.longValue()));
w.addDocument(doc);
try (DirectoryReader reader = DirectoryReader.open(w)) {
final NumberFieldType ft = new NumberFieldType("ul", NumberType.UNSIGNED_LONG);
IndexNumericFieldData fielddata = (IndexNumericFieldData) ft.fielddataBuilder(
"index",
() -> { throw new UnsupportedOperationException(); }
).build(null, null);
assertEquals(IndexNumericFieldData.NumericType.UNSIGNED_LONG, fielddata.getNumericType());
DocValueFetcher.Leaf fetcher = fielddata.load(reader.leaves().get(0)).getLeafValueFetcher(DocValueFormat.UNSIGNED_LONG);
assertTrue(fetcher.advanceExact(0));
assertEquals(1, fetcher.docValueCount());
final Object value = fetcher.nextValue();
assertTrue(value instanceof BigInteger);
assertEquals(expectedValue, value);
}
IOUtils.close(w, dir);
}
}

0 comments on commit a2a01f8

Please sign in to comment.