[Developers] Merge two queries

Christopher R. Maden crism at metaweb.com
Wed Dec 5 15:51:32 UTC 2007


Sebastian Kurt <kurt at inf.fu-berlin.de> wrote:
> Thanks for your help, but it is not what i was looking for. Perhaps not 
> exactly defined by me. I will try again:
> 
> I query for ID's belonging to a <keyword>
> 
> after that (extracting IDs with PHP), I query foreach ID what CATEGORY 
> it is in and whats its name:
> 
> Now the question is, if it is possible to merge both:
> "Query for CATEGORIES (and name of given IDs) of a given <keyword>"

Sure.  Here’s how I built it up.

Query for instances of (literally) “keyword” and their types (with a limit, just for testing):

{
  "someids" : {
    "query" : [
      {
        "limit" : 5,
        "name" : null,
        "name~=" : "keyword",
        "type" : [
          {
            "id" : null
          }
        ]
      }
    ]
  }
}

But since we are primarily interested in the types, turn the query inside-out (here the limits aren’t for testing, they are because we only need at least one instance at each level, and help the query run faster):

{
  "someids" : {
    "query" : [
      {
        "id" : null,
        "instance" : [
          {
            "limit" : 1,
            "name" : null,
            "name~=" : "keyword"
          }
        ],
        "type" : "/type/type"
      }
    ]
  }
}

Now let’s find the domains of those types — but while we’re at it, let’s turn that inside-out, too:

{
  "someids" : {
    "query" : [
      {
        "id" : null,
        "type" : "/type/domain",
        "types" : [
          {
            "instance" : [
              {
                "limit" : 1,
                "name" : null,
                "name~=" : "keyword"
              }
            ],
            "limit" : 1
          }
        ]
      }
    ]
  }
}

Now, what categories hold those domains?

{
  "someids" : {
    "query" : [
      {
        "domains" : [
          {
            "limit" : 1,
            "type" : "/type/domain",
            "types" : [
              {
                "instance" : [
                  {
                    "limit" : 1,
                    "name" : null,
                    "name~=" : "keyword"
                  }
                ],
                "limit" : 1
              }
            ]
          }
        ],
        "id" : null,
        "name" : null,
        "type" : "/freebase/domain_category"
      }
    ]
  }
}

The answer is “System” and “Arts & Entertainment.”

~Chris
-- 
Christopher R. Maden
Data Architect
Metaweb Technologies, Inc.
<URL: http://www.metaweb.com/ >


More information about the Developers mailing list