Appsync graphql: How to filter based on entry in an array field - graphql

In my code I have created filter as:
const myFilter: TableMyEntityFilterInput = {targets: {contains: 'username'}};
'targets' field is an array:
targets?: Array | null;
My objective is to fetch those records which has 'username' as an entry in 'targets' field.
But it does't work. Empty array is fetched. But if I use similar criteria on a simple string field, it works.
How to get it working for array field?
Edit:
'targets' sample value:
[ { "S" : "[\"Messi\",\"Ronaldo\"]" }]

CONTAINS is supported for lists: When evaluating "a CONTAINS b", "a" can be a list; however, "b" cannot be a set, a map, or a list.

Related

Python OpenSearch retrieve records based on element in a list

So I need to retrieve records based on a field called "cash_transfer_ids" which is a python list.
I want to retrieve all records whose cash_transfer_ids contain a specific id value (a string).
What should the query be look like? Should I use match or term query?
Example: I want to retrieve any record whose cash_transfer_ids field contains 'abc'
Then I may get record such as
record 1: cash_transfer_ids:['abc']
record 2: cash_transfer_ids:['dfdfd', 'abc']
etc...
Thanks very much for any help!
if cash_transfer_ids is type keyword I try filter with Term.
term = "abc"
query = {
"query": {
"term": {
"cash_transfer_ids": {
"value": term
}
}
}
}
response = get_client_es().search(index="idx_test", body=query)

ES reversed filtering

Pardon the title, not sure how better to describe the problem.
Anyway, I have a table with entries(id, name, age, ..dynamic fields) and filter_groups(id, filters[])
Each filter group as a list of filters of the form {filter, field, value} that is used client-side to filter an HTML table of entries
Exmaples:
[{ filter: 'less_than', field: 'age' value: 10 },
{filter: 'is', field: 'name' value: "john doe" }]
If I wanted to fetch all the entries matching a particular filter group, it seems fairly straightforward to construct the query and send it to ES using the filters.
However, if you reverse the situation and given an entry want to fetch all the filter_groups whose filters match the entry, how would you go about doing this?
Thanks

RethinkDB: how to append to an array in a nested structure

I am trying to append to an array in a nested field, which I have to find based on runtime information.
Here's an example:
r.db("test")
.table("test")
.insert({ "stock": [{ "bin":"abc", "entries":[{ "state":1 }] }] })
The idea is that the document contains a "stock" key, which is an array of multiple "storage bins". Each bin has a name and a number of entries. I need to be able to append to entries in one of the bins, atomically, without affecting other bins.
I tried this approach:
r.db("test")
.table("test")
.update(function(item) {
return {"stock": item("stock")
.filter({ "bin": "abc" })
.append({ "state":42 })
}
})
…but that does not append at the right level, and I am not certain if it will preserve existing bins with names other than "abc".
When updating an element of an array, you should use changeAt with an index, or map over the array instead of using filter.
Here is what that query might look like:
r.table("test")
.update(function(item){
return {"stock": item("stock").map(function(stock){
r.branch(stock.hasFields({"bin": "abc"})
stock.merge({"entries": stock("entries").append({"state": 42})}),
stock)})}})
Alternatively, if you stored your entries in an object instead of an array, like this:
{ "stock": {"abc": {"entries":[{ "state":1 }] }} }
The update query might look like this:
r.table("test")
.filter(r.row.hasFields({"stock": {"abc": true}}))
.update({"stock": {"abc": r.row("stock")("abc").append({"state": 42})}})

In Couchbase or N1QL how can I check if the values in an array match

In a couchbase I have the following document structure...
{
name: "bob",
permissions: [
2,
4,
6
]
}
I need to be able to create a view, or N1QL query which will check if the permissions for "bob" are contained within a given array.
e.g I have an array with contents
[1,2,3,4,5,6]
I need the "bob" document to be returned because my array contains 2,4,6 and so does "bob"
If my array contained 1,3,4,5,6 "bob" should not be selected because my array does not contain "2"
Essentially I want to match any documents whose permission entries are all contained in my array.
The solution can either a view or an N1QL query.
Using N1QL, you can do the following:
SELECT * FROM my_bucket WHERE EVERY p IN permissions SATISFIES p IN [ 1,2,3,4,5,6 ] END;

ElasticSearch, how to search for a document containing a specific array element

I am having a little problem with elasticsearch and wonder if someone can help me solve it.
I have a document containing an array of tuples (publications).
Something like :
{
....
publications: [
{
item1: 385294,
item2: 11
},
{
item1: 395078,
item2: 1
}
]
....
}
The problem i have is for retrieving documents who contain a specific tuple, for exemple (item1 = 395078 AND item2 = 1).
Whatever i try, it seems to always treat item1 and item2 separately, i fail to tell elasticsearch that item1 and item2 must have a specific value inside the same tuple, not accross the whole array...
Is there something i'm missing here ?
Thanks
This is not possible in the straight way.
ElasticSearch flattens the array before checking for condition.
Which mean
elasticSearch matches
a=x AND b=y1 to [{a=x,b=y},{a=x1,b=y1}] which doesnt happen in the conventianal array checking.
What you can do here is
Usage of nested type - https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html (but for each element in array , an extra document would be created)
Store the array as
publications: [
{
385294:11
},
{
395078:1
}
]

Resources