From 3fa544f3024215ca4f9de34f541f08586c8b1824 Mon Sep 17 00:00:00 2001 From: jzonthemtn Date: Mon, 9 Dec 2024 09:06:48 -0500 Subject: [PATCH] Creating judgments index if it does not exist. Signed-off-by: jzonthemtn --- .../scripts/create-judgments-now.sh | 24 ++++---- .../SearchQualityEvaluationRestHandler.java | 60 +++++++++++++++++++ 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/opensearch-search-quality-evaluation-plugin/scripts/create-judgments-now.sh b/opensearch-search-quality-evaluation-plugin/scripts/create-judgments-now.sh index b35ddc8..c8744c6 100755 --- a/opensearch-search-quality-evaluation-plugin/scripts/create-judgments-now.sh +++ b/opensearch-search-quality-evaluation-plugin/scripts/create-judgments-now.sh @@ -4,18 +4,18 @@ echo "Deleting existing judgments index..." curl -s -X DELETE http://localhost:9200/judgments echo "Creating judgments index..." -curl -s -X PUT http://localhost:9200/judgments -H 'Content-Type: application/json' -d' - { - "mappings": { - "properties": { - "judgments_id": { "type": "keyword" }, - "query_id": { "type": "keyword" }, - "query": { "type": "keyword" }, - "document_id": { "type": "keyword" }, - "judgment": { "type": "double" } - } - } - }' +#curl -s -X PUT http://localhost:9200/judgments -H 'Content-Type: application/json' -d' +# { +# "mappings": { +# "properties": { +# "judgments_id": { "type": "keyword" }, +# "query_id": { "type": "keyword" }, +# "query": { "type": "keyword" }, +# "document_id": { "type": "keyword" }, +# "judgment": { "type": "double" } +# } +# } +# }' echo "Creating judgments..." curl -s -X POST "http://localhost:9200/_plugins/search_quality_eval/judgments?click_model=coec&max_rank=20" diff --git a/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/SearchQualityEvaluationRestHandler.java b/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/SearchQualityEvaluationRestHandler.java index 094f6a5..d0e9277 100644 --- a/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/SearchQualityEvaluationRestHandler.java +++ b/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/SearchQualityEvaluationRestHandler.java @@ -10,6 +10,10 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.opensearch.action.admin.indices.create.CreateIndexRequest; +import org.opensearch.action.admin.indices.create.CreateIndexResponse; +import org.opensearch.action.admin.indices.exists.indices.IndicesExistsRequest; +import org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse; import org.opensearch.action.delete.DeleteRequest; import org.opensearch.action.delete.DeleteResponse; import org.opensearch.action.index.IndexRequest; @@ -43,6 +47,8 @@ import java.util.Map; import java.util.UUID; +import static org.opensearch.eval.SearchQualityEvaluationPlugin.JUDGMENTS_INDEX_NAME; + public class SearchQualityEvaluationRestHandler extends BaseRestHandler { private static final Logger LOGGER = LogManager.getLogger(SearchQualityEvaluationRestHandler.class); @@ -200,6 +206,9 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli if (request.method().equals(RestRequest.Method.POST)) { + // Create the judgments index. + createJudgmentsIndex(client); + final long startTime = System.currentTimeMillis(); final String clickModel = request.param("click_model", "coec"); final int maxRank = Integer.parseInt(request.param("max_rank", "20")); @@ -366,4 +375,55 @@ public void onFailure(Exception e) { } + private void createJudgmentsIndex(final NodeClient client) { + + // If the judgments index does not exist we need to create it. + final IndicesExistsRequest indicesExistsRequest = new IndicesExistsRequest(JUDGMENTS_INDEX_NAME); + + client.admin().indices().exists(indicesExistsRequest, new ActionListener<>() { + + @Override + public void onResponse(final IndicesExistsResponse indicesExistsResponse) { + + if(!indicesExistsResponse.isExists()) { + + // TODO: Read this from a resource file instead. + final String mapping = "{\n" + + " \"properties\": {\n" + + " \"judgments_id\": { \"type\": \"keyword\" },\n" + + " \"query_id\": { \"type\": \"keyword\" },\n" + + " \"query\": { \"type\": \"keyword\" },\n" + + " \"document_id\": { \"type\": \"keyword\" },\n" + + " \"judgment\": { \"type\": \"double\" }\n" + + " }\n" + + " }"; + + // Create the judgments index. + final CreateIndexRequest createIndexRequest = new CreateIndexRequest(JUDGMENTS_INDEX_NAME); + createIndexRequest.mapping(mapping); + + client.admin().indices().create(createIndexRequest, new ActionListener<>() { + @Override + public void onResponse(CreateIndexResponse createIndexResponse) { + LOGGER.info("Judgments index created: {}", JUDGMENTS_INDEX_NAME); + } + + @Override + public void onFailure(Exception ex) { + throw new RuntimeException("Unable to create judgments index: " + JUDGMENTS_INDEX_NAME, ex); + } + }); + + } + } + + @Override + public void onFailure(Exception ex) { + throw new RuntimeException("Unable to determine if the judgments index exists.", ex); + } + + }); + + } + }