Skip to content

Commit

Permalink
Creating judgments index if it does not exist.
Browse files Browse the repository at this point in the history
Signed-off-by: jzonthemtn <[email protected]>
  • Loading branch information
jzonthemtn committed Dec 9, 2024
1 parent c8643c9 commit 3fa544f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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);
}

});

}

}

0 comments on commit 3fa544f

Please sign in to comment.