how to create firestore composite index for conditional query? - javascript

I'm using react and fetching docs from my firestore collection. and when i fetch the docs i assigned a conditional query and the query direction changes depending on the value provided.
const getData = async () => {
const constraints = [];
if (price)
constraints.push(orderBy("price", price == "1" ? "desc" : "asc"));
if (date)
constraints.push(orderBy("postedDate", date == "1" ? "desc" : "asc"));
if (type)
constraints.push(orderBy("type", type == "1" ? "desc" : "asc"));
// there are more conditional queries here. 8 more to be exact
const livings = collection(db, "livingPosts");
let q = query(livings, ...constraints);
const qSnapshot = await getDocs(q);
const dataQ = qSnapshot.docs.map((doc) => ({
...doc.data(),
id: doc.id,
}));
// console.log(dataQ);
setDatas(dataQ);
};
useEffect(() => {
getData();
}, []);
as you can see in the above code i've implemented conditional query. and there are more queries that i have not included. so my question is that how can i create indexes for all of these queries?
i created indexes with the link firebase provided me to create it. but it made me create 4 composite indexes just for 2 queries. (for price and date), the order is
price - asc , date - asc
price - desc , date - asc
price - asc , date - desc
price - desc , date - asc
these were the indexes. so do i have to create every possible indexes like these? if so there are number of combinations i have to do and the max number of indexes are 200. please show me the right way

So do i have to create every possible indexes like these
Yes you need to create all the indexes corresponding to the queries your app may potentially execute.
For that you can use the URL present in the error messages (which means that you need to execute all the possible queries, i.e. play all the scenarii) or you can use the Firebase (or Google Cloud) consoles to build them manually.
For the default limit of maximum 200 composite indexes you can actually contact the Firebase support to ask for an increase of this maximum number.

Related

What is the equivalent of Postgres' array overlap (&&) function in GraphQL?

In Postgres, the overlap ("&&") operator of arrays are used to check if two arrays have any element in common.
For example,
Query
Result
ARRAY[1,4,3] && ARRAY[2,1]
true
ARRAY[1,4,3] && ARRAY[2,5]
false
Is there anything similar in GraphQL?
Also, is there anything I can use in Hasura for this?
A more specific real use case I can think of is as follows. (I just made them up just for clarification and did not run or validate the syntax.)
// Assume a you want to find all restaurants with any of the selected cuisines.
const restaurantA = {..., cuisineType: ['Fusion', 'Chinese', 'Japanese', 'Korean']}
const restaurantB = {..., cuisineType: ['Vegetarian', 'Thai']}
const restaurantC = {..., cuisineType: ['European']}
// Assume the data above is stored in Postgres (as an array in column field, not using a bridge table.
const SQL_1 = "SELECT *
FROM restaurants
WHERE cuisine_type && ['Korean', 'Japanese']" // Expect restaurantA and restaurantC
const SQL_2 = "SELECT *
FROM restaurants
WHERE cuisine_type && ['Vegetarian', 'European']" // Expect restaurantB and restaurantC
### GOAL: I want to use GraphQL, Apollo, Hasura to have the same effect as SQL_1 and SQL_2

Is there any option to find whether a text is present in either one of the field in firestore [duplicate]

From the docs:
You can also chain multiple where() methods to create more specific queries (logical AND).
How can I perform an OR query?
Example:
Give me all documents where the field status is open OR upcoming
Give me all documents where the field status == open OR createdAt <= <somedatetime>
OR isn't supported as it's hard for the server to scale it (requires keeping state to dedup). The work around is to issue 2 queries, one for each condition, and dedup on the client.
Edit (Nov 2019):
Cloud Firestore now supports IN queries which are a limited type of OR query.
For the example above you could do:
// Get all documents in 'foo' where status is open or upcmoming
db.collection('foo').where('status','in',['open','upcoming']).get()
However it's still not possible to do a general OR condition involving multiple fields.
With the recent addition of IN queries, Firestore supports "up to 10 equality clauses on the same field with a logical OR"
A possible solution to (1) would be:
documents.where('status', 'in', ['open', 'upcoming']);
See Firebase Guides: Query Operators | in and array-contains-any
suggest to give value for status as well.
ex.
{ name: "a", statusValue = 10, status = 'open' }
{ name: "b", statusValue = 20, status = 'upcoming'}
{ name: "c", statusValue = 30, status = 'close'}
you can query by ref.where('statusValue', '<=', 20) then both 'a' and 'b' will found.
this can save your query cost and performance.
btw, it is not fix all case.
I would have no "status" field, but status related fields, updating them to true or false based on request, like
{ name: "a", status_open: true, status_upcoming: false, status_closed: false}
However, check Firebase Cloud Functions. You could have a function listening status changes, updating status related properties like
{ name: "a", status: "open", status_open: true, status_upcoming: false, status_closed: false}
one or the other, your query could be just
...where('status_open','==',true)...
Hope it helps.
This doesn't solve all cases, but for "enum" fields, you can emulate an "OR" query by making a separate boolean field for each enum-value, then adding a where("enum_<value>", "==", false) for every value that isn't part of the "OR" clause you want.
For example, consider your first desired query:
Give me all documents where the field status is open OR upcoming
You can accomplish this by splitting the status: string field into multiple boolean fields, one for each enum-value:
status_open: bool
status_upcoming: bool
status_suspended: bool
status_closed: bool
To perform your "where status is open or upcoming" query, you then do this:
where("status_suspended", "==", false).where("status_closed", "==", false)
How does this work? Well, because it's an enum, you know one of the values must have true assigned. So if you can determine that all of the other values don't match for a given entry, then by deduction it must match one of the values you originally were looking for.
See also
in/not-in/array-contains-in: https://firebase.google.com/docs/firestore/query-data/queries#in_and_array-contains-any
!=: https://firebase.googleblog.com/2020/09/cloud-firestore-not-equal-queries.html
I don't like everyone saying it's not possible.
it is if you create another "hacky" field in the model to build a composite...
for instance, create an array for each document that has all logical or elements
then query for .where("field", arrayContains: [...]
you can bind two Observables using the rxjs merge operator.
Here you have an example.
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/merge';
...
getCombinatedStatus(): Observable<any> {
return Observable.merge(this.db.collection('foo', ref => ref.where('status','==','open')).valueChanges(),
this.db.collection('foo', ref => ref.where('status','==','upcoming')).valueChanges());
}
Then you can subscribe to the new Observable updates using the above method:
getCombinatedStatus.subscribe(results => console.log(results);
I hope this can help you, greetings from Chile!!
We have the same problem just now, luckily the only possible values for ours are A,B,C,D (4) so we have to query for things like A||B, A||C, A||B||C, D, etc
As of like a few months ago firebase supports a new query array-contains so what we do is make an array and we pre-process the OR values to the array
if (a) {
array addObject:#"a"
}
if (b) {
array addObject:#"b"
}
if (a||b) {
array addObject:#"a||b"
}
etc
And we do this for all 4! values or however many combos there are.
THEN we can simply check the query [document arrayContains:#"a||c"] or whatever type of condition we need.
So if something only qualified for conditional A of our 4 conditionals (A,B,C,D) then its array would contain the following literal strings: #["A", "A||B", "A||C", "A||D", "A||B||C", "A||B||D", "A||C||D", "A||B||C||D"]
Then for any of those OR combinations we can just search array-contains on whatever we may want (e.g. "A||C")
Note: This is only a reasonable approach if you have a few number of possible values to compare OR with.
More info on Array-contains here, since it's newish to firebase docs
If you have a limited number of fields, definitely create new fields with true and false like in the example above. However, if you don't know what the fields are until runtime, you have to just combine queries.
Here is a tags OR example...
// the ids of students in class
const students = [studentID1, studentID2,...];
// get all docs where student.studentID1 = true
const results = this.afs.collection('classes',
ref => ref.where(`students.${students[0]}`, '==', true)
).valueChanges({ idField: 'id' }).pipe(
switchMap((r: any) => {
// get all docs where student.studentID2...studentIDX = true
const docs = students.slice(1).map(
(student: any) => this.afs.collection('classes',
ref => ref.where(`students.${student}`, '==', true)
).valueChanges({ idField: 'id' })
);
return combineLatest(docs).pipe(
// combine results by reducing array
map((a: any[]) => {
const g: [] = a.reduce(
(acc: any[], cur: any) => acc.concat(cur)
).concat(r);
// filter out duplicates by 'id' field
return g.filter(
(b: any, n: number, a: any[]) => a.findIndex(
(v: any) => v.id === b.id) === n
);
}),
);
})
);
Unfortunately there is no other way to combine more than 10 items (use array-contains-any if < 10 items).
There is also no other way to avoid duplicate reads, as you don't know the ID fields that will be matched by the search. Luckily, Firebase has good caching.
For those of you that like promises...
const p = await results.pipe(take(1)).toPromise();
For more info on this, see this article I wrote.
J
OR isn't supported
But if you need that you can do It in your code
Ex : if i want query products where (Size Equal Xl OR XXL : AND Gender is Male)
productsCollectionRef
//1* first get query where can firestore handle it
.whereEqualTo("gender", "Male")
.addSnapshotListener((queryDocumentSnapshots, e) -> {
if (queryDocumentSnapshots == null)
return;
List<Product> productList = new ArrayList<>();
for (DocumentSnapshot snapshot : queryDocumentSnapshots.getDocuments()) {
Product product = snapshot.toObject(Product.class);
//2* then check your query OR Condition because firestore just support AND Condition
if (product.getSize().equals("XL") || product.getSize().equals("XXL"))
productList.add(product);
}
liveData.setValue(productList);
});
For Flutter dart language use this:
db.collection("projects").where("status", whereIn: ["public", "unlisted", "secret"]);
actually I found #Dan McGrath answer working here is a rewriting of his answer:
private void query() {
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("STATUS")
.whereIn("status", Arrays.asList("open", "upcoming")) // you can add up to 10 different values like : Arrays.asList("open", "upcoming", "Pending", "In Progress", ...)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(#Nullable QuerySnapshot queryDocumentSnapshots, #Nullable FirebaseFirestoreException e) {
for (DocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
// I assume you have a model class called MyStatus
MyStatus status= documentSnapshot.toObject(MyStatus.class);
if (status!= null) {
//do somthing...!
}
}
}
});
}

Firestore orderBy Timestamp object

I am trying to order a query by timestamp.
In my document I have a field called "date" which has this form:
date = {
nanoseconds: 963000000,
seconds: 1594917688
}
In my code I have this:
let photosArray = [];
firebase
.getDatabase()
.collection("photos")
.doc(firebase.getCurrentUser().uid)
.collection("userPhotos")
.orderBy("date", "asc") // Sorted by date in ascending direction
.onSnapshot((snapshot) => {
let changes = snapshot.docChanges();
changes.forEach((change) => {
if (change.type === "added") {
// Get the new photo
const photo = change.doc.data();
// Add the photo to the photos list
photosArray.push(photo);
}
});
// The last photo is at the top of the list
setPhotos(photosArray);
But when I render the list of photos, they are unsorted... For example: the first one taken 2 hours ago, the second one taken 1 minute ago, and the last one taken 2 years ago.
UPDATE
This is how I store the date in firestore
Firebase.js:
getTimestamp = () => firebase.firestore.FieldValue.serverTimestamp();
PhotoUploader.js
await firestore
.collection("photos")
.doc(userId)
.collection("userPhotos")
.add({
id,
date: firebase.getTimestamp(),
});
If your date field shows a map with two nested fields, that is not really a timestamp, and it won't sort the way you expect. You should take a look at the code that adds the date field to the document, and make sure it uses a timestamp correctly. Either that, or use a single timestamp numeric value that will sort the way you expect.

Sequelize query broke down

I revisited a query I wrote a couple days ago and, as it should, it miraculously broke down.
It mostly consists of this:
...
let filter = req.body.filter;
let filter_value = req.body.filter_value;
let filter_from = req.body.filter_from;
let filter_to = req.body.filter_to;
let filterSystem = (filter === "createdAt") ?
{[filter]:{
[Op.or]:[
filter_value,
{[Op.between]:[filter_from,filter_to]},
{[Op.between]:[filter_from,filter_value]},
{[Op.between]:[filter_value,filter_to]}
]
}}
: (filter === "favorites") ?
{[filter]:filter_value}
: {};
let results = await Promise.all([
await models.bookmark.findAll({
attributes:[
'guid',
'link',
'createdAt',
'description',
'favorites'
],
...
where:filterSystem
})
])
res.json({
length: results[0].length,
data: results[0]
})
...
Using Postman to input data to the body, I can see the query go through in the console:
If I input "filter_from" and "filter_value" dates and leave out everything but the [filter_from,filter_value] block -
Executing (default): SELECT `guid`, `link`, `createdAt`, `description`, `favorites` FROM `bookmarks` AS `bookmark` WHERE `bookmark`.`createdAt` BETWEEN '2020-04-30T15:34:48.877Z' AND '2020-04-30T15:37:00.170Z' ORDER BY `bookmark`.`createdAt` ASC LIMIT 0, 50;
All it returns is an empty array.
If all of the search options are present -
SELECT `guid`, `link`, `createdAt`, `description`, `favorites` FROM `bookmarks` AS `bookmark` WHERE (`bookmark`.`createdAt` BETWEEN '2020-04-30T15:34:48.877Z' AND NULL OR `bookmark`.`createdAt` BETWEEN '2020-04-30T15:34:48.877Z' AND '2020-04-30T15:37:00.170Z' OR `bookmark`.`createdAt` BETWEEN '2020-04-30T15:37:00.170Z' AND NULL) ORDER BY `bookmark`.`createdAt` ASC LIMIT 0, 50;
It only returns the result matching filter_value, though there should be around 6.
Searching without any filters returns all the 8 results, using "favorites" and "true" for the "filter" and "filter_value" respectively works just fine.
When making the query, I was following this Query by Date Range for a column field in Sequelize
What could go wrong - ISO 8601 got deprecated overnight? But that's the format the base stores all the dates in. Getting no warnings/errors in the console either.
After some tinkering, I dropped [Op.between] altogether and switched to
where:{
[filter]:{
[Op.and]:[
{[Op.gt]:filter_from},
{[Op.lt]:filter_to}
]
}
}
And now it's working.
Just leaving this here, in case someone gets a similar issue.
In the end, I needed objects for comparison, and it seems Op.between doesn't have support for them - but Op.and/or sure do.

Firestore multiple compound indexes

I have a filter component that can filter based on multiple fields. All of the fields are using the equality operator, and I have sorting based on "created" field.
Something along the lines of
let queryBuilder = firebase.firestore()
.collection('myCollection')
.doc(docId)
.collection('myInnerCollection')
.orderBy('created', 'desc');
const {field1, field2, field3} = filter;
if (field1) {
queryBuilder = queryBuilder.where('field1', '==', field1);
}
if (field2) {
queryBuilder = queryBuilder.where('field2', '==', field2);
}
if (field3) {
queryBuilder = queryBuilder.where('field3', '==', field3);
}
For every field I'm required to create a compound index with 'created' field. i.e:
index field1(+) created(-)
index field2(+) created(-)
index field3(+) created(-)
Once I do that, every combination of filtering works. So my question is, if I know that field1 is always populated should I create just two compounded indexes:
index field1(+) field2(+) created(-)
index field1(+) field3(+) created(-)
Or having the 3 indexes is also ok?
Is there any performance/storage penalty of using each one? if so, what is the penalty (assuming I have 100K records in my collection)?

Categories

Resources