Skip to content

Commit 080deee

Browse files
committed
protoc code
Signed-off-by: Lantao Jin <ltjin@amazon.com>
1 parent 6c3efa1 commit 080deee

1 file changed

Lines changed: 40 additions & 5 deletions

File tree

opensearch/src/main/java/org/opensearch/sql/opensearch/request/OpenSearchQueryRequest.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import lombok.Getter;
2020
import lombok.ToString;
2121
import org.opensearch.action.search.*;
22+
import org.opensearch.cluster.metadata.DataStream;
23+
import org.opensearch.cluster.metadata.IndexMetadata;
24+
import org.opensearch.cluster.metadata.Metadata;
25+
import org.opensearch.common.Nullable;
2226
import org.opensearch.common.settings.Settings;
2327
import org.opensearch.common.unit.TimeValue;
2428
import org.opensearch.common.xcontent.XContentType;
@@ -31,6 +35,7 @@
3135
import org.opensearch.search.SearchModule;
3236
import org.opensearch.search.builder.PointInTimeBuilder;
3337
import org.opensearch.search.builder.SearchSourceBuilder;
38+
import org.opensearch.search.sort.SortOrder;
3439
import org.opensearch.sql.opensearch.data.value.OpenSearchExprValueFactory;
3540
import org.opensearch.sql.opensearch.response.OpenSearchResponse;
3641
import org.opensearch.sql.opensearch.storage.OpenSearchIndex;
@@ -73,6 +78,8 @@ public class OpenSearchQueryRequest implements OpenSearchRequest {
7378

7479
private SearchResponse searchResponse = null;
7580

81+
private Metadata metadata;
82+
7683
/** Constructor of OpenSearchQueryRequest. */
7784
public OpenSearchQueryRequest(
7885
String indexName, int size, OpenSearchExprValueFactory factory, List<String> includes) {
@@ -110,13 +117,15 @@ public OpenSearchQueryRequest(
110117
OpenSearchExprValueFactory factory,
111118
List<String> includes,
112119
TimeValue cursorKeepAlive,
113-
String pitId) {
120+
String pitId,
121+
@Nullable Metadata metadata) {
114122
this.indexName = indexName;
115123
this.sourceBuilder = sourceBuilder;
116124
this.exprValueFactory = factory;
117125
this.includes = includes;
118126
this.cursorKeepAlive = cursorKeepAlive;
119127
this.pitId = pitId;
128+
this.metadata = metadata;
120129
}
121130

122131
/**
@@ -191,10 +200,36 @@ public OpenSearchResponse searchWithPIT(Function<SearchRequest, SearchResponse>
191200
}
192201
// Set sort field for search_after
193202
if (this.sourceBuilder.sorts() == null) {
194-
this.sourceBuilder.sort(DOC_FIELD_NAME, ASC);
195-
// Workaround to preserve sort location more exactly,
196-
// see https://github.com/opensearch-project/sql/pull/3061
197-
this.sourceBuilder.sort(METADATA_FIELD_ID, ASC);
203+
boolean specifiedSort = false;
204+
if (metadata != null) {
205+
// 1. sort by `index.sort.field` if existed
206+
IndexMetadata indexMetadata = metadata.index(indexName.toString());
207+
if (indexMetadata != null) {
208+
List<String> sortFields = indexMetadata.getSettings().getAsList("index.sort.field");
209+
List<String> sortDirs = indexMetadata.getSettings().getAsList("index.sort.order");
210+
for (int i = 0; i < sortFields.size(); i++) {
211+
this.sourceBuilder.sort(sortFields.get(i), SortOrder.fromString(sortDirs.get(i)));
212+
specifiedSort = true;
213+
}
214+
}
215+
// 2. sort by `@timestamp` if the data stream is time-series optimized
216+
if (!specifiedSort) {
217+
DataStream dataStream = metadata.dataStreams().get(indexName.toString());
218+
if (dataStream != null) {
219+
String timestampField = dataStream.getTimeStampField().getName();
220+
if (timestampField.equals("@timestamp")) {
221+
this.sourceBuilder.sort(timestampField, ASC);
222+
specifiedSort = true;
223+
}
224+
}
225+
}
226+
}
227+
if (!specifiedSort) {
228+
this.sourceBuilder.sort(DOC_FIELD_NAME, ASC);
229+
// Workaround to preserve sort location more exactly,
230+
// see https://github.com/opensearch-project/sql/pull/3061
231+
this.sourceBuilder.sort(METADATA_FIELD_ID, ASC);
232+
}
198233
}
199234
SearchRequest searchRequest = new SearchRequest().source(this.sourceBuilder);
200235
this.searchResponse = searchAction.apply(searchRequest);

0 commit comments

Comments
 (0)