# RecordLayer: index only query

**URL:** <https://forums.foundationdb.org/t/recordlayer-index-only-query/3425>\
**Category:** Using Layers\
**Created:** [July 5, 2022, 9:29am UTC](https://forums.foundationdb.org/t/recordlayer-index-only-query/3425 "2022-07-05T09:29:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mping-exo](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/mping-exo/32/1374_2.png) [@mping-exo](https://forums.foundationdb.org/u/mping-exo)\
**Post date:** [July 5, 2022, 9:29am UTC](https://forums.foundationdb.org/t/recordlayer-index-only-query/3425/1 "2022-07-05T09:29:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alloc](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/alloc/32/9_2.png) [@alloc](https://forums.foundationdb.org/u/alloc)\
**Post date:** [July 26, 2022, 1:42pm UTC](https://forums.foundationdb.org/t/recordlayer-index-only-query/3425/2 "2022-07-26T13:42:03Z")

</div>

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:

```java
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:

```sql
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.)

---

<div class="post-metadata">

**Author:** ![mping-exo](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/mping-exo/32/1374_2.png) [@mping-exo](https://forums.foundationdb.org/u/mping-exo)\
**Post date:** [July 27, 2022, 1:54pm UTC](https://forums.foundationdb.org/t/recordlayer-index-only-query/3425/3 "2022-07-27T13:54:23Z")

</div>

> [@alloc](#):
>
> `setRequiredResults`

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.
