Skip to content

Conversation

@bevzzz
Copy link
Collaborator

@bevzzz bevzzz commented Mar 26, 2025

This PR adds functionality for working with cross-references.
Check ReferencesITest.java for complete examples.

Create collection with reference properties

client.collections.create("Artists",
  c -> c.properties(
    Property.reference("bornIn", "Countries"), // single-target
    Property.reference("hasAwards", "Oscars", "Grammys"))); // multi-target

// Add a property to existing collection
var artists = client.collections.use("Artists");
artists.config.addProperty(Property.reference("featuredIn", "Movies"));

Insert object with reference properties

artists.data.insert(
  Map.of("name", "Alex"),
  opt -> opt
    .reference("hasAwards", Reference.collection("Grammys", "grammy-0")) // Explicit collection name (useful for multi-target references)
    .reference("hasAwards", Reference.uuids("grammy-1", "grammy-2")) // UUIDs (Weaviate will search different collections for these objects)
    .reference("hasAwards", Reference.objects(grammy_3, oscar_3))); // Create fully-qualified references (collection + uuid) from WeaviateObjects

Retrieve references and their properties in query results

artists.data.get("artist-0",
  artist -> artist.returnReferences(
    // Artist's references
    QueryReference.multi("hasAwards", "Oscars",
      oscar -> oscar
        .returnProperties("weight")),
            // Nested references
    QueryReference.multi("hasAwards", "Grammys",
      grammy -> grammy
        .returnReferences(
          QueryReference.single("presentedBy"
            academy -> academy
              // Property of an arbitrarily nested reference
              .returnProperties("description"))
        .returnMetadata(MetadataField.ID))));

While the tests do not show that, working with references is possible in other queries as well (e.g. NearText).

Out of scope

  • Deleting / updating references

@bevzzz bevzzz changed the base branch from main to v6 March 26, 2025 13:30
Copy link

@orca-security-eu orca-security-eu bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Passed Passed SAST high 0   medium 1   low 0   info 0 View in Orca
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca
🛡️ The following SAST misconfigurations have been detected
NAME FILE
medium Insecure Random Number Generator Usage in Java .../ConcurrentTest.java View in code

Copy link

@orca-security-eu orca-security-eu bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca

@bevzzz bevzzz marked this pull request as ready for review April 29, 2025 11:25
@bevzzz bevzzz self-assigned this Apr 29, 2025
@bevzzz bevzzz added the v6 label Apr 29, 2025
@dirkkul
Copy link
Collaborator

dirkkul commented Apr 29, 2025

     Property.reference("bornIn", "Countries"), // single-target
    Property.reference("hasAwards", "Oscars", "Grammys") // multi-target

This is a great solution!

.reference("hasAwards", Reference.uuids("grammy-1", "grammy-2") // UUIDs (Weaviate will search different collections for these objects)

Can we also accept UUIDs directly?

artists.data.get("artist-0",

also really cool :)

            QueryReference.multi("hasAwards", "Oscars",
                oscar -> oscar
                  .returnProperties("weight")),

As a non-java coder - do I need to predefine oscar in any way or is this just a lambda-variable with a nice name? :)

@bevzzz
Copy link
Collaborator Author

bevzzz commented Apr 29, 2025

Can we also accept UUIDs directly?

I take it you mean something like .reference("hasAwards", "grammy-1", "grammy-2")?
That's a pattern I was playing around with (also in other parts of the API), but decided to leave that util some time in the future because:

  1. I couldn't find a solution that would be consistent across all examples
  2. Having 3 constructors for Reference and 3 overloaded .reference methods felt redundant (that is not to say that we shouldn't do that -- I didn't want to clutter the code in this early stage)

As a non-java coder - do I need to predefine Oscar in any way or is this just a lambda-variable with a nice name? :)

Just a nice name to make this example more readable 😃

@bevzzz bevzzz closed this Apr 29, 2025
@bevzzz bevzzz reopened this Apr 29, 2025
@bevzzz bevzzz merged commit 8649708 into v6 May 6, 2025
2 checks passed
@bevzzz bevzzz deleted the v6-references branch May 6, 2025 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants