1

I don't have much experience with rdf sparql, only on occasion I do something very simple. Here are 2 samples of my data:

1 Sample:

@prefix ew: <http://ew.com/content/> .    
@prefix sesame: <http://www.openrdf.org/schema/sesame#> .

ew:50467332 ew:name "He resigns, citing PTSD"^^xsd:token ;
        ew:section "health"^^xsd:token ;    
        dc:isPartOf <http://ew.go.com/Health> ;
        dc:created "2018-01-08T13:54:14.340-08:00"^^xsd:dateTime ;
        dc:date "2018-01-08T18:23:47.000-05:00"^^xsd:dateTime ;
        dc:identifier "50467332"^^xsd:int ; 
        dc:modified "2018-01-08T15:24:01.547-08:00"^^xsd:dateTime .

2 Sample:

@prefix ew: <http://ew.com/content/> .
@prefix sesame: <http://www.openrdf.org/schema/sesame#> .

ew:50477535 ew:name "Having a Baby in Your 40s"^^xsd:token ;
        ew:section "agm"^^xsd:token ;   
        dc:isPartOf <http://ew.go.com/agm/Wellness> ;
        dc:created "2018-01-09T08:32:57.047-08:00"^^xsd:dateTime ;
        dc:date "2016-08-18T09:52:00.000-04:00"^^xsd:dateTime ; 
        dc:identifier "50477535"^^xsd:int ;
        dc:modified "2018-01-09T08:32:57.047-08:00"^^xsd:dateTime .

I have rdf triples where ew:section can be of many variations, but I'm interested in finding only these 2 variations, namely:

ew:section "health"^^xsd:token ;    
   and 
ew:section "agm"^^xsd:token ;   

The query I'm testing right now has them both in OPTIONAL blocks, but the result I get back is a mixture of all sorts of "section", and the ones I'm interested in. How can I look for only those 2 section? I understand that I'm getting unwanted data because I have my sections in "OPTIONAL", but I'm not sure how to make both of them equally "important". I'm attempting to use UNION at the moment, but without positive results. Much appreciate your time. Here is my query:

PREFIX dc:     <http://purl.org/dc/terms/>
PREFIX mrss:   <http://search.yahoo.com/mrss/>
PREFIX search: <http://www.openrdf.org/contrib/lucenesail#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX ew: <http://ew.com/content/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT ?id
WHERE{
        ?sub dc:identifier ?id;
                dc:date ?date;
                search:matches ?match.

        ?match  dc:date ?dateSearch;                
                search:max "50";
                search:sort search:order.

        OPTIONAL {?sub news:section "agm"^^xsd:token;
                        dc:isPartOf <http://ew.go.com/agm/Wellness>.}       
        OPTIONAL {?sub $sectionClause.}         
}
ORDER BY DESC(?date)
user2917629
  • 883
  • 2
  • 15
  • 30
  • [`UNION`](https://www.w3.org/TR/sparql11-query/#alternatives), [`VALUES`](https://www.w3.org/TR/sparql11-query/#inline-data) or [`FILTER`](https://www.w3.org/TR/sparql11-query/#expressions). – Stanislav Kralin Jan 09 '18 at 19:16
  • Possible duplicate of [Alternatives to SPARQL query with lots of UNIONs](https://stackoverflow.com/questions/15035337/alternatives-to-sparql-query-with-lots-of-unions) – Stanislav Kralin Jan 09 '18 at 19:22
  • @stanislav-kralin With Union I have {?match ... }UNION{?match...} and I get 0 results :( – user2917629 Jan 09 '18 at 19:36
  • Try putting your `UNION` query into your question. The partial you put in a comment isn't sufficient for useful advice, but it looks like you've used incorrect syntax for SPARQL and/or for your desired query. – TallTed Jan 09 '18 at 19:49
  • `OPTIONAL` is a left-join and not used as an alternative. As StanislavKralin said, use one of the other constructs. Moreover, why do you apply fulltext-search here when you know both literals? use `?sub dc:identifier ?id; dc:date ?date; news:section ?section . FILTER( ?section IN ("agm"^^xsd:token, "health"^^xsd:token) )` – UninformedUser Jan 10 '18 at 08:41

1 Answers1

0

What worked best for me is to use 'OR', like this:

?match ew:section "health OR http\\\\:\\\\/\\\\/purl.org\\\\/dc\\\\/terms\\\\/isPartOf:http\\\\:\\\\/\\\\/ew.go.com\\\\/agm\\\\/Wellness"^^xsd:token;  
  • without OPTIONAL and without UNION, this is the cleanest so far.
user2917629
  • 883
  • 2
  • 15
  • 30
  • That's non-standard SPARQL for sure. No need for this, fulltext-search is overkill as you know the literals that should be used for matching. – UninformedUser Jan 10 '18 at 08:42
  • I dont know the literals, the ones I use in samples are only samples for tests, also it is only a very small part of my query that I simplified to test what I need. @AKSW – user2917629 Jan 10 '18 at 17:32
  • Ok, that wasn't clear from your question. Indeed then you need either REGEX (SPARQL built-in) or some fulltext feature (not SPARQL built-in). Indeed what you have is a Lucene query and should be fine. – UninformedUser Jan 11 '18 at 08:09