BlueBrain / nexus-forge

Building and Using Knowledge Graphs made easy
https://nexus-forge.readthedocs.io
GNU Lesser General Public License v3.0
39 stars 19 forks source link

Error When Using forge.search / forge.sparql #182

Closed tobiasschweizer closed 3 years ago

tobiasschweizer commented 3 years ago

Hi

I encountered a problem when using forge.search / forge.sparql. The request is rejected and the following errors are returned: HTTPError: 400 Client Error: Bad Request for url: https://***defaultSparqlIndex/sparql and QueryingError: 400 Client Error: Bad Request for url: https://***defaultSparqlIndex/sparql.

The URL seems valid: it consists of the server URL, the projects, and contains the endpoint config from the yml file (url encoded). From the admin interface, I am able to run the queries without any problem and they are sent to the same endpoint.

Example: forge.sparql("SELECT ?s ?p ?o WHERE {?s ?p ?o} LIMIT 20")

---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
/opt/tljh/user/lib/python3.7/site-packages/kgforge/specializations/stores/bluebrain_nexus.py in _sparql(self, query, limit, offset)
    403                 self.service.sparql_endpoint, data=query, headers=self.service.headers_sparql)
--> 404             response.raise_for_status()
    405         except Exception as e:

/opt/tljh/user/lib/python3.7/site-packages/requests/models.py in raise_for_status(self)
    939         if http_error_msg:
--> 940             raise HTTPError(http_error_msg, response=self)
    941 

HTTPError: 400 Client Error: Bad Request for url: https://***defaultSparqlIndex/sparql

During handling of the above exception, another exception occurred:

QueryingError                             Traceback (most recent call last)
<ipython-input-38-25a4c77e3117> in <module>
----> 1 forge.sparql("SELECT ?s ?p ?o WHERE {?s ?p ?o} LIMIT 20")

/opt/tljh/user/lib/python3.7/site-packages/kgforge/core/commons/execution.py in wrapper(*args, **kwargs)
     62 
     63         try:
---> 64             return fun(*args, **kwargs)
     65         except Exception as e:
     66             stack = traceback.extract_stack()

/opt/tljh/user/lib/python3.7/site-packages/kgforge/core/forge.py in sparql(self, query, debug, limit, offset)
    336     def sparql(self, query: str, debug: bool = False, limit: int = 100,
    337                offset: Optional[int] = None) -> List[Resource]:
--> 338         return self._store.sparql(query, debug, limit, offset)
    339 
    340     @catch

/opt/tljh/user/lib/python3.7/site-packages/kgforge/core/archetypes/store.py in sparql(self, query, debug, limit, offset)
    254             print(*["Submitted query:", *qr.splitlines()], sep="\n   ")
    255             print()
--> 256         return self._sparql(qr, limit, offset)
    257 
    258     def _sparql(self, query: str, limit: int, offset: int) -> List[Resource]:

/opt/tljh/user/lib/python3.7/site-packages/kgforge/specializations/stores/bluebrain_nexus.py in _sparql(self, query, limit, offset)
    404             response.raise_for_status()
    405         except Exception as e:
--> 406             raise QueryingError(e)
    407         else:
    408             data = response.json()

QueryingError: 400 Client Error: Bad Request for url: https://***defaultSparqlIndex/sparql

Version used: nexusforge 0.6.2

I'd be grateful for any hint. Thanks a lot!

MFSY commented 3 years ago

Hi @tobiasschweizer , Which version of Nexus Delta store are you using/hitting ? v1.4 or greater ?

If you are Nexus Delta 1.4 then set the following SPARQL endpoint config (within the store config section):

searchendpoints:
    sparql:
      endpoint: "https://bluebrain.github.io/nexus/vocabulary/defaultSparqlIndex"
      Content-Type: application/sparql-query
      Accept: application/ld+json

From Nexus Delta 1.5 the content-type and Accept headers for SPARQL have changed. You also might want to adapt the endpoint to be the SPARQL view URL you want to target.

tobiasschweizer commented 3 years ago

Hi @MFSY

Thanks a lot for your reply.

It's

Nexus Delta v Nexus Fusion v1.5.1

Here's my config:

Store:
  name: BlueBrainNexus
  searchendpoints:
    sparql:
      endpoint: "https://bluebrain.github.io/nexus/vocabulary/defaultSparqlIndex"

From the Nexus admin web interface (SPARQL query editor), my SPARQL queries work fine. The URL is identical: https://server/v1/views/org/proj/https%3A%2F%2Fbluebrain.github.io%2Fnexus%2Fvocabulary%2FdefaultSparqlIndex/sparql.

Still I am getting a 400 when trying it with forge.search or forge.sparql.

Anything I should change in the config? Thanks a lot!

annakristinkaufmann commented 3 years ago

Hi @tobiasschweizer !

You could try either removing the LIMIT from the query itself and instead provide it as separate argument or set the limit argument of the forge.sparql() method to None:

forge.sparql("SELECT ?s ?p ?o WHERE {?s ?p ?o}", limit=20) forge.sparql("SELECT ?s ?p ?o WHERE {?s ?p ?o} LIMIT 20", limit=None)

To see the query that is being run, you can use debug=True on forge.sparql():

forge.sparql("SELECT ?s ?p ?o WHERE {?s ?p ?o}", limit=20, debug=True)

We have opened an issue to allow the LIMIT on the query itself without having to set the limit argument to None: https://github.com/BlueBrain/nexus-forge/issues/189

tobiasschweizer commented 3 years ago

Hi @annakristinkaufmann

Great, forge.sparql("SELECT ?s ?p ?o WHERE {?s ?p ?o}", limit=20, debug=True) works!

Magically, forge.search also works now :-)

Many thanks!