|
7 | 7 |
|
8 | 8 | package org.elasticsearch.xpack.inference.integration; |
9 | 9 |
|
10 | | -import org.elasticsearch.action.DocWriteResponse; |
| 10 | +import org.elasticsearch.action.DocWriteResponse;; |
11 | 11 | import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; |
12 | 12 | import org.elasticsearch.action.search.SearchRequest; |
13 | | -import org.elasticsearch.action.search.SearchResponse; |
14 | 13 | import org.elasticsearch.common.settings.Settings; |
15 | 14 | import org.elasticsearch.core.TimeValue; |
16 | 15 | import org.elasticsearch.index.IndexVersion; |
17 | | -import org.elasticsearch.index.IndexVersions; |
18 | | -import org.elasticsearch.index.query.QueryBuilders; |
19 | 16 | import org.elasticsearch.license.LicenseSettings; |
20 | 17 | import org.elasticsearch.plugins.Plugin; |
21 | 18 | import org.elasticsearch.search.builder.SearchSourceBuilder; |
| 19 | +import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; |
22 | 20 | import org.elasticsearch.test.ESIntegTestCase; |
23 | 21 | import org.elasticsearch.test.index.IndexVersionUtils; |
24 | 22 | import org.elasticsearch.xcontent.XContentBuilder; |
|
38 | 36 | import java.util.stream.Collectors; |
39 | 37 |
|
40 | 38 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 39 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHighlight; |
| 40 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; |
| 41 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse; |
41 | 42 | import static org.hamcrest.Matchers.equalTo; |
42 | 43 |
|
43 | 44 | public class SemanticTextIndexVersionIT extends ESIntegTestCase { |
44 | | - private static final IndexVersion SEMANTIC_TEXT_INTRODUCED_VERSION = IndexVersions.SEMANTIC_TEXT_FIELD_TYPE; |
45 | | - private static final IndexVersion SEMANTIC_TEXT_NEW_FORMAT = IndexVersions.INFERENCE_METADATA_FIELDS_BACKPORT; |
| 45 | + private static final IndexVersion SEMANTIC_TEXT_INTRODUCED_VERSION = IndexVersion.fromId(8512000); |
46 | 46 |
|
47 | 47 | private Set<IndexVersion> availableVersions; |
48 | | - private static final int MIN_NUMBER_OF_TESTS_TO_RUN = 10; |
| 48 | + private static final int MIN_NUMBER_OF_TESTS_TO_RUN = 1; |
49 | 49 |
|
50 | 50 | @Before |
51 | 51 | public void setup() throws Exception { |
52 | 52 | Utils.storeSparseModel(client()); |
53 | | - availableVersions = IndexVersionUtils.allReleasedVersions() |
54 | | - .stream() |
55 | | - .filter((version -> version.onOrAfter(SEMANTIC_TEXT_INTRODUCED_VERSION))) |
| 53 | + availableVersions = IndexVersionUtils.allReleasedVersions().stream() |
| 54 | + .filter(indexVersion -> indexVersion.after(SEMANTIC_TEXT_INTRODUCED_VERSION)) |
56 | 55 | .collect(Collectors.toSet()); |
57 | 56 |
|
58 | 57 | logger.info("Available versions for testing: {}", availableVersions); |
@@ -100,11 +99,11 @@ protected Map<String, IndexVersion> createRandomVersionIndices() throws IOExcept |
100 | 99 | return result; |
101 | 100 | } |
102 | 101 |
|
103 | | - public void test() throws Exception { |
| 102 | + public void testSemanticText() throws Exception { |
104 | 103 | Map<String, IndexVersion> indices = createRandomVersionIndices(); |
105 | 104 | for (String indexName : indices.keySet()) { |
106 | 105 | IndexVersion version = indices.get(indexName); |
107 | | - logger.info("Testing index [{}] with version [{}]", indexName, version); |
| 106 | + logger.info("Testing index [{}] with version [{}] [{}]", indexName, version, version.toReleaseVersion()); |
108 | 107 |
|
109 | 108 | // Test index creation |
110 | 109 | assertTrue("Index " + indexName + " should exist", indexExists(indexName)); |
@@ -136,51 +135,35 @@ public void test() throws Exception { |
136 | 135 |
|
137 | 136 | // Test data ingestion |
138 | 137 | String[] text = new String[] { "inference test", "another inference test" }; |
| 138 | + DocWriteResponse docWriteResponse = client().prepareIndex(indexName) |
| 139 | + .setSource(Map.of("semantic_field", text)) |
| 140 | + .get(); |
139 | 141 |
|
140 | | - DocWriteResponse response = client().prepareIndex(indexName).setSource(Map.of("semantic_field", text)).get(); |
141 | | - |
142 | | - assertEquals("Document should be created", "created", response.getResult().toString().toLowerCase()); |
| 142 | + assertEquals("Document should be created", "created", docWriteResponse.getResult().toString().toLowerCase()); |
143 | 143 |
|
| 144 | + // Ensure index is ready |
144 | 145 | client().admin().indices().refresh(new RefreshRequest(indexName)).get(); |
145 | | - |
146 | | - // Simple search |
147 | | - SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().trackTotalHits(true); |
148 | | - SearchResponse searchResponse = client().search(new SearchRequest(indexName).source(sourceBuilder)).get(); |
149 | | - try { |
150 | | - assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(1L)); |
151 | | - } finally { |
152 | | - searchResponse.decRef(); |
153 | | - } |
154 | | - |
155 | | - // Search with query |
156 | | - SearchResponse searchWithQueryResponse = null; |
157 | | - if (version.after(SEMANTIC_TEXT_NEW_FORMAT)) { |
158 | | - searchWithQueryResponse = client().search( |
159 | | - new SearchRequest(indexName).source( |
160 | | - sourceBuilder.query(QueryBuilders.matchQuery("semantic_field", "another inference test")) |
161 | | - ) |
162 | | - ).get(); |
163 | | - } else { |
164 | | - String semanticQuery = """ |
165 | | - { |
166 | | - "semantic": { |
167 | | - "field": "semantic_field", |
168 | | - "query": "inference" |
169 | | - } |
170 | | - } |
171 | | - """; |
172 | | - searchWithQueryResponse = client().search( |
173 | | - new SearchRequest(indexName).source(sourceBuilder.query(new SemanticQueryBuilder("semantic_field", "inference test"))) |
174 | | - ).get(); |
175 | | - } |
176 | | - |
177 | | - try { |
178 | | - assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(1L)); |
179 | | - } finally { |
180 | | - searchResponse.decRef(); |
181 | | - } |
182 | | - |
| 146 | + ensureGreen(indexName); |
| 147 | + |
| 148 | + // Semantic Search |
| 149 | + SearchSourceBuilder sourceBuilder = new SearchSourceBuilder() |
| 150 | + .query(new SemanticQueryBuilder("semantic_field", "inference")) |
| 151 | + .trackTotalHits(true); |
| 152 | + |
| 153 | + assertResponse(client().search(new SearchRequest(indexName).source(sourceBuilder)), response -> { |
| 154 | + assertHitCount(response, 1L); |
| 155 | + }); |
| 156 | + |
| 157 | + //Semantic Search with highlighter |
| 158 | + SearchSourceBuilder sourceHighlighterBuilder = new SearchSourceBuilder() |
| 159 | + .query(new SemanticQueryBuilder("semantic_field", "inference")) |
| 160 | + .highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("semantic_field").numOfFragments(1))) |
| 161 | + .trackTotalHits(true); |
| 162 | + |
| 163 | + assertResponse(client().search(new SearchRequest(indexName).source(sourceBuilder)), response -> { |
| 164 | + assertHitCount(response, 1L); |
| 165 | + assertHighlight(response, 0, "semantic_field", 0, 2, equalTo("inference")); |
| 166 | + }); |
183 | 167 | } |
184 | 168 | } |
185 | | - |
186 | 169 | } |
0 commit comments