|
| 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 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.search.profile.dfs; |
| 10 | + |
| 11 | +import org.apache.lucene.tests.util.English; |
| 12 | +import org.elasticsearch.action.index.IndexRequestBuilder; |
| 13 | +import org.elasticsearch.action.search.SearchResponse; |
| 14 | +import org.elasticsearch.action.search.SearchType; |
| 15 | +import org.elasticsearch.index.query.QueryBuilder; |
| 16 | +import org.elasticsearch.search.profile.ProfileResult; |
| 17 | +import org.elasticsearch.search.profile.SearchProfileDfsPhaseResult; |
| 18 | +import org.elasticsearch.search.profile.SearchProfileShardResult; |
| 19 | +import org.elasticsearch.search.profile.query.CollectorResult; |
| 20 | +import org.elasticsearch.search.profile.query.QueryProfileShardResult; |
| 21 | +import org.elasticsearch.search.vectors.KnnSearchBuilder; |
| 22 | +import org.elasticsearch.test.ESIntegTestCase; |
| 23 | +import org.elasticsearch.xcontent.XContentFactory; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import static org.elasticsearch.search.profile.query.RandomQueryGenerator.randomQueryBuilder; |
| 30 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 31 | +import static org.hamcrest.Matchers.emptyOrNullString; |
| 32 | +import static org.hamcrest.Matchers.equalTo; |
| 33 | +import static org.hamcrest.Matchers.greaterThan; |
| 34 | +import static org.hamcrest.Matchers.is; |
| 35 | +import static org.hamcrest.Matchers.not; |
| 36 | +import static org.hamcrest.Matchers.notNullValue; |
| 37 | + |
| 38 | +public class DfsProfilerIT extends ESIntegTestCase { |
| 39 | + |
| 40 | + private static final int KNN_DIM = 3; |
| 41 | + |
| 42 | + public void testProfileDfs() throws Exception { |
| 43 | + String textField = "text_field"; |
| 44 | + String numericField = "number"; |
| 45 | + String vectorField = "vector"; |
| 46 | + String indexName = "text-dfs-profile"; |
| 47 | + createIndex(indexName, vectorField); |
| 48 | + ensureGreen(); |
| 49 | + |
| 50 | + int numDocs = randomIntBetween(10, 50); |
| 51 | + IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs]; |
| 52 | + for (int i = 0; i < numDocs; i++) { |
| 53 | + docs[i] = client().prepareIndex(indexName) |
| 54 | + .setId(String.valueOf(i)) |
| 55 | + .setSource( |
| 56 | + textField, |
| 57 | + English.intToEnglish(i), |
| 58 | + numericField, |
| 59 | + i, |
| 60 | + vectorField, |
| 61 | + new float[] { randomFloat(), randomFloat(), randomFloat() } |
| 62 | + ); |
| 63 | + } |
| 64 | + indexRandom(true, docs); |
| 65 | + refresh(); |
| 66 | + int iters = between(5, 10); |
| 67 | + for (int i = 0; i < iters; i++) { |
| 68 | + QueryBuilder q = randomQueryBuilder(List.of(textField), List.of(numericField), numDocs, 3); |
| 69 | + logger.info("Query: {}", q); |
| 70 | + |
| 71 | + SearchResponse resp = client().prepareSearch() |
| 72 | + .setQuery(q) |
| 73 | + .setTrackTotalHits(true) |
| 74 | + .setProfile(true) |
| 75 | + .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) |
| 76 | + .setKnnSearch(new KnnSearchBuilder(vectorField, new float[] { randomFloat(), randomFloat(), randomFloat() }, 5, 50)) |
| 77 | + .get(); |
| 78 | + |
| 79 | + assertNotNull("Profile response element should not be null", resp.getProfileResults()); |
| 80 | + assertThat("Profile response should not be an empty array", resp.getProfileResults().size(), not(0)); |
| 81 | + for (Map.Entry<String, SearchProfileShardResult> shard : resp.getProfileResults().entrySet()) { |
| 82 | + for (QueryProfileShardResult searchProfiles : shard.getValue().getQueryProfileResults()) { |
| 83 | + for (ProfileResult result : searchProfiles.getQueryResults()) { |
| 84 | + assertNotNull(result.getQueryName()); |
| 85 | + assertNotNull(result.getLuceneDescription()); |
| 86 | + assertThat(result.getTime(), greaterThan(0L)); |
| 87 | + } |
| 88 | + CollectorResult result = searchProfiles.getCollectorResult(); |
| 89 | + assertThat(result.getName(), is(not(emptyOrNullString()))); |
| 90 | + assertThat(result.getTime(), greaterThan(0L)); |
| 91 | + } |
| 92 | + SearchProfileDfsPhaseResult searchProfileDfsPhaseResult = shard.getValue().getSearchProfileDfsPhaseResult(); |
| 93 | + assertThat(searchProfileDfsPhaseResult, is(notNullValue())); |
| 94 | + for (ProfileResult result : searchProfileDfsPhaseResult.getQueryProfileShardResult().getQueryResults()) { |
| 95 | + assertNotNull(result.getQueryName()); |
| 96 | + assertNotNull(result.getLuceneDescription()); |
| 97 | + assertThat(result.getTime(), greaterThan(0L)); |
| 98 | + } |
| 99 | + CollectorResult result = searchProfileDfsPhaseResult.getQueryProfileShardResult().getCollectorResult(); |
| 100 | + assertThat(result.getName(), is(not(emptyOrNullString()))); |
| 101 | + assertThat(result.getTime(), greaterThan(0L)); |
| 102 | + ProfileResult statsResult = searchProfileDfsPhaseResult.getDfsShardResult(); |
| 103 | + assertThat(statsResult.getQueryName(), equalTo("statistics")); |
| 104 | + assertThat(result.getTime(), greaterThan(0L)); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private void createIndex(String name, String vectorField) throws IOException { |
| 110 | + assertAcked( |
| 111 | + prepareCreate(name).setMapping( |
| 112 | + XContentFactory.jsonBuilder() |
| 113 | + .startObject() |
| 114 | + .startObject("properties") |
| 115 | + .startObject(vectorField) |
| 116 | + .field("type", "dense_vector") |
| 117 | + .field("dims", KNN_DIM) |
| 118 | + .field("index", true) |
| 119 | + .field("similarity", "cosine") |
| 120 | + .endObject() |
| 121 | + .endObject() |
| 122 | + .endObject() |
| 123 | + ) |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments