Use variable in ContainsStr

I’m trying to do a text search and it’s almost working. However, it’s only working if I hardcode the search term. I want to use a variable.

This is working:

q.Map(
    q.Filter(
      q.Paginate(q.Match(q.Index("all_accounts"))),
      q.Lambda("accountRef",
        q.ContainsStr(
          q.LowerCase(q.Select(["data","name"], q.Get(q.Var("accountRef")))),
          'yada'
        )
      )
    ),
    q.Lambda("accountRef", q.Get(q.Var("accountRef")))
  )
)

But this is not working:

const searchTerm = 'yada'

q.Map(
    q.Filter(
      q.Paginate(q.Match(q.Index("all_accounts"))),
      q.Lambda("accountRef",
        q.ContainsStr(
          q.LowerCase(q.Select(["data","name"], q.Get(q.Var("accountRef")))),
            searchTerm
        )
      )
    ),
    q.Lambda("accountRef", q.Get(q.Var("accountRef")))
  )
)

Any help would be much appreciated!

Hi @jennie,

I haven’t tried to reproduce this yet but I don’t see anything obviously wrong. What’s the error message (if any) you’re getting back?

Cory

I’m not getting an error message. The query with the variable only returns an empty array like this:

{ data: [] }

But if I hardcode the exact same search term, I get an array with all the filtered documents.

I’ve adjusted your example to use the People documents created in the indexing tutorial:

const searchTerm = 'alan'

client.query(
  q.Map(
    q.Filter(
      q.Paginate(q.Documents(q.Collection("People"))),
      q.Lambda("ref",
        q.ContainsStr(
          q.LowerCase(q.Select(["data","first"], q.Get(q.Var("ref")))),
          searchTerm
        )
      )
    ),
    q.Lambda("ref", q.Get(q.Var("ref")))
  )
)
.then((result) => console.log(result))
.catch((error) => console.log("Error:" , error))

The output is:

{
  data: [
    {
      ref: Ref(Collection("People"), "326306123302830592"),
      ts: 1647448619100000,
      data: {
        first: 'Alan',
        last: 'Perlis',
        age: 97,
        degrees: [ 'BA', 'MA', 'PhD' ],
        letter: 'A'
      }
    },
    {
      ref: Ref(Collection("People"), "326306123303879168"),
      ts: 1647448619100000,
      data: {
        first: 'Alan',
        last: 'Turing',
        age: 107,
        degrees: [ 'BA', 'MA', 'MS', 'PhD' ],
        letter: 'B'
      }
    }
  ]
}

@jennie I can’t reproduce what you’re seeing. I used your same code on a collection named accounts with three documents, each having a name field with the string yada in it.

I used this code:

  const searchTerm = 'yada'
  client.query(
    q.Map(
        q.Filter(
          q.Paginate(q.Match(q.Index("all_accounts"))),
          q.Lambda("accountRef",
            q.ContainsStr(
              q.LowerCase(q.Select(["data","name"], q.Get(q.Var("accountRef")))),
              searchTerm
            )
          )
        ),
        q.Lambda("accountRef", q.Get(q.Var("accountRef")))
    )
  )
  .then(function (res) { console.log('Result:', res) })
  .catch(function (err) { console.log('Error:', err) })

I get back all three docs in the console.

Can you confirm which version of the JS driver you’re using? And are you using any other JS libraries?

Thank you for your help. It’s working now. I needed to convert my search term to lower case as well :see_no_evil:

1 Like

Thank’s for your help! It was my mistake, the code actually was working all the time. I just forgot to convert my search term to lower case as well.

1 Like