Class QueryService

java.lang.Object
ai.chatur.cortex.core.query.QueryService

public class QueryService extends Object
Answers questions about the knowledge graph.

All lookups run against the inference dataset, so results include both approved assertions and statements derived from them by the reasoner. Provenance, which lives in the provenance graph of the assertions dataset and is excluded from inference, is looked up there.

  • Constructor Summary

    Constructors
    Constructor
    Description
    QueryService(org.apache.jena.query.Dataset inferences, org.apache.jena.query.Dataset assertions, org.apache.jena.ontapi.model.OntModel ontModel, UsageService usageService)
    Creates the service.
  • Method Summary

    Modifier and Type
    Method
    Description
    List<ai.chatur.cortex.ProvenancedStatement>
    Returns everything known about a resource, with the creation timestamp of each statement where provenance was recorded.
    List<ai.chatur.cortex.Term>
    Returns the known instances of an ontology class.
    query(String sparql)
    Runs a SPARQL query against the knowledge graph.
    search(String text)
    Finds resources by fuzzy full-text search over their labels.
    List<ai.chatur.cortex.SearchResult>
    Searches the full-text index and returns the matching subjects.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • QueryService

      public QueryService(org.apache.jena.query.Dataset inferences, org.apache.jena.query.Dataset assertions, org.apache.jena.ontapi.model.OntModel ontModel, UsageService usageService)
      Creates the service.
      Parameters:
      inferences - the dataset holding the assertions enriched by inference
      assertions - the dataset holding the approved assertions and their provenance
      ontModel - the ontology model, used to resolve classes and shorten URIs
      usageService - view counts, which weight the ranking of search results
  • Method Details

    • getInstances

      public List<ai.chatur.cortex.Term> getInstances(String type)
      Returns the known instances of an ontology class.
      Parameters:
      type - the local name of the ontology class
      Returns:
      the instance identifiers sorted alphabetically, empty if the class is unknown
    • describe

      public List<ai.chatur.cortex.ProvenancedStatement> describe(String id)
      Returns everything known about a resource, with the creation timestamp of each statement where provenance was recorded.

      The statements come from the inference dataset; their creation timestamps come from the provenance graph of the assertions dataset. Each statement is returned once: statements carrying several provenance records — for example because they were asserted by more than one ingestion — report their earliest creation timestamp.

      Parameters:
      id - the identifier of the resource within the Cortex namespace, or a full URI
      Returns:
      the statements about the resource, sorted by predicate
    • query

      public String query(String sparql)
      Runs a SPARQL query against the knowledge graph.
      Parameters:
      sparql - a SPARQL SELECT, ASK, or DESCRIBE query
      Returns:
      SELECT and ASK results formatted as text, DESCRIBE results serialized in Turtle syntax, or null for other query types
    • search

      public String search(String text)
      Finds resources by fuzzy full-text search over their labels.

      Each term of the input is matched approximately, so small typos and spelling variations still find their target.

      Parameters:
      text - the text to search for
      Returns:
      the matches with their relevance scores, formatted as text and ranked best first
    • searchSubjects

      public List<ai.chatur.cortex.SearchResult> searchSubjects(String text)
      Searches the full-text index and returns the matching subjects.

      Longer terms are matched approximately, so small typos and spelling variations still find their target. Every term must occur in the same indexed literal, so adding a word narrows the results.

      The index holds one document per literal, so a resource matching on both its label and its comment produces several hits; each resource is reported once, keeping its best-scoring match.

      Parameters:
      text - the text to search for
      Returns:
      the matching subjects ranked best first, empty if nothing matches