Skip to content

A platform for community discussion. Free, open, simple.

FIX: Preserve original keyword matches in combined AI search (#45328)

Krzysztof Kotlarekd81a7cb81b29
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/services/ai-search-session.js b/plugins/discourse-ai/assets/javascripts/discourse/services/ai-search-session.js
index 1e4fa1387b8..682eb291bf9 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/services/ai-search-session.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/services/ai-search-session.js
@@ -14,11 +14,13 @@ const SEARCH_AGAIN_WINDOW_MS = 3 * 60 * 1000;
// how long the tabs wait on the reply's judgements before judging the query alone
const INTENT_WAIT_MS = 1000;
-function findsAsMuch(results, other) {
- return (
- Boolean(results) &&
- (results.posts?.length ?? 0) >= (other?.posts?.length ?? 0)
- );
+function preservesMatches(results, other) {
+ if (!results) {
+ return false;
+ }
+
+ const postIds = new Set((results.posts ?? []).map((post) => post.id));
+ return (other?.posts ?? []).every((post) => postIds.has(post.id));
}
/**
@@ -291,12 +293,12 @@ export default class AiSearchSession extends Service {
return;
}
- // the rewrite can land first, and stays when it found at least as much
+ // A rewrite can land first, but must retain the original matches.
const empty = { posts: [], resultTypes: [] };
- if (!findsAsMuch(this.topicsResults, topics)) {
+ if (!preservesMatches(this.topicsResults, topics)) {
this.topicsResults = topics ?? empty;
}
- if (scopeKey && !findsAsMuch(this.contextResults, context)) {
+ if (scopeKey && !preservesMatches(this.contextResults, context)) {
this.contextResults = context ?? empty;
}
@@ -398,7 +400,7 @@ export default class AiSearchSession extends Service {
this.search.results = shown;
}
- // the rewrite drops filler words, so it is preferred unless it finds less
+ // More results do not guarantee that the original matches survived a rewrite.
async #applyRewrite(update) {
const query = this.query;
const rewritten = update.keyword_query || "";
@@ -415,10 +417,10 @@ export default class AiSearchSession extends Service {
return;
}
- if (findsAsMuch(topics, this.topicsResults)) {
+ if (preservesMatches(topics, this.topicsResults)) {
this.topicsResults = topics;
}
- if (scopeKey && findsAsMuch(context, this.contextResults)) {
+ if (scopeKey && preservesMatches(context, this.contextResults)) {
this.contextResults = context;
}
this.#showSelectedResults();
diff --git a/plugins/discourse-ai/test/javascripts/unit/services/ai-search-session-test.js b/plugins/discourse-ai/test/javascripts/unit/services/ai-search-session-test.js
new file mode 100644
index 00000000000..d3cd2d76e2c
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/unit/services/ai-search-session-test.js
@@ -0,0 +1,151 @@
+import { settled } from "@ember/test-helpers";
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import { translateResults } from "discourse/lib/search";
+import pretender, { response } from "discourse/tests/helpers/create-pretender";
+
+function searchResults(ids) {
+ return {
+ posts: ids.map((id) => ({ id, topic_id: id, blurb: "Battery expansion" })),
+ topics: ids.map((id) => ({ id, title: "Battery expansion" })),
+ grouped_search_result: {},
+ };
+}
+
+module("Unit | Service | ai-search-session", function (hooks) {
+ setupTest(hooks);
+
+ test("keeps original matches when a larger rewrite result omits one", async function (assert) {
+ const session = this.owner.lookup("service:ai-search-session");
+ const search = this.owner.lookup("service:search");
+ const discoveries = this.owner.lookup("service:discobot-discoveries");
+ const original = await translateResults(searchResults([1, 64334]));
+ session.query = "Batterie erweiterung";
+ session.scope = { key: "category:1" };
+ session.selectedTab = "topics";
+ session.topicsResults = original;
+ session.contextResults = original;
+ search.activeGlobalSearchTerm = session.query;
+ search.results = original;
+ discoveries.activeRequestId = "current-request";
+
+ pretender.get("/search/query", () => response(searchResults([1, 2, 3])));
+
+ session.onDiscovery({
+ query: session.query,
+ request_id: discoveries.activeRequestId,
+ phase: "rewritten",
+ keyword_query: "battery expansion",
+ });
+ discoveries.cancelDiscoveryTimeout();
+ await settled();
+
+ assert.deepEqual(
+ search.results.posts.map((post) => post.id),
+ [1, 64334],
+ "the menu preserves original keyword matches"
+ );
+ assert.deepEqual(
+ search.results.resultTypes[0]?.results.map((post) => post.id),
+ [1, 64334],
+ "the rendered topic group uses the chosen matches"
+ );
+
+ session.selectTab("context");
+ assert.deepEqual(
+ search.results.posts.map((post) => post.id),
+ [1, 64334],
+ "scoped keyword results preserve original matches too"
+ );
+ });
+
+ test("accepts a rewrite that retains all original matches", async function (assert) {
+ const session = this.owner.lookup("service:ai-search-session");
+ const search = this.owner.lookup("service:search");
+ const discoveries = this.owner.lookup("service:discobot-discoveries");
+ const original = await translateResults(searchResults([1, 64334]));
+ session.query = "Batterie erweiterung";
+ session.scope = { key: "category:1" };
+ session.selectedTab = "topics";
+ session.topicsResults = original;
+ session.contextResults = original;
+ search.activeGlobalSearchTerm = session.query;
+ search.results = original;
+ discoveries.activeRequestId = "current-request";
+
+ pretender.get("/search/query", () =>
+ response(searchResults([1, 2, 64334]))
+ );
+
+ session.onDiscovery({
+ query: session.query,
+ request_id: discoveries.activeRequestId,
+ phase: "rewritten",
+ keyword_query: "battery expansion",
+ });
+ discoveries.cancelDiscoveryTimeout();
+ await settled();
+
+ assert.deepEqual(
+ search.results.posts.map((post) => post.id),
+ [1, 2, 64334],
+ "the menu preserves original keyword matches"
+ );
+ assert.deepEqual(
+ search.results.resultTypes[0]?.results.map((post) => post.id),
+ [1, 2, 64334],
+ "the rendered topic group uses the chosen matches"
+ );
+
+ session.selectTab("context");
+ assert.deepEqual(
+ search.results.posts.map((post) => post.id),
+ [1, 2, 64334],
+ "scoped keyword results preserve original matches too"
+ );
+ });
+
+ test("uses rewritten matches when the original query finds nothing", async function (assert) {
+ const session = this.owner.lookup("service:ai-search-session");
+ const search = this.owner.lookup("service:search");
+ const discoveries = this.owner.lookup("service:discobot-discoveries");
+ const original = await translateResults(searchResults([]));
+ session.query = "Batterie erweiterung";
+ session.scope = { key: "category:1" };
+ session.selectedTab = "topics";
+ session.topicsResults = original;
+ session.contextResults = original;
+ search.activeGlobalSearchTerm = session.query;
+ search.results = original;
+ discoveries.activeRequestId = "current-request";
+
+ pretender.get("/search/query", () => response(searchResults([1, 2])));
+
+ session.onDiscovery({
+ query: session.query,
+ request_id: discoveries.activeRequestId,
+ phase: "rewritten",
+ keyword_query: "battery expansion",
+ });
+ discoveries.cancelDiscoveryTimeout();
+ await settled();
+
+ assert.deepEqual(
+ search.results.posts.map((post) => post.id),
+ [1, 2],
+ "the menu preserves original keyword matches"
+ );
+ assert.deepEqual(
+ search.results.resultTypes[0]?.results.map((post) => post.id),
+ [1, 2],
+ "the rendered topic group uses the chosen matches"
+ );
+
+ session.selectTab("context");
+ assert.deepEqual(
+ search.results.posts.map((post) => post.id),
+ [1, 2],
+ "scoped keyword results preserve original matches too"
+ );
+ });
+});