You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "weiqingy (via GitHub)" <gi...@apache.org> on 2026/08/12 04:33:03 UTC
[I] [Bug] Elasticsearch queryEmbedding applies filters as a post-filter, returning fewer than k documents [flink-agents]
weiqingy opened a new issue, #999:
URL: https://github.com/apache/flink-agents/issues/999
### Search before asking
- [X] I searched in the [issues](https://github.com/apache/flink-agents/issues) and found nothing similar.
### Description
`ElasticsearchVectorStore.queryEmbedding` attaches its filter with `SearchRequest.Builder.postFilter(...)`, while the KNN clause itself carries no `filter` option.
Elasticsearch applies `post_filter` after the kNN phase has already selected its `k` nearest hits, so the filter can only remove documents from that set. It never pulls in matching documents that fell outside the top `k`. A caller asking for 5 documents matching `user_id=alice` can get zero back, even with hundreds of alice's documents indexed, if the 5 nearest vectors happen to belong to other users.
`get` and `delete` build the same filter map into a real query clause, so they are exhaustive. The three methods diverge, and the divergence is not visible from the API.
Both filter forms are affected, since the unified `filters` map and a raw `filter_query` are merged into a single query by `combineQueryJson` before it is attached.
Expected: `queryEmbedding` returns up to `k` documents that match the filter.
Actual: it returns whichever of the unfiltered `k` nearest happen to match, which can be none.
The Elasticsearch Java client exposes `KnnSearch.Builder.filter(...)`, which applies the filter during the kNN search rather than after it.
This would be a user-visible behavior change. Callers who pass a filter to `queryEmbedding` today receive at most the matching subset of the top `k`, and would afterwards receive up to `k` matching documents.
### How to reproduce
1. Start a single-node Elasticsearch with `xpack.security.enabled=false`.
2. Index documents belonging to several users, each carrying metadata such as `user_id`.
3. Call `queryEmbedding` with `k` of 5, a filter for one user, and an embedding nearest to another user's documents.
4. Fewer than 5 documents come back, possibly none, even though more than 5 documents match the filter.
Note that `ElasticsearchVectorStoreTest` is annotated `@Disabled("Should setup Elasticsearch server.")`, so the existing `testFiltersDsl` coverage does not run in CI and would not catch this.
### Version and environment
`main`, elasticsearch-java 8.19.0. Noticed while reviewing #997.
### Are you willing to submit a PR?
- [X] I'm willing to submit a PR!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] Elasticsearch queryEmbedding applies filters as a post-filter, returning fewer than k documents [flink-agents]
Posted by "kaiwangleo (via GitHub)" <gi...@apache.org>.
kaiwangleo commented on issue #999:
URL: https://github.com/apache/flink-agents/issues/999#issuecomment-5291209396
Follow-up: generalized the test gate to `@EnabledIfEnvironmentVariable(named = ES_HOST, matches = .+)`, so any non-empty Elasticsearch endpoint can enable the suite instead of requiring an HTTP URL scheme. The focused Spotless check and all 6 `ElasticsearchVectorStoreTest` cases pass locally. Commit `086b9b4b` is pushed; the complete CI has been triggered again.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] Elasticsearch queryEmbedding applies filters as a post-filter, returning fewer than k documents [flink-agents]
Posted by "kaiwangleo (via GitHub)" <gi...@apache.org>.
kaiwangleo commented on issue #999:
URL: https://github.com/apache/flink-agents/issues/999#issuecomment-5290390069
Update: commit `e2d30eff` has been pushed to the rebased #1011 branch. The complete CI run [31776270564](https://github.com/apache/flink-agents/actions/runs/31776270564) finished successfully, including the standalone Elasticsearch vector-store job and all Java, Python, cross-language, style, and install checks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] Elasticsearch queryEmbedding applies filters as a post-filter, returning fewer than k documents [flink-agents]
Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on issue #999:
URL: https://github.com/apache/flink-agents/issues/999#issuecomment-5274073089
Hi @kaiwangleo, thanks for going through the code on this. Your read matches what #1001 does, step for step. On the compatibility question, I am comfortable with it: only filtered `queryEmbedding` calls change, and today those are returning the wrong thing anyway.
The gap #1001 leaves is CI. `ElasticsearchVectorStoreTest` is still `@Disabled`, so even the new regression test never runs there. That is the piece I would most like to see fixed, and it is yours if you want it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] Elasticsearch queryEmbedding applies filters as a post-filter, returning fewer than k documents [flink-agents]
Posted by "kaiwangleo (via GitHub)" <gi...@apache.org>.
kaiwangleo commented on issue #999:
URL: https://github.com/apache/flink-agents/issues/999#issuecomment-5275017828
Thanks for the confirmation. I will fold the minimal fix into PR #997, limited to the Elasticsearch vector-store path already touched there: move the combined unified/raw filter from post_filter onto the KNN clause's ilter, so filtering happens during candidate selection and filtered queries can return up to k matching documents. Unfiltered queries and the existing AND semantics remain unchanged. I will also keep the change focused on this PR's Elasticsearch scope and verify the request construction/formatting before pushing.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] Elasticsearch queryEmbedding applies filters as a post-filter, returning fewer than k documents [flink-agents]
Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on issue #999:
URL: https://github.com/apache/flink-agents/issues/999#issuecomment-5288871170
@kaiwangleo thanks for the work on this, and for taking on the CI part. That was the piece I most wanted to see fixed.
#1001 already fixes this issue. The part that is still open is the CI gap. `ElasticsearchVectorStoreTest` is `@Disabled`, so none of this runs in CI today. That is genuinely useful work and it is yours.
Would you be up for scoping #1011 down to just the CI enablement, on top of #1001? The two changes are independent, and splitting them lets a reviewer weigh the CI cost on its own merits.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] Elasticsearch queryEmbedding applies filters as a post-filter, returning fewer than k documents [flink-agents]
Posted by "wenjin272 (via GitHub)" <gi...@apache.org>.
wenjin272 closed issue #999: [Bug] Elasticsearch queryEmbedding applies filters as a post-filter, returning fewer than k documents
URL: https://github.com/apache/flink-agents/issues/999
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] Elasticsearch queryEmbedding applies filters as a post-filter, returning fewer than k documents [flink-agents]
Posted by "kaiwangleo (via GitHub)" <gi...@apache.org>.
kaiwangleo commented on issue #999:
URL: https://github.com/apache/flink-agents/issues/999#issuecomment-5263760287
I reviewed the current implementation and the proposed fix against the Elasticsearch Java client API.\n\nThe issue is reproducible from the code path:\n\n- ilters and rgs.filter_query are combined by combineQueryJson.\n- get and delete attach that JSON as the main query.\n- queryEmbedding currently attaches the same JSON through SearchRequest.Builder.postFilter(...), after the KNN phase has selected k hits.\n- The Elasticsearch client version is 8.19.0, whose KNN request builder exposes a filter on the KNN clause.\n\nProposed implementation:\n\n1. Keep the existing iltersToBoolMustJson and combineQueryJson behavior unchanged, so unified filters and raw ilter_query retain their current AND semantics.\n2. When combined != null, attach the parsed JSON to the KNN clause via KnnSearch.Builder.filter(...) instead of SearchRequest.Builder.postFilter(...).\n3. Remove the separate postFilter assignment; leave unfiltered KNN requests unchanged.\n4. Preserve the existing k and
um_candidates handling and exception behavior.\n\nCompatibility impact:\n\n- This is an intentional behavior fix for filtered queryEmbedding calls.\n- Unfiltered calls are unchanged.\n- Filtered calls will return up to k matching documents, instead of an arbitrary subset of the unfiltered top k; callers may therefore receive more results than before, up to the requested k.\n- The raw JSON filter remains caller-supplied, so unsupported or invalid Elasticsearch DSL continues to fail through the client.\n\nTesting plan:\n\n- Enable or add a focused test with multiple documents whose nearest vectors belong to a different metadata group, then assert that a filtered KNN query returns matching documents up to k.\n- Cover the unified ilters map, raw ilter_query, and both forms combined.\n- Keep the existing no-filter KNN path covered.\n- The current Elasticsearch test class is disabled because it requires a running server; I would either add a focused request-construction unit test with
a mocked client or extend the integration setup so the regression runs in CI rather than remaining disabled.\n\nThis is a user-visible behavior change, so I would appreciate committer confirmation that this approach and compatibility expectation are acceptable before I open the PR.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] Elasticsearch queryEmbedding applies filters as a post-filter, returning fewer than k documents [flink-agents]
Posted by "kaiwangleo (via GitHub)" <gi...@apache.org>.
kaiwangleo commented on issue #999:
URL: https://github.com/apache/flink-agents/issues/999#issuecomment-5275057104
补充更正:按你的要求,这次直接在 Issue #999 对当前分支实施修复并提交,不扩展到其他向量存储或无关测试。修复点保持不变:将合并后的过滤条件放到 Elasticsearch KNN 子句的 ilter 中,避免 post_filter 在取 k 个候选之后才过滤。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] Elasticsearch queryEmbedding applies filters as a post-filter, returning fewer than k documents [flink-agents]
Posted by "kaiwangleo (via GitHub)" <gi...@apache.org>.
kaiwangleo commented on issue #999:
URL: https://github.com/apache/flink-agents/issues/999#issuecomment-5290206561
Thanks for the clarification. #1001 is now merged, so I rebased #1011 onto the latest `main` and scoped it to CI enablement only.
- Removed the duplicate production code, documentation, and filtered-KNN feature-test changes from #1011.
- Kept `ElasticsearchVectorStoreTest` enabled when `ES_HOST` is configured, including explicit store lifecycle cleanup.
- Added a standalone `elasticsearch_tests` CI job and kept the existing multi-module Surefire guard so upstream modules without the focused test do not fail early.
- Local verification passed: focused Spotless check and `ElasticsearchVectorStoreTest` (6 tests, 0 failures).
I will push the rebased CI-only branch next and report the complete CI result once GitHub finishes running it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org