Skip to content

Commit

Permalink
Adding opensearch query running.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzonthemtn committed Nov 28, 2024
1 parent 6f95612 commit 2687f87
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
import org.opensearch.client.Client;
import org.opensearch.eval.SearchQualityEvaluationPlugin;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.search.SearchHit;
import org.opensearch.search.builder.SearchSourceBuilder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class OpenSearchQuerySetRunner extends QuerySetRunner {

Expand All @@ -42,18 +45,40 @@ public QuerySetRunResult run(String querySetId) {
// The queries from the query set that will be run.
final Collection<String> queries = (Collection<String>) searchResponse.getHits().getAt(0).getSourceAsMap().get("queries");

// The results of each query.
final Collection<QueryResult> queryResults = new ArrayList<>();

// 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));
searchSourceBuilder.query(QueryBuilders.matchQuery("title", query));
// TODO: Just fetch the id ("asin") field and not all the unnecessary fields.

// TODO: Allow for setting this index name.
final SearchRequest searchRequest = new SearchRequest("ecommerce");
getQuerySetSearchRequest.source(getQuerySetSearchSourceBuilder);

final SearchResponse sr = client.search(searchRequest).get();

final List<String> orderedDocumentIds = new ArrayList<>();

for(final SearchHit hit : sr.getHits().getHits()) {

// TODO: This field needs to be customizable.
orderedDocumentIds.add(hit.getFields().get("asin").toString());

}

queryResults.add(new QueryResult(orderedDocumentIds));

}

// TODO: Calculate the search metrics given the results and the judgments.
final SearchMetrics searchMetrics = new SearchMetrics();
final QuerySetRunResult querySetRunResult = new QuerySetRunResult(searchMetrics);
return querySetRunResult;

return new QuerySetRunResult(queryResults, searchMetrics);

} catch (Exception e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 java.util.List;

public class QueryResult {

private final List<String> orderedDocumentIds;

public QueryResult(final List<String> orderedDocumentIds) {
this.orderedDocumentIds = orderedDocumentIds;
}

public List<String> getOrderedDocumentIds() {
return orderedDocumentIds;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@
*/
package org.opensearch.eval.runners;

import java.util.Collection;

public class QuerySetRunResult {

private SearchMetrics searchMetrics;
private final Collection<QueryResult> queryResults;
private final SearchMetrics searchMetrics;

public QuerySetRunResult(final SearchMetrics searchMetrics) {
public QuerySetRunResult(final Collection<QueryResult> queryResults, final SearchMetrics searchMetrics) {
this.queryResults = queryResults;
this.searchMetrics = searchMetrics;
}

public SearchMetrics getSearchMetrics() {
return searchMetrics;
}

public void setSearchMetrics(SearchMetrics searchMetrics) {
this.searchMetrics = searchMetrics;
public Collection<QueryResult> getQueryResults() {
return queryResults;
}

}

0 comments on commit 2687f87

Please sign in to comment.