# Multiple filters query problem

**URL:** https://discuss.aerospike.com/t/multiple-filters-query-problem/2419
**Category:** Query & Indexing
**Created:** [January 23, 2016, 1:58pm UTC](https://discuss.aerospike.com/t/multiple-filters-query-problem/2419 "2016-01-23T13:58:01Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![sohel2020](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@sohel2020](https://discuss.aerospike.com/u/sohel2020)
#### Post date: [January 23, 2016, 1:58pm UTC](https://discuss.aerospike.com/t/multiple-filters-query-problem/2419/1 "2016-01-23T13:58:01Z")

</div>

I got a problem and stuck.

```
+----------+-----------+------+--------------+---------------+---------------+
| from| to | type | data | timestamp | uid |
+----------+-----------+------+--------------+---------------+---------------+
| "a" | "c" | "m" | "hi, he llo" | 1453286107693 | 1453286036182 |
| "a" | "b" | "m" | "hi, he llo" | 1453286068605 | 1453286036183 |
| "b" | "c" | "m" | "hi, he llo" | 1453286068605 | 1453286036184 |
| "a" | "b" | "m" | "hi, he llo" | 1453286068605 | 1453286036184 |
| "b" | "c" | "m" | "hi, he llo" | 1453286068605 | 1453286036184 |
+----------+-----------+------+--------------+---------------+---------------+

```

How can I run query to get data from above table where from “a” and to “b” or from “b” and to “a”.

We tried SELECT \* FROM test.message\_data WHERE (from=“a” AND to=“b”) OR (from=“b” AND to=“a”)

It’s only filtering for SELECT \* FROM test.message\_data WHERE from=“a”. Is there way to do it?

---

<div class="post-metadata">

### Author: ![ManuelSchmidt](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/manuelschmidt/32/424_2.png) [@ManuelSchmidt](https://discuss.aerospike.com/u/ManuelSchmidt)
#### Post date: [January 23, 2016, 2:21pm UTC](https://discuss.aerospike.com/t/multiple-filters-query-problem/2419/2 "2016-01-23T14:21:55Z")

</div>

Yes, it is possible but you need to use a stream UDF for the second/third/… WHERE-clause. You can choose the most selective condition as your WHERE and implement any further filtering through server-side UDFs.

[http://www.aerospike.com/launchpad/query\_multiple\_filters.html](http://www.aerospike.com/launchpad/query_multiple_filters.html)

---

<div class="post-metadata">

### Author: ![sohel2020](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@sohel2020](https://discuss.aerospike.com/u/sohel2020)
#### Post date: [January 23, 2016, 3:20pm UTC](https://discuss.aerospike.com/t/multiple-filters-query-problem/2419/3 "2016-01-23T15:20:06Z")

</div>

@ManuelSchmidt would you pls write the query for me? because I’m very new in aerospike.

---

<div class="post-metadata">

### Author: ![ManuelSchmidt](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/manuelschmidt/32/424_2.png) [@ManuelSchmidt](https://discuss.aerospike.com/u/ManuelSchmidt)
#### Post date: [January 23, 2016, 6:09pm UTC](https://discuss.aerospike.com/t/multiple-filters-query-problem/2419/4 "2016-01-23T18:09:00Z")

</div>

What client (language) are you using? I’ll see if I manage to create a working example for you this week.

---

<div class="post-metadata">

### Author: ![sohel2020](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@sohel2020](https://discuss.aerospike.com/u/sohel2020)
#### Post date: [January 23, 2016, 8:18pm UTC](https://discuss.aerospike.com/t/multiple-filters-query-problem/2419/5 "2016-01-23T20:18:08Z")

</div>

@ManuelSchmidt I’m using nodejs. please sir I need badly.

---

<div class="post-metadata">

### Author: ![Justine\_Thomas](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@Justine\_Thomas](https://discuss.aerospike.com/u/Justine_Thomas)
#### Post date: [January 27, 2016, 2:11pm UTC](https://discuss.aerospike.com/t/multiple-filters-query-problem/2419/6 "2016-01-27T14:11:28Z")

</div>

Here’s a piece of code I’d written for Java. Hope you can convert it according to your needs.

1. The Java File : [Aerospike/AverageDAO.java at master · LeloucH-Zer0/Aerospike · GitHub](https://github.com/LeloucH-Zer0/Aerospike/blob/master/AverageDAO.java)

You can go through the comments in the file to understand the inputs needed.

1. The Lua File : [Aerospike/genericUdf.lua at master · LeloucH-Zer0/Aerospike · GitHub](https://github.com/LeloucH-Zer0/Aerospike/blob/master/genericUdf.lua)

I believe you can use this code without making any changes whatsoever.

I had designed the thing to be as generic as possible. Hope this helps 🙂 . Feel free to respond on this thread if you need any clarifications.

---

<div class="post-metadata">

### Author: ![rbotzer](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/rbotzer/32/2114_2.png) [@rbotzer](https://discuss.aerospike.com/u/rbotzer)
#### Post date: [February 5, 2016, 10:20pm UTC](https://discuss.aerospike.com/t/multiple-filters-query-problem/2419/7 "2016-02-05T22:20:49Z")

</div>

You would first [query](http://www.aerospike.com/docs/guide/query.html) for records where **from** = _val1_ and apply the following [stream UDF](http://www.aerospike.com/docs/udf/developing_stream_udfs.html) on the matched records

```nohighlight
local function from_to_filter(val1, val2)
  return function(rec)
    if (rec['from'] == val1 and rec['to'] == val2) or
       (rec['from'] == val2 and rec['to'] == val1) then
      return true
    end
    return false
  end
end

local function map_record(rec)
  local ret = map()
  for i, bin_name in ipairs(record.bin_names(rec)) do
    ret[bin_name] = rec[bin_name]
  end
  return ret
end

function from_to_match(stream, val1, val2)
  return stream : filter(from_to_filter(val1, val2)) : map(map_record)
end

```
