7

I'm trying to write an MQL query that filters out null values.

The query I have now (can be executed using the MQL Query Editor):

[
  {
    "/common/topic/image" : [
      {
        "id" : null
      }
    ],
    "article" : [
      {
        "content" : null
      }
    ],
    "name" : "bill gates",
    "type" : "/common/topic"
  }
]

The results I am getting:

[
  {
    "/common/topic/image" : [
      {
        "id" : "/guid/9202a8c04000641f8000000004fb4c01"
      },
      {
        "id" : "/wikipedia/images/commons_id/4486276"
      }
    ],
    "article" : [
      {
        "content" : null
      },
      {
        "content" : "/guid/9202a8c04000641f800000000903535d"
      }
    ],
    "name" : "Bill Gates",
    "type" : "/common/topic"
  }
]

I'm trying to figure out how I can filter out the "content" : null match in the "article" array at query time. I looked through the MQL documentation but I didn't see a clear way to do this.

Eric Schoonover
  • 47,184
  • 49
  • 157
  • 202

1 Answers1

10

To filter out articles that don't have any content assigned to them you'll have to expand the content id attribute and set the optional directive to false.

[
  {
    "/common/topic/image" : [
      {
        "id" : null
      }
    ],
    "article" : [
      {
        "content" : {
          "id" : null,
          "optional" : false
        }
      }
    ],
    "name" : "bill gates",
    "type" : "/common/topic"
  }
]

This will give you the following result:

[
  {
    "/common/topic/image" : [
      {
        "id" : "/guid/9202a8c04000641f8000000004fb4c01"
      },
      {
        "id" : "/wikipedia/images/commons_id/4486276"
      }
    ],
    "article" : [
      {
        "content" : {
          "id" : "/guid/9202a8c04000641f800000000903535d"
        }
      }
    ],
    "name" : "Bill Gates",
    "type" : "/common/topic"
  }
]

For more information about using the optional directive see the documentation here.

Shawn Simister
  • 4,613
  • 1
  • 26
  • 31
  • 2
    Who are you and how to you know so much about MQL? ;) – Eric Schoonover Mar 24 '09 at 19:05
  • Haha, I'm just a member of the Freebase community who has been hacking around in MQL for a year or so. I'm happy to be able to help people learn the ropes and hopefully get more developers building applications using the Freebase data. – Shawn Simister Mar 24 '09 at 19:20
  • when I switch the optional flag to false, it tells me: "Query too difficult." – fandang May 08 '15 at 15:35