Skip to content

Commit

Permalink
Deprecate max_token_score field of neural_sparse query (opensearch-pr…
Browse files Browse the repository at this point in the history
…oject#478)

* rm bounded linear feature query

Signed-off-by: zhichao-aws <[email protected]>

* deprecate max_token_score

Signed-off-by: zhichao-aws <[email protected]>

* add changelog

Signed-off-by: zhichao-aws <[email protected]>

* tidy

Signed-off-by: zhichao-aws <[email protected]>

* fix ut

Signed-off-by: zhichao-aws <[email protected]>

* add ut

Signed-off-by: zhichao-aws <[email protected]>

* add deprecation annotation

Signed-off-by: zhichao-aws <[email protected]>

---------

Signed-off-by: zhichao-aws <[email protected]>
  • Loading branch information
zhichao-aws committed Nov 22, 2023
1 parent ef19ffa commit 04bf2a4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 280 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ Fixed exception for case when Hybrid query being wrapped into bool query ([#490]
### Documentation
### Maintenance
### Refactoring
Deprecate the `max_token_score` field in `neural_sparse` query clause ([#478](https://github.com/opensearch-project/neural-search/pull/478))
237 changes: 0 additions & 237 deletions src/main/java/org/apache/lucene/BoundedLinearFeatureQuery.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.lucene.BoundedLinearFeatureQuery;
import org.apache.lucene.document.FeatureField;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.Query;
import org.opensearch.common.SetOnce;
import org.opensearch.core.ParseField;
Expand Down Expand Up @@ -62,8 +61,11 @@ public class NeuralSparseQueryBuilder extends AbstractQueryBuilder<NeuralSparseQ
static final ParseField QUERY_TEXT_FIELD = new ParseField("query_text");
@VisibleForTesting
static final ParseField MODEL_ID_FIELD = new ParseField("model_id");
// We use max_token_score field to help WAND scorer prune query clause in lucene 9.7. But in lucene 9.8 the inner
// logics change, this field is not needed any more.
@VisibleForTesting
static final ParseField MAX_TOKEN_SCORE_FIELD = new ParseField("max_token_score");
@Deprecated
static final ParseField MAX_TOKEN_SCORE_FIELD = new ParseField("max_token_score").withAllDeprecated();

private static MLCommonsClientAccessor ML_CLIENT;

Expand Down Expand Up @@ -164,9 +166,6 @@ public static NeuralSparseQueryBuilder fromXContent(XContentParser parser) throw
sparseEncodingQueryBuilder.modelId(),
String.format(Locale.ROOT, "%s field must be provided for [%s] query", MODEL_ID_FIELD.getPreferredName(), NAME)
);
if (sparseEncodingQueryBuilder.maxTokenScore != null && sparseEncodingQueryBuilder.maxTokenScore <= 0) {
throw new IllegalArgumentException(MAX_TOKEN_SCORE_FIELD.getPreferredName() + " must be larger than 0.");
}

return sparseEncodingQueryBuilder;
}
Expand Down Expand Up @@ -238,14 +237,9 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
Map<String, Float> queryTokens = queryTokensSupplier.get();
validateQueryTokens(queryTokens);

final Float scoreUpperBound = maxTokenScore != null ? maxTokenScore : Float.MAX_VALUE;

BooleanQuery.Builder builder = new BooleanQuery.Builder();
for (Map.Entry<String, Float> entry : queryTokens.entrySet()) {
builder.add(
new BoostQuery(new BoundedLinearFeatureQuery(fieldName, entry.getKey(), scoreUpperBound), entry.getValue()),
BooleanClause.Occur.SHOULD
);
builder.add(FeatureField.newLinearQuery(fieldName, entry.getKey(), entry.getValue()), BooleanClause.Occur.SHOULD);
}
return builder.build();
}
Expand Down
Loading

0 comments on commit 04bf2a4

Please sign in to comment.