Find lists containing ALL values in a set?

前端 未结 1 1177
面向向阳花
面向向阳花 2021-01-12 09:23

How can I find lists that contain each of a set of items in SPARQL? Let\'s say I have this data:

  ( \"         


        
1条回答
  •  执笔经年
    2021-01-12 09:49

    Finding lists with multiple required values

    If you're looking for lists that contain all of a number of values, you'll need to use a more complicated query. This query finds all the ?s values that have a ?list value, and then filters out those where there is not a word that is not in the list. The list of words is specified using a values block.

    prefix rdf: 
    
    select ?s {
      ?s  ?list .
      filter not exists {                   #-- It's not the case
         values ?word { "new" "york" }      #-- that any of the words
         filter not exists {                #-- are not
           ?list rdf:rest*/rdf:first ?word  #-- in the list.
         }
      }
    }
    
    --------------------------
    | s                      |
    ==========================
    |  |
    --------------------------
    

    Find alternative strings in a list

    On the other hand, if you're just trying to search for one of a number of options, you're using the right property path, but you've got the wrong filter expression. You want strings equals to "new" or "york". No string is equal to both "new" and "york". You just need to do filter(?t = "new" || ?t = "york") or better yet use in: filter(?t in ("new", "york")). Here's a full example with results:

    prefix rdf: 
    
    select ?s ?t {
      ?s /rdf:rest*/rdf:first ?t .
      filter(?t in ("new","york"))
    }
    
    -----------------------------------
    | s                      | t      |
    ===================================
    |  | "new"  |
    |  | "york" |
    |   | "new"  |
    -----------------------------------
    

    0 讨论(0)
提交回复
热议问题