Using endsWith filter together with expanding user's memberOf doesn't work - microsoft-graph-api

I want to fetch all users together with their group names from MS Graph API.
It works as long as I don't want to do some advanced filtering (endsWith()).
Here's the select query I'm running:
https://graph.microsoft.com/v1.0/users?$select=displayName,userPrincipalName,mail,id,givenName,surname,memberOf&$expand=memberOf($select=displayName)
It produces a nice set of users together with the display name's of groups they are in.
I wanted to restrict the result to a specific mail domain (i have added ConsistencyLevel: eventual), however I am unable to do it unless I remove the memberOf expand. This works:
https://graph.microsoft.com/v1.0/users?$select=displayName,userPrincipalName,mail,id,givenName,surname,memberOf&$count=true&$filter=endsWith(mail,'#somedomain.com')
But as expected, there are no groups. In fact, there are no groups at all despite selecting memberOf! I thought I'll just fetch groups first and then pair them in my code, but to get them I must expand, and with expand the filter doesn't work!
This produces an Request_UnsupportedQuery error:
https://graph.microsoft.com/v1.0/users?$select=displayName,userPrincipalName,mail,id,givenName,surname,memberOf&$expand=memberOf($select=displayName)&$count=true&$filter=endsWith(mail,'#somedomain.com')
Is there any way to achieve what I want without filtering manually or issuing multiple requests and mapping stuff on my own? We're talking about directories containing tens of thousands of users.. Graph seemed to be nice at the beginning but as I wanted to do anything more complex it turns out to be a limited POS where one query totally excludes another. I was also unable to filter by the expanded memberOf in any way

Based on Microsoft Docs
$expand is not currently supported with advanced queries.

Related

Microsoft Graph API using filter on get sharepoint lists

I'm trying to filter on the sharepoint lists, but the semantics seems to be different to the default semantics.
What I already tried was (with $ and without; single quotes and no quotes):
https://graph.microsoft.com/v1.0/sites/root/lists?filter=name eq 'Something'
https://graph.microsoft.com/v1.0/sites/root/lists?$filter=name eq 'Something'
https://graph.microsoft.com/v1.0/sites/root/lists?filter=id eq 'CFFF1460-B4D7-419C-A921-61B5279BBDDC'
https://graph.microsoft.com/v1.0/sites/root/lists?$filter=id eq 'CFFF1460-B4D7-419C-A921-61B5279BBDDC'
https://graph.microsoft.com/v1.0/sites/root/lists?filter=id eq CFFF1460-B4D7-419C-A921-61B5279BBDDC
https://graph.microsoft.com/v1.0/sites/root/lists?$filter=id eq CFFF1460-B4D7-419C-A921-61B5279BBDDC
But everything returns an array containing all lists and not only the subset matching the desired criteria.
So how can I filter on sharepoint lists?
If you know the id of the list you want to filter and get a response for.
YOu can run a graph API query like this.
https://graph.microsoft.com/v1.0/sites/root/lists/{list-id}
This will give you data about that list.
Let me know if you need further details on this.
Unfortunately (like Marc already mentioned in the comments) it is not possible to filter on SharePoint lists. If you need to, you have to do it on the client side, by reading the list without any filter and make a LINQ statement (or something similar) on the received collection. Be aware that (like all collections in Graph) you don't get always all elements at once. Potentially you have to call the next link request from the last response and wade through more requests and elements till you find what you need.
So when you found what you need, it is a good idea to store the list id within a memory cache or Redis cache for faster lookups the next time, you need this information.

Visual studio online query items without tags

When are working with various Work Item types (user stories,Tasks, etc) we assign tags to reference the area of work. This makes it easy to filter the Backlogs items view to find any related stories.
I would like to build a query to identify Work Items where the tags have not yet been assigned.
I know this can be achieved using excel and filtering, however I specifically would like to do this using the queries. Is this possible??
Thanks in advance for any assistance.
Firstly, I have to say that it is not possible to create work item query to show work items which don't contain tags. As you see that the Operator for Tags is Contains or Does Not Contain, it is not possible to use these two operators to filter out these non-tagged work items.
Secondly, as you have more than 100 tags, it is not an effective way to use "Does Not Contain" operator to exclude all tagged work items.
So,
How about you adding a 'Null' tag to all non-tagged work items to specify that these work items don't have any tags? With this approach, you can create a work item query with Tags Contains Null to list these non-tagged work items.
If you don't want to take this approach, you need to work with excel just as you mentioned above, or take Dave's advice to work with API.

Get page number for an item in ElasticSearch results

I have an alphabetized index of people. My goal is to find what page of that index a person is listed on. For instance, "Tim Curry" might be listed on page 5 of the "T" section. Currently I'm getting the page number with ActiveRecord; Elasticsearch results are 20 per page, so I can work out the page number based on the index. But it seems wiser to get the page number directly from Elasticsearch if at all possible to ensure that I'm getting the right page. Is there a way to get this data from ES?
def page_index
letter= self.name[0].downcase
index=Person.where("lower(name) like?", "#{letter}%").order("lower(name)").pluck(:id).index(self.id)
page=index/20 + 1
end
This functionality does not come bundled with ElasticSearch. Using the results per page and index is the correct approach if that is the functionality you are looking for.
Since it's not clear exactly which document you need, or what the overall UX you are trying to achieve is, I would keep in mind you can always search your index(ces) for a specific document via various mean (filtered query term on name if you need "Tim Curry", id or _uid etc.).
Also ES is a full-text based search client, finding one Object and it's properties might be better served via a database call.
Again this is slightly heresay, as I don't know what exactly you need or are trying to achieve overall, however finding the page of a specific result in your set of returned results is best down via accessing index in your results and simple math.

Parsing Wikipedia countries, regions, cities

Is it possible to get a list of all Wikipedia countries, regions and cities with relations between them? I couldn't find any API appropriate for this task.
What is be the easiest way to parse all the information I need?
PS: I know, that there are another datasources I can get this information from. But I am interested in Wikipedia...
[2020 update] this is now best done using the Wikidata Query Service, you can run super specific queries with a bit of SPARQL, example: Find all countries and their label. See Wikidata Query Help
It might be a bit tedious to get the whole graph but you can get most of the data from the experimental/non-official Wikidata Query API.
I suggest the following workflow:
Go to an instance of the kind of entities you want to work with, say Estonia (Q191) and look for its instance of (P31) properties, you will find: country, sovereign state, member of the UN, member of the EU, etc.
Use the Wikidata Query API claim command to output every entity
that as the chosen P31 property. Lets try with country (Q6256):
http://wdq.wmflabs.org/api?q=claim[31:6256]
It outputs an array of numeric ids: that's your countries! (notice that the result is still incomplete as there are only 141 items found: either countries are missing from Wikidata, or, as suggested by Nemo in comments, some countries are to be found in country (Q6256) subclasses(P279))
You may want more than ids though, so you can ask Wikidata Official API for entities data:
https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q16&format=json&props=labels|claims&languages=en|fr
(here Canada(Q16) data, in json, with only claims and labels data, in English and French. Look at the documentation to adapt parameters to your needs)
You can query multiple entities at a time, with a limit of 50, as follow:
https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q16|Q17|Q20|Q27|Q28|Q29|Q30|Q31|Q32|Q33|Q34|Q35|Q36|Q37|Q38|Q39|Q40|Q41|Q43|Q45|Q77|Q79|Q96|Q114&format=json&props=labels|claims&languages=en|fr
From every countries data, you could look for entities registered as administrative subdivisions (P150) and repeat on those new entities.
Aternatively, you can get all the tree of administrative subdivisions with the tree command. For instance, for France(Q142) that would be http://wdq.wmflabs.org/api?q=tree[142][150] Tadaaa, 36994 items! But that's way harder to refine given the different kinds of subdivision you can encounter from a country to another. And avoid doing this kind of query from a browser, it might crash.
You now just have to find cities by countries by refining this last query with the claim command, and the appropriate sub-class(P279) of municipality(Q15284) entity (all available here): for France, that's commune (Q484170), so your request looks like
http://wdq.wmflabs.org/api?q=tree[142][150] AND claim[31:484170]
then repeat for all the countries: have fun!
You should go with Wikidata and/or dbpedia.
Personally I'd start with Wikidata as it's directly using MediaWiki, with the same API so you can use similar code. I would use pywikibot to get started. Like that you can still request pages from Wikipedia where that makes sense (e.g. list pages or categories).
Here's a nice overview of ways to access Wikidata

Building a (simple) twitter-clone with CouchDB

I'm trying to build a (simple) twitter-clone which uses CouchDB as Database-Backend.
Because of its reduced feature set, I'm almost finished with coding, but there's one thing left I can't solve with CouchDB - the per user timeline.
As with twitter, the per user timeline should show the tweets of all people I'm following, in a chronological order. With SQL it's a quite simple Select-Statement, but I don't know how to reproduce this with CouchDBs Map/Reduce.
Here's the SQL-Statement I would use with an RDBMS:
SELECT * FROM tweets WHERE user_id IN [1,5,20,33,...] ORDER BY created_at DESC;
CouchDB schema details
user-schema:
{
_id:xxxxxxx,
_rev:yyyyyy,
"type":"user",
"user_id":1,
"username":"john",
...
}
tweet-schema:
{
"_id":"xxxx",
"_rev":"yyyy",
"type":"tweet",
"text":"Sample Text",
"user_id":1,
...
"created_at":"2011-10-17 10:21:36 +000"
}
With view collations it's quite simple to query CouchDB for a list of "all tweets with user_id = 1 ordered chronologically".
But how do I retrieve a list of "all tweets which belongs to the users with the ID 1,2,3,... ordered chronologically"? Do I need another schema for my application?
The best way of doing this would be to save the created_at as a timestamp and then create a view, and map all tweets to the user_id:
function(doc){
if(doc.type == 'tweet'){
emit(doc.user_id, doc);
}
}
Then query the view with the user id's as keys, and in your application sort them however you want(most have a sort method for arrays).
Edited one last time - Was trying to make it all in couchDB... see revisions :)
Is that a CouchDB-only app? Or do you use something in between for additional buisness logic. In the latter case, you could achieve this by running multiple queries.
This might include merging different views. Another approach would be to add a list of "private readers" for each tweet. It allows user-specific (partial) views, but also introduces the complexity of adding the list of readers for each new tweet, or even updating the list in case of new followers or unfollow operations.
It's important to think of possible operations and their frequencies. So when you're mostly generating lists of tweets, it's better to shift the complexity into the way how to integrate the reader information into your documents (i.e. integrating the readers into your tweet doc) and then easily build efficient view indices.
If you have many changes to your data, it's better to design your database not to update too many existing documents at the same time. Instead, try to add data by adding new documents and aggregate via complex views.
But you have shown an edge case where the simple (1-dimensional) list-based index is not enough. You'd actually need secondary indices to filter by time and user-ids (given that fact that you also need partial ranges for both). But this not possible in CouchDB, so you need to work around by shifting "query" data into your docs and use them when building the view.

Resources