Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for vector search with Query#findNearest #1827

Merged
merged 22 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a971555
Refactor to support find nearest
MarkDuckworth Sep 17, 2024
44d364d
Merge branch 'main' of github.com:googleapis/java-firestore into mark…
MarkDuckworth Sep 17, 2024
8fe28ce
FindNearest with tests
MarkDuckworth Sep 18, 2024
151f245
Cleanup and comments
MarkDuckworth Sep 18, 2024
c2da4a8
Cleanup and addressing API breaking changes
MarkDuckworth Sep 18, 2024
dff55dc
PR feedback and adding tests
MarkDuckworth Sep 26, 2024
a29c6b5
PR comment changes
MarkDuckworth Sep 27, 2024
96a3940
Merge branch 'main' of github.com:googleapis/java-firestore into mark…
MarkDuckworth Sep 27, 2024
b391efb
chore: generate libraries at Fri Sep 27 17:15:13 UTC 2024
cloud-java-bot Sep 27, 2024
7bd43aa
Fix dependency check
MarkDuckworth Sep 27, 2024
58756e0
Merge branch 'markduckworth/find-nearest' of github.com:googleapis/ja…
MarkDuckworth Sep 27, 2024
264e103
chore: generate libraries at Fri Sep 27 17:33:40 UTC 2024
cloud-java-bot Sep 27, 2024
bb83ee7
PR remove unnecessary override
MarkDuckworth Sep 27, 2024
31e9be2
Merge branch 'markduckworth/find-nearest' of github.com:googleapis/ja…
MarkDuckworth Sep 27, 2024
1f61461
PR feedback
MarkDuckworth Oct 1, 2024
f29120c
Merge branch 'main' of github.com:googleapis/java-firestore into mark…
MarkDuckworth Oct 1, 2024
87fcff4
chore: generate libraries at Tue Oct 1 19:36:20 UTC 2024
cloud-java-bot Oct 1, 2024
5fa33f5
Example code
MarkDuckworth Oct 1, 2024
1fe96df
javadoc fix
MarkDuckworth Oct 1, 2024
c606e57
Merge branch 'main' of github.com:googleapis/java-firestore into mark…
MarkDuckworth Oct 1, 2024
7cfab37
Refactor GenericQuerySnapshot so the constructor does not accept a do…
MarkDuckworth Oct 1, 2024
84a8f8b
chore: generate libraries at Tue Oct 1 21:53:10 UTC 2024
cloud-java-bot Oct 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.firestore;

import javax.annotation.Nullable;

/**
* Represents the options that are used to configure the use of OpenTelemetry for telemetry
MarkDuckworth marked this conversation as resolved.
Show resolved Hide resolved
* collection in the Firestore SDK.
*/
public class FindNearestOptions {
private final @Nullable FieldPath distanceResultField;

private final @Nullable Double distanceThreshold;

@Nullable
public FieldPath getDistanceResultField() {
return distanceResultField;
}

@Nullable
public Double getDistanceThreshold() {
return distanceThreshold;
}

FindNearestOptions(FindNearestOptions.Builder builder) {
this.distanceThreshold = builder.distanceThreshold;
this.distanceResultField = builder.distanceResultField;
}

public static FindNearestOptions.Builder newBuilder() {
return new FindNearestOptions.Builder();
}

public static final class Builder {
private @Nullable FieldPath distanceResultField;

private @Nullable Double distanceThreshold;

private Builder() {
distanceThreshold = null;
distanceResultField = null;
}

private Builder(FindNearestOptions options) {
this.distanceThreshold = options.distanceThreshold;
this.distanceResultField = options.distanceResultField;
}

public Builder setDistanceResultField(String fieldPath) {
this.distanceResultField = FieldPath.fromDotSeparatedString(fieldPath);
return this;
}

public Builder setDistanceResultField(@Nullable FieldPath fieldPath) {
this.distanceResultField = fieldPath;
return this;
}

public Builder setDistanceThreshold(@Nullable Double distanceThreshold) {
this.distanceThreshold = distanceThreshold;
return this;
}

public FindNearestOptions build() {
return new FindNearestOptions(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.firestore;

import com.google.cloud.Timestamp;
import com.google.cloud.firestore.encoding.CustomClassMapper;
import com.google.common.collect.ImmutableList;
import java.util.*;
tom-andersen marked this conversation as resolved.
Show resolved Hide resolved
import javax.annotation.Nonnull;

public abstract class GenericQuerySnapshot<QueryT> implements Iterable<QueryDocumentSnapshot> {
protected final QueryT query;
protected final Timestamp readTime;

volatile List<DocumentChange> documentChanges;
volatile List<QueryDocumentSnapshot> documents;
tom-andersen marked this conversation as resolved.
Show resolved Hide resolved

volatile DocumentSet documentSet;
tom-andersen marked this conversation as resolved.
Show resolved Hide resolved

protected GenericQuerySnapshot(
QueryT query,
Timestamp readTime,
final List<QueryDocumentSnapshot> documents,
final DocumentSet documentSet,
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems to me that documentSet is just converted to documents.

I suggest you remove parameter, and just have subclasses use documents parameter instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also pre-existing logic, so I'm not certain of the reasoning, but there are several instances of lazy-loading and memoization in this code. My suspicion is that it's related to performance. I don't have strong feelings on this either way. What are your thoughts with that context?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is of slightly greater concern because documentSet is also omitted from equals method.

I prefer this get fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed this.

I don't think there was an issue before. The value of documentSet was tested for equality because it's value was converted by the getDocuments() method.

However, I think the new approach is equally fine. The benefits of lazily converting documentSet to a list of document snapshots are rarely if ever seen.

final List<DocumentChange> documentChanges) { // Elevated access level for mocking.
MarkDuckworth marked this conversation as resolved.
Show resolved Hide resolved
this.query = query;
this.readTime = readTime;
this.documentChanges =
documentChanges != null ? Collections.unmodifiableList(documentChanges) : documentChanges;
this.documentSet = documentSet;
this.documents = documents;
}

/**
* Returns the query for the snapshot.
*
* @return The backing query that produced this snapshot.
*/
@Nonnull
public QueryT getQuery() {
return query;
}

/**
* Returns the time at which this snapshot was read.
*
* @return The read time of this snapshot.
*/
@Nonnull
public Timestamp getReadTime() {
return readTime;
}

/**
* Returns the documents in this QuerySnapshot as a List in order of the query.
*
* @return The list of documents.
*/
@Nonnull
public List<QueryDocumentSnapshot> getDocuments() {
if (documents == null) {
synchronized (documentSet) {
if (documents == null) {
documents = documentSet.toList();
}
}
}
return Collections.unmodifiableList(documents);
}

/** Returns true if there are no documents in the QuerySnapshot. */
public boolean isEmpty() {
return this.size() == 0;
}

@Nonnull
public Iterator<QueryDocumentSnapshot> iterator() {
return getDocuments().iterator();
}

/**
* Returns the contents of the documents in the QuerySnapshot, converted to the provided class, as
* a list.
*
* @param clazz The POJO type used to convert the documents in the list.
*/
@Nonnull
public <T> List<T> toObjects(@Nonnull Class<T> clazz) {
List<QueryDocumentSnapshot> documents = getDocuments();
List<T> results = new ArrayList<>(documents.size());
for (DocumentSnapshot documentSnapshot : documents) {
results.add(
CustomClassMapper.convertToCustomClass(
documentSnapshot.getData(), clazz, documentSnapshot.getReference()));
}

return results;
}

/**
* Returns the list of documents that changed since the last snapshot. If it's the first snapshot
* all documents will be in the list as added changes.
*
* @return The list of documents that changed since the last snapshot.
*/
@Nonnull
public List<DocumentChange> getDocumentChanges() {
if (documentChanges == null) {
synchronized (documents) {
if (documentChanges == null) {
int size = documents.size();
ImmutableList.Builder<DocumentChange> builder =
ImmutableList.builderWithExpectedSize(size);
for (int i = 0; i < size; ++i) {
builder.add(new DocumentChange(documents.get(i), DocumentChange.Type.ADDED, -1, i));
}
documentChanges = builder.build();
}
}
}

return Collections.unmodifiableList(documentChanges);
tom-andersen marked this conversation as resolved.
Show resolved Hide resolved
}

public int size() {
return getDocuments().size();
}

public boolean equals(Object o) {
tom-andersen marked this conversation as resolved.
Show resolved Hide resolved
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
QuerySnapshot that = (QuerySnapshot) o;
return Objects.equals(query, that.query)
&& Objects.equals(this.size(), that.size())
tom-andersen marked this conversation as resolved.
Show resolved Hide resolved
&& Objects.equals(this.getDocumentChanges(), that.getDocumentChanges())
&& Objects.equals(this.getDocuments(), that.getDocuments());
}

@Override
public int hashCode() {
return Objects.hash(query, this.getDocumentChanges(), this.getDocuments());
}
}
Loading
Loading