Query with Primary and Secondary Indexes




Lets say I have the following information.

ip_from, ip_to, country_code, country_name, region_code, region, city_code, city_name, isp_name, mobile_carrier.

The following data:, **, **, **, **, **, **, **, **.

I have the following IP:

How can I return the data by searching with

I read this:


So I know its possible with AQL, but how to do with the PHP client?

To clarify, I want to search the primary index ip_from and secondary index ip_to and return the matches.



I assume you mean searching for all records where the given IP is between the ip_from and ip_to. There have been other discussions on how to query an IP range: https://discuss.aerospike.com/search?q=ip%20range

In general you can query on a secondary index in PHP using query(). You can pass the results of a secondary index query to a stream UDF for further processing using aggregate(). The article you mentioned covers using stream UDFs to implement secondary/tertiary/etc filters on top of the query.


Thanks Ronen. I checked out the other replies, but I don’t actually see anyone with a solution or a clear solution that I could understand and implement. A bit frustrating really.

I’m not sure now that a UDF will resolve my problem. Seeing as I want to do a ip > ip_from and ip < ip_to.

Unless I flatten my 18m row ip_from/ip_to database into a single ip column, then predicate between will work fine. But I may end up with 1B+ rows and the performance may then be very slow.

Still not sure the best way to resolve this.


Hi Paul,

Sounds like a coding problem that’s not directly related to Aerospike. Perhaps you could try posting it on StackOverflow so others can jump in and help.


If it was a coding problem, it would already be fixed :smile:

See if you can get someone from aerospike to add more verbose information to Ip ranges/CIDR



Done - please let us know how this ends up working for you.