PictoPy icon indicating copy to clipboard operation
PictoPy copied to clipboard

BUG: IndexWriter allows updating index sort fields if the index sort is not set in the config

Open harshvardhanrp opened this issue 1 month ago • 2 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

What happened?

If an index was written with an index sort, and is later opened by an IndexWriter whose config has no index sort, doc-values updates on the sort field are accepted. The same happens after addIndexes(Directory) of a sorted index into an unsorted writer.

The segments still say they are sorted by that field, but after the update their documents are no longer in that order. Code that trusts the segment sort then returns wrong results with no error:

  • a sorted top-N search returns the wrong top hit (early termination)
  • IndexSortSortedNumericDocValuesRangeQuery misses matching documents

The problem stays after a later merge, even by a writer that has the correct sort. CheckIndex at the slow level reports: "segment has indexSort=<long: "sort"> but docID=1998 sorts after docID=1999" The default CheckIndex level does not run this check.

Cause: validateIndexSort() skips everything when config.getIndexSort() is null, and the update guards only check config.getIndexSortFields(), not the sort recorded in each segment.

Steps to reproduce:

  1. Index 2000 docs with NumericDocValuesField("sort", i + 1) and setIndexSort(new Sort(new SortField("sort", SortField.Type.LONG))), then forceMerge(1)
  2. Open a new IndexWriter with a plain IndexWriterConfig (no index sort)
  3. updateNumericDocValue(term for the last doc, "sort", 0) -> no exception
  4. searcher.search(MatchAllDocsQuery.INSTANCE, 1, indexSort) returns sort=1, expected 0

I have a fix with tests and can open a PR.

Record

  • [x] I agree to follow this project's Code of Conduct

harshvardhanrp avatar Oct 10 '26 19:10 harshvardhanrp

👋 Thanks for opening this issue! The maintainers will review it shortly. Till then, do not open any PR. This is a strict rule we like to follow around here :)

github-actions[bot] avatar Oct 10 '26 19:10 github-actions[bot]

Link your account with Gitcord

Thanks for opening this issue, @harshvardhanrp!

To receive Discord notifications and contributor tracking for this organization:

  1. Join Discord: https://discord.gg/hjUhu33uAn
  2. In Discord, run /link harshvardhanrp
  3. Paste the verification code into your GitHub bio (or a public gist)
  4. Click Verify in Discord (or run /verify-link harshvardhanrp)

Once linked, Gitcord can notify you about assignments, mentions, and more.

— Posted by Gitcord

gitcordapp[bot] avatar Oct 10 '26 19:10 gitcordapp[bot]