Sparql将整个对象分组到列表中

时间:2019-07-16 10:56:54

标签: sparql

我有一个餐厅列表,并希望将它们按两个标识符归类在列表中。响应将在JSON中显示以下内容:

[
    {
        "restaurantChainId": "1",
        "restaurantTypeId": "001",
        "restaurantList": [
            {
                "restaurantId": "1-1",
                "restaurantChainId": "1",
                "restaurantTypeId": "001",
                "restaurant name": "Pizza Place London"
                "restaurant location": "London"
            },
            {
                "restaurantId": "1-2",
                "restaurantChainId": "1",
                "restaurantTypeId": "001",
                "restaurant name": "Pizza Place Birmingham"
                "restaurant location": "Birmingham"
            }
        ]
    },
    {
        "restaurantChainId": "1",
        "restaurantTypeId": "002",
        "restaurantList": [
            {
                "restaurantId": "1-3",
                "restaurantChainId": "1",
                "restaurantTypeId": "002",
                "restaurant name": "Pizza Place Manchester"
                "restaurant location": "Manchester"
            }
        ]
    },
    {
        "restaurantChainId": "2",
        "restaurantTypeId": "002",
        "restaurantList": [
            {
                "restaurantId": "2-1",
                "restaurantChainId": "2",
                "restaurantTypeId": "002",
                "restaurant name": "Taco Town London"
                "restaurant location": "London"
            },
            {
                "restaurantId": "2-2",
                "restaurantChainId": "2",
                "restaurantTypeId": "002",
                "restaurant name": "Taco Town Newcastle"
                "restaurant location": "Newcastle"
            }
        ]
    }
]

因此数组中的对象具有一个列表,其中列表中的每个共享restaurantChainIdrestaurantTypeId

我尝试了以下group_concat查询:

SELECT ?restaurantChainId (GROUP_CONCAT(?restaurantTypeId; SEPARATOR=", ") AS ?restaurantTypeIdList)
WHERE {
  ?restaurant <http://example.com/terms#docType> "restaurant" ;
       <http://example.com/terms#restaurantTypeId> ?restaurantTypeId;
       <http://example.com/terms#institutionFid> ?restaurantChainId .
  FILTER (strstarts(str(?restaurantTypeId), '00'))
} GROUP BY ?restaurantChainId

但是显然,这只是为每个restaurantTypeId生成一个列表restaurantChainId。因此,它已正确地对事物进行了分组。我把事情归为正确的组,但我只拥有我实际上想要该餐厅的所有相关信息的每个餐厅的ID列表。我真的不知道该怎么做。可能吗?

改进的查询

SELECT ?restaurantChainId  (GROUP_CONCAT(DISTINCT ?restaurantTypeId; SEPARATOR=", ") AS ?restaurantTypeIdList) (GROUP_CONCAT(?restaurant; SEPARATOR=", ") AS ?restaurant)
WHERE {
  ?restaurant <http://example.co.uk/terms#docType> "restaurant" ;
       <http://example.co.uk/terms#swiftbic> ?restaurantTypeId;
       <http://example.co.uk/terms#institutionFid> ?restaurantChainId ;
       <http://example.co.uk/terms#fid> ?officeId .

  FILTER (strstarts(str(?restaurantTypeId), 'ABA'))
} GROUP BY ?restaurantChainId ?restaurantTypeId

这是当前结果:

{
  "head" : {
    "vars" : [ "restaurantChainId", "restaurantTypeIdList", "restaurant" ]
  },
  "results" : {
    "bindings" : [ {
      "restaurantChainId" : {
        "type" : "literal",
        "value" : "XYZ"
      },
      "restaurantTypeIdList" : {
        "type" : "literal",
        "value" : "ABA3"
      },
      "restaurant" : {
        "type" : "literal",
        "value" : "http://example.co.uk/restaurant/XYZ-4, http://example.co.uk/restaurant/XYZ-5, http://example.co.uk/restaurant/XYZ-6"
      }
    }, {
      "restaurantChainId" : {
        "type" : "literal",
        "value" : "ABC"
      },
      "restaurantTypeIdList" : {
        "type" : "literal",
        "value" : "ABA1"
      },
      "restaurant" : {
        "type" : "literal",
        "value" : "http://example.co.uk/restaurant/ABC-14, http://example.co.uk/restaurant/ABC-15, http://example.co.uk/restaurant/ABC-4"
      }
    }, {
      "restaurantChainId" : {
        "type" : "literal",
        "value" : "XYZ"
      },
      "restaurantTypeIdList" : {
        "type" : "literal",
        "value" : "ABA1"
      },
      "restaurant" : {
        "type" : "literal",
        "value" : "http://example.co.uk/restaurant/XYZ-2, http://example.co.uk/restaurant/XYZ-1"
      }
    }, {
      "restaurantChainId" : {
        "type" : "literal",
        "value" : "XYZ"
      },
      "restaurantTypeIdList" : {
        "type" : "literal",
        "value" : "ABA2"
      },
      "restaurant" : {
        "type" : "literal",
        "value" : "http://example.co.uk/restaurant/XYZ-3"
      }
    }, {
      "restaurantChainId" : {
        "type" : "literal",
        "value" : "ABC"
      },
      "restaurantTypeIdList" : {
        "type" : "literal",
        "value" : "ABA2"
      },
      "restaurant" : {
        "type" : "literal",
        "value" : "http://example.co.uk/restaurant/ABC-9"
      }
    } ]
  }
}

因此,实际的分组是正确的,我为每家餐厅提供了uri,但我不知道如何获取与它们具体相关的信息。

0 个答案:

没有答案