GraphQL - Get{}
Get{} syntax and query structureβ
A Get{}
function is always based on the schema. For example, if you've created a schema with a class Articles
which has the properties title
, url
and wordCount
, you can query it as follows:
- GraphQL
- Python
- JavaScript
- Go
- Java
- Curl
{
Get {
Article {
title
url
wordCount
}
}
}
import weaviate
client = weaviate.Client("http://localhost:8080")
result = client.query.get("Article", ["title", "url", "wordCount"]).do()
print(result)
const weaviate = require('weaviate-client');
const client = weaviate.client({
scheme: 'http',
host: 'localhost:8080',
});
client.graphql
.get()
.withClassName('Article')
.withFields('title url wordCount')
.do()
.then(res => {
console.log(res)
})
.catch(err => {
console.error(err)
});
package main
import (
"context"
"fmt"
"github.com/weaviate/weaviate-go-client/v4/weaviate"
"github.com/weaviate/weaviate-go-client/v4/weaviate/graphql"
)
func main() {
cfg := weaviate.Config{
Host: "localhost:8080",
Scheme: "http",
}
client := weaviate.New(cfg)
fields := []graphql.Field{
{Name: "title"},
{Name: "url"},
{Name: "wordCount"},
}
ctx := context.Background()
result, err := client.GraphQL().Get().
WithClassName("Article").
WithFields(fields...).
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.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();
Result<GraphQLResponse> result = client.graphQL().get()
.withClassName("Article")
.withFields(title, url, wordCount)
.run();
if (result.hasErrors()) {
System.out.println(result.getError());
return;
}
System.out.println(result.getResult());
}
}
$ echo '{
"query": "{
Get {
Article {
title
url
wordCount
}
}
}"
}' | curl \
-X POST \
-H 'Content-Type: application/json' \
-d @- \
http://localhost:8080/v1/graphql
π’ Try out this GraphQL example in the Weaviate Console.
The above query will result in something like the following:
{
"data": {
"Get": {
"Article": [{
"title": "βJokerβ Is a Viewing Experience of Rare, Numbing Emptiness",
"url": "https://www.newyorker.com/culture/the-front-row/joker-is-a-viewing-experience-of-rare-numbing-emptiness",
"wordCount": 1794
}]
}
}
}
Query beacon referencesβ
If you've set a beacon reference in the schema, you can query it as follows:
- GraphQL
- Python
- JavaScript
- Go
- Java
- Curl
{
Get {
Article {
title
url
wordCount
inPublication { # the reference
... on Publication { # you always set the destination class
name # the property related to target class
}
}
}
}
}
import weaviate
client = weaviate.Client("http://localhost:8080")
result = client.query.get("Article", ["title", "url", "wordCount", "inPublication {... on Publication {name }}"])
print(result)
const weaviate = require('weaviate-client');
const client = weaviate.client({
scheme: 'http',
host: 'localhost:8080',
});
client.graphql
.get()
.withClassName('Article')
.withFields('title url wordCount inPublication {... on Publication {name}}')
.do()
.then(res => {
console.log(res)
})
.catch(err => {
console.error(err)
});
package main
import (
"context"
"fmt"
"github.com/weaviate/weaviate-go-client/v4/weaviate"
"github.com/weaviate/weaviate-go-client/v4/weaviate/graphql"
)
func main() {
cfg := weaviate.Config{
Host: "localhost:8080",
Scheme: "http",
}
client := weaviate.New(cfg)
ctx := context.Background()
fields := []graphql.Field{
{Name: "title"},
{Name: "url"},
{Name: "wordCount"},
{Name: "inPublication", Fields: []graphql.Field{
{Name: "... on Publication", Fields: []graphql.Field{
{Name: "name"},
}},
}},
}
result, err := client.GraphQL().Get().
WithClassName("Article").
WithFields(fields...).
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.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 inPublication = Field.builder()
.name("inPublication")
.fields(new Field[]{
Field.builder()
.name("... on Publication")
.fields(new Field[]{
Field.builder().name("name").build()
})
.build()
})
.build();
Result<GraphQLResponse> result = client.graphQL().get()
.withClassName("Article")
.withFields(title, url, wordCount, inPublication)
.run();
if (result.hasErrors()) {
System.out.println(result.getError());
return;
}
System.out.println(result.getResult());
}
}
$ echo '{
"query": "{
Get {
Article {
title
url
wordCount
inPublication {
... on Publication {
name
}
}
}
}
}"
}' | curl \
-X POST \
-H 'Content-Type: application/json' \
-d @- \
http://localhost:8080/v1/graphql
π’ Try out this GraphQL example in the Weaviate Console.
The above query will result in something like the following:
{
"data": {
"Get": {
"Article": [{
"title": "βJokerβ Is a Viewing Experience of Rare, Numbing Emptiness",
"url": "https://www.newyorker.com/culture/the-front-row/joker-is-a-viewing-experience-of-rare-numbing-emptiness",
"wordCount": 1794,
"inPublication": [
{
"name": "New Yorker"
}
]
}]
}
}
}
Additional propertiesβ
For every Get{} request you can get additional information about the returned data object(s) by using additional properties. You can recognize these properties because they are in the object _additional{}
. Additional properties can help you interpret query results and can for example be used for projection and visualization of the retrieved data. An overview of all additional properties and how to use them is documented here.
Vector Search Operatorsβ
To combine Get { }
with a vector search argument, here is an overview of the supported arguments and links to their detailed documentation:
Argument | Description | Required Modules (at least one of) | Learn More |
---|---|---|---|
nearObject | Find the nearest neighbors of an object referenced by its id | none - works out of the box | Learn more |
nearVector | Find the nearest neighbors to any vector | none - works out of the box | Learn more |
nearText | Vectorize a text query and perform a vector search based on it | text2vec-transformers , text2vec-contextionary , text2vec-openai , multi2vec-clip , text2vec-huggingface , text2vec-cohere | Transformers, Contextionary, OpenAI, CLIP, Huggingface, Cohere |
nearImage | Vectorize an image and perform a vector search based on it | multi2vec-clip , img2vec-neural | CLIP, Img2Vec |
hybrid | Combine dense and sparse vectors to deliver best of both search methods | none - works out of the box | Learn more |
bm25 | Keyword search with BM25F ranking | none - works out of the box | Learn more |
Filtersβ
Get{}
functions can be extended with search filters (both semantic filters as traditional filters). Because the filters work on multiple core functions (like Aggregate{}
) there is a specific documentation page dedicated to filters.
Sortingβ
Note: Support for sorting was added in v1.13.0
.
You can sort results by any primitive property, typically a text
, string
,
number
, or int
property. When a query has a natural order (e.g. because of a
near<Media>
vector search), adding a sort operator will override the order.
See filters β sorting for more information.
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.