Skip to content

Commit

Permalink
Adding query set runner.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzonthemtn committed Nov 28, 2024
1 parent f2c56b1 commit 6f95612
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,28 @@
import org.opensearch.action.delete.DeleteResponse;
import org.opensearch.action.index.IndexRequest;
import org.opensearch.action.index.IndexResponse;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.action.support.WriteRequest;
import org.opensearch.client.node.NodeClient;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.eval.judgments.clickmodel.coec.CoecClickModel;
import org.opensearch.eval.judgments.clickmodel.coec.CoecClickModelParameters;
import org.opensearch.eval.runners.OpenSearchQuerySetRunner;
import org.opensearch.eval.runners.QuerySetRunResult;
import org.opensearch.eval.samplers.AllQueriesQuerySampler;
import org.opensearch.eval.samplers.AllQueriesQuerySamplerParameters;
import org.opensearch.eval.samplers.ProbabilityProportionalToSizeAbstractQuerySampler;
import org.opensearch.eval.samplers.ProbabilityProportionalToSizeParameters;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.jobscheduler.spi.schedule.IntervalSchedule;
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestResponse;
import org.opensearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -150,37 +147,21 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
// Handle running query sets.
} else if(QUERYSET_RUN_URL.equalsIgnoreCase(request.path())) {

final String id = request.param("id");

// Get the query set.
final SearchSourceBuilder getQuerySetSearchSourceBuilder = new SearchSourceBuilder();
getQuerySetSearchSourceBuilder.query(QueryBuilders.matchQuery("_id", id));

final SearchRequest getQuerySetSearchRequest = new SearchRequest(SearchQualityEvaluationPlugin.QUERY_SETS_INDEX_NAME);
getQuerySetSearchRequest.source(getQuerySetSearchSourceBuilder);
final String querySetId = request.param("id");

try {

final SearchResponse searchResponse = client.search(getQuerySetSearchRequest).get();

// The queries from the query set that will be run.
final Collection<String> queries = (Collection<String>) searchResponse.getHits().getAt(0).getSourceAsMap().get("queries");
final OpenSearchQuerySetRunner openSearchQuerySetRunner = new OpenSearchQuerySetRunner(client);
final QuerySetRunResult querySetRunResult = openSearchQuerySetRunner.run(querySetId);

// TODO: Initiate the running of the query set.
for(final String query : queries) {

// TODO: What should this query be?
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("_id", id));

}
// TODO: Index the querySetRunResult.

} catch (Exception ex) {
LOGGER.error("Unable to retrieve query set with ID {}", id);
LOGGER.error("Unable to retrieve query set with ID {}", querySetId);
return restChannel -> restChannel.sendResponse(new BytesRestResponse(RestStatus.INTERNAL_SERVER_ERROR, ex.getMessage()));
}

return restChannel -> restChannel.sendResponse(new BytesRestResponse(RestStatus.OK, "{\"message\": \"Query set " + id + " run initiated.\"}"));
return restChannel -> restChannel.sendResponse(new BytesRestResponse(RestStatus.OK, "{\"message\": \"Query set " + querySetId + " run initiated.\"}"));

// Handle the on-demand creation of implicit judgments.
} else if(IMPLICIT_JUDGMENTS_URL.equalsIgnoreCase(request.path())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.eval.runners;

import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.client.Client;
import org.opensearch.eval.SearchQualityEvaluationPlugin;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.search.builder.SearchSourceBuilder;

import java.util.Collection;

public class OpenSearchQuerySetRunner extends QuerySetRunner {

final Client client;

public OpenSearchQuerySetRunner(final Client client) {
this.client = client;
}

@Override
public QuerySetRunResult run(String querySetId) {

// Get the query set.
final SearchSourceBuilder getQuerySetSearchSourceBuilder = new SearchSourceBuilder();
getQuerySetSearchSourceBuilder.query(QueryBuilders.matchQuery("_id", querySetId));

final SearchRequest getQuerySetSearchRequest = new SearchRequest(SearchQualityEvaluationPlugin.QUERY_SETS_INDEX_NAME);
getQuerySetSearchRequest.source(getQuerySetSearchSourceBuilder);

try {

final SearchResponse searchResponse = client.search(getQuerySetSearchRequest).get();

// The queries from the query set that will be run.
final Collection<String> queries = (Collection<String>) searchResponse.getHits().getAt(0).getSourceAsMap().get("queries");

// TODO: Initiate the running of the query set.
for(final String query : queries) {

// TODO: What should this query be?
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("_id", querySetId));

}

final SearchMetrics searchMetrics = new SearchMetrics();
final QuerySetRunResult querySetRunResult = new QuerySetRunResult(searchMetrics);
return querySetRunResult;

} catch (Exception e) {
throw new RuntimeException(e);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.eval.runners;

public class QuerySetRunResult {

private SearchMetrics searchMetrics;

public QuerySetRunResult(final SearchMetrics searchMetrics) {
this.searchMetrics = searchMetrics;
}

public SearchMetrics getSearchMetrics() {
return searchMetrics;
}

public void setSearchMetrics(SearchMetrics searchMetrics) {
this.searchMetrics = searchMetrics;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.eval.runners;

public abstract class QuerySetRunner {

abstract QuerySetRunResult run(String querySetId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.eval.runners;

public class SearchMetrics {

}

0 comments on commit 6f95612

Please sign in to comment.