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

add support for builder constructor in neural query builder #1047

Conversation

will-hwang
Copy link
Contributor

@will-hwang will-hwang commented Dec 30, 2024

Description

Adds support for builder constructor for Neural Query Builder

This PR adds support for builder constructor with @Builder lombok annotation in NeuralQueryBuilder

Related Issues

Resolves #[Issue number to be closed when this PR is merged]

#1025

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@@ -23,6 +23,7 @@
import java.util.Objects;
import java.util.function.Supplier;

import lombok.Builder;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we want to validate the NeuralQueryBuilder before we create it like what we are doing in the KNNQueryBuilder. And to enforce this we will also need to either remove @NoArgsConstructor
@AllArgsConstructor or make them private.

Currently I think the builder is mainly used for our internal testing but I think it's good to enforce that only valid NeuralQueryBuilder can be created through our builder in case in future people accidentally use it create some invalid NeuralQueryBuilder in our business logic.

Copy link
Contributor

Choose a reason for hiding this comment

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

Besides I think currently we are using AllArgsConstructor to create NeuralQueryBuilder for some tests can we replace them with the builder you add? In this way if we need to add a new field we don't need to modify the tests that don't care about the new field.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed in the newest commit

@junqiu-lei
Copy link
Member

Please sign off your commits and add this PR to CHANGELOG.

@will-hwang will-hwang force-pushed the support_for_builder_in_neural_query_builder branch from 344c2ef to b98e6d0 Compare January 1, 2025 00:07
@will-hwang will-hwang force-pushed the support_for_builder_in_neural_query_builder branch from 3b4fbf1 to d8b7fe5 Compare January 2, 2025 21:27
Copy link
Member

@martin-gaievski martin-gaievski left a comment

Choose a reason for hiding this comment

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

Why there are no changes in any of the callers? If we made both existing constructors private then I assume we should replace calls to that constructor(s) with the new builder class

public NeuralQueryBuilder build() {
validateQueryParameters(fieldName, queryText, queryImage);
int k = this.k == null ? DEFAULT_K : this.k;
boolean queryTypeIsProvided = validateKNNQueryType(k, maxDistance, minScore);
Copy link
Member

Choose a reason for hiding this comment

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

why we're adding this logic here, it wasn't part of the original constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As part of this change, we wanted to add validation before instantiation so ensure that only valid NeuralSearchBuilder can be instantiated. See previous comment here: #1047 (comment)

Let me know your thoughts!

Copy link
Member

Choose a reason for hiding this comment

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

I'm rely on @junqiu-lei expertise here, I'm good if he's good

Copy link
Member

Choose a reason for hiding this comment

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

I think we don't need int k = this.k == null ? DEFAULT_K : this.k; at line 204.

If the default k is provided before validateKNNQueryType, when user use radial search parameter(only provide minScore or maxDistance), then validateKNNQueryType will return false and set to use default k at line 207.

Copy link
Member

@junqiu-lei junqiu-lei Jan 3, 2025

Choose a reason for hiding this comment

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

The CI is failed in radial integ test due to this change:

NeuralQueryIT > testQueryWithBoostAndImageQueryAndRadialQuery FAILED
    java.lang.IllegalArgumentException: Only one of k, max_distance, or min_score can be provided
        at __randomizedtesting.SeedInfo.seed([D0122DFF0BE48EA8:885F5A920D6E8D5B]:0)
        at org.opensearch.neuralsearch.query.NeuralQueryBuilder.validateKNNQueryType(NeuralQueryBuilder.java:540)
        at org.opensearch.neuralsearch.query.NeuralQueryBuilder$Builder.build(NeuralQueryBuilder.java:205)
        at org.opensearch.neuralsearch.query.NeuralQueryIT.testQueryWithBoostAndImageQueryAndRadialQuery(NeuralQueryIT.java:151)

@will-hwang
Copy link
Contributor Author

Why there are no changes in any of the callers? If we made both existing constructors private then I assume we should replace calls to that constructor(s) with the new builder class

@martin-gaievski All the callers have been changed to the new builder class. See the changes in all 14 files

@martin-gaievski
Copy link
Member

Why there are no changes in any of the callers? If we made both existing constructors private then I assume we should replace calls to that constructor(s) with the new builder class

@martin-gaievski All the callers have been changed to the new builder class. See the changes in all 14 files

Those are all test classes; I mean classes related to application logic. I do see one reference that hasn't been changed (https://github.com/opensearch-project/neural-search/blob/main/src/main/java/org/opensearch/neuralsearch/plugin/NeuralSearch.java#L112). Do you have an idea why that hasn't been changed after you made the constructor private?

@will-hwang
Copy link
Contributor Author

Why there are no changes in any of the callers? If we made both existing constructors private then I assume we should replace calls to that constructor(s) with the new builder class

@martin-gaievski All the callers have been changed to the new builder class. See the changes in all 14 files

Those are all test classes; I mean classes related to application logic. I do see one reference that hasn't been changed (https://github.com/opensearch-project/neural-search/blob/main/src/main/java/org/opensearch/neuralsearch/plugin/NeuralSearch.java#L112). Do you have an idea why that hasn't been changed after you made the constructor private?

As per QuerySpec specification:

public QuerySpec(     
    String name,
    org. opensearch. core. common. io. stream. Writeable. Reader<T> reader,
    org. opensearch. index. query. QueryParser<T> parser )
}

the parameter expects a Reader type, not a NeuralSearchQuery instance. I believe this is why compiler allows NeuralQueryBuilder::new. I've changed the callers to the new builder class in other classes, including in flaky test suite.

@heemin32
Copy link
Collaborator

heemin32 commented Jan 3, 2025

Why there are no changes in any of the callers? If we made both existing constructors private then I assume we should replace calls to that constructor(s) with the new builder class

@martin-gaievski All the callers have been changed to the new builder class. See the changes in all 14 files

Those are all test classes; I mean classes related to application logic. I do see one reference that hasn't been changed (https://github.com/opensearch-project/neural-search/blob/main/src/main/java/org/opensearch/neuralsearch/plugin/NeuralSearch.java#L112). Do you have an idea why that hasn't been changed after you made the constructor private?

The NeuralQueryBuilder::new is mapped to public NeuralQueryBuilder(StreamInput in)
https://github.com/opensearch-project/neural-search/blob/main/src/main/java/org/opensearch/neuralsearch/query/NeuralQueryBuilder.java#L117

heemin32
heemin32 previously approved these changes Jan 3, 2025
@heemin32 heemin32 added skip-changelog backport 2.x Label will add auto workflow to backport PR to 2.x branch labels Jan 3, 2025
@heemin32 heemin32 dismissed their stale review January 3, 2025 20:15

Approved by mistake

Copy link
Member

@martin-gaievski martin-gaievski left a comment

Choose a reason for hiding this comment

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

Looks good to me

@heemin32 heemin32 merged commit 2ecd32c into opensearch-project:main Jan 7, 2025
41 checks passed
opensearch-trigger-bot bot pushed a commit that referenced this pull request Jan 7, 2025
* add support for builder constructor in neural query builder

Signed-off-by: will-hwang <[email protected]>

* create custom builder class to enforce valid neural query builder instantiation

Signed-off-by: will-hwang <[email protected]>

* refactor code to remove duplicate

Signed-off-by: will-hwang <[email protected]>

* include new constructor in qa packages

Signed-off-by: will-hwang <[email protected]>

* refactor code to remove unnecessary code

Signed-off-by: will-hwang <[email protected]>

* fix bug in neural query builder instantiation

Signed-off-by: will-hwang <[email protected]>

---------

Signed-off-by: will-hwang <[email protected]>
(cherry picked from commit 2ecd32c)
@martin-gaievski martin-gaievski mentioned this pull request Jan 8, 2025
5 tasks
will-hwang added a commit to will-hwang/neural-search that referenced this pull request Jan 9, 2025
…ch-project#1047)

* add support for builder constructor in neural query builder

Signed-off-by: will-hwang <[email protected]>

* create custom builder class to enforce valid neural query builder instantiation

Signed-off-by: will-hwang <[email protected]>

* refactor code to remove duplicate

Signed-off-by: will-hwang <[email protected]>

* include new constructor in qa packages

Signed-off-by: will-hwang <[email protected]>

* refactor code to remove unnecessary code

Signed-off-by: will-hwang <[email protected]>

* fix bug in neural query builder instantiation

Signed-off-by: will-hwang <[email protected]>

---------

Signed-off-by: will-hwang <[email protected]>
(cherry picked from commit 2ecd32c)
heemin32 pushed a commit to heemin32/neural-search that referenced this pull request Jan 9, 2025
…ch-project#1047)

* add support for builder constructor in neural query builder

Signed-off-by: will-hwang <[email protected]>

* create custom builder class to enforce valid neural query builder instantiation

Signed-off-by: will-hwang <[email protected]>

* refactor code to remove duplicate

Signed-off-by: will-hwang <[email protected]>

* include new constructor in qa packages

Signed-off-by: will-hwang <[email protected]>

* refactor code to remove unnecessary code

Signed-off-by: will-hwang <[email protected]>

* fix bug in neural query builder instantiation

Signed-off-by: will-hwang <[email protected]>

---------

Signed-off-by: will-hwang <[email protected]>
(cherry picked from commit 2ecd32c)
will-hwang added a commit to will-hwang/neural-search that referenced this pull request Jan 9, 2025
…ch-project#1047)

* add support for builder constructor in neural query builder

Signed-off-by: will-hwang <[email protected]>

* create custom builder class to enforce valid neural query builder instantiation

Signed-off-by: will-hwang <[email protected]>

* refactor code to remove duplicate

Signed-off-by: will-hwang <[email protected]>

* include new constructor in qa packages

Signed-off-by: will-hwang <[email protected]>

* refactor code to remove unnecessary code

Signed-off-by: will-hwang <[email protected]>

* fix bug in neural query builder instantiation

Signed-off-by: will-hwang <[email protected]>

---------

Signed-off-by: will-hwang <[email protected]>
(cherry picked from commit 2ecd32c)
Signed-off-by: will-hwang <[email protected]>
junqiu-lei pushed a commit that referenced this pull request Jan 9, 2025
…ilder (#1084)

* add support for builder constructor in neural query builder (#1047)

Signed-off-by: will-hwang <[email protected]>
martin-gaievski pushed a commit that referenced this pull request Jan 10, 2025
* add support for builder constructor in neural query builder

Signed-off-by: will-hwang <[email protected]>

* create custom builder class to enforce valid neural query builder instantiation

Signed-off-by: will-hwang <[email protected]>

* refactor code to remove duplicate

Signed-off-by: will-hwang <[email protected]>

* include new constructor in qa packages

Signed-off-by: will-hwang <[email protected]>

* refactor code to remove unnecessary code

Signed-off-by: will-hwang <[email protected]>

* fix bug in neural query builder instantiation

Signed-off-by: will-hwang <[email protected]>

---------

Signed-off-by: will-hwang <[email protected]>
martin-gaievski pushed a commit that referenced this pull request Jan 13, 2025
* add support for builder constructor in neural query builder

Signed-off-by: will-hwang <[email protected]>

* create custom builder class to enforce valid neural query builder instantiation

Signed-off-by: will-hwang <[email protected]>

* refactor code to remove duplicate

Signed-off-by: will-hwang <[email protected]>

* include new constructor in qa packages

Signed-off-by: will-hwang <[email protected]>

* refactor code to remove unnecessary code

Signed-off-by: will-hwang <[email protected]>

* fix bug in neural query builder instantiation

Signed-off-by: will-hwang <[email protected]>

---------

Signed-off-by: will-hwang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.x Label will add auto workflow to backport PR to 2.x branch skip-changelog v2.19.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants