Skip to content

Optimize MULTI edge with sortKey queries which use mixed indexes(#4923) - #5150

Open
batrived wants to merge 1 commit into
JanusGraph:masterfrom
batrived:feat/4923-mixed-index-sort-key-field
Open

batrived wants to merge 1 commit into
JanusGraph:masterfrom
batrived:feat/4923-mixed-index-sort-key-field

Conversation

@batrived

@batrived batrived commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4923.

For a MULTI label with a sort key, the edge column is [type+dir][sortKey][adjacent][relationId], but RelationIdentifier has no sort-key values. So every edge a mixed index returns for such a label is resolved by reading every edge of that label on its vertex: N hits x O(degree).

Change

  • New option index.[X].store-edge-sort-key (MASKABLE, default false; nothing changes unless enabled). With it on, each document of such an edge carries its encoded sort key in a retrieval-only field, jg_system____sortkey (stored, not searchable). The name follows the prefix convention of internal index fields so that it can't collide with a field of an existing index. Queries return it with each hit, and the edge is read with a single column lookup.
  • A hit whose sort key no longer matches its edge (sort key changed, or a document written before the option was on) falls back to the existing scan, so a stale value never hides an edge. Instances with different settings can run side by side; REINDEX backfills the field.
  • Provider SPI: new default methods IndexProvider.queryHits(...) returning IndexHit (id + nullable sort key), gated by IndexFeatures.supportsEdgeSortKeyField(). Third-party providers keep working, without the faster lookup.
  • Backend: Elasticsearch only for now. Lucene and Solr report no support, so the option is ignored for them; they can opt in later through IndexFeatures.supportsEdgeSortKeyField() without core changes.
    • The field is mapped as a keyword with index: false and doc_values: false, and read via _source filtering.
    • Each edge store is decided once per instance, on its first write. With external mappings the field is added only when allow-mapping-update permits, and used unmapped under "dynamic": false. A mapping that can't hold the field (strict without it, dynamic without update permission, or mapped as something other than keyword/text) has the store's edge documents written without it, with one warning, instead of failing the index commit after the storage commit. Queries of such a store don't request _source.
    • The backend reads the option itself, so stores that can't carry the field (vertex indexes, or the option off) cost nothing on writes. clearStore forgets the decision for the store.
  • FORK labels, constrained multiplicities and sort keys over 256 encoded bytes carry no field.
  • A mixed index field can no longer be named jg_system____sortkey.
  • EdgeIdWithSortKey (the id + sort key that travels with a hit) equals only another wrapper of the same id; the index intersection unwraps before testing membership.
  • Bug fix: EdgeSerializer.getQuery reassigned keyEndPos per implicit key, so with sort key + adjacent + relation id constrained on a DESC label the slice never matched. Happy to split this into its own PR.

Tests

  • JanusGraphIndexTest: with 100–200 edges of one label on a vertex, a mixed index hit reads 1 edgestore entry with the option on and ≥ the label's degree with it off. Covered for ASC, DESC, a two-key sort key, a sort-key change, a stale sort key, REINDEX backfill, a composite + mixed edge query, FORK labels and the reserved field name.
  • IndexProviderTest: the field is returned per hit after add, update, remove and restore, for plain and raw queries, and is not searchable. Runs for every provider that supports it.
  • ElasticsearchConfigTest: under a strict external mapping and a dynamic one without update permission, the edge document is written without the field and its hit carries no sort key.
  • IndexSerializerTest, EdgeSerializerTest (DESC query; fails without the fix), EdgeIdWithSortKeyTest, and ES unit tests for the request body and hit parsing.
  • Ran locally: EdgeIdWithSortKeyTest, EdgeSerializerTest, IndexSerializerTest, the janusgraph-es unit tests. Not run locally: the Elasticsearch integration suites (ElasticsearchIndexTest, ElasticsearchConfigTest, ElasticsearchJanusGraphIndexTest; no Docker), which are the only end-to-end coverage now that the feature is Elasticsearch-only, so this is a draft until CI has run them.

For all changes:

  • Is there an issue associated with this PR? Is it referenced in the commit message?
  • Does your PR body contain #xyz where xyz is the issue number you are trying to resolve?
  • Has your PR been rebased against the latest commit within the target branch (typically master)?
  • Is your initial contribution a single, squashed commit? (three commits for review; will squash before merge)

For code changes:

  • Have you written and/or updated unit tests to verify your changes?
  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?
  • If applicable, have you updated the LICENSE.txt file, including the main LICENSE.txt file in the root of this repository?
  • If applicable, have you updated the NOTICE.txt file, including the main NOTICE.txt file found in the root of this repository?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered?

🤖 Generated with Claude Code

@batrived batrived changed the title Read mixed index edge hits of sort-keyed labels with a single column lookup (#4923) Optimize MULTI edge with sortKey queries using mixed indexes(#4923) Nov 19, 2026
@batrived
batrived marked this pull request as ready for review November 19, 2026 21:29
@batrived
batrived force-pushed the feat/4923-mixed-index-sort-key-field branch from cbc6b5a to a44360b Compare November 19, 2026 22:57
…lookup (JanusGraph#4923)

An edge of a MULTI label with a sort key is stored under the column
[type+dir][sortKey][adjacent][relationId]. Its id has no sort-key values,
so every edge a mixed index returned for such a label was resolved by
reading every edge of that label on its vertex.

With the new option index.[X].store-edge-sort-key (default false), each
such edge document in an Elasticsearch index carries its encoded sort key
in a retrieval-only field, jg_system____sortkey. Queries return it with
each hit, and the edge is read with a single column lookup. A hit whose
sort key no longer matches its edge falls back to the scan, so a stale
value never hides an edge. Edge ids, document ids and the composite index
format are unchanged.

- IndexProvider.queryHits (default methods) returns hits with the field;
  IndexFeatures.supportsEdgeSortKeyField gates it. Lucene, Solr and
  third-party providers report no support, so the option is ignored for
  them and their queries behave as before.
- Elasticsearch maps the field as a non-indexed keyword and reads it from
  _source. Where an external mapping cannot hold the field and may not be
  updated, the store's edge documents are written without it, with one
  warning, instead of failing the index commit.
- EdgeIdWithSortKey equals only another EdgeIdWithSortKey of the same id;
  SubqueryIterator unwraps before testing membership.
- EdgeSerializer.getQuery no longer moves the sort-key end past the
  adjacent vertex id, which made fully constrained queries on DESC labels
  miss the column.
- A mixed index field can no longer be named jg_system____sortkey.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Balmukund Trivedi <btrivedipublic@gmail.com>
@batrived
batrived force-pushed the feat/4923-mixed-index-sort-key-field branch from a44360b to 96ade82 Compare November 19, 2026 23:35
@batrived batrived changed the title Optimize MULTI edge with sortKey queries using mixed indexes(#4923) Optimize MULTI edge with sortKey queries which use mixed indexes(#4923) Nov 20, 2026
@batrived

Copy link
Copy Markdown
Contributor Author

@porunov Please take a look. Thanks!

This branch has not been deployed

No deployments
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.

Edge materialization from a mixed index scans the full adjacency when the edge label has a sort key (seek is defeated)

1 participant