Home > front end >  Aerospike filter expression on boolean fields not working
Aerospike filter expression on boolean fields not working

Time:05-17

I've written the following Aerospike filter in Java. Both Field1 and Field2 are booleans. For some reason, the "filterByField.ValidBoth" condition does not yield true, although the record matches the criteria.

Since it's a boolean, I'm using 1 for true and 0 for false.

Am I missing something?

public Exp getFilterByFieldFilter(FilterByField filterByField) {
    if (filterByField == null || "".equals(filterByField)) {
        return Exp.val(true);
    }
    if (filterByField == filterByField.All) {
        return Exp.val(true);
    } else if (filterByField == filterByField.ValidBoth) {
        return Exp.and(Exp.eq(Exp.intBin("Field1"), Exp.val(0)),
                Exp.eq(Exp.intBin("Field2"), Exp.val(0)));
    }
}

From what I can see from database results through AQL, those which are not set to true are not reflected in the result set.

Should I write my filter is a different way to check this condition? If so what would that condition look like?

I tried checking for Exp.val(NULL) but got error.

Here's my database result set through AQL

 [
        {
          "PK": "1",
          "Name": "ABC",
          "Field1": 1,
          "Field2": 1
        },
        {
          "PK": "2",
          "Name": "EFG",
          "Field1": 1
        },
        {
          "PK": "3",
          "Name": "XYZ",
        }
        
    ]

CodePudding user response:

If bin names Field1 and Field2 contain boolean values then your expression should be constructed in this fashion (whatever the desired logic is):

Exp.eq(Exp.boolBin("Field1"), Exp.val(false))

I tested the construct below, seems to work for me:

WritePolicy wPolicy = new WritePolicy();

Bin b1 = new Bin("Field1", Value.get(0));
Bin b2 = new Bin("Field2", Value.get(0));
Bin b3 = new Bin("Data", Value.get("data"));

wPolicy.recordExistsAction = RecordExistsAction.REPLACE;

client.put(wPolicy, key, b1, b2, b3);

//client.put(wPolicy, key, b1, b3);

Exp condFilter = Exp.and( 
Exp.eq(Exp.intBin("Field1"),Exp.val(0) ),
Exp.eq(Exp.intBin("Field2"),Exp.val(0) )
);

Policy policy = new Policy();

policy.filterExp = Exp.build(condFilter);
Record record = client.get(policy, key);
System.out.println("Read back the record.");

System.out.println("Record values are:");
System.out.println(record);

//Get record without filter condition
record = client.get(null, key);
System.out.println(record);

Valid condition:

Read back the record.
Record values are:
(gen:18),(exp:0),(bins:(Field1:0),(Field2:0),(Data:data))
(gen:18),(exp:0),(bins:(Field1:0),(Field2:0),(Data:data))

Invalid Condition (no Field2 bin):

Read back the record.
Record values are:
null
(gen:19),(exp:0),(bins:(Field1:0),(Data:data))
  • Related