Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating judgments index if it does not exist #53

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

});

}

}
Loading