A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(search): match messages that have every word in the query
Hampus63dd214f5361
diff --git a/fluxer_api/pkgs/elasticsearch_search/src/adapters/ElasticsearchIndexAdapter.ts b/fluxer_api/pkgs/elasticsearch_search/src/adapters/ElasticsearchIndexAdapter.tsindex 1c4fbabae..98e165129 100644--- a/fluxer_api/pkgs/elasticsearch_search/src/adapters/ElasticsearchIndexAdapter.ts+++ b/fluxer_api/pkgs/elasticsearch_search/src/adapters/ElasticsearchIndexAdapter.ts@@ -57,6 +57,7 @@ interface ElasticsearchIndexAdapterOptions<TFilters> {searchType?: 'best_fields' | 'bool_prefix';fuzziness?: string;minimumShouldMatch?: string;+ operator?: 'and' | 'or';buildFilters: (filters: TFilters) => Array<ElasticsearchFilter | undefined>;buildSort?: (filters: TFilters) => Array<SortCombinations> | undefined;lock?: ElasticsearchDistributedLock;@@ -75,6 +76,7 @@ export class ElasticsearchIndexAdapter<protected readonly searchType: 'best_fields' | 'bool_prefix';protected readonly fuzziness: string | undefined;protected readonly minimumShouldMatch: string | undefined;+ protected readonly operator: 'and' | 'or';protected readonly buildFilters: (filters: TFilters) => Array<ElasticsearchFilter | undefined>;protected readonly buildSort: ((filters: TFilters) => Array<SortCombinations> | undefined) | undefined;private readonly lock: ElasticsearchDistributedLock | undefined;@@ -87,6 +89,7 @@ export class ElasticsearchIndexAdapter<this.searchType = options.searchType ?? 'best_fields';this.fuzziness = options.fuzziness;this.minimumShouldMatch = options.minimumShouldMatch;+ this.operator = options.operator ?? 'or';this.buildFilters = options.buildFilters;this.buildSort = options.buildSort;this.lock = options.lock;@@ -277,7 +280,7 @@ export class ElasticsearchIndexAdapter<query,fields: this.searchableFields,type: this.searchType,- operator: 'or',+ operator: this.operator,...(this.fuzziness != null ? {fuzziness: this.fuzziness} : {}),...(this.minimumShouldMatch != null ? {minimum_should_match: this.minimumShouldMatch} : {}),},diff --git a/fluxer_api/pkgs/elasticsearch_search/src/adapters/ElasticsearchMessageAdapter.ts b/fluxer_api/pkgs/elasticsearch_search/src/adapters/ElasticsearchMessageAdapter.tsindex 47a6a41ac..1a1d305ac 100644--- a/fluxer_api/pkgs/elasticsearch_search/src/adapters/ElasticsearchMessageAdapter.ts+++ b/fluxer_api/pkgs/elasticsearch_search/src/adapters/ElasticsearchMessageAdapter.ts@@ -107,6 +107,7 @@ export class ElasticsearchMessageAdapter extends ElasticsearchIndexAdapter<Messaclient: options.client,index: ELASTICSEARCH_INDEX_DEFINITIONS.messages,searchableFields: ['content', 'embedContent'],+ operator: 'and',buildFilters: buildMessageFilters,buildSort: buildMessageSort,lock: options.lock,@@ -138,10 +139,9 @@ export class ElasticsearchMessageAdapter extends ElasticsearchIndexAdapter<Messaconst filterClauses = compactFilters(buildMessageFilters({...originalFilters, contents: undefined, exactPhrases: undefined}),);- const should: Array<Record<string, unknown>> = contents.map((term) => ({- multi_match: {query: term, fields: ['content', 'embedContent'], type: 'best_fields'},+ const must: Array<Record<string, unknown>> = contents.map((term) => ({+ multi_match: {query: term, fields: ['content', 'embedContent'], type: 'best_fields', operator: 'and'},}));- const must: Array<Record<string, unknown>> = [{bool: {should, minimum_should_match: 1}}];for (const phrase of exactPhrases) {must.push({bool: {@@ -162,7 +162,7 @@ export class ElasticsearchMessageAdapter extends ElasticsearchIndexAdapter<Messaconst filterClauses = compactFilters(buildMessageFilters({...originalFilters, exactPhrases: undefined}));const must: Array<Record<string, unknown>> = [];if (query) {- must.push({multi_match: {query, fields: ['content'], type: 'best_fields'}});+ must.push({multi_match: {query, fields: ['content', 'embedContent'], type: 'best_fields', operator: 'and'}});}for (const phrase of exactPhrases) {must.push({diff --git a/fluxer_api/src/api/infrastructure/ElasticsearchIndexAdapter.test.ts b/fluxer_api/src/api/infrastructure/ElasticsearchIndexAdapter.test.tsindex ed3f4a5a2..15fad6bdb 100644--- a/fluxer_api/src/api/infrastructure/ElasticsearchIndexAdapter.test.ts+++ b/fluxer_api/src/api/infrastructure/ElasticsearchIndexAdapter.test.ts@@ -24,6 +24,7 @@ interface SearchConfiguration {fuzziness?: string;minimumShouldMatch?: string;searchType?: 'best_fields' | 'bool_prefix';+ operator?: 'and' | 'or';}interface RecordedRequest {@@ -221,6 +222,12 @@ const queryCases: Array<QueryCase> = [query: 'needle',must: [{multi_match: {query: 'needle', fields: ['content^2', 'id'], type: 'best_fields', operator: 'or'}}],},+ {+ name: 'text query with every word required',+ query: 'good idea',+ operator: 'and',+ must: [{multi_match: {query: 'good idea', fields: ['content^2', 'id'], type: 'best_fields', operator: 'and'}}],+ },{name: "text query, fuzziness omitted, minimum '2'",query: 'needle',diff --git a/fluxer_api/src/api/search/meilisearch/MeilisearchDomainAdapters.ts b/fluxer_api/src/api/search/meilisearch/MeilisearchDomainAdapters.tsindex 89a1e6be9..a53d23f70 100644--- a/fluxer_api/src/api/search/meilisearch/MeilisearchDomainAdapters.ts+++ b/fluxer_api/src/api/search/meilisearch/MeilisearchDomainAdapters.ts@@ -210,6 +210,7 @@ export class MeilisearchMessageAdapter extends MeilisearchIndexAdapter<MessageSebuildFilters: buildMessageFilters,buildSort: buildTimestampSort,buildQuery: buildMessageQuery,+ matchingStrategy: 'all',});}}diff --git a/fluxer_api/src/api/search/meilisearch/MeilisearchIndexAdapter.test.ts b/fluxer_api/src/api/search/meilisearch/MeilisearchIndexAdapter.test.tsindex 39be32488..0253cb59a 100644--- a/fluxer_api/src/api/search/meilisearch/MeilisearchIndexAdapter.test.ts+++ b/fluxer_api/src/api/search/meilisearch/MeilisearchIndexAdapter.test.ts@@ -123,6 +123,7 @@ describe('MeilisearchMessageAdapter', () => {sort: ['createdAt:asc', 'id:asc'],attributesToSearchOn: ['content', 'embedContent'],showRankingScore: false,+ matchingStrategy: 'all',},});});diff --git a/fluxer_api/src/api/search/meilisearch/MeilisearchIndexAdapter.ts b/fluxer_api/src/api/search/meilisearch/MeilisearchIndexAdapter.tsindex 5212ea17d..a4fa91a78 100644--- a/fluxer_api/src/api/search/meilisearch/MeilisearchIndexAdapter.ts+++ b/fluxer_api/src/api/search/meilisearch/MeilisearchIndexAdapter.ts@@ -21,6 +21,7 @@ interface MeilisearchIndexAdapterOptions<TFilters> {buildFilters: (filters: TFilters) => Array<MeilisearchFilter | undefined>;buildSort?: (filters: TFilters) => Array<string> | undefined;buildQuery?: (query: string, filters: TFilters) => string;+ matchingStrategy?: 'all' | 'last';}interface MeilisearchIndexSettings {@@ -58,6 +59,7 @@ export class MeilisearchIndexAdapter<protected readonly buildFilters: (filters: TFilters) => Array<MeilisearchFilter | undefined>;protected readonly buildSort: ((filters: TFilters) => Array<string> | undefined) | undefined;protected readonly buildQuery: ((query: string, filters: TFilters) => string) | undefined;+ protected readonly matchingStrategy: 'all' | 'last' | undefined;private readonly pendingBulkTaskIds = new Set<number>();private initialized = false;@@ -67,6 +69,7 @@ export class MeilisearchIndexAdapter<this.buildFilters = options.buildFilters;this.buildSort = options.buildSort;this.buildQuery = options.buildQuery;+ this.matchingStrategy = options.matchingStrategy;}async initialize(): Promise<void> {@@ -197,6 +200,7 @@ export class MeilisearchIndexAdapter<sort: this.buildSort?.(filters),attributesToSearchOn: this.indexDefinition.searchableAttributes,showRankingScore: false,+ ...(this.matchingStrategy ? {matchingStrategy: this.matchingStrategy} : {}),...(facets && facets.length > 0 ? {facets} : {}),},);diff --git a/fluxer_docs/src/content/docs/http-api/search.mdx b/fluxer_docs/src/content/docs/http-api/search.mdxindex cdf443b83..5e4641f8a 100644--- a/fluxer_docs/src/content/docs/http-api/search.mdx+++ b/fluxer_docs/src/content/docs/http-api/search.mdx@@ -8,7 +8,7 @@ import RouteHeader from '@/components/RouteHeader.astro';Message search finds indexed messages that match a text query and filters. One request can search a channel, a guild, your direct messages or several of these.-How the text query matches words and typos depends on the instance's search backend (Elasticsearch or Meilisearch), but filters always match exactly.+A message matches the text query only when it has every word of it. How words and typos match depends on the instance's search backend (Elasticsearch or Meilisearch), but filters always match exactly.## Message search result object