Examples
All the following examples use the Datagraphs constructor, set up as follows:
1// Require the library
2const Datagraphs = require("@datalanguage/datagraphs-client");
3
4// Create the Data Graphs client
5const datagraphs = new Datagraphs();
Fetching Datasets
Fetching datasets includes options for paging and sorting the results.
1// Fetch all Datasets
2const datasets = await datagraphs.datasets.all();
3
4// Pagination with tokens
5const datasets = await datagraphs.datasets.all({ nextPageToken: "...", pageSize: 20 });
6
7// Sorting by Dataset property
8const 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.
1// Creating a Concept in a Dataset that has a single type
2const concept = await datagraphs.concepts.create("datasetId", { label: "My New Concept"});
3
4// When the target Dataset has multiple types, the new Concept must include the type
5const 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.
1// Get a concept
2const concept = await datagraphs.concepts.get(conceptId);
3
4// Embedding all related properties
5const concept = await datagraphs.concepts.get(conceptId, { embed: "_all" });
6
7// Embedding all related properties to a specific depth
8const concept = await datagraphs.concepts.get(conceptId, { embed: "level:2" });
9
10// Embedding specific related properties
11const 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
1// Using a search term
2const response = await datagraphs.concepts.search({ q: "searchTerm" });
3
4// Using an NQL query
5const response = await datagraphs.concepts.search({ filter: "property1:value,property2:value" });
6
7// Using facets
8const response = await datagraphs.concepts.search({ facets: [ "type1", "type2" ] });
9
10// Using date facets
11const response = await datagraphs.concepts.search({ dateFacets: "publishedDate:1d:1w:1M" });
12
Targeted search results
1// Return only concepts from a particular a Dataset
2const response = await datagraphs.concepts.search({ dataset: "id1" });
3
4// Return only those Concepts specified by id
5const response = await datagraphs.concepts.search({ ids: [ "id1", "id2", "id3" ] });
6
7// Return only those properties specified by name
8const response = await datagraphs.concepts.search({ fields: [ "property1", "property2" ] });
Pagination and sorting
1// Pagination with page tokens (unlimited results)
2const response = await datagraphs.concepts.search({ nextPageToken: "...", pageSize: 20 });
3
4// Pagination with page numbers (limited to 10,000 results)
5const response = await datagraphs.concepts.search({ pageNo: 2, pageSize: 20 });
6
7// Sorting by Concept property
8const response = await datagraphs.concepts.search({ sort: "property1:asc" });
Embedding related concepts
1// Embedding all properties
2const response = await datagraphs.concepts.search({ embed: "_all" });
3
4// Embedding all related properties to a specified depth
5const response = await datagraphs.concepts.search({ embed: "level:2" });
6
7// Embedding specific properties
8const 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.
1// Creating a candidate Concept
2const candidate = await datagraphs.candidates.create({ label: "My New Candidate" });
For more information, see candidates.create().