From 2687f878be23b3af2b6cefc35bdcf84ee51eb9e3 Mon Sep 17 00:00:00 2001 From: jzonthemtn Date: Thu, 28 Nov 2024 09:37:35 -0500 Subject: [PATCH] Adding opensearch query running. --- .../runners/OpenSearchQuerySetRunner.java | 31 +++++++++++++++++-- .../opensearch/eval/runners/QueryResult.java | 25 +++++++++++++++ .../eval/runners/QuerySetRunResult.java | 12 ++++--- 3 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/runners/QueryResult.java diff --git a/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/runners/OpenSearchQuerySetRunner.java b/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/runners/OpenSearchQuerySetRunner.java index d58d28f..6729ea5 100644 --- a/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/runners/OpenSearchQuerySetRunner.java +++ b/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/runners/OpenSearchQuerySetRunner.java @@ -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 { @@ -42,18 +45,40 @@ public QuerySetRunResult run(String querySetId) { // The queries from the query set that will be run. final Collection queries = (Collection) searchResponse.getHits().getAt(0).getSourceAsMap().get("queries"); + // The results of each query. + final Collection 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 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); diff --git a/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/runners/QueryResult.java b/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/runners/QueryResult.java new file mode 100644 index 0000000..e26679f --- /dev/null +++ b/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/runners/QueryResult.java @@ -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 orderedDocumentIds; + + public QueryResult(final List orderedDocumentIds) { + this.orderedDocumentIds = orderedDocumentIds; + } + + public List getOrderedDocumentIds() { + return orderedDocumentIds; + } + +} diff --git a/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/runners/QuerySetRunResult.java b/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/runners/QuerySetRunResult.java index 8710fd7..2491e3d 100644 --- a/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/runners/QuerySetRunResult.java +++ b/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/runners/QuerySetRunResult.java @@ -8,11 +8,15 @@ */ package org.opensearch.eval.runners; +import java.util.Collection; + public class QuerySetRunResult { - private SearchMetrics searchMetrics; + private final Collection queryResults; + private final SearchMetrics searchMetrics; - public QuerySetRunResult(final SearchMetrics searchMetrics) { + public QuerySetRunResult(final Collection queryResults, final SearchMetrics searchMetrics) { + this.queryResults = queryResults; this.searchMetrics = searchMetrics; } @@ -20,8 +24,8 @@ public SearchMetrics getSearchMetrics() { return searchMetrics; } - public void setSearchMetrics(SearchMetrics searchMetrics) { - this.searchMetrics = searchMetrics; + public Collection getQueryResults() { + return queryResults; } }