Skip to content

fix(elasticsearch): read activity logs and trackers by the complete object ID - #61

Merged
paullla merged 2 commits into
Locastic:masterfrom
paullla:fix/elasticsearch-exact-object-id
Dec 4, 2026
Merged

paullla merged 2 commits into
Locastic:masterfrom
paullla:fix/elasticsearch-exact-object-id

Conversation

@paullla

@paullla paullla commented Dec 4, 2026 •

Copy link
Copy Markdown
Member
Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Issues -
License MIT

Problem

With storage: elasticsearch, the default index mapping saves objectId as analyzed text, and the storage read it with a term query. Such a query compares words and not the complete ID:

  • getActivityLogsByClassAndId() with the ID 15 also returned the logs of the ID 15-b.
  • With the ID 15-b or a UUID, it returned nothing.
  • The read of the current data tracker used the same query. It found no tracker for a UUID, and no tracker for 15 when 15-b also exists. The update handler then saved no log, so the bundle did not log updates of such objects.

Number IDs with no similar ID worked. The doctrine and in_memory storages did not have this defect.

Changes

  • New internal class Bridge\Elasticsearch\Query\ElasticsearchExactMatchQuery. Its search body has two checks: a phrase query on the indexed field, which keeps the read fast, and a term query on a runtime keyword field that reads the saved value from the document source, which makes the read exact. Existing indexes work, and a reindex is not necessary.
  • ElasticsearchActivityLogStorage::findByIndexAndObjectId() and ElasticsearchCurrentDataTrackerStorage::findByClassAndObjectId() use the new class.
  • A server that does not permit runtime fields or inline scripts (search.allow_expensive_queries: false, script.allowed_types, script.allowed_contexts) gets the phrase query alone, so the reads do not fail there. ElasticsearchService does this second search for getItemByQuery() and getCollection().
  • CHANGELOG (Fixed), with two upgrade notes.

Backward compatibility

  • No interface changed, and no configuration option changed.
  • Visible change: a read returns only the logs of the complete object ID, with the letter case as saved.
  • After the upgrade, the first Edited log of an object that had the defect shows all changes since its last saved tracker. The CHANGELOG says how to start from the current data instead.

Verification

  • 44 new tests: 5 functional tests with a new fixture that has a string ID (they fail on master), and 39 unit tests of the new class, the service and the two storages. Full suite: 168 tests pass. PHPStan reports no errors, and PHP-CS-Fixer reports 0 fixable files.
  • Query check on Elasticsearch 8.17.0 and 9.0.0, 80 cases each, 0 failures: the default mapping, objectId as keyword, objectId as long, and an index with no mapping.
  • The real storage classes on Elasticsearch 9.0.0 with each of the three server limits: no read fails, and an index that does not exist still gives the 404 error.
  • Read time on an index with 300,000 trackers: 1 to 5 ms for a number ID and for a UUID, the same as the old query.

…bject ID

The default index mapping saves objectId as analyzed text, and the storage
read it with a term query. Such a query compares words and not the complete
ID. A read for the ID "15" also returned the logs of the ID "15-b", and a read
for "15-b" or a UUID returned nothing. The read of the current data tracker
used the same query, so the bundle did not log updates of such objects.

ElasticsearchExactMatchQuery builds a search body with two checks: a phrase
query on the indexed field, which keeps the read fast, and a term query on a
runtime keyword field that reads the saved value from the document source,
which makes the read exact. Existing indexes work, and a reindex is not
necessary.

ElasticsearchActivityLogStorage::findByIndexAndObjectId() and
ElasticsearchCurrentDataTrackerStorage::findByClassAndObjectId() use the new
class. A server that does not permit runtime fields or inline scripts gets
the phrase query alone, so the reads do not fail there.
…service

The two storages wrapped their service call in a callback of
ElasticsearchExactMatchQuery, which ran the search again when a server did
not permit the exact check. The storages now build the search body and make
the plain getItemByQuery() and getCollection() calls again.

ElasticsearchService runs the search. If the server rejects the exact checks
of the search body, it runs the search one more time with no exact checks.
Other errors, and a rejected search body that is not from the bundle, go to
the caller. ElasticsearchExactMatchQuery only builds and changes search
bodies. Its search() method with the @template type is removed.
@paullla
paullla merged commit 31c51fb into Locastic:master Dec 4, 2026
13 checks passed
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.

1 participant