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

Merge main into 2.x #91

Merged
merged 9 commits into from
Dec 19, 2024
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @gsingers @macohen @sstults
* @gsingers @macohen @sstults @JohannesDaniel
178 changes: 0 additions & 178 deletions src/javaRestTest/java/com/o19s/es/ltr/action/LTRStatsActionIT.java

This file was deleted.

28 changes: 23 additions & 5 deletions src/main/java/com/o19s/es/explore/ExplorerQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package com.o19s.es.explore;

import org.apache.lucene.search.QueryVisitor;
import org.opensearch.ltr.settings.LTRSettings;
import org.opensearch.ltr.stats.LTRStats;
import org.opensearch.ltr.stats.StatName;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermStates;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Weight;
Expand All @@ -44,10 +47,12 @@
public class ExplorerQuery extends Query {
private final Query query;
private final String type;
private final LTRStats ltrStats;

public ExplorerQuery(Query query, String type) {
public ExplorerQuery(Query query, String type, LTRStats ltrStats) {
this.query = query;
this.type = type;
this.ltrStats = ltrStats;
}

private boolean isCollectionScoped() {
Expand All @@ -61,6 +66,7 @@ private boolean isCollectionScoped() {

public String getType() { return this.type; }

@SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
@Override
public boolean equals(Object other) {
return sameClassAs(other) &&
Expand All @@ -77,7 +83,7 @@ public Query rewrite(IndexReader reader) throws IOException {
Query rewritten = query.rewrite(reader);

if (rewritten != query) {
return new ExplorerQuery(rewritten, type);
return new ExplorerQuery(rewritten, type, ltrStats);
}

return this;
Expand All @@ -89,8 +95,20 @@ public int hashCode() {
}

@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost)
throws IOException {
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
if (!LTRSettings.isLTRPluginEnabled()) {
throw new IllegalStateException("LTR plugin is disabled. To enable, update ltr.plugin.enabled to true");
}

try {
return createWeightInternal(searcher, scoreMode, boost);
} catch (Exception e) {
ltrStats.getStats().get(StatName.LTR_REQUEST_ERROR_COUNT.getName()).increment();
throw e;
}
}

private Weight createWeightInternal(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
if (!scoreMode.needsScores()) {
return searcher.createWeight(query, scoreMode, boost);
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/com/o19s/es/explore/ExplorerQueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package com.o19s.es.explore;

import org.opensearch.ltr.stats.LTRStats;
import org.opensearch.ltr.stats.StatName;
import org.apache.lucene.search.Query;
import org.opensearch.core.ParseField;
import org.opensearch.core.common.ParsingException;
Expand Down Expand Up @@ -53,18 +55,20 @@ public class ExplorerQueryBuilder extends AbstractQueryBuilder<ExplorerQueryBuil

private QueryBuilder query;
private String type;
private LTRStats ltrStats;

public ExplorerQueryBuilder() {
}


public ExplorerQueryBuilder(StreamInput in) throws IOException {
public ExplorerQueryBuilder(StreamInput in, LTRStats ltrStats) throws IOException {
super(in);
query = in.readNamedWriteable(QueryBuilder.class);
type = in.readString();
this.ltrStats = ltrStats;
}

public static ExplorerQueryBuilder fromXContent(XContentParser parser) throws IOException {
public static ExplorerQueryBuilder fromXContent(XContentParser parser, LTRStats ltrStats) throws IOException {
final ExplorerQueryBuilder builder;

try {
Expand All @@ -79,6 +83,7 @@ public static ExplorerQueryBuilder fromXContent(XContentParser parser) throws IO
if (builder.statsType() == null) {
throw new ParsingException(parser.getTokenLocation(), "Field [" + TYPE_NAME + "] is mandatory.");
}
builder.ltrStats(ltrStats);
return builder;
}

Expand All @@ -99,7 +104,8 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
return new ExplorerQuery(query.toQuery(context), type);
ltrStats.getStat(StatName.LTR_REQUEST_TOTAL_COUNT.getName()).increment();
return new ExplorerQuery(query.toQuery(context), type, ltrStats);
}

@Override
Expand All @@ -109,6 +115,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
ExplorerQueryBuilder rewritten = new ExplorerQueryBuilder();
rewritten.type = this.type;
rewritten.query = Rewriteable.rewrite(query, queryRewriteContext);
rewritten.ltrStats = this.ltrStats;
rewritten.boost(boost());
rewritten.queryName(queryName());

Expand Down Expand Up @@ -144,6 +151,11 @@ public ExplorerQueryBuilder query(QueryBuilder query) {
return this;
}

public ExplorerQueryBuilder ltrStats(LTRStats ltrStats) {
this.ltrStats = ltrStats;
return this;
}

public String statsType() {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.DocIdSetIterator;
import org.opensearch.ltr.settings.LTRSettings;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -78,6 +79,10 @@ public int hashCode() {
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost)
throws IOException {
if (!LTRSettings.isLTRPluginEnabled()) {
throw new IllegalStateException("LTR plugin is disabled. To enable, update ltr.plugin.enabled to true");
}

assert scoreMode.needsScores() : "Should not be used in filtering mode";
return new PostingsExplorerWeight(this, this.term, TermStates.build(searcher, this.term,
scoreMode.needsScores()),
Expand Down
Loading