Skip to content

Sort in memory the orders by keys which the index backend can't sort - #5121

Merged
porunov merged 1 commit into
JanusGraph:masterfrom
porunov:sort-text-keys-in-memory
Oct 23, 2026
Merged

porunov merged 1 commit into
JanusGraph:masterfrom
porunov:sort-text-keys-in-memory

Conversation

@porunov

@porunov porunov commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Fixes #5120

A graph query which a mixed index answers, ordered by a key which the index holds, had the index sort the results, whatever the key's mapping. None of JanusGraph's index backends sorts by the strings of a key of TEXT mapping, which is the default for strings, as the index holds the words of each string rather than the string. So g.V().has('kind', 'p').order().by('title'), of such a title, failed with Could not call index on Lucene (unexpected docvalues type NONE for field 'title' (expected=SORTED)) and on Elasticsearch and OpenSearch, which don't sort by a text field (Text fields are not optimised for operations that require per-document field data like aggregations and sorting, after Fielddata is disabled on [title] on Elasticsearch), while Solr returned the vertices in the order of the least word of each title, ascending or descending, and a query of JanusGraph's API put those without a title first when ascending. Elasticsearch and OpenSearch failed in the same way on a STRING key with a custom string-analyzer, which JanusGraph maps to a text field, and on a TEXTSTRING key with one, whose whole strings, by which the index sorts such a key, are then a text field too. And Lucene failed on a UUID key, by which it doesn't sort (Unsupported order specified on field [uid] with datatype [class java.util.UUID]).

JanusGraph now gives the index an order only by the keys which its backend can sort by, and sorts the results of the other orders itself, as it does for any order which no index covers: it reads every match first, and fails with a QueryException at 1,000,000 of them or more. That holds for the traversals, which fold the order into their query, for the queries of JanusGraph's API, and under a custom index selection strategy which takes the index to sort the results by the deprecated IndexSelectionUtil.indexCoversOrder(MixedIndexType, OrderList). An order by a TEXTSTRING key without a custom string-analyzer is still sorted by the index, by its whole strings.

Changes

  • IndexInformation.supportsOrdering(KeyInformation) (new, true by default), which IndexProvider extends: whether the index can sort documents by a key with the given information, by its whole values. It sits next to supports(KeyInformation) and supports(KeyInformation, JanusGraphPredicate), as IndexSerializer holds the providers as IndexInformation. An index provider which doesn't implement it is given every order, as before. Whether the index can sort by a key of several values is still IndexFeatures.supportsOrderingListProperty().
  • MetricInstrumentedIndexProvider, which wraps each provider of a graph with metrics enabled, and the tests' TestMockIndexProvider forward it to the provider.
  • IndexSerializer.supportsOrdering(MixedIndexType, ParameterIndexField) (new) asks the index's provider about the field's key information, as supports(MixedIndexType, ParameterIndexField) does.
  • IndexSelectionUtil.indexCoversOrder(MixedIndexType, OrderList, IndexSerializer) takes an index to cover an order only where it can sort by each of the order's keys. The index selection strategies decide by it whether the index they choose first sorts the results, and IndexSerializer.getQuery(MixedIndexType, Condition, OrderList) gives the index's query the order only where it holds. A custom strategy which still takes such an index to sort the results, as one which decides by the deprecated indexCoversOrder(MixedIndexType, OrderList) does, gets results which GraphCentricQueryBuilder.sortedOnlyWhereTheIndexSorts (Order by a key of several values as the indexes do #5102) sorts, as the index's query has no order. The javadoc of the deprecated method and the comment of sortedOnlyWhereTheIndexSorts say so.
  • LuceneIndex.supportsOrdering: numbers, dates and booleans, and the strings of a STRING key or of the dual field of a TEXTSTRING key, whatever their analyzers, as getSortOrder sorts by the doc values which buildIndexFields adds for them. Not a TEXT or DEFAULT string key, nor a UUID or geoshape key, for which getSortOrder has no sort type.
  • ElasticSearchIndex.supportsOrdering: numbers, dates, booleans and UUIDs, and a STRING or TEXTSTRING key without a custom string analyzer, whose field which addOrderToQuery sorts by is a keyword. Not a TEXT or DEFAULT string key, nor a STRING key, or a TEXTSTRING key, with a custom string analyzer, which pushMapping maps to a text field (createTextMapping(stringAnalyzer)).
  • SolrIndex.supportsOrdering: numbers, dates, booleans and UUIDs, and every string key but a TEXT or DEFAULT one, which mapKey2Field maps to *_t, of the example schema's text_general type, which Solr sorts by the least word of each document. A STRING key and the dual field of a TEXTSTRING key are of the string type, whatever the analyzer, which Solr applies to the values of the queries only.
  • Docs: the Ordering section of the index performance page says which keys a mixed index doesn't sort by, that JanusGraph sorts their orders, and how to map a string key to have the index sort by it, and its point on keys of SINGLE cardinality refers to that; its note that a Lucene index fails an order by a UUID key now says that JanusGraph sorts it.
  • Changelog: a section after Elasticsearch sorts by Byte and Short keys, with the failures and the wrong order, the cost of sorting in memory, which on Solr can make a query fail which Solr answered, the mappings which the indexes sort by and what changing a key's mapping takes, and, for code built on these classes, the new methods and what an index provider should implement or forward. The section on UUIDs said that a Lucene index fails an order by a UUID key, and now refers to the new section.

Tests

  • JanusGraphIndexTest.testOrderByATextKeyIsSortedByJanusGraph (every index backend; the mixed index is named mixed, which the Solr test container creates). Vertices of kinds p and q with a title of TEXT mapping, a note of the DEFAULT one and a heading, of TEXTSTRING, which holds the title, in mixed case and of several words, so that the order of the strings is none of the orders of their words; some have no title or no note. They are written into the graph and into a TinkerGraph, and sixteen traversals return the same from both:

    • orders by title and by note, ascending and descending, with limits and a range, by both keys, and a count() after an order;
    • orders by kind, which the index can sort by, and by title, either way round;
    • orders by title and note next to textContains() on the same key, which the index answers;
    • orders by heading, alone and next to textContains().

    They return the same again in a transaction which adds a vertex, gives one a title and takes another one's, and changes a title and a note. A new transaction's profiles show that the index's queries carry no order by title or note, also next to the text predicate, none by kind next to title, and the order by heading.

  • JanusGraphIndexTest.testResultsWhichAStrategyHoldsSortedByAnIndexWithoutTheOrderAreSortedInMemory (from Order by a key of several values as the indexes do #5102, every index backend) also orders by a title of TEXT mapping under DeprecatedOrderCoverageIndexSelectionStrategy, which takes the index to sort the results, as its javadoc now says for any key which the index holds: tx.query().orderBy('title', asc) and desc come in the order of the whole titles, the vertex without a title last.

  • JanusGraphIndexTest.testUUIDsAreOrderedAsTheirStringForms and testOrderByUUIDPutsElementsWithoutTheKeyLast (from Compare UUIDs as their string forms, as the indexes and storage do #5088) now run on Lucene too, whose supportsOrderingByUUID() override, and the method, are gone: JanusGraph sorts these orders on Lucene, in the order of the UUIDs' string forms, with the elements without the key last.

  • ElasticsearchJanusGraphIndexTest.testOrderByAStringKeyWithAStringAnalyzerIsSortedByJanusGraph: a code of STRING mapping and a caption of TEXTSTRING mapping, both with the standard string analyzer. Five orders by them, ascending and descending, with limits and next to textContains(), return what a TinkerGraph returns, and a new transaction's profiles show that the index's queries carry no order by either, while they carry one by kind, a STRING key without an analyzer. JanusGraphIndexTest's onBoth and results, and the new indexSortsBy, are protected for it.

  • LuceneIndexTest, ElasticsearchIndexTest and SolrIndexTest: testOrderingSupport check each provider's answers for the keys of IndexProviderTest's mapping (STRING and TEXTSTRING keys with and without custom analyzers, TEXT keys with and without a text analyzer, a DEFAULT string key), the number, date and boolean types, UUIDs and geoshapes.

  • IndexSelectionUtilTest.anIndexCoversAnOrderOnlyByTheKeysWhichItsProviderCanSortBy (new): an index whose provider can't sort by a TEXT key covers an order by a STRING key, but not one by the TEXT key, alone, after the STRING key or before it, and one whose provider doesn't implement supportsOrdering covers each of these orders.

  • MetricInstrumentedIndexProviderTest.supportsOrderingShouldBeTheProvidersAnswer: the wrapper answers as the provider does.

Mutations, each breaking one part of the change, are all caught (23, on Lucene, on Elasticsearch 9.5.4 in a container, and on Solr in the JVM):

  • indexCoversOrder asking the provider about the first key of the order alone, or about the last alone: IndexSelectionUtilTest, for [ASC(kind), ASC(title)] or [ASC(title), ASC(kind)], and the Lucene text order test, at the order by kind and title, or by title and kind (unexpected docvalues type NONE for field 'title').
  • indexCoversOrder not asking the provider, or IndexSerializer.supportsOrdering answering true: IndexSelectionUtilTest, and on Lucene the text order test and the strategy test fail with Could not call index (unexpected docvalues type NONE for field 'title'), and, for the first, the UUID tests too (Unsupported order specified on field [uid]).
  • The default of IndexInformation.supportsOrdering false: IndexSelectionUtilTest, whose provider which doesn't implement it covers no order.
  • MetricInstrumentedIndexProvider not forwarding the method: MetricInstrumentedIndexProviderTest.
  • Lucene sorting by every string key: the text order test and the strategy test (unexpected docvalues type NONE). Sorting by a DEFAULT one, which Mapping.getMapping gives as DEFAULT rather than TEXT: the text order test (unexpected docvalues type NONE for field 'note'). Not sorting by a TEXTSTRING key: the profile check of heading. Not by a STRING key: testOrderingListProperty's profile check of DESC(name1). Sorting by a UUID key: the UUID tests (Unsupported order specified on field [uid]). By no number, date or boolean: testOrderingListProperty's profile check of DESC(age1). Each also fails LuceneIndexTest.testOrderingSupport.
  • Elasticsearch sorting by every string key: the text order test and the strategy test (Fielddata is disabled on [title]); ignoring the string analyzer: the analyzed test (Fielddata is disabled on [code]); ignoring it for a TEXTSTRING key alone: the analyzed test (Fielddata is disabled on [caption__STRING]); not sorting by a TEXTSTRING key: the profile check of heading. Each also fails ElasticsearchIndexTest.testOrderingSupport, which alone catches not sorting by a UUID key, which leaves the graph tests with the same results, as JanusGraph sorts UUIDs as the index does, and sorting by a geoshape key.
  • On OpenSearch 3.9.0, Elasticsearch's provider sorting by every string key fails the text order test and the analyzed test with Text fields are not optimised for operations that require per-document field data like aggregations and sorting, for title and code.
  • Solr sorting by every string key: the text order test returns [3, 5, 1, 2, 7] for [2, 3, 5, 7, 1], and the strategy test [d, c, b, a] for [a, c, b, d]. Sorting by a DEFAULT one, deciding by Mapping.getMapping rather than getStringMapping: the text order test's order by note returns [4, 7, 2] for [2, 5, 7]. Not sorting by a TEXTSTRING key: the profile check of heading. Each also fails SolrIndexTest.testOrderingSupport, which alone catches not sorting by a STRING key with a string analyzer, or by a UUID key.

On master 3148803d4, with these tests, but for the unit tests, which call the new methods:

  • Lucene: testOrderByATextKeyIsSortedByJanusGraph and the strategy test fail with JanusGraphException: Could not call index, caused by IllegalStateException: unexpected docvalues type NONE for field 'title' (expected=SORTED). Re-index with correct docvalues type.; testUUIDsAreOrderedAsTheirStringForms and testOrderByUUIDPutsElementsWithoutTheKeyLast with IllegalArgumentException: Unsupported order specified on field [uid] with datatype [class java.util.UUID].
  • Elasticsearch 9.5.4: the text order test and the strategy test fail with Could not call index, caused by a 400 Bad Request: Fielddata is disabled on [title] in [janusgraph_mixed]. Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default., and the analyzed test with the same for [code].
  • Solr 9.10.1 in the JVM: the text order test returns [3, 5, 1, 7, 2] where TinkerGraph gives [2, 3, 5, 7, 1], which is the order of the least word of each title, and the strategy test returns [d, c, b, a] for [a, c, b, d] ascending, the vertex without a title first.
  • The orders by kind and title, and by title and kind, each run on its own on master, fail on Lucene (unexpected docvalues type NONE for field 'title') and on Elasticsearch (Fielddata is disabled on [title]), and Solr returns [6, 3, 1, 7, 2] for [6, 2, 3, 7, 1] and [7, 2, 1] for [1, 7, 5].

Locally, Java 11, with checkstyle (no violations):

  • On an earlier state of this change, whose main code is this change's but for a comment: janusgraph-driver (132), janusgraph-core (245), janusgraph-inmemory (715, 85 skipped), janusgraph-test (1,132), janusgraph-berkeleyje (398, 12 skipped) and janusgraph-lucene (466, 148 skipped) pass, as do BerkeleyElasticsearchTest (147, 1 skipped) and ElasticsearchIndexTest (317) against Elasticsearch 9.5.4 in a container, and JanusGraphIndexTest on Solr 9.10.1 in the JVM, SolrRunner's MiniSolrCloud with BerkeleyJE as BerkeleySolrTest configures it (135, 1 skipped), with the harness's collections.txt given the listPropertyOrdering collection, which master's testOrderingListProperty needs. Against OpenSearch 3.9.0 (-Popensearch3), the new tests of BerkeleyElasticsearchTest, its strategy and UUID tests, and ElasticsearchIndexTest.testOrderingSupport pass.
  • On this change as it stands, which adds the orders by two keys and their case of IndexSelectionUtilTest, and the positive profile check of the Elasticsearch test, the tests of this PR pass again: IndexSelectionUtilTest, MetricInstrumentedIndexProviderTest, the text order, strategy and UUID tests and testOrderingListProperty on Lucene, and LuceneIndexTest's testOrderingSupport and testSupport; the text order, analyzed, strategy and UUID tests on Elasticsearch 9.5.4, and ElasticsearchIndexTest's two; the text order, strategy and UUID tests on Solr in the JVM, and SolrIndexTest's two.
  • CQL and the other storage backends are left to CI, as are Solr and Elasticsearch in their CI containers.

Notes

  • Cost on Solr. Lucene, Elasticsearch and OpenSearch failed these orders, but Solr answered them, in the wrong order, by reading only the first hits up to the limit; for a DEFAULT key whose values are single words, the order was right but for case. JanusGraph now reads every match before it returns the first one, and a query of 1,000,000 matches or more fails with a QueryException. The changelog warns of this, and says how to map a key to have the index sort by it, which takes a new key, or a new index and a reindex.
  • The providers decide by the key's mapping and analyzers, as their supports(...) methods do, not by the index's actual mapping or schema. A key which an Elasticsearch mapping of one's own (index.[X].elasticsearch.create.use-external-mappings) or a Solr schema of one's own (dyn-fields disabled, or a mapped-name) holds otherwise than JanusGraph would is answered by its JanusGraph mapping: such a TEXT key in a field which the backend can sort by, such as a keyword, is now sorted by JanusGraph, which gives the same results, and a STRING key in a text field still has the index sort by it. An Elasticsearch text field given fielddata by a custom parameter would be sorted by its words, not by the strings, so leaving it to JanusGraph is right there too.
  • UUIDs on Lucene. The UUID order tests of Compare UUIDs as their string forms, as the indexes and storage do #5088 skipped Lucene, which failed them; they now run there. This changes what a Lucene index does with an order by a UUID key, which index-performance.md and the UUID section of the changelog said fails.
  • A custom index selection strategy which builds the order of the index's query itself, rather than through IndexSerializer.getQuery (or AbstractIndexSelectionStrategy.addToJointQuery, which calls it), as none of JanusGraph's strategies or the tests' strategies does, can still have the index sort by such a key; JanusGraph doesn't strip such an order.
  • A direct index query (graph.indexQuery(...).orderBy(...)) still gives the index the order it is asked for.
  • TestMockIndexProvider forwards the new method as it forwards supports(...); as test infrastructure, it isn't mutation-tested.
  • Follow-ups, left out here:
    • On master as here, indexCoversOrder takes an order key whose field in the index isn't ENABLED, but REGISTERED or INSTALLED, as one which the index can sort by, as MixedIndexType.indexesKey holds such a field, while the conditions (AbstractIndexSelectionStrategy.coversAll), GraphCentricQueryBuilder.requireValues and JanusGraphMixedIndexAggStep take only ENABLED fields.
    • ElasticSearchIndex.supportsOrdering doesn't consider the custom parameters of a key, such as doc_values: false or a normalizer, which change whether or how Elasticsearch sorts by it.

🤖 Generated with Claude Code

Fixes JanusGraph#5120

A mixed index which held the key of a graph query's order was given the
order whatever the key's mapping, but none of JanusGraph's index
backends sorts by the strings of a key of TEXT mapping, which is the
default for strings, as the index holds the words of each string. So an
order by such a key, such as g.V().has('kind', 'p').order().by('title'),
also next to a key which the index can sort by, failed with "Could not
call index" on Lucene, which keeps no doc values of the field, and
on Elasticsearch and OpenSearch, which don't sort by a text field, and
Solr returned the elements in the order of the least word of each
string. Elasticsearch and OpenSearch failed in the same way on a STRING
key with a custom string analyzer, and on such a TEXTSTRING key, whose
whole strings are then a text field too, and Lucene on a UUID key, by
which it doesn't sort.

IndexInformation, and so IndexProvider, now tells by
supportsOrdering(KeyInformation) whether the index can sort by a key,
true by default, so that an index provider which doesn't implement it is
given every order, as before. Lucene can by numbers, dates, booleans and
the strings of a STRING or TEXTSTRING key, of which it keeps doc values,
Elasticsearch by numbers, dates, booleans, UUIDs and the strings of a
STRING or TEXTSTRING key without a custom string analyzer, which it maps
to a keyword, and Solr by numbers, dates, booleans, UUIDs and every
string key but a TEXT one, which is of a text type.
IndexSelectionUtil.indexCoversOrder asks it for each key of the order,
so that IndexSerializer gives the index no order by a key which the
backend can't sort by, and the index selection strategies don't take
the index to sort the results. Where a custom strategy still does, as
one which decides by the deprecated indexCoversOrder, the index's query
has no order, and GraphCentricQueryBuilder sorts the results already.
JanusGraph sorts these orders itself, reading every match first, as any
order which no index covers.

Signed-off-by: Oleksandr Porunov <alexandr.porunov@gmail.com>
Co-authored-by: Oleksandr Porunov <alexandr.porunov@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The capability checks reuse the existing sorting fallback, preserve provider compatibility, and have focused regression coverage with no unresolved blocking findings.

No open issues 🎉

What changed in this PR

Fixes #5120 by using JanusGraph’s existing in-memory sorting when a mixed-index backend cannot correctly order a property’s whole values. Supported orders remain delegated to the backend.

Changes:

  • Adds a backward-compatible ordering capability API, backend implementations, and provider-wrapper delegation.
  • Checks every order key before delegating sorting, with regression tests for text mappings, analyzers, UUIDs, and custom index selection.
  • Documents fallback behavior, memory costs, and mapping choices for backend sorting.
File Description
janusgraph-solr/​src/​test/​java/​org/​janusgraph/​diskstorage/​solr/​SolrIndexTest.java Tests Solr ordering capabilities.
janusgraph-solr/​src/​main/​java/​org/​janusgraph/​diskstorage/​solr/​SolrIndex.java Declares sortable Solr mappings and types.
janusgraph-lucene/​src/​test/​java/​org/​janusgraph/​diskstorage/​lucene/​LuceneIndexTest.java Tests Lucene ordering capabilities.
janusgraph-lucene/​src/​test/​java/​org/​janusgraph/​diskstorage/​lucene/​BerkeleyLuceneTest.java Removes the UUID ordering test exclusion.
janusgraph-lucene/​src/​main/​java/​org/​janusgraph/​diskstorage/​lucene/​LuceneIndex.java Declares sortable Lucene mappings and types.
janusgraph-es/​src/​test/​java/​org/​janusgraph/​diskstorage/​es/​ElasticsearchJanusGraphIndexTest.java Tests fallback sorting for custom string analyzers.
janusgraph-es/​src/​test/​java/​org/​janusgraph/​diskstorage/​es/​ElasticsearchIndexTest.java Tests Elasticsearch ordering capabilities.
janusgraph-es/​src/​main/​java/​org/​janusgraph/​diskstorage/​es/​ElasticSearchIndex.java Rejects backend ordering for unsupported text mappings and analyzers.
janusgraph-core/​src/​test/​java/​org/​janusgraph/​graphdb/​query/​index/​IndexSelectionUtilTest.java Tests per-key capability checks and default compatibility.
janusgraph-core/​src/​test/​java/​org/​janusgraph/​diskstorage/​util/​MetricInstrumentedIndexProviderTest.java Tests capability delegation through the metrics wrapper.
janusgraph-core/​src/​main/​java/​org/​janusgraph/​graphdb/​query/​index/​IndexSelectionUtil.java Requires backend ordering support for every order key.
janusgraph-core/​src/​main/​java/​org/​janusgraph/​graphdb/​query/​graph/​GraphCentricQueryBuilder.java Updates the comment explaining sorting fallback.
janusgraph-core/​src/​main/​java/​org/​janusgraph/​graphdb/​database/​IndexSerializer.java Exposes provider ordering capabilities to query planning.
janusgraph-core/​src/​main/​java/​org/​janusgraph/​diskstorage/​util/​MetricInstrumentedIndexProvider.java Forwards ordering capability checks.
janusgraph-core/​src/​main/​java/​org/​janusgraph/​diskstorage/​indexing/​IndexInformation.java Adds the default ordering capability method.
janusgraph-backend-testutils/​src/​main/​java/​org/​janusgraph/​graphdb/​TestMockIndexProvider.java Forwards ordering capabilities in test infrastructure.
janusgraph-backend-testutils/​src/​main/​java/​org/​janusgraph/​graphdb/​JanusGraphIndexTest.java Adds cross-backend fallback regressions and enables Lucene UUID coverage.
janusgraph-backend-testutils/​src/​main/​java/​org/​janusgraph/​graphdb/​DeprecatedOrderCoverageIndexSelectionStrategy.java Updates documentation of legacy strategy behavior.
docs/​schema/​index-management/​index-performance.md Explains unsupported orders and in-memory fallback.
docs/​changelog.md Records behavior changes, sorting costs, and API compatibility.

Review effort: Balanced

@porunov
porunov marked this pull request as ready for review October 23, 2026 01:50
@porunov porunov added this to the 1.2.0 milestone Oct 23, 2026
@porunov
porunov merged commit 1859475 into JanusGraph:master Oct 23, 2026
217 of 324 checks passed
porunov added a commit to porunov/janusgraph that referenced this pull request Oct 23, 2026
Fixes JanusGraph#5124

Since JanusGraph#5108, a traversal's order().by(key) requires a value of the key,
so g.V().order().by("name").limit(10) returns what
g.V().has("name").order().by("name").limit(10) returns. The second reads
the mixed index which holds name with an exists query, sorted by the
index, and stops after the first hits. The first has no condition which
an index answers, so it read every vertex and sorted them in memory,
which fails with a QueryException from a million vertices on, and which
query.force-index refused as a full scan. So did
g.V().hasLabel("person").order().by("name").limit(10) with an index of
the persons, and an order of edges by a key of an edge mixed index. The
presence of the order keys joined the query's condition only after the
index selection strategy had chosen the indexes for the other
conditions, so that it would not add a mixed index next to one which
answers them, which a joint query reads whole.

Where the strategy chooses no index for the query's own conditions, it
now chooses again for those conditions together with the presence of a
value of each order key, among the mixed indexes of the result type
which may answer them, as its own candidates, and which sort the
elements by the query's order: which hold every order key, which their
backend sorts by as JanusGraph orders its values, a key of several
values included (IndexSelectionUtil.indexCoversOrder, which asks each
backend since JanusGraph#5121), and none of them with custom parameters, which
Elasticsearch takes into its mapping, so that they may keep it from
sorting by the key. The brute-force strategy, which the default one
takes up to query.index-select-threshold candidates, weighs an index
which holds a key as a text, which no backend sorts by, as one which
holds it as a string: left among the candidates, such an index would be
taken in place of one which holds the key as a string, by chance, where
both answer the query alike. The approximate strategy prefers the one
which sorts, by indexCoversOrder.

The query takes that choice where it is a single index which covers
every condition: a mixed index which holds every order key and answers
the query whole, as it answers a has(key). It takes it only where the
index is one of the candidates, as nothing keeps a strategy from taking
another one. The query is then fitted, and sorted where the index sorts
by the query's order, as it does with JanusGraph's strategies
(sortedOnlyWhereTheIndexSorts checks it), so that a limit reads only the
first hits, and a count() or an aggregation of a key of the index right
after the order comes from the index. The strategies decide as for a
has(key) whether a mixed index covers such a condition, by the status of
the key's field and IndexSerializer.supportsExistsQuery, and take an
index restricted to a label only for a query of that label, whose
condition the index then covers too.

Any other query reads every element as before. One whose index would
leave a condition to be checked in memory would read the index in
growing batches and look each element up, and a joint query of several
indexes reads all but the first whole: every element with a value of
their keys. An or() alone still runs each of its branches as a query of
its own where no index covers it in the first choice, as none covers it
in the second either. A query of JanusGraph's API, which keeps the
elements without the key, is unaffected.

Signed-off-by: Oleksandr Porunov <alexandr.porunov@gmail.com>
Co-authored-by: Oleksandr Porunov <alexandr.porunov@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
porunov added a commit that referenced this pull request Oct 24, 2026
Fixes #5124

Since #5108, a traversal's order().by(key) requires a value of the key,
so g.V().order().by("name").limit(10) returns what
g.V().has("name").order().by("name").limit(10) returns. The second reads
the mixed index which holds name with an exists query, sorted by the
index, and stops after the first hits. The first has no condition which
an index answers, so it read every vertex and sorted them in memory,
which fails with a QueryException from a million vertices on, and which
query.force-index refused as a full scan. So did
g.V().hasLabel("person").order().by("name").limit(10) with an index of
the persons, and an order of edges by a key of an edge mixed index. The
presence of the order keys joined the query's condition only after the
index selection strategy had chosen the indexes for the other
conditions, so that it would not add a mixed index next to one which
answers them, which a joint query reads whole.

Where the strategy chooses no index for the query's own conditions, it
now chooses again for those conditions together with the presence of a
value of each order key, among the mixed indexes of the result type
which may answer them, as its own candidates, and which sort the
elements by the query's order: which hold every order key, which their
backend sorts by as JanusGraph orders its values, a key of several
values included (IndexSelectionUtil.indexCoversOrder, which asks each
backend since #5121), and none of them with custom parameters, which
Elasticsearch takes into its mapping, so that they may keep it from
sorting by the key. The brute-force strategy, which the default one
takes up to query.index-select-threshold candidates, weighs an index
which holds a key as a text, which no backend sorts by, as one which
holds it as a string: left among the candidates, such an index would be
taken in place of one which holds the key as a string, by chance, where
both answer the query alike. The approximate strategy prefers the one
which sorts, by indexCoversOrder.

The query takes that choice where it is a single index which covers
every condition: a mixed index which holds every order key and answers
the query whole, as it answers a has(key). It takes it only where the
index is one of the candidates, as nothing keeps a strategy from taking
another one. The query is then fitted, and sorted where the index sorts
by the query's order, as it does with JanusGraph's strategies
(sortedOnlyWhereTheIndexSorts checks it), so that a limit reads only the
first hits, and a count() or an aggregation of a key of the index right
after the order comes from the index. The strategies decide as for a
has(key) whether a mixed index covers such a condition, by the status of
the key's field and IndexSerializer.supportsExistsQuery, and take an
index restricted to a label only for a query of that label, whose
condition the index then covers too.

Any other query reads every element as before. One whose index would
leave a condition to be checked in memory would read the index in
growing batches and look each element up, and a joint query of several
indexes reads all but the first whole: every element with a value of
their keys. An or() alone still runs each of its branches as a query of
its own where no index covers it in the first choice, as none covers it
in the second either. A query of JanusGraph's API, which keeps the
elements without the key, is unaffected.

Signed-off-by: Oleksandr Porunov <alexandr.porunov@gmail.com>
Co-authored-by: Oleksandr Porunov <alexandr.porunov@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A mixed index is given an order by a TEXT string key, which none of JanusGraph's index backends can sort by

2 participants