Can't implement numeric filter

I have an Algolia index where each record looks like this:

{

* sessionDate:1606741138
* customer1FirstName:

"Donna"

* customer1LastName:

"King"

}

I want to filter my results numerically, so I can get a number range of dates in seconds. I send a query like this:

{
  "query": "",
  "filters": "sessionDate:1606741138 TO 1606741139"
}

and get 0 results!

When I send my data to the Algolia index I am using the js function parseInt() to force the value to be a number, but it still looks like a string when I browse my index? - tho when I edit the record it looks like a number? I can’t think of anything else except a type error that would make it return 0 results?

The value also needs to be a number when indexing, from your sample, it seems like that’s the case, but the * parts make it not valid JSON. How do you index the data to Algolia?

I use a coud function to push an array of these objects to the index to create it

const childData = {
              objectID: childKey,
              indexData: {
                sessionDate: parseInt(session.sessionDate._seconds),
                customer1FirstName:
                  session.customerInfo.customers.customer1.customerFirstName,
                customer1LastName:
                  session.customerInfo.customers.customer1.customerLastName,
              },
            }

The whole function is

exports.sendCollectionToAlgolia = functions

  .runWith(runtimeOpts)

  .https.onRequest(async (req, res) => {

    admin

      .firestore()

      .collection('sessions')

      .get()

      .then((docs) => {

        let _sessions = []

        docs.forEach((doc) => {

          let session = doc.data()

          const childKey = doc.id

          if (

            session.customerInfo.customers.customer1.customerFirstName &&

            session.customerInfo.customers.customer1.customerLastName

          ) {

            const childData = {

              objectID: childKey,

              indexData: {

                sessionDate: parseInt(session.sessionDate._seconds),

                customer1FirstName:

                  session.customerInfo.customers.customer1.customerFirstName,

                customer1LastName:

                  session.customerInfo.customers.customer1.customerLastName,

              },

            }

            console.log(

              'PUSHING TO SESSIONS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!',

              childData

            )

            _sessions.push(childData)

          }

        })

        return _sessions

      })

      .then((_sessions) => {

        console.log('ALL SESSIONS', _sessions.length, _sessions.slice(0, 5))

        index

          .saveObjects(_sessions)

          .then(() => {

            console.log('Contacts imported into Algolia')

          })

          .catch((error) => {

            console.error('Error when importing contact into Algolia', error)

            process.exit(1)

          })

      })

  })

looking at that, your filter has to be indexData.sessionDate:1606741138 TO indexData.sessionDate:1606741139

So this request body:

{
  "query": "",
  "filters": "indexData.sessionDate:1606741138 TO indexData.sessionDate:1606741139"
}

gets this response:

{
  "message": "filters: Unexpected token string(indexData.sessionDate) expected numeric at col 36",
  "status": 400
}

Am I missing something here?

AH!

filters: 'indexData.sessionDate:1606741138 TO 1606741139',

works! Thanks for your help :slight_smile:

1 Like

I just recognised I copied from the wrong place indeed, here’s a filters validator for the future: filters | API parameters | API Reference | Algolia Documentation

1 Like