Skip to content

Enable sort optimization on int, short and byte fields #127968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/127968.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 127968
summary: "Enable sort optimization on int, short and byte fields"
area: Search
type: enhancement
issues:
- 127965
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.upgrades;

import com.carrotsearch.randomizedtesting.annotations.Name;

import org.elasticsearch.client.Request;
import org.elasticsearch.common.settings.Settings;

import java.util.List;
import java.util.Map;

/**
* Tests that index sorting works correctly after a rolling upgrade.
*/
public class IndexSortUpgradeIT extends AbstractRollingUpgradeTestCase {

public IndexSortUpgradeIT(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
}

@SuppressWarnings("unchecked")
public void testIndexSortForNumericTypes() throws Exception {
record IndexConfig(String indexName, String fieldName, String fieldType) {}
var configs = new IndexConfig[] {
new IndexConfig("index_byte", "byte_field", "byte"),
new IndexConfig("index_short", "short_field", "short"),
new IndexConfig("index_int", "int_field", "integer") };

if (isOldCluster()) {
int numShards = randomIntBetween(1, 3);
for (var config : configs) {
createIndex(
config.indexName(),
Settings.builder()
.put("index.number_of_shards", numShards)
.put("index.number_of_replicas", 0)
.put("index.sort.field", config.fieldName())
.put("index.sort.order", "desc")
.build(),
"""
{
"properties": {
"%s": {
"type": "%s"
}
}
}
""".formatted(config.fieldName(), config.fieldType())
);
}
}

final int numDocs = randomIntBetween(10, 25);
for (var config : configs) {
var bulkRequest = new Request("POST", "/" + config.indexName() + "/_bulk");
StringBuilder bulkBody = new StringBuilder();
for (int i = 0; i < numDocs; i++) {
bulkBody.append("{\"index\": {}}\n");
bulkBody.append("{\"" + config.fieldName() + "\": ").append(i).append("}\n");
}
bulkRequest.setJsonEntity(bulkBody.toString());
bulkRequest.addParameter("refresh", "true");
var bulkResponse = client().performRequest(bulkRequest);
assertOK(bulkResponse);

var searchRequest = new Request("GET", "/" + config.indexName() + "/_search");
searchRequest.setJsonEntity("""
{
"query": {
"match_all": {}
},
"sort": {
"%s": {
"order": "desc"
}
}
}
""".formatted(config.fieldName()));
var searchResponse = client().performRequest(searchRequest);
assertOK(searchResponse);
var responseBody = entityAsMap(searchResponse);
var hits = (List<Map<String, Object>>) ((Map<String, Object>) responseBody.get("hits")).get("hits");
int previousValue = ((Number) ((List<Object>) hits.get(0).get("sort")).get(0)).intValue();
;
for (int i = 1; i < hits.size(); i++) {
int currentValue = ((Number) ((List<Object>) hits.get(i).get("sort")).get(0)).intValue();
assertTrue("Sort values are not in desc order ", previousValue >= currentValue);
previousValue = currentValue;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
setup:
- do:
indices.create:
index: index_long
body:
mappings:
properties:
field1:
type: long
field2:
type: long

- do:
indices.create:
index: index_int
body:
mappings:
properties:
field1:
type: integer
field2:
type: integer

- do:
indices.create:
index: index_short
body:
mappings:
properties:
field1:
type: short
field2:
type: short

- do:
indices.create:
index: index_byte
body:
mappings:
properties:
field1:
type: byte
field2:
type: byte

- do:
bulk:
refresh: true
index: index_long
body:
- '{ "index" : { "_id" : "long1" } }'
- '{"field1" : 10}'
- '{ "index" : { "_id" : "long2" } }'
- '{"field1" : 20, "field2": 20}'
- '{ "index" : { "_id" : "long3" } }'
- '{"field1" : 30}'
- '{ "index" : { "_id" : "long4" } }'
- '{"field1" : 40, "field2": 40}'
- '{ "index" : { "_id" : "long5" } }'
- '{"field1" : 50}'

- do:
bulk:
refresh: true
index: index_int
body:
- '{ "index" : { "_id" : "int1" } }'
- '{"field1" : 11, "field2": 11}'
- '{ "index" : { "_id" : "int2" } }'
- '{"field1" : 21}'
- '{ "index" : { "_id" : "int3" } }'
- '{"field1" : 31, "field2": 31}'
- '{ "index" : { "_id" : "int4" } }'
- '{"field1" : 41}'
- '{ "index" : { "_id" : "int5" } }'
- '{"field1" : 51, "field2": 51}'

- do:
bulk:
refresh: true
index: index_short
body:
- '{ "index" : { "_id" : "short1" } }'
- '{"field1" : 12}'
- '{ "index" : { "_id" : "short2" } }'
- '{"field1" : 22, "field2": 22}'
- '{ "index" : { "_id" : "short3" } }'
- '{"field1" : 32}'
- '{ "index" : { "_id" : "short4" } }'
- '{"field1" : 42, "field2": 42}'
- '{ "index" : { "_id" : "short5" } }'
- '{"field1" : 52}'

- do:
bulk:
refresh: true
index: index_byte
body:
- '{ "index" : { "_id" : "byte1" } }'
- '{"field1" : 13, "field2": 13}'
- '{ "index" : { "_id" : "byte2" } }'
- '{"field1" : 23}'
- '{ "index" : { "_id" : "byte3" } }'
- '{"field1" : 33, "field2": 33}'
- '{ "index" : { "_id" : "byte4" } }'
- '{"field1" : 43}'
- '{ "index" : { "_id" : "byte5" } }'
- '{"field1" : 53, "field2": 53}'


---
"Simple sort":
- do:
search:
index: index_long,index_int,index_short,index_byte
body:
sort: [ { field1: { "order": "asc"} } ]
- match: { hits.hits.0.sort.0: 10 }
- match: { hits.hits.1.sort.0: 11 }
- match: { hits.hits.2.sort.0: 12 }
- match: { hits.hits.3.sort.0: 13 }
- match: { hits.hits.4.sort.0: 20 }
- match: { hits.hits.5.sort.0: 21 }
- match: { hits.hits.6.sort.0: 22 }
- match: { hits.hits.7.sort.0: 23 }
- match: { hits.hits.8.sort.0: 30 }
- match: { hits.hits.9.sort.0: 31 }

- do:
search:
index: index_long,index_int,index_short,index_byte
body:
sort: [ { field1: { "order": "asc"} } ]
search_after: [31]
- match: { hits.hits.0.sort.0: 32 }
- match: { hits.hits.1.sort.0: 33 }
- match: { hits.hits.2.sort.0: 40 }
- match: { hits.hits.3.sort.0: 41 }
- match: { hits.hits.4.sort.0: 42 }
- match: { hits.hits.5.sort.0: 43 }
- match: { hits.hits.6.sort.0: 50 }
- match: { hits.hits.7.sort.0: 51 }
- match: { hits.hits.8.sort.0: 52 }
- match: { hits.hits.9.sort.0: 53 }

---
"Sort missing values sort last":
- requires:
cluster_features: [ "search.sort.int_sort_for_int_short_byte_fields" ]
reason: "Integer Sort is used on integer, short, byte field types"
- do:
search:
index: index_long,index_int,index_short,index_byte
body:
sort: [ { field2: { "order": "asc" } } ]

- match: { hits.hits.0.sort.0: 11 }
- match: { hits.hits.1.sort.0: 13 }
- match: { hits.hits.2.sort.0: 20 }
- match: { hits.hits.3.sort.0: 22 }
- match: { hits.hits.4.sort.0: 31 }
- match: { hits.hits.5.sort.0: 33 }
- match: { hits.hits.6.sort.0: 40 }
- match: { hits.hits.7.sort.0: 42 }
- match: { hits.hits.8.sort.0: 51 }
- match: { hits.hits.9.sort.0: 53 }

- do:
search:
index: index_long,index_int,index_short,index_byte
body:
sort: [ { field2: { "order": "asc" } } ]
search_after: [ 53 ]

# Then all documents with missing field2
# missing values on fields with integer type return Integer.MAX_VALUE
# missing values on fields with long type return Long.MAX_VALUE
- match: { hits.hits.0.sort.0: 2147483647 }
- match: { hits.hits.1.sort.0: 2147483647 }
- match: { hits.hits.2.sort.0: 2147483647 }
- match: { hits.hits.3.sort.0: 2147483647 }
- match: { hits.hits.4.sort.0: 2147483647 }
- match: { hits.hits.5.sort.0: 2147483647 }
- match: { hits.hits.6.sort.0: 2147483647 }
- match: { hits.hits.7.sort.0: 9223372036854775807 }
- match: { hits.hits.8.sort.0: 9223372036854775807 }
- match: { hits.hits.9.sort.0: 9223372036854775807 }

Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ private static XContentBuilder createTestMapping() {
public void testIndexSort() {
SortField dateSort = new SortedNumericSortField("date", SortField.Type.LONG, false);
dateSort.setMissingValue(Long.MAX_VALUE);
SortField numericSort = new SortedNumericSortField("numeric_dv", SortField.Type.LONG, false);
numericSort.setMissingValue(Long.MAX_VALUE);
SortField numericSort = new SortedNumericSortField("numeric_dv", SortField.Type.INT, false);
numericSort.setMissingValue(Integer.MAX_VALUE);
SortField keywordSort = new SortedSetSortField("keyword_dv", false);
keywordSort.setMissingValue(SortField.STRING_LAST);
Sort indexSort = new Sort(dateSort, numericSort, keywordSort);
Expand Down
Loading
Loading