|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.qa.rest; |
| 9 | + |
| 10 | +import org.elasticsearch.client.Request; |
| 11 | +import org.elasticsearch.client.ResponseException; |
| 12 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 13 | +import org.elasticsearch.xpack.esql.action.EsqlCapabilities; |
| 14 | +import org.junit.After; |
| 15 | +import org.junit.Before; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import static org.elasticsearch.xpack.esql.CsvTestsDataLoader.createRerankInferenceEndpoint; |
| 22 | +import static org.elasticsearch.xpack.esql.CsvTestsDataLoader.deleteRerankInferenceEndpoint; |
| 23 | +import static org.hamcrest.core.StringContains.containsString; |
| 24 | + |
| 25 | +public class RestRerankTestCase extends ESRestTestCase { |
| 26 | + |
| 27 | + @Before |
| 28 | + @After |
| 29 | + public void assertRequestBreakerEmpty() throws Exception { |
| 30 | + EsqlSpecTestCase.assertRequestBreakerEmpty(); |
| 31 | + } |
| 32 | + |
| 33 | + @Before |
| 34 | + public void setUpInferenceEndpoint() throws IOException { |
| 35 | + createRerankInferenceEndpoint(adminClient()); |
| 36 | + } |
| 37 | + |
| 38 | + @Before |
| 39 | + public void setUpTestIndex() throws IOException { |
| 40 | + Request request = new Request("PUT", "/rerank-test-index"); |
| 41 | + request.setJsonEntity(""" |
| 42 | + { |
| 43 | + "mappings": { |
| 44 | + "properties": { |
| 45 | + "title": { "type": "text" }, |
| 46 | + "author": { "type": "text" } |
| 47 | + } |
| 48 | + } |
| 49 | + }"""); |
| 50 | + assertEquals(200, client().performRequest(request).getStatusLine().getStatusCode()); |
| 51 | + |
| 52 | + request = new Request("POST", "/rerank-test-index/_bulk"); |
| 53 | + request.addParameter("refresh", "true"); |
| 54 | + request.setJsonEntity(""" |
| 55 | + { "index": {"_id": 1} } |
| 56 | + { "title": "The Future of Exploration", "author": "John Doe" } |
| 57 | + { "index": {"_id": 2} } |
| 58 | + { "title": "Deep Sea Exploration", "author": "Jane Smith" } |
| 59 | + { "index": {"_id": 3} } |
| 60 | + { "title": "History of Space Exploration", "author": "Alice Johnson" } |
| 61 | + """); |
| 62 | + assertEquals(200, client().performRequest(request).getStatusLine().getStatusCode()); |
| 63 | + } |
| 64 | + |
| 65 | + @After |
| 66 | + public void wipeData() throws IOException { |
| 67 | + try { |
| 68 | + adminClient().performRequest(new Request("DELETE", "/rerank-test-index")); |
| 69 | + } catch (ResponseException e) { |
| 70 | + // 404 here just means we had no indexes |
| 71 | + if (e.getResponse().getStatusLine().getStatusCode() != 404) { |
| 72 | + throw e; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + deleteRerankInferenceEndpoint(adminClient()); |
| 77 | + } |
| 78 | + |
| 79 | + public void testRerankWithSingleField() throws IOException { |
| 80 | + assumeTrue("semantic text capability not available", EsqlCapabilities.Cap.RERANK.isEnabled()); |
| 81 | + |
| 82 | + String query = """ |
| 83 | + FROM rerank-test-index |
| 84 | + | WHERE match(title, "exploration") |
| 85 | + | RERANK "exploration" ON title WITH test_reranker |
| 86 | + | EVAL _score = ROUND(_score, 5) |
| 87 | + """; |
| 88 | + |
| 89 | + Map<String, Object> result = runEsqlQuery(query); |
| 90 | + |
| 91 | + var expectedValues = List.of( |
| 92 | + List.of("Jane Smith", "Deep Sea Exploration", 0.02941), |
| 93 | + List.of("John Doe", "The Future of Exploration", 0.02632), |
| 94 | + List.of("Alice Johnson", "History of Space Exploration", 0.02381) |
| 95 | + ); |
| 96 | + |
| 97 | + assertResultMap(result, defaultOutputColumns(), expectedValues); |
| 98 | + } |
| 99 | + |
| 100 | + public void testRerankWithMultipleFields() throws IOException { |
| 101 | + assumeTrue("semantic text capability not available", EsqlCapabilities.Cap.RERANK.isEnabled()); |
| 102 | + |
| 103 | + String query = """ |
| 104 | + FROM rerank-test-index |
| 105 | + | WHERE match(title, "exploration") |
| 106 | + | RERANK "exploration" ON title, author WITH test_reranker |
| 107 | + | EVAL _score = ROUND(_score, 5) |
| 108 | + """; |
| 109 | + |
| 110 | + Map<String, Object> result = runEsqlQuery(query); |
| 111 | + ; |
| 112 | + var expectedValues = List.of( |
| 113 | + List.of("Jane Smith", "Deep Sea Exploration", 0.01818), |
| 114 | + List.of("John Doe", "The Future of Exploration", 0.01754), |
| 115 | + List.of("Alice Johnson", "History of Space Exploration", 0.01515) |
| 116 | + ); |
| 117 | + |
| 118 | + assertResultMap(result, defaultOutputColumns(), expectedValues); |
| 119 | + } |
| 120 | + |
| 121 | + public void testRerankWithPositionalParams() throws IOException { |
| 122 | + assumeTrue("semantic text capability not available", EsqlCapabilities.Cap.RERANK.isEnabled()); |
| 123 | + |
| 124 | + String query = """ |
| 125 | + FROM rerank-test-index |
| 126 | + | WHERE match(title, "exploration") |
| 127 | + | RERANK ? ON title WITH ? |
| 128 | + | EVAL _score = ROUND(_score, 5) |
| 129 | + """; |
| 130 | + |
| 131 | + Map<String, Object> result = runEsqlQuery(query, "[\"exploration\", \"test_reranker\"]"); |
| 132 | + |
| 133 | + var expectedValues = List.of( |
| 134 | + List.of("Jane Smith", "Deep Sea Exploration", 0.02941), |
| 135 | + List.of("John Doe", "The Future of Exploration", 0.02632), |
| 136 | + List.of("Alice Johnson", "History of Space Exploration", 0.02381) |
| 137 | + ); |
| 138 | + |
| 139 | + assertResultMap(result, defaultOutputColumns(), expectedValues); |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + public void testRerankWithNamedParams() throws IOException { |
| 144 | + assumeTrue("semantic text capability not available", EsqlCapabilities.Cap.RERANK.isEnabled()); |
| 145 | + |
| 146 | + String query = """ |
| 147 | + FROM rerank-test-index |
| 148 | + | WHERE match(title, ?queryText) |
| 149 | + | RERANK ?queryText ON title WITH ?inferenceId |
| 150 | + | EVAL _score = ROUND(_score, 5) |
| 151 | + """; |
| 152 | + |
| 153 | + Map<String, Object> result = runEsqlQuery(query, "[{\"queryText\": \"exploration\"}, {\"inferenceId\": \"test_reranker\"}]"); |
| 154 | + |
| 155 | + var expectedValues = List.of( |
| 156 | + List.of("Jane Smith", "Deep Sea Exploration", 0.02941), |
| 157 | + List.of("John Doe", "The Future of Exploration", 0.02632), |
| 158 | + List.of("Alice Johnson", "History of Space Exploration", 0.02381) |
| 159 | + ); |
| 160 | + |
| 161 | + assertResultMap(result, defaultOutputColumns(), expectedValues); |
| 162 | + } |
| 163 | + |
| 164 | + public void testRerankWithMissingInferenceId() { |
| 165 | + assumeTrue("semantic text capability not available", EsqlCapabilities.Cap.RERANK.isEnabled()); |
| 166 | + |
| 167 | + String query = """ |
| 168 | + FROM rerank-test-index |
| 169 | + | WHERE match(title, "exploration") |
| 170 | + | RERANK "exploration" ON title WITH test_missing |
| 171 | + | EVAL _score = ROUND(_score, 5) |
| 172 | + """; |
| 173 | + |
| 174 | + ResponseException re = expectThrows(ResponseException.class, () -> runEsqlQuery(query)); |
| 175 | + assertThat(re.getMessage(), containsString("Inference endpoint not found")); |
| 176 | + } |
| 177 | + |
| 178 | + private static List<Map<String, String>> defaultOutputColumns() { |
| 179 | + return List.of( |
| 180 | + Map.of("name", "author", "type", "text"), |
| 181 | + Map.of("name", "title", "type", "text"), |
| 182 | + Map.of("name", "_score", "type", "double") |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + private Map<String, Object> runEsqlQuery(String query) throws IOException { |
| 187 | + RestEsqlTestCase.RequestObjectBuilder builder = RestEsqlTestCase.requestObjectBuilder().query(query); |
| 188 | + return RestEsqlTestCase.runEsqlSync(builder); |
| 189 | + } |
| 190 | + |
| 191 | + private Map<String, Object> runEsqlQuery(String query, String params) throws IOException { |
| 192 | + RestEsqlTestCase.RequestObjectBuilder builder = RestEsqlTestCase.requestObjectBuilder().query(query).params(params); |
| 193 | + return RestEsqlTestCase.runEsqlSync(builder); |
| 194 | + } |
| 195 | +} |
0 commit comments