MQL Monday: Nested containment

Let’s say you want to find all the Freebase users who live in the USA, but they have their locations set to things like “San Francisco” or “New Orleans”. How do you ask for “USA, and anything within the USA”?

While MQL doesn’t currently support transitive closure, the /location/ location/containedby property model was designed to make the type of query you are attempting to write possible. When creating /location/ location topics the idea is to provide (at least) two levels of hierarchical containment in the containedby property. For example, in the US, cities should specify what county they are in as well as what state they are contained in.

As your query indicates universities should be co-typed as locations and should specify the city they are contained in. Using the two- level of hierarchy containment model, cities should be contained in states, and similarly states should be contained in the US. Therefore, you should be able to get what you are looking for with the query:

Note: the ID for the US is /guid/9202a8c04000641f8000000000959f60

  {
     "query":[{
       "/location/location/containedby":[{
         "/location/location/containedby":[{
           "/location/location/containedby":[{
             "id":"/guid/9202a8c04000641f8000000000959f60"
           }],
           "name":null
         }],
         "name":null
       }],
       "name":null,
       "type":"/education/university"
     }]
  }

Someday, MQL will probably handle transitivity operations – but until then it is possible to construct queries which utilize the containment models based on some minimal assumptions about the data.

This tip came from the MQL Cookbook: Perform transitive queries

One Response to “MQL Monday: Nested containment”

  1. John Bäckstrand Says:

    Cool, I wanted to do this for my Toys domain for listing things by country, even when they have more specifici locations.

About

Freebase is a free database of the world's information. This is the official Freebase blog.