|
| 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.inference.integration; |
| 9 | + |
| 10 | +import org.elasticsearch.action.DocWriteResponse; |
| 11 | +import org.elasticsearch.action.search.SearchRequest; |
| 12 | +import org.elasticsearch.common.settings.Settings; |
| 13 | +import org.elasticsearch.index.query.BoolQueryBuilder; |
| 14 | +import org.elasticsearch.index.query.MatchQueryBuilder; |
| 15 | +import org.elasticsearch.index.query.QueryBuilder; |
| 16 | +import org.elasticsearch.index.query.QueryBuilders; |
| 17 | +import org.elasticsearch.inference.TaskType; |
| 18 | +import org.elasticsearch.license.LicenseSettings; |
| 19 | +import org.elasticsearch.plugins.Plugin; |
| 20 | +import org.elasticsearch.reindex.ReindexPlugin; |
| 21 | +import org.elasticsearch.search.builder.SearchSourceBuilder; |
| 22 | +import org.elasticsearch.search.vectors.KnnVectorQueryBuilder; |
| 23 | +import org.elasticsearch.test.ESIntegTestCase; |
| 24 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 25 | +import org.elasticsearch.xpack.core.ml.search.SparseVectorQueryBuilder; |
| 26 | +import org.elasticsearch.xpack.core.ml.vectors.TextEmbeddingQueryVectorBuilder; |
| 27 | +import org.elasticsearch.xpack.inference.FakeMlPlugin; |
| 28 | +import org.elasticsearch.xpack.inference.LocalStateInferencePlugin; |
| 29 | +import org.elasticsearch.xpack.inference.mock.TestInferenceServicePlugin; |
| 30 | +import org.elasticsearch.xpack.inference.queries.SemanticQueryBuilder; |
| 31 | +import org.junit.After; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.Collection; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.function.BiFunction; |
| 39 | + |
| 40 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 41 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse; |
| 42 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 43 | +import static org.hamcrest.CoreMatchers.is; |
| 44 | + |
| 45 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE, numDataNodes = 1) |
| 46 | +public class ManyInferenceQueryClausesIT extends ESIntegTestCase { |
| 47 | + private static final String INDEX_NAME = "test_index"; |
| 48 | + |
| 49 | + private static final Map<String, Object> SPARSE_EMBEDDING_SERVICE_SETTINGS = Map.of("model", "my_model", "api_key", "my_api_key"); |
| 50 | + private static final Map<String, Object> TEXT_EMBEDDING_SERVICE_SETTINGS = Map.of( |
| 51 | + "model", |
| 52 | + "my_model", |
| 53 | + "dimensions", |
| 54 | + 256, |
| 55 | + "similarity", |
| 56 | + "cosine", |
| 57 | + "api_key", |
| 58 | + "my_api_key" |
| 59 | + ); |
| 60 | + |
| 61 | + private final Map<String, TaskType> inferenceIds = new HashMap<>(); |
| 62 | + |
| 63 | + @Override |
| 64 | + protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { |
| 65 | + return Settings.builder().put(LicenseSettings.SELF_GENERATED_LICENSE_TYPE.getKey(), "trial").build(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 70 | + return List.of(LocalStateInferencePlugin.class, TestInferenceServicePlugin.class, ReindexPlugin.class, FakeMlPlugin.class); |
| 71 | + } |
| 72 | + |
| 73 | + @After |
| 74 | + public void cleanUp() { |
| 75 | + IntegrationTestUtils.deleteIndex(client(), INDEX_NAME); |
| 76 | + for (var entry : inferenceIds.entrySet()) { |
| 77 | + IntegrationTestUtils.deleteInferenceEndpoint(client(), entry.getValue(), entry.getKey()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public void testManySemanticQueryClauses() throws Exception { |
| 82 | + manyQueryClausesTestCase(randomIntBetween(18, 24), SemanticQueryBuilder::new, TaskType.SPARSE_EMBEDDING); |
| 83 | + } |
| 84 | + |
| 85 | + public void testManyMatchQueryClauses() throws Exception { |
| 86 | + manyQueryClausesTestCase(randomIntBetween(18, 24), MatchQueryBuilder::new, TaskType.SPARSE_EMBEDDING); |
| 87 | + } |
| 88 | + |
| 89 | + public void testManySparseVectorQueryClauses() throws Exception { |
| 90 | + manyQueryClausesTestCase(randomIntBetween(18, 24), (f, q) -> new SparseVectorQueryBuilder(f, null, q), TaskType.SPARSE_EMBEDDING); |
| 91 | + } |
| 92 | + |
| 93 | + public void testManyKnnQueryClauses() throws Exception { |
| 94 | + int clauseCount = randomIntBetween(18, 24); |
| 95 | + manyQueryClausesTestCase( |
| 96 | + clauseCount, |
| 97 | + (f, q) -> new KnnVectorQueryBuilder(f, new TextEmbeddingQueryVectorBuilder(null, q), clauseCount, clauseCount * 10, null, null), |
| 98 | + TaskType.TEXT_EMBEDDING |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + private void manyQueryClausesTestCase( |
| 103 | + int clauseCount, |
| 104 | + BiFunction<String, String, QueryBuilder> clauseGenerator, |
| 105 | + TaskType semanticTextFieldTaskType |
| 106 | + ) throws Exception { |
| 107 | + Map<String, Object> inferenceEndpointServiceSettings = getServiceSettings(semanticTextFieldTaskType); |
| 108 | + Map<String, String> semanticTextFields = new HashMap<>(clauseCount); |
| 109 | + for (int i = 0; i < clauseCount; i++) { |
| 110 | + String fieldName = randomAlphaOfLength(10); |
| 111 | + String inferenceId = randomIdentifier(); |
| 112 | + |
| 113 | + createInferenceEndpoint(semanticTextFieldTaskType, inferenceId, inferenceEndpointServiceSettings); |
| 114 | + semanticTextFields.put(fieldName, inferenceId); |
| 115 | + } |
| 116 | + |
| 117 | + XContentBuilder mapping = IntegrationTestUtils.generateSemanticTextMapping(semanticTextFields); |
| 118 | + assertAcked(prepareCreate(INDEX_NAME).setMapping(mapping)); |
| 119 | + |
| 120 | + BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); |
| 121 | + for (String semanticTextField : semanticTextFields.keySet()) { |
| 122 | + Map<String, Object> source = Map.of(semanticTextField, randomAlphaOfLength(10)); |
| 123 | + DocWriteResponse docWriteResponse = client().prepareIndex(INDEX_NAME).setSource(source).get(TEST_REQUEST_TIMEOUT); |
| 124 | + assertThat(docWriteResponse.getResult(), is(DocWriteResponse.Result.CREATED)); |
| 125 | + |
| 126 | + boolQuery.should(clauseGenerator.apply(semanticTextField, randomAlphaOfLength(10))); |
| 127 | + } |
| 128 | + client().admin().indices().prepareRefresh(INDEX_NAME).get(); |
| 129 | + |
| 130 | + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(boolQuery).size(clauseCount); |
| 131 | + SearchRequest searchRequest = new SearchRequest(new String[] { INDEX_NAME }, searchSourceBuilder); |
| 132 | + assertResponse(client().search(searchRequest), response -> { |
| 133 | + assertThat(response.getSuccessfulShards(), equalTo(response.getTotalShards())); |
| 134 | + assertThat(response.getHits().getTotalHits().value(), equalTo((long) clauseCount)); |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + private void createInferenceEndpoint(TaskType taskType, String inferenceId, Map<String, Object> serviceSettings) throws IOException { |
| 139 | + IntegrationTestUtils.createInferenceEndpoint(client(), taskType, inferenceId, serviceSettings); |
| 140 | + inferenceIds.put(inferenceId, taskType); |
| 141 | + } |
| 142 | + |
| 143 | + private static Map<String, Object> getServiceSettings(TaskType taskType) { |
| 144 | + return switch (taskType) { |
| 145 | + case SPARSE_EMBEDDING -> SPARSE_EMBEDDING_SERVICE_SETTINGS; |
| 146 | + case TEXT_EMBEDDING -> TEXT_EMBEDDING_SERVICE_SETTINGS; |
| 147 | + default -> throw new IllegalArgumentException("Unhandled task type [" + taskType + "]"); |
| 148 | + }; |
| 149 | + } |
| 150 | +} |
0 commit comments