0

I have a long query in Sparql and and I want to order results by numeric field "sentiment" Everything is working fine if I include the field in SELECT and GROUPBY, otherwise is crashing. But I would like to render the query without showing the field "sentiment" like in a SQL query. Is this posible in SPARQL?

The query is:

SELECT  ?Sentiment ?Restaurant ?Name ?Address ?Score ?City ?State (SAMPLE(?photo) as ?image) SAMPLE(?Text) as ?text) (SAMPLE(?Review) as ?review) (SAMPLE(?TStyle) as ?style) (SAMPLE(?TAmbiance) as   ?ambient) (SAMPLE(?TService) as ?service)
where { ?Restaurant :hasName ?Name;
                :Dish ?Dish;
                :Score ?Score;
                :hasAddress ?Address;
                :photo ?photo;
                :HasReview ?Review;
                :Parking ?Parking;
                :CreditCard ?CreditCard;
                :Delivery ?Delivery;
                :Kids ?Kids;
                   :hasTopic ?o;
                   :Sentiment ?Sentiment.
   ?Address :hasCity ?City;
            :hasState ?State;
            :Lat ?Lat;
            :Long ?Long.
   ?Review :Text ?Text.
   BIND (exists{?Restaurant :hasTopic :style} AS ?TStyle).              
   BIND (exists{?Restaurant :hasTopic :ambiance} AS ?TAmbiance).
   BIND (exists{?Restaurant :hasTopic :service} AS ?TService).

   FILTER(contains(str(?Text), "sushi"))
   } GROUP BY ?Restaurant ?Name ?Address ?Score ?City ?State ?Sentiment
   ORDER BY DESC((?Sentiment))

How I could form the query and order the results without showing the field "sentiment" itself or improve the query in general?

Thanks.

TallTed
  • 9,069
  • 2
  • 22
  • 37
terseason
  • 80
  • 9
  • why can't you just remove `?Sentiment` from the `SELECT` part then? – UninformedUser Dec 19 '19 at 11:52
  • Then the query is not ordering the results properly. – terseason Dec 19 '19 at 12:22
  • 1
    What is your SPARQL engine? This sounds like a bug. Possibly the bug is triggered by the doubled parentheses in your `ORDER BY DESC((?Sentiment))` -- so I'd try changing that to `ORDER BY DESC(?Sentiment)` (and removing `?Sentiment` from the `SELECT`). – TallTed Dec 19 '19 at 14:43
  • I'm using Allegrograph. Yes I already tried to quit the parenthesis and select, and the ordering is not correct. – terseason Dec 19 '19 at 14:58
  • Before adding SAMPLE(?Text) as ?text) is working fine. The problem comes when adding GROUPBY. – terseason Dec 19 '19 at 16:19
  • I agree with @TallTed - this sounds like a bug. Perhaps you should reach out to the Allegrograph developers. – Jeen Broekstra Dec 19 '19 at 22:19
  • I notice you're missing an open-paren in the `SELECT`... `SAMPLE(?Text) as ?text)` should be `(SAMPLE(?Text) as ?text)`. Also, you said before adding `?text`, things worked fine -- but then you said the problem arose when adding `GROUP BY` (i.e., not when adding the `?text`) -- so perhaps you should show the query that works, and the changed query that fails, so we can see just what changes you're making. – TallTed Dec 19 '19 at 22:26

2 Answers2

1

Thanks for the report. This issue of ORDER BY potentially going wrong if the variable is not projected, is a known AllegroGraph issue (bug25806) that will be fixed in the upcoming 7.0 release.

0

While I don't know the specifications of SPARQL enough to decide whether this ever makes sense outside of a bug workaround, you can always "mask" results by wrapping your query as a SPARQL 1.1 subquery:

SELECT ?var1 ?var2 ... ?varn
{
 ...
}

"masking" ?varx becomes

SELECT ?var1 ?var2 ... ?var(x-1) ?var(x+1) ... ?varn
{
 SELECT ?var1 ?var2 ... ... ?varn
 {
  ...
 }
}
Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118