How to write GQL `CONTAINS` comparator query in objectify - google-app-engine

I use google cloud datastore to store and objectify to read the data. The entity that i use holds a list property and i would like to filter the records through multiple values and the results should be returned only if all the values are present in the list
GQL now supports CONTAINS query filter link here . I would like to know how this can be achieved in objectify
Assuming there are three entities that holds a list field
(Entity1) => list = ["id1","id2","id3","id4"]
(Entity2) => list = ["id1","id2","id3"]
(Entity3) => list = ["id1"]
queryList = ["id1","id2"]
How do i write an objectify query that uses contains. For example, ofy().load().type( Entity.class ).filter( "list contains" , queryList ).list(); so that only entities 1 and 2 are returned
I am familiar that IN filter ofy().load().type( Entity.class ).filter( "list in" , queryList ).list();
will return all three entities.
But the requirement is to fetch only entities that has both id1 and id2

You'll want to use an AND query, which should be ...filter("list", "id1").filter("list", "id2")

Related

PocketBase filter data by with multiple relation set

i have a collection where the field "users" contains the id's of two users.
how can i search for this dataset where this both user id's are.
i tried
users = ["28gjcow5t1mkn7q", "frvl86sutarujot"]
still don't work
This is a relation, so there must be a collection that allows you multiple, non unique results, so this table you are looking at to, is the the dataset, you can query the whole dataset on script with
// you can also fetch all records at once via getFullList
const records = await pb.collection('tlds').getFullList(200 /* batch size */, {
sort: '-created',
});
I sugest you to look into: js-sdk/pocketbase on github.

Firebase Flutter - Request with multiple where arrayContains

In my firebase documents, I have a field named "tags" that is a List, for example tags = ["Amazing", "Great", "Disappointing"].
I want to filter the documents to query, so the user select a list of tags, for example filteredTags = [Amazing", "Great"].
In my request, I want to retrieve all documents that have all elements of filteredTags in there tags list.
This query does not work because it looks for a list within tags, which is just a list of string :
query = query.where(KeyTags, whereIn: filteredTags);
And this query return an error, because flutter does not allow to have multiple arrayContains in the same query (works if I have only 1 tag in filteredTags list) :
for(dynamic tag in filteredTags){
query = query.where(KeyTags, arrayContains: tag);
}
Finally, this one work but is not what I look for (it retrieves documents that have one of the filteredTags, whereas I want documents that have all of them :
query = query.where(KeyTags, arrayContainsAny: filteredTags);
Any idea of how to do it ?
Thanks !
What you're describing is an arrayContainsAll type operator, which doesn't exist at the moment.
The only way to implement this now is to store the tags as a map with subfields for each tag and then a value, and then query for those values with equality checks in your query. For example:
tags: {
"Amazing": true,
"Great": true,
"Disappointing": true
}
And:
query
.where("tags.Amazing", isEqualTo: true)
.where("tags.Great", isEqualTo: true)
Also see:
Firestore search array contains for multiple values
Firestore array contains query for list of elements
Firestore query - array contains all
Firestore: Multiple 'array-contains'

How to get all vector ids from Milvus2.0?

I used to use Milvus1.0. And I can get all IDs from Milvus1.0 by using get_collection_stats and list_id_in_segment APIs.
These days I am trying Milvus2.0. And I also want to get all IDs from Milvus2.0. But I don't find any ways to do it.
milvus v2.0.x supports queries using boolean expressions.
This can be used to return ids by checking if the field is greater than zero.
Let's assume you are using this schema for your collection.
referencing: https://github.com/milvus-io/pymilvus/blob/master/examples/hello_milvus.py
as of 3/8/2022
fields = [
FieldSchema(name="pk", dtype=DataType.INT64, is_primary=True, auto_id=False),
FieldSchema(name="random", dtype=DataType.DOUBLE),
FieldSchema(name="embeddings", dtype=DataType.FLOAT_VECTOR, dim=dim)
]
schema = CollectionSchema(fields, "hello_milvus is the simplest demo to introduce the APIs")
hello_milvus = Collection("hello_milvus", schema, consistency_level="Strong")
Remember to insert something into your collection first... see the pymilvus example.
Here you want to query out all ids (pk)
You cannot currently list ids specific to a segment, but this would return all ids in a collection.
res = hello_milvus.query(
expr = "pk >= 0",
output_fields = ["pk", "embeddings"]
)
for x in res:
print(x["pk"], x["embeddings"])
I think this is the only way to do it now, since they removed list_id_in_segment

Optimize IQueryable query to let EF generate a single SQL query instead multiple. Child collection of an entity must contains a custom collection

The goal is to have a single query that will be generated by the EF and MSSQL will execute it in one go. Having the current implementation, everything works correctly, but not optimal. To be more specific, looking at the SQL Server Profiler logs, it makes additional exec sp_executesql queries per each company to fetch data (in example below, it would be Products).
Say, we have selected product ids.
List<int> selectedProductIds = { 1, 2, 3 };
We filter over a collection of Companies to get only those companies that have ALL selected products.
And a query where we dynamically extend it as many as we need, thankfully to IQueryable interface.
Imagine x of type Company and it contains a collection of Products.
if (selectedProductIds.Count > 0)
{
query = query.Where(x => selectedProductIds.All(id => x.Products.Select(p => p.ProductId).Contains(id)));
}
Is there any way to rewrite the predicate using LINQ? I know I can make a dynamic SQL query myself anytime, but I am trying to understand what I miss in terms of EF/LINQ. Thanks!
The version of Entity Framework Core is 2.1.
UPD:
Company products are unique and never duplicated within a company entity. No need to make distinct.
Try the following query:
if (selectedProductIds.Count > 0)
{
query = query.Where(x => x.Products
.Where(p => selectedProductIds.Contains(p.ProductId))
.Count() == selectedProductIds.Count
);
}

Querying mongoDB document based on Array element

This is one user's notes. I want to query and get only the notes of this use with "activeFlag:1". My query object code is
findAccountObj =
{ _id: objectID(req.body.accountId),
ownerId: req.body.userId,
bookId: req.body.bookId,
"notes.activeFlag": 1 };
But this query returns all the notes, including the ones with "activeFlag:0".
How do I fix this?
If you are on v2.2, use elementmatch operator. v3.2 and above allow aggregation and filter to return a subset of a document.
here is an example Retrieve only the queried element in an object array in MongoDB collection

Resources