# Secondary Index on Custom Java Object

**URL:** https://discuss.aerospike.com/t/secondary-index-on-custom-java-object/6485
**Category:** Query & Indexing
**Tags:** index
**Created:** [July 16, 2019, 4:55pm UTC](https://discuss.aerospike.com/t/secondary-index-on-custom-java-object/6485 "2019-07-16T16:55:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sd.hrishi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/sd.hrishi/32/1230_2.png) [@sd.hrishi](https://discuss.aerospike.com/u/sd.hrishi)
#### Post date: [July 16, 2019, 4:55pm UTC](https://discuss.aerospike.com/t/secondary-index-on-custom-java-object/6485/1 "2019-07-16T16:55:24Z")

</div>

I have two classes.

```
class A {

    String aName;
    B b;

    public A(String aName, B b) {
        this.aName = aName;
        this.b = b;
    }

    public String getaName() {
        return aName;
    }

    public B getB() {
        return b;
    }
}

class B {
    String bName;

    public B(String bName) {
        this.bName = bName;
    }

    public String getbName() {
        return bName;
    }
}

```

I am storing A as a set in Aerospike and **A.aName** is primary key. I want a secondary key on **A.b**. I have created index on **A.b** attribute and able to persist also. But search from the index is not returning anything. As per my understanding, Aerospike supports only three type of indexes: String, Numeric and Geo,. Is there any option for custom object.

Cross posted to Stack Overflow: [Secondary Index on Custom Java Object in Aerospike - Stack Overflow](https://stackoverflow.com/questions/57062877/secondary-index-on-custom-java-object-in-aerospike).

---

<div class="post-metadata">

### Author: ![Albot](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/albot/32/2076_2.png) [@Albot](https://discuss.aerospike.com/u/Albot)
#### Post date: [July 17, 2019, 5:57pm UTC](https://discuss.aerospike.com/t/secondary-index-on-custom-java-object/6485/2 "2019-07-17T17:57:13Z")

</div>

Are you storing it as a binary blob? What type is A.b ?

---

<div class="post-metadata">

### Author: ![sd.hrishi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/sd.hrishi/32/1230_2.png) [@sd.hrishi](https://discuss.aerospike.com/u/sd.hrishi)
#### Post date: [July 18, 2019, 6:40am UTC](https://discuss.aerospike.com/t/secondary-index-on-custom-java-object/6485/3 "2019-07-18T06:40:20Z")

</div>

Yes, I am storing as object through Java client, which would be Blob i think.

---

<div class="post-metadata">

### Author: ![Albot](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/albot/32/2076_2.png) [@Albot](https://discuss.aerospike.com/u/Albot)
#### Post date: [July 19, 2019, 5:05am UTC](https://discuss.aerospike.com/t/secondary-index-on-custom-java-object/6485/4 "2019-07-19T05:05:29Z")

</div>

Binary blobs can be anything, there’s definitely no way to index that. If there is a particular field in your class you can pull out and set as a separate bin with a string/long type then that would work…

---

<div class="post-metadata">

### Author: ![sd.hrishi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/sd.hrishi/32/1230_2.png) [@sd.hrishi](https://discuss.aerospike.com/u/sd.hrishi)
#### Post date: [July 19, 2019, 6:34am UTC](https://discuss.aerospike.com/t/secondary-index-on-custom-java-object/6485/5 "2019-07-19T06:34:41Z")

</div>

Thanks @Albot, we did the same thing and it works also but wanted to know some other alternative.

---

<div class="post-metadata">

### Author: ![kporter](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/kporter/32/515_2.png) [@kporter](https://discuss.aerospike.com/u/kporter)
#### Post date: [August 20, 2019, 12:01am UTC](https://discuss.aerospike.com/t/secondary-index-on-custom-java-object/6485/6 "2019-08-20T00:01:18Z")

</div>

This was also posted to Stack Overflow where @rbotzer provides a few ideas for modelling.

> <https://stackoverflow.com/questions/57062877/secondary-index-on-custom-java-object-in-aerospike/57318560#57318560>
