RecordLayer: index only query

I would like a way to retrieve only the keys for a given query, but it seems it is currently unsupported.

Is there a way to query only a secondary index with a given eg: filter? This would allow me to create secondary indexes for the keys I want to retrieve; the alternative would be to create an additional record type just to hold the keys that I’m interested in.

What do you mean by “keys” in this context?

If you mean the indexed keys (that is, you want the data in the index but you don’t want the full record), there is a .setRequiredResults method on the RecordQuery builder. So something like:

RecordQuery.newBuilder()
    .setRecordType("MyRecordType")
    .setFilter(Query.field("myIndexedField").equalsValue("foo"))
    .setRequiredResults(List.of(Key.Expressions.field("myOtherIndexedField")))
    .build()

Is equivalent to a SQL statement like:

SELECT myOtherIndexedField FROM MyRecordType WHERE myIndexedField = 'foo'

And if you have an index like Key.Expressions.concatenateFields("myIndexedField", "myOtherIndexedField") or Key.Expressions.keyWithValue(Key.Expressions.field("myIndexedField"), Key.Expressions.field("myOtherIndexedField")), this will get executed by scanning the index, and then returning “partial records”, that is, records of type MyRecordType but only the fields that have data in the index will be populated. That allows the record lookup to be skipped, but you still get a protobuf message back that has the fields you need. (Note: all other fields will be unset, and there isn’t anything in the API to stop you from trying to read a field on the record that wasn’t in the index, so you have to be a little careful there when using the results.)

1 Like

Hah, I was unaware of this! Thanks alot, this is exactly what I was looking for.
I would say that this belongs in the FDB intro docs; I’ll cook a PR when I have some time.