Search Threads with logical OR

Hello everyone!

I would like to filter threads using a search as described here: Nylas v3 Email, Calendar, and Contacts API docs | Nylas Docs

My use case is that a user will type their search string and this will be used to find the threads that either contain this in their subject or there is an email associated with that search string in the thread. After that the threads shall be returned from last to first.

I see I can use the columns “subject” and “any_email” which would serve my purpose quite well but there is not an option for a logical OR between the two. I can see that the ordering is by default that the latest thread should come first so in that aspect everything is ok.

I could see one solution being that I could make 2 separate requests in the same API, one with “subject” and another with “any_email” but there is an issue there in both ordering of the eventual response data and pagination.

Another solution I have considered for this is to use search_query_native and do something like the following: (I am using Microsoft provider)

filter_query = (
f"$filter=contains(subject,‘{search_string}’) or "
f"contains(from/emailAddress/address,‘{search_string}’)"
)

The problem with the above solution however is that results do not come ordered by latest date, and I can not seem to find a way to order them.

Any ideas?

Thanks a lot in advance :slight_smile:

Hello @gpapidas why don’t put the results in an array, make the date a date type and then sort the array the way you need it…I would do it that way :thinking:

That would unfortunately not work quite well. Here’s an example.

If we assume we are making 2 separate requests, one with subject and one with any email then we will get 2 separate responses.

Let’s assume that from the subject request the response is the following.

data = [{“value”: 1, “date”: Sep 5th}, {“value”: 2, “date”: Sep 4th}, {“value”: 3, “date”: Sep 3rd}] and then we have a page_token to go to page 2, where we would have values 4, 5 and 6 with dates Sep 2, Sep 1, August 31 respectively.

Now let’s say that in a similar manner my request with the “any_email” parameter has the following response.

page 1: data = [{“value”: 10, “date”: Sep 4th}, {“value”: 20, “date”: Sep 4th}, {“value”: 30, “date”: Sep 3rd}] and
page 2: data = [{“value”: 30, “date”: Sep 3rd}, {“value”: 40, “date”: Sep 2nd}, {“value”: 50, “date”: Sep 1st}]

now, if we combine the results of the first page and we want to serve 3 elements per page, we will serve, according to dates the following.

data = [{“value”: 1, “date”: Sep 5th}, {“value”: 2, “date”: Sep 4th}, {“value”: 10, “date”: Sep 4th}]

and that would probably work fine.

But what happens when the user wants to go to the second page?
How could I get the next correct set of data which would be the following

data = [{“value”: 20, “date”: Sep 4th}, {“value”: 3, “date”: Sep 3rd}, {“value”: 30, “date”: Sep 3rd}]

I mean, I could probably do something custom, but all the solutions I have thought involve creating a somewhat complex solution by keeping correct order from Nylas multiple API requests while also solving a problem of keeping 2 next_cursor values.

That’s why ideally I’d like to avoid this if possible.