Get in touch
Get in touch

Examples

All the following examples use the Datagraphs constructor, set up as follows:

Copy
// Require the library
const Datagraphs = require("@datalanguage/datagraphs-client");

// Create the Data Graphs client
const datagraphs = new Datagraphs();

Fetching Datasets

Fetching datasets includes options for paging and sorting the results.

Copy
// Fetch all Datasets
const datasets = await datagraphs.datasets.all();

// Pagination with tokens
const datasets = await datagraphs.datasets.all({ nextPageToken: "...", pageSize: 20 });

// Sorting by Dataset property
const datasets = await datagraphs.datasets.all({ sort: "totalConcepts:desc" });

For full Datasets reference, see datasets.all().

Create a Concept

Creating a Concept requires the id of the target Dataset as well as the Concept itself. If the Dataset has more than one type, the new Concept must define its type.

Copy
// Creating a Concept in a Dataset that has a single type
const concept = await datagraphs.concepts.create("datasetId", { label: "My New Concept"});

// When the target Dataset has multiple types, the new Concept must include the type
const concept = await datagraphs.concepts.create("datasetId", { label: "My New Concept", type: "Concept" });

For more information, see concepts.create().

Get a Concept

Fetching a concept requires only its id, but related properties can also be included in the results.

Copy
// Get a concept
const concept = await datagraphs.concepts.get(conceptId);

// Embedding all related properties  
const concept = await datagraphs.concepts.get(conceptId, { embed: "_all" });

// Embedding all related properties to a specific depth 
const concept = await datagraphs.concepts.get(conceptId, { embed: "level:2" });

// Embedding specific related properties 
const concept = await datagraphs.concepts.get(conceptId, { embed: ["property1", "property2"] });

For more information, see concepts.get().

Search Concepts

There are a variety ways to filter and target a Concepts search and to organise and enhance the results. Each of the following examples can be combined to produce more complex queries.

Filtering search results

Copy
// Using a search term
const response = await datagraphs.concepts.search({ q: "searchTerm" });

// Using an NQL query
const response = await datagraphs.concepts.search({ filter: "property1:value,property2:value" });

// Using facets
const response = await datagraphs.concepts.search({ facets: [ "type1", "type2" ] });

// Using date facets
const response = await datagraphs.concepts.search({ dateFacets: "publishedDate:1d:1w:1M" });

Targeted search results

Copy
// Return only concepts from a particular a Dataset
const response = await datagraphs.concepts.search({ dataset: "id1" });

// Return only those Concepts specified by id
const response = await datagraphs.concepts.search({ ids: [ "id1", "id2", "id3" ] });

// Return only those properties specified by name
const response = await datagraphs.concepts.search({ fields: [ "property1", "property2" ] });

Pagination and sorting

Copy
// Pagination with page tokens (unlimited results)
const response = await datagraphs.concepts.search({ nextPageToken: "...", pageSize: 20 });

// Pagination with page numbers (limited to 10,000 results)
const response = await datagraphs.concepts.search({ pageNo: 2, pageSize: 20 });

// Sorting by Concept property
const response = await datagraphs.concepts.search({ sort: "property1:asc" });

Embedding related concepts

Copy
// Embedding all properties
const response = await datagraphs.concepts.search({ embed: "_all" });

// Embedding all related properties to a specified depth
const response = await datagraphs.concepts.search({ embed: "level:2" });

// Embedding specific properties
const response = await datagraphs.concepts.search({ embed: ["property1", "property2"] });

For full details of query parameters when searching for Concepts, see concepts.search().

Create a new candidate concept

This requires a candidate Concept object in which only the label property is required.

Copy
// Creating a candidate Concept
const candidate = await datagraphs.candidates.create({ label: "My New Candidate" });

For more information, see candidates.create().