luaudfaerospike

Aerospike - Query with Multiple Filter parameters


I'm trying to query aerospike using multiple filters taking reference from this link.

I am able to query aerospike based on the given lua script for 1 filter parameter but stuck up with lua script when have to pass more than 2 filter parameters (for example passing two more parameters like age, gender with password).
This is my first time with lua.

Lua Script:

local function map_profile(record)
 return map {name=record.name, password=record.password}
end
function check_password(stream,password)
 local function filter_password(record)
   return record.password == password
 end
 return stream : filter(filter_password) : map(map_profile)
end

Thanks in advance.


Solution

  • The filter function can have extra parameters and return a closure which can both access those, while still conforming to the expected stub of one parameter being the record, with a boolean return value.

    local function filter_password(password)
      return function(rec)
        if rec['password'] and (type(rec['password']) == 'string') and
           rec['password'] == password then
          return true
        end
        return false
      end
    end
    
    local function map_profile(record)
      return map {name=record.name, password=record.password}
    end
    
    function check_password(stream,password)
      return stream : filter(filter_password(password)) : map(map_profile)
    end
    

    However, the best way to query or scan for multiple filters these days (ever since release 3.12) is to use predicate filtering. In the majority of cases (unless you need to compare the values of two of the record's bins to each other in some way) you would skip UDFs and use the PredExp class (in the Java client, or its equivalent in another). You'd get back only those records that matched the filter, regardless of how complex of an expression you built. See the examples in the Aerospike Java client, or the C, C# and Go clients.