Hey @sebawita ,
Here is an example with dummy data:
import weaviate
from weaviate.util import generate_uuid5
client = weaviate.Client("http://localhost:8080")
schema = {
"classes": [{
"class": "Article",
"description": "An article",
"invertedIndexConfig": {
"indexNullState": True,
},
"properties": [
{
"dataType": [
"text"
],
"description": "Article text",
"name": "article_text"
},
{
"dataType": [
"Metadata"
],
"description": "Article metadata",
"name": "hasMetadata"
}
]
},{
"class": "Metadata",
"description": "Article Metadata",
"invertedIndexConfig": {
"indexNullState": True,
},
"properties": [
{
"dataType": [
"text"
],
"description": "Title of the article",
"name": "title"
},
{
"dataType": [
"text"
],
"description": "The author of the article",
"name": "author_name"
}
]
}]
}
client.schema.create(schema)
# Metadata - 1
data_object={
'title': 'first article title'
}
uuid = client.data_object.create(
data_object=data_object,
class_name='Metadata',
uuid=generate_uuid5('first article title',"Metadata"),
vector=[0.12345] * 1536
)
print(uuid) #19e253f5-c5ad-5031-b37e-1c3fb4d1e42c
# Metadata - 2
data_object={
'author_name': 'second article author name'
}
uuid = client.data_object.create(
data_object=data_object,
class_name='Metadata',
uuid=generate_uuid5('second article author name',"Metadata"),
vector=[0.12345] * 1536
)
print(uuid) #ad5646e3-756d-5290-b682-814d3f301bb2
#Article - 1
data_object={
'article_text': 'first article'
}
uuid = client.data_object.create(
data_object=data_object,
class_name='Article',
uuid=generate_uuid5('first article',"Article"),
vector=[0.12345] * 1536
)
print(uuid) #1cb50188-c31d-568c-9c4b-c778363bfd5c
# Article -2
data_object={
'article_text': 'second article'
}
uuid = client.data_object.create(
data_object=data_object,
class_name='Article',
uuid=generate_uuid5('second article',"Article"),
vector=[0.12345] * 1536
)
print(uuid) #314848d1-930a-5110-bfc5-dc7e4b51405f
# add cross references
# first article -> first metadata
client.data_object.reference.add(
from_class_name="Article",
from_uuid="1cb50188-c31d-568c-9c4b-c778363bfd5c",
from_property_name="hasMetadata",
to_class_name="Metadata",
to_uuid="19e253f5-c5ad-5031-b37e-1c3fb4d1e42c",
)
#second article -> second metadata
client.data_object.reference.add(
from_class_name="Article",
from_uuid="314848d1-930a-5110-bfc5-dc7e4b51405f",
from_property_name="hasMetadata",
to_class_name="Metadata",
to_uuid="ad5646e3-756d-5290-b682-814d3f301bb2",
)
Queries on Article
class:
Article - fetch Article objects where Metadata title
is null
query Get {
Get {
Article(
where: {path: ["hasMetadata", "Metadata", "title"], valueBoolean: true, operator: IsNull}
) {
article_text
hasMetadata {
... on Metadata {
title
}
}
}
}
}
Response:
{
"data": {
"Get": {
"Article": [
{
"article_text": "first article",
"hasMetadata": [
{
"title": "first article title"
}
]
},
{
"article_text": "second article",
"hasMetadata": [
{
"title": null
}
]
}
]
}
}
}
Article - fetch Article objects where Metadata title
is not null
query Get {
Get {
Article(
where: {path: ["hasMetadata", "Metadata", "title"], valueBoolean: false, operator: IsNull}
) {
article_text
hasMetadata {
... on Metadata {
title
}
}
}
}
}
Response:
{
"data": {
"Get": {
"Article": []
}
}
}
Queries on Metadata
class:
Metadata - fetch Metadata where title
is null
query Get {
Get {
Metadata(where: {operator: IsNull, path: ["title"], valueBoolean: true}) {
author_name
title
}
}
}
Response:
{
"data": {
"Get": {
"Metadata": [
{
"author_name": "second article author name",
"title": null
}
]
}
}
}
Metadata - fetch Metadata where title
is notnull
query Get {
Get {
Metadata(where: {operator: IsNull, path: ["title"], valueBoolean: false}) {
author_name
title
}
}
}
Response:
{
"data": {
"Get": {
"Metadata": [
{
"author_name": null,
"title": "first article title"
}
]
}
}
}