jennie
March 16, 2022, 10:42am
1
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
jennie
March 16, 2022, 2:27pm
3
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.
ewan
March 16, 2022, 4:49pm
4
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?
jennie
March 17, 2022, 8:12am
6
Thank you for your help. It’s working now. I needed to convert my search term to lower case as well
1 Like
jennie
March 17, 2022, 8:16am
7
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