Keyword scoring loses document ID types after saving and loading
Problem
Keyword scoring indexes can change document IDs after a save/load round trip. For example, the string ID "001" becomes the integer 1. With content=True, searching then raises KeyError, because the separately serialized content still uses the original string key. Deleting with the original ID also stops removing the indexed document.
Reproduced on master f3ceece76a8b0f1103d736aa18f77fb10a269969 (txtai 9.14.0), Python 3.13.13, macOS. No model download is required.
Reproduction
import tempfile
from txtai.scoring import ScoringFactory
config = {"method": "bm25", "terms": True, "content": True}
with tempfile.TemporaryDirectory() as directory:
path = directory + "/index"
scoring = ScoringFactory.create(config)
scoring.index([("001", "orchard apple", None), ("002", "coastal ocean", None)])
print(scoring.search("orchard")) # id='001', text='orchard apple'
scoring.save(path)
scoring.close()
scoring = ScoringFactory.create(config)
scoring.load(path)
try:
scoring.delete(["001"])
print(scoring.count()) # 2; expected 1
print(scoring.search("ocean")) # KeyError: 2
finally:
scoring.close()With content=False, a search returns the converted integer IDs instead of raising. I also reproduced the reverse conversion with IDs [-1, 2] and [1, "notes"]: integers become strings after loading, and searches with content enabled raise KeyError.
Expected behavior and cause
Saving and loading should preserve document IDs, their types, and the ability to search and delete using those IDs.
Terms.save() writes IDs to an SQLite TEXT column. Terms.load() then guesses their types by converting every ID to an integer if all IDs pass str.isdigit(). This loses the distinction between numeric strings and integers, and cannot restore negative integers or mixed string/integer IDs.
I plan to add regression coverage and preserve ID types using the existing MessagePack serializer, while keeping a read path for legacy text-ID indexes. Previously saved indexes may already have lost type information, so the fix would preserve new saves rather than infer information that is no longer present.
This was found through code analysis and local reproduction, with AI assistance.
Source: neuml/txtai