Introduction
- Weaviate has RESTful API endpoints to query data, but Weaviate’s query language is GraphQL.
- You can query a Weaviate after you’ve created a schema and populated it with data.
Prerequisites
- Connect to a Weaviate instance.
If you haven’t set up a Weaviate instance yet, check the Getting started guide. In this guide we assume your instance is running athttp://localhost:8080
with text2vec-contextionary as vectorization module. - Upload a schema.
Learn how to create and upload a schema here. In this guide we assume to have a similar schema uploaded with the classesPublication
,Article
andAuthor
. - Add data.
Make sure there is data available in your Weaviate instance, you can read how to do this in the previous guide. In this tutorial we assume there are data objects ofPublication
s,Article
s andAuthor
s present.
How to get data
1. Define a query.
The easiest GraphQL queries to get data from Weaviate are Get{}
queries. Let’s say we want to retrieve all the articles (there title, authors, url and wordCount) that are published by “Wired”, the GraphQL query will look as follows:
{
Get {
Article ( where: {
operator:Equal,
valueString:"Wired",
path: ["inPublication", "Publication", "name"]
}) {
title
url
wordCount
hasAuthors{
... on Author {
name
}
}
}
}
}
2. Send the query
There are multiple ways to connect to Weaviate’s (GraphQL) API to query data. Raw GraphQL queries can be directly posted in the GraphiQL interface in the Console, but can also be sent via curl, the Python or JavaScript client.
{
Get {
Article (where: {
operator:Equal,
valueString:"Wired",
path: ["inPublication", "Publication", "name"]
}){
title
url
wordCount
hasAuthors{
... on Author {
name
}
}
}
}
}
import weaviate
client = weaviate.Client("http://localhost:8080")
get_articles_query = """
{
Get {
Article (where: {
operator:Equal,
valueString:"Wired",
path: ["inPublication", "Publication", "name"]
}){
title
url
wordCount
hasAuthors{
... on Author {
name
}
}
}
}
}
"""
query_result = client.query.raw(get_articles_query)
print(query_result)
const weaviate = require("weaviate-client");
const client = weaviate.client({
scheme: 'http',
host: 'localhost:8080',
});
client.graphql
.get()
.withClassName('Article')
.withFields('title url wordCount HasAuthors{ ... on Author { name }}')
.withWhere({
operator: 'Equal',
path: ['inPublication", "Publication", "name'],
valueString:"Wired"
})
.do()
.then(res => {
console.log(res)
})
.catch(err => {
console.error(err)
});
package main
import (
"context"
"fmt"
"github.com/semi-technologies/weaviate-go-client/v4/weaviate"
"github.com/semi-technologies/weaviate-go-client/v4/weaviate/filters"
"github.com/semi-technologies/weaviate-go-client/v4/weaviate/graphql"
)
func main() {
cfg := weaviate.Config{
Host: "localhost:8080",
Scheme: "http",
}
client := weaviate.New(cfg)
where := filters.Where().
WithPath([]string{"inPublication", "Publication", "name"}).
WithOperator(filters.Equal).
WithValueString("Wired")
fields := []graphql.Field{
{Name: "title"},
{Name: "url"},
{Name: "wordCount"},
{Name: "hasAuthors", Fields: []graphql.Field{
{Name: "... on Author", Fields: []graphql.Field{
{Name: "name"},
}},
}},
}
ctx := context.Background()
result, err := client.GraphQL().Get().
WithClassName("Article").
WithFields(fields...).
WithWhere(where).
Do(ctx)
if err != nil {
panic(err)
}
fmt.Printf("%v", result)
}
package technology.semi.weaviate;
import technology.semi.weaviate.client.Config;
import technology.semi.weaviate.client.WeaviateClient;
import technology.semi.weaviate.client.base.Result;
import technology.semi.weaviate.client.v1.filters.Operator;
import technology.semi.weaviate.client.v1.filters.WhereFilter;
import technology.semi.weaviate.client.v1.graphql.model.GraphQLResponse;
import technology.semi.weaviate.client.v1.graphql.query.fields.Field;
public class App {
public static void main(String[] args) {
Config config = new Config("http", "localhost:8080");
WeaviateClient client = new WeaviateClient(config);
Field title = Field.builder().name("title").build();
Field url = Field.builder().name("url").build();
Field wordCount = Field.builder().name("wordCount").build();
Field hasAuthors = Field.builder()
.name("hasAuthors")
.fields(new Field[]{
Field.builder()
.name("... on Author")
.fields(new Field[]{
Field.builder().name("name").build()
}).build()
}).build();
WhereFilter where = WhereFilter.builder()
.path(new String[]{ "inPublication", "Publication", "name" })
.operator(Operator.Equal)
.valueString("Wired")
.build();
Result<GraphQLResponse> result = client.graphQL().get()
.withClassName("Article")
.withFields(title, url, wordCount, hasAuthors)
.withWhere(where)
.run();
if (result.hasErrors()) {
System.out.println(result.getError());
return;
}
System.out.println(result.getResult());
}
}
$ echo '{
"query": "{
Get {
Article (where: {
operator: Equal,
valueString:\"Wired\",
path: [\"inPublication\", \"Publication\", \"name\"]
}){
title
url
wordCount
hasAuthors{
... on Author {
name
}
}
}
}
}"
}' | curl \
-X POST \
-H 'Content-Type: application/json' \
-d @- \
http://localhost:8080/v1/graphql
🟢 Click here to try out this graphql example in the Weaviate Console.
- View the results
The results of the previous query looks something like the following in JSON:{ "data": { "Get": { "Article": [ { "hasAuthors": [ { "name": "Condé Nast" }, { "name": "Vince Beise" }, { "name": "Vince Beiser" }, { "name": "Wired Staff" } ], "title": "The War Vet, the Dating Site, and the Phone Call From Hell", "url": "https://www.wired.com/story/the-phone-call-from-hell#intcid=recommendations_wired-homepage-right-rail-popular_1ef3340d-1896-4aff-9f81-0caa132856ac_popular4-1", "wordCount": 731 }, { "hasAuthors": [ { "name": "Matt Simon" }, { "name": "Matt Simo" } ], "title": "Not to Ruin the Super Bowl, but the Sea Is Consuming Miami", "url": "https://www.wired.com/story/rising-seas-are-coming-for-miamis-super-bowls#intcid=recommendations_default-popular_b3b30847-4aa3-4970-8e77-f7558a7cccd8_popular4-1", "wordCount": 586 }, { "hasAuthors": [ { "name": "Condé Nast" }, { "name": "Gregory Barbe" } ], "title": "The Startup That Aims to Decrypt Blockchain for Business", "url": "https://wired.com/story/startup-aims-decrypt-blockchain-business/", "wordCount": 636 }, { "hasAuthors": [ { "name": "Lauren Goode" }, { "name": "Lauren Good" } ], "title": "The Biggest Apple Maps Change Is One You Can't See", "url": "https://www.wired.com/story/apple-maps-redesign#intcid=recommendations_wired-homepage-right-rail-popular_291bba78-5a92-4551-a70b-2b93d1cd3e7a_popular4-1", "wordCount": 543 }, { "hasAuthors": [ { "name": "Will Knight" }, { "name": "Will Knigh" } ], "title": "Apple's Latest Deal Shows How AI Is Moving Right Onto Devices", "url": "https://www.wired.com/story/apples-deal-shows-ai-moving-devices/", "wordCount": 680 }, { "hasAuthors": [ { "name": "Condé Nast" } ], "title": "Traveling for the Holidays? Here's How to Not Get Sick", "url": "https://wired.com/story/traveling-for-the-holidays-heres-how-to-not-get-sick/", "wordCount": 608 }, { "hasAuthors": [ { "name": "Graeme Mcmillan" }, { "name": "Graeme Mcmilla" } ], "title": "Sanders and Warren's Big Debate Dust-Up Tops This Week's Internet News Roundup", "url": "https://www.wired.com/story/internet-week-253/", "wordCount": 364 }, { "hasAuthors": [ { "name": "Condé Nast" }, { "name": "Michael Hard" }, { "name": "Laura Mallonee" }, { "name": "Michael Hardy" } ], "title": "The Neveda Town Where Storm Area 51 Sort Of Happened", "url": "https://www.wired.com/story/rachel-nevada-area-51/", "wordCount": 511 }, { "hasAuthors": [ { "name": "Laura Mallonee" }, { "name": "Laura Mallone" }, { "name": "Chris Colin" }, { "name": "Adam Rogers" } ], "title": "Homelessness in the Living Rooms of the Rich", "url": "https://www.wired.com/story/san-francisco-shelters-living-room/", "wordCount": 502 } ] } }, "errors": null }
Next steps
- Make more advanced queries, for example to explore data with semantic search, in the next tutorial.
- View other GraphQL methods in the GraphQL documentation.
More resources
If you can’t find the answer to your question here, please look at the:
- Frequently Asked Questions. Or,
- Knowledge base of old issues. Or,
- For questions: Stackoverflow. Or,
- For issues: Github. Or,
- Ask your question in the Slack channel: Slack.