Sorting Order of Freebase MQL Read Service results - freebase

I am trying to get a list of items using a list of MIDs
Previously I asked this question Array of Freebase MIDs and sending multiple queries to freebase
So my MQL query looks like this now:
[{
"mid": null,
"name": null,
"topics:mid|=":[
"/m/045c7b",
"/m/0d6lp",
"/m/021ympy",
...
]
}]
Sample MQL Query URL
However, the default order seems to be based on something like index or timestamp. I would like the order of the results to mirror the order the MIDs are listed in the query - is this possible? If so, any hints on what the MQL would look like would be awesome :)

Not possible with MQL - you'll have to do the sorting client-side. For what it's worth, the default order is undefined, so don't rely on it for anything.

Related

Firestore query comparing two arrays without array-contains

Is it possible to compare two arrays in a Firestore query without 'array-contains'?
Something like this:
query = query.where(
"info.currentLocation",
"==",
currentUserBasicInfo.currentLocation
);
info.currentLocation and currentUserBasicInfo.currentLocation are both arrays with mapped values. I can't change the data structure and I already have an 'array-contains' in the query. Because the two arrays have to be identical for this to run, I feel like there has to be a way to compare two arrays without having to use 'array-contains' or 'array-contains-any' or 'IN'.
Any suggestions? Is this possible?
The Problem
What you need is something similar to array-contains-all.
So let's say that in firebase you have this:
And then in your code you have something like this:
const usersIDs = [
'q4nemfzg19OCmwm1EauiZwdYD4z1',
'UzmgPRtlRSZX44erF6VrkYQ5Kyf1',
];
Then you want to compare with something like this:
yourCollectionRef.where('users', 'array-contains-all', usersIDs).get()
// or
yourCollectionRef.where('users', '==', usersIDs).get()
But this is currently not possible. The array-contains-all is still a requested feature and the == is not gonna work.
The Solution
So the good news is that I have a solution for you in case you are willing to change your collection structure.
Instead of saving the data in array, save it in a map object and the items would be the keys.
Like this:
And then you can easily use the filter == as you want. Of course you can transform the array into a map object or simply have it like this:
const usersMap = {
q4nemfzg19OCmwm1EauiZwdYD4z1: null,
UzmgPRtlRSZX44erF6VrkYQ5Kyf1: null,
};
And this would be your final query:
yourCollectionRef.where('usersMap', '==', usersMap).get();
Then if you need in the future to get all of the documents from a specific user that's inside the usersMap you can use this query here:
yourCollectionRef.where(`usersMap.${userId}`, '==', null).get();
"array-contains" method has ability to query for elements within arrays[1]. This means you can keep your elements as an array, and easily query for them without having to resort to the map hack.
If you don't want to use this method one alternative could be iterate both arrays until match the values as you need.
However, in this document[2] you can find multiple examples for perform simple and compound queries.
[1] https://firebase.google.com/docs/firestore/query-data/queries
[2] https://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html

How to query for list of properties in a document

I will be receiving flat documents that will have slightly different schemas.
For example:
{
"FirstName": "Jim",
"LastName: "Bob"
}
And another one, that would simply have:
{
"FullName": "Jim Bob"
}
Is it possible to query the Person collection to retrieve the list of unique properties (not the values)?
[
"FirstName",
"LastName",
"FullName"
]
According to my research, it is not supported in cosmos db query syntax so far. You could refer to this similar feedback and adopt the suggestions from cosmos db team.
Also, I think you could get all the names of properties by below coding workaround.
Create and init a hashmap.
Query the documents and get the results array.
Loop the array and convert every json to map.
Push the elements into initial hashmap to make sure the list of properties is unique.

Freebase query - 10000 most popular people

I am looking for a way to construct a freebase mql query that will return a list of names of popular ('commonly searched on google') people.
Currently, if I do a simple query like:
{
"type" : "people/person",
"name" : [],
"limit" : 5
}
I get
"Jack Abramoffa"
"Bob Ney"
"David Safavian"
"Kåre Kristiansen"
"Adam Murimuth"
Is there a way to modify the query in a way that will sort the elements by their google search rank, or any other measure of popularity?
You can't do it via MQL, but the Freebase Search API returns topics in ranked fashion. The default scoring algorithm uses how well linked a topic is https://developers.google.com/freebase/v1/search-cookbook#scoring-and-ranking. Google doesn't provide search query popularity rankings through any of the Freebase APIs.

Getting wiki summary in freebase

Here is a tv show query. How do I get the request to pull in the wiki summary?
[{
"id" : "/m/0d68qy",
"name": null
}]​
The topic summary is linked to a topic via the /common/topic/article property. You can retrieve this data in one API call using the new Topic API like this:
https://www.googleapis.com/freebase/v1/topic/m/0d68qy?filter=/common/topic/article

freebase - get description of person

I have the following MQL query which successfully returns the record for William Shakespeare.
[{
"/type/object/name": null,
"/type/object/id": "/en/william_shakespeare"
}]​
http://tinyurl.com/cnpma3f
I am trying to get the description attribute. When I add "description": null, I get a 'no description attribute found' error. Yet, looking at the record in freebase, it should be there:
http://www.freebase.com/experimental/topic/standard/en/william_shakespeare
The topic API aggregates results across a number of Freebase services; the descriptions are stored separately from the other data about a topic and you'll have to use the text service to fetch them, rather than MQL directly.
As an aside, you should probably consider changing to the new Freebase APIs; those hosted at (www|api).freebase.com are deprecated and will (allegedly) be turned off in October.

Resources