Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(tags): ANY/ALL filtering for tag filters in search (#4677)
gbirmanc5b68a99a503
diff --git a/js/app/packages/app/component/next-soup/filters/filter-store/compile.test.ts b/js/app/packages/app/component/next-soup/filters/filter-store/compile.test.tsindex 821156b6ec..9b7f67037c 100644--- a/js/app/packages/app/component/next-soup/filters/filter-store/compile.test.ts+++ b/js/app/packages/app/component/next-soup/filters/filter-store/compile.test.ts@@ -169,4 +169,45 @@ describe('compileToAst', () => {'!': { l: { RootSender: 'macro|[email protected]' } },});});++ it('compiles tag filters as one OR group across definitions by default', () => {+ const ast = compileToAst(+ queryStateFrom({+ include: {+ tagFilters: [+ { propertyId: 'def-1', type: 'select', value: 'opt-1' },+ { propertyId: 'def-2', type: 'select', value: 'opt-2' },+ ],+ },+ })+ );++ expect(ast.propf).toEqual({+ '|': [+ { l: { pd: 'def-1', v: { so: 'opt-1' } } },+ { l: { pd: 'def-2', v: { so: 'opt-2' } } },+ ],+ });+ });++ it('compiles tag filters as an AND group when tagFilterMode is all', () => {+ const ast = compileToAst(+ queryStateFrom({+ include: {+ tagFilterMode: 'all',+ tagFilters: [+ { propertyId: 'def-1', type: 'select', value: 'opt-1' },+ { propertyId: 'def-2', type: 'select', value: 'opt-2' },+ ],+ },+ })+ );++ expect(ast.propf).toEqual({+ '&': [+ { l: { pd: 'def-1', v: { so: 'opt-1' } } },+ { l: { pd: 'def-2', v: { so: 'opt-2' } } },+ ],+ });+ });});diff --git a/js/app/packages/app/component/next-soup/filters/filter-store/compile.ts b/js/app/packages/app/component/next-soup/filters/filter-store/compile.tsindex 18d08fbd43..f5a5d6e1cb 100644--- a/js/app/packages/app/component/next-soup/filters/filter-store/compile.ts+++ b/js/app/packages/app/component/next-soup/filters/filter-store/compile.ts@@ -47,7 +47,7 @@ type DateRangeFieldName =type CompiledFieldName = Exclude<FieldName,- 'properties' | 'tagFilters' | DateRangeFieldName+ 'properties' | 'tagFilters' | 'tagFilterMode' | DateRangeFieldName>;const AST = {@@ -378,12 +378,16 @@ export function compileToAst(state: QueryState): TargetAstMap {}}- // Tags: one OR group across every selected tag, regardless of which+ // Tags: one group across every selected tag, regardless of which// definition owns it. Pushed as a single propf entry so it ANDs with the- // status/priority groups but ORs internally (match any selected tag).+ // status/priority groups. Internally the group ORs (match any selected+ // tag) unless tagFilterMode is 'all', which ANDs the per-tag literals.const includeTags = state.include.tagFilters ?? [];if (includeTags.length) {- byTarget.propf.push(AST.or(includeTags.map(propertyToAst)));+ const tagAsts = includeTags.map(propertyToAst);+ byTarget.propf.push(+ state.include.tagFilterMode === 'all' ? AST.and(tagAsts) : AST.or(tagAsts)+ );}pushDateRangeFiltersToTargets(byTarget, state.include, state.exclude);diff --git a/js/app/packages/app/component/next-soup/filters/filter-store/types.ts b/js/app/packages/app/component/next-soup/filters/filter-store/types.tsindex 9191d0ac01..9b79f9fb09 100644--- a/js/app/packages/app/component/next-soup/filters/filter-store/types.ts+++ b/js/app/packages/app/component/next-soup/filters/filter-store/types.ts@@ -23,6 +23,8 @@ export type PropertyFilter = {value: string;};+export type TagFilterMode = 'any' | 'all';+export type ArrayFieldFilters = {documentId?: string[];fileType?: string[];@@ -61,6 +63,9 @@ export type ArrayFieldFilters = {};export type ScalarFieldFilters = {+ // How the selected tagFilters combine: 'any' (default when absent) matches+ // items holding at least one selected tag, 'all' requires every one.+ tagFilterMode?: TagFilterMode;documentSeen?: boolean;documentDone?: boolean;isEmailAttachment?: boolean;diff --git a/js/app/packages/app/component/next-soup/soup-view/create-search-state.ts b/js/app/packages/app/component/next-soup/soup-view/create-search-state.tsindex b094835215..4351c09994 100644--- a/js/app/packages/app/component/next-soup/soup-view/create-search-state.ts+++ b/js/app/packages/app/component/next-soup/soup-view/create-search-state.ts@@ -185,10 +185,14 @@ function filterDataToQueryFilters(data: QueryState): EntityFilters {filters.property_filters = propertyFilters;}- // Tags: match on the option ids alone (globally unique), OR'd across all tag- // definitions. No definition id is sent — the backend matches values only.+ // Tags: match on the option ids alone (globally unique), combined across+ // all tag definitions. No definition id is sent — the backend matches+ // values only. The mode picks any-of (default) vs all-of combining.if (include.tagFilters?.length) {filters.tag_option_ids = include.tagFilters.map((t) => t.value);+ if (include.tagFilterMode === 'all') {+ filters.tag_filter_mode = 'all';+ }}return filters;diff --git a/js/app/packages/app/component/next-soup/soup-view/filters-bar/search/search-facet-chip.tsx b/js/app/packages/app/component/next-soup/soup-view/filters-bar/search/search-facet-chip.tsxindex c0f62a2b23..b49c22d333 100644--- a/js/app/packages/app/component/next-soup/soup-view/filters-bar/search/search-facet-chip.tsx+++ b/js/app/packages/app/component/next-soup/soup-view/filters-bar/search/search-facet-chip.tsx@@ -1,3 +1,4 @@+import type { TagFilterMode } from '@app/component/next-soup/filters/filter-store/types';import { useSplitPanelOrThrow } from '@app/component/split-layout/layoutUtils';import { Combobox } from '@kobalte/core/combobox';import XIcon from '@phosphor/x.svg';@@ -11,7 +12,7 @@ import {Switch,} from 'solid-js';import { SearchableMultiSelect } from '../searchable-multi-select';-import type { FacetOption, SearchFacetVM } from './search-facets';+import type { FacetModeVM, FacetOption, SearchFacetVM } from './search-facets';interface SearchFacetChipProps {facet: SearchFacetVM;@@ -94,6 +95,37 @@ const SingleValueSegment = (props: {);};+const MODE_OPTIONS: { id: TagFilterMode; label: string }[] = [+ { id: 'any', label: 'any of' },+ { id: 'all', label: 'all of' },+];++const ModeSegment = (props: { mode: FacetModeVM }) => (+ <Dropdown>+ <Dropdown.Trigger+ variant="ghost"+ class="inline-flex items-center px-2 h-auto! text-ink-muted hover:bg-ink/5 active:bg-ink/8 rounded-none"+ >+ {MODE_OPTIONS.find((o) => o.id === props.mode.value())?.label}+ </Dropdown.Trigger>+ <Dropdown.Content class="shadow-menu">+ <Dropdown.Group>+ <For each={MODE_OPTIONS}>+ {(option) => (+ <Dropdown.Item+ onSelect={() => props.mode.onSelect(option.id)}+ closeOnSelect+ >+ <span class="flex-1 truncate">{option.label}</span>+ <SingleSelectCheck active={props.mode.value() === option.id} />+ </Dropdown.Item>+ )}+ </For>+ </Dropdown.Group>+ </Dropdown.Content>+ </Dropdown>+);+const MultiValueSegment = (props: {facet: Extract<SearchFacetVM, { kind: 'multi' }>;}) => {@@ -140,6 +172,21 @@ export const SearchFacetChip = (props: SearchFacetChipProps) => (<ChipDivider />+ <Show+ when={+ props.facet.kind === 'multi' &&+ props.facet.mode?.visible() &&+ props.facet.mode+ }+ >+ {(mode) => (+ <>+ <ModeSegment mode={mode()} />+ <ChipDivider />+ </>+ )}+ </Show>+<Switch><Match when={props.facet.kind === 'single' && props.facet}>{(facet) => (diff --git a/js/app/packages/app/component/next-soup/soup-view/filters-bar/search/search-facets.tsx b/js/app/packages/app/component/next-soup/soup-view/filters-bar/search/search-facets.tsxindex 20807fddee..c51bb03727 100644--- a/js/app/packages/app/component/next-soup/soup-view/filters-bar/search/search-facets.tsx+++ b/js/app/packages/app/component/next-soup/soup-view/filters-bar/search/search-facets.tsx@@ -1,6 +1,7 @@import type {CallStatus,PropertyFilter,+ TagFilterMode,} from '@app/component/next-soup/filters/filter-store/types';import { EntityIcon } from '@core/component/EntityIcon';import { UserIcon } from '@core/component/UserIcon';@@ -142,6 +143,15 @@ type FacetBase = {reset: () => void;};+/** Optional any-of/all-of segment on a multi facet, à la Linear's label+ * filter. Rendered as its own dropdown segment between the facet label and+ * the values, only while `visible` (mode is meaningless under 2 values). */+export type FacetModeVM = {+ value: Accessor<TagFilterMode>;+ onSelect: (mode: TagFilterMode) => void;+ visible: Accessor<boolean>;+};+export type SearchFacetVM = FacetBase &(| {@@ -158,6 +168,7 @@ export type SearchFacetVM = FacetBase &placeholder: string;preserveOrder?: boolean;onOnly?: (id: string) => void;+ mode?: FacetModeVM;});@@ -250,7 +261,7 @@ function multiFacet(args: {options: Accessor<SearchableOption[]>;activeIds: Accessor<string[]>;onChange: (ids: string[]) => void;-}): SearchFacetVM {+}): Extract<SearchFacetVM, { kind: 'multi' }> {return {kind: 'multi',id: args.id,@@ -446,24 +457,31 @@ export function useSearchFacets(onChange: controller.setTaskCreatedBy,});- const tags = multiFacet({- id: 'tags',- label: 'Tags',- neutralLabel: 'Any tag',- placeholder: 'Filter by tag...',- options: tagSource.options,- activeIds: () => controller.tags().map((t) => t.value),- onChange: (ids) => {- const byOption = tagSource.defByOption();- controller.setTags(- ids.reduce<PropertyFilter[]>((acc, id) => {- const propertyId = byOption.get(id);- if (propertyId) acc.push({ propertyId, type: 'select', value: id });- return acc;- }, [])- );+ const tags: SearchFacetVM = {+ ...multiFacet({+ id: 'tags',+ label: 'Tags',+ neutralLabel: 'Any tag',+ placeholder: 'Filter by tag...',+ options: tagSource.options,+ activeIds: () => controller.tags().map((t) => t.value),+ onChange: (ids) => {+ const byOption = tagSource.defByOption();+ controller.setTags(+ ids.reduce<PropertyFilter[]>((acc, id) => {+ const propertyId = byOption.get(id);+ if (propertyId) acc.push({ propertyId, type: 'select', value: id });+ return acc;+ }, [])+ );+ },+ }),+ mode: {+ value: controller.tagMode,+ onSelect: controller.setTagMode,+ visible: () => controller.tags().length >= 2,},- });+ };// Tags show only where tagging applies (all/documents/tasks/emails/agents/// folders), gated behind both the broad tags flag and the search-viewdiff --git a/js/app/packages/app/component/next-soup/soup-view/filters-bar/search/search-filters-state.ts b/js/app/packages/app/component/next-soup/soup-view/filters-bar/search/search-filters-state.tsindex 3a59181d0a..0944c5c12b 100644--- a/js/app/packages/app/component/next-soup/soup-view/filters-bar/search/search-filters-state.ts+++ b/js/app/packages/app/component/next-soup/soup-view/filters-bar/search/search-filters-state.ts@@ -9,6 +9,7 @@ import {callStatusFromAttended,type FieldFilters,type PropertyFilter,+ type TagFilterMode,} from '@app/component/next-soup/filters/filter-store/types';import { useSoupView } from '@app/component/next-soup/soup-view/soup-view-context';import { SYSTEM_PROPERTY_IDS } from '@property/constants';@@ -72,8 +73,10 @@ export type SearchFiltersState = SearchFiltersSections & {// Selected tags. Applied only on the TAG_SEARCH_TYPES types and read live// from the compiled query, so they persist across those types but clear// when switching to a type where tags do not apply (channels/calls/etc).- // Each entry carries its owning definition id and option id; combined as one OR.+ // Each entry carries its owning definition id and option id.tags: PropertyFilter[];+ // How the selected tags combine: match any of them (default) or all.+ tagMode: TagFilterMode;};export const DEFAULT_SECTIONS: SearchFiltersSections = {@@ -101,6 +104,9 @@ export function compileSearchQuery(state: SearchFiltersState): Query {// silently empties a search narrowed to channels/calls.if (TAG_SEARCH_TYPES.has(state.type) && state.tags.length) {include.tagFilters = state.tags;+ if (state.tagMode === 'all') {+ include.tagFilterMode = 'all';+ }}if (state.type === 'all') return { include, exclude };@@ -202,6 +208,9 @@ export function createSearchFiltersController() {);const taskCreatedBy = createMemo(() => withoutNil(include().documentOwnerId));const tags = createMemo<PropertyFilter[]>(() => include().tagFilters ?? []);+ const tagMode = createMemo<TagFilterMode>(+ () => include().tagFilterMode ?? 'any'+ );const currentSections = (): SearchFiltersSections => ({email: { importance: emailImportance(), inboxIds: emailInbox() },@@ -222,6 +231,7 @@ export function createSearchFiltersController() {// switches (they compile only for TAG_SEARCH_TYPES, so `tags()` reads empty// on other types — snapshot them on switch-away so the selection survives).let stashedTags: PropertyFilter[] = [];+ let stashedTagMode: TagFilterMode = 'any';const apply = (state: SearchFiltersState) =>batch(() => {@@ -233,7 +243,13 @@ export function createSearchFiltersController() {});const applySections = (sections: Partial<SearchFiltersSections>) =>- apply({ type: type(), tags: tags(), ...currentSections(), ...sections });+ apply({+ type: type(),+ tags: tags(),+ tagMode: tagMode(),+ ...currentSections(),+ ...sections,+ });const setType = (next: SearchTypeValue) => {const current = type();@@ -265,9 +281,12 @@ export function createSearchFiltersController() {// Refresh the tag snapshot from the live value only while leaving a type// that compiles tags; other types read empty and would clobber it.- if (TAG_SEARCH_TYPES.has(current)) stashedTags = tags();+ if (TAG_SEARCH_TYPES.has(current)) {+ stashedTags = tags();+ stashedTagMode = tagMode();+ }- apply({ type: next, tags: stashedTags, ...stash });+ apply({ type: next, tags: stashedTags, tagMode: stashedTagMode, ...stash });};return {@@ -275,7 +294,20 @@ export function createSearchFiltersController() {setType,tags,setTags: (filters: PropertyFilter[]) =>- apply({ type: type(), tags: filters, ...currentSections() }),+ apply({+ type: type(),+ tags: filters,+ tagMode: tagMode(),+ ...currentSections(),+ }),+ tagMode,+ setTagMode: (mode: TagFilterMode) =>+ apply({+ type: type(),+ tags: tags(),+ tagMode: mode,+ ...currentSections(),+ }),emailImportance,setEmailImportance: (importance: boolean | undefined) =>applySections({ email: { importance, inboxIds: emailInbox() } }),diff --git a/js/app/packages/app/component/next-soup/soup-view/soup-view-context.tsx b/js/app/packages/app/component/next-soup/soup-view/soup-view-context.tsxindex 07651635b3..ed88371e04 100644--- a/js/app/packages/app/component/next-soup/soup-view/soup-view-context.tsx+++ b/js/app/packages/app/component/next-soup/soup-view/soup-view-context.tsx@@ -522,18 +522,28 @@ export const SoupViewContextProvider: FlowComponent<};};- // Active tag option ids, used to gate optimistic websocket inserts so an- // active tag filter is honored even on the grouped render path.+ // Active tag option ids and combine mode, used to gate optimistic websocket+ // inserts so an active tag filter is honored even on the grouped render path.const activeTagOptionIds = createMemo(() =>(queryFilters.state.include.tagFilters ?? []).map((t) => t.value));+ const activeTagFilterMode = () =>+ queryFilters.state.include.tagFilterMode ?? 'any';const soupItemMatchesActiveFilters = (item: SoupApiItem,view: ListView | undefined): boolean => {if (!soupItemMatchesListView(item, view)) return false;- if (!soupItemMatchesTagFilter(item, activeTagOptionIds())) return false;+ if (+ !soupItemMatchesTagFilter(+ item,+ activeTagOptionIds(),+ activeTagFilterMode()+ )+ ) {+ return false;+ }return soup.predicates.test(mapApiSoupItemToEntity(item) as SoupEntity,@@ -618,13 +628,14 @@ export const SoupViewContextProvider: FlowComponent<let transformed = items();const ctx = getFilterContext();const tagOptionIds = activeTagOptionIds();+ const tagFilterMode = activeTagFilterMode();const next = [];for (const entity of transformed) {if (!soup.predicates.test(entity, ctx)) {continue;}- if (!entityMatchesTagFilter(entity, tagOptionIds)) {+ if (!entityMatchesTagFilter(entity, tagOptionIds, tagFilterMode)) {continue;}next.push(entity);diff --git a/js/app/packages/app/component/next-soup/soup-view/soup-view-tabs.tsx b/js/app/packages/app/component/next-soup/soup-view/soup-view-tabs.tsxindex 467f8544b3..e70f47ed29 100644--- a/js/app/packages/app/component/next-soup/soup-view/soup-view-tabs.tsx+++ b/js/app/packages/app/component/next-soup/soup-view/soup-view-tabs.tsx@@ -178,11 +178,15 @@ export const useApplyPreset = () => {// Tags are a query-only refinement with no backing predicate, so the// predicate-driven reconstruction above never carries them. Preserve the- // active selection explicitly like any other refinement.+ // active selection (and its combine mode) explicitly like any other+ // refinement.const activeTagFilters = queryFilters.state.include.tagFilters;if (activeTagFilters?.length) {mergedFilters = mergeQuery(mergedFilters, {- include: { tagFilters: activeTagFilters.map((t) => ({ ...t })) },+ include: {+ tagFilters: activeTagFilters.map((t) => ({ ...t })),+ tagFilterMode: queryFilters.state.include.tagFilterMode,+ },});}diff --git a/js/app/packages/app/component/next-soup/soup-view/soup-view.tsx b/js/app/packages/app/component/next-soup/soup-view/soup-view.tsxindex 10d7f8ec89..b74756ca9c 100644--- a/js/app/packages/app/component/next-soup/soup-view/soup-view.tsx+++ b/js/app/packages/app/component/next-soup/soup-view/soup-view.tsx@@ -399,6 +399,7 @@ export const SoupView = (props: SoupViewProps) => {if (!query?.include?.tagFilters?.length) return query;const include = { ...query.include };delete include.tagFilters;+ delete include.tagFilterMode;return { ...query, include };};diff --git a/js/app/packages/app/constants/list-views.ts b/js/app/packages/app/constants/list-views.tsindex 438a592049..10e539be3f 100644--- a/js/app/packages/app/constants/list-views.ts+++ b/js/app/packages/app/constants/list-views.ts@@ -1,5 +1,9 @@+import type { TagFilterMode } from '@app/component/next-soup/filters/filter-store/types';import type { EntityData } from '@entity';-import type { SoupApiItem } from '@service-storage/generated/schemas';+import type {+ SoupApiItem,+ SoupProperty,+} from '@service-storage/generated/schemas';import { match } from 'ts-pattern';export const LIST_VIEWS = [@@ -71,33 +75,43 @@ export const soupItemMatchesListView = (.with('companies', () => item.tag === 'crmCompany').exhaustive();-/**- * Whether a soup item carries at least one of the given tag option ids. Tags are- * SelectOption property values on the item and option ids are globally unique, so- * the owning definition is not consulted. An empty filter matches everything.- * Used to gate optimistic websocket inserts so the grouped render (which skips- * client predicates) still honors an active tag filter.- */-export const soupItemMatchesTagFilter = (- item: SoupApiItem,- tagOptionIds: readonly string[]+const propertiesMatchTagFilter = (+ properties: readonly SoupProperty[] | undefined,+ tagOptionIds: readonly string[],+ mode: TagFilterMode): boolean => {if (tagOptionIds.length === 0) return true;- const properties =- 'properties' in item.data ? item.data.properties : undefined;if (!properties?.length) return false;- const wanted = new Set(tagOptionIds);+ const held = new Set<string>();for (const { value } of properties) {- if (- value?.type === 'SelectOption' &&- value.value.some((id) => wanted.has(id))- ) {- return true;+ if (value?.type === 'SelectOption') {+ for (const id of value.value) held.add(id);}}- return false;+ return mode === 'all'+ ? tagOptionIds.every((id) => held.has(id))+ : tagOptionIds.some((id) => held.has(id));};+/**+ * Whether a soup item carries the given tag option ids — at least one of them+ * ('any', the default) or every one ('all'). Tags are SelectOption property+ * values on the item and option ids are globally unique, so the owning+ * definition is not consulted. An empty filter matches everything. Used to+ * gate optimistic websocket inserts so the grouped render (which skips client+ * predicates) still honors an active tag filter.+ */+export const soupItemMatchesTagFilter = (+ item: SoupApiItem,+ tagOptionIds: readonly string[],+ mode: TagFilterMode = 'any'+): boolean =>+ propertiesMatchTagFilter(+ 'properties' in item.data ? item.data.properties : undefined,+ tagOptionIds,+ mode+ );+/*** `soupItemMatchesTagFilter` for mapped entities. Rendered rows are checked* against the active tag filter because the server only tag-filters the@@ -107,19 +121,11 @@ export const soupItemMatchesTagFilter = (*/export const entityMatchesTagFilter = (entity: EntityData,- tagOptionIds: readonly string[]-): boolean => {- if (tagOptionIds.length === 0) return true;- const properties = 'properties' in entity ? entity.properties : undefined;- if (!properties?.length) return false;- const wanted = new Set(tagOptionIds);- for (const { value } of properties) {- if (- value?.type === 'SelectOption' &&- value.value.some((id) => wanted.has(id))- ) {- return true;- }- }- return false;-};+ tagOptionIds: readonly string[],+ mode: TagFilterMode = 'any'+): boolean =>+ propertiesMatchTagFilter(+ 'properties' in entity ? entity.properties : undefined,+ tagOptionIds,+ mode+ );diff --git a/js/app/packages/service-clients/service-search/generated/models/entityFilters.ts b/js/app/packages/service-clients/service-search/generated/models/entityFilters.tsindex 9aee9f04e4..77c4f75ff4 100644--- a/js/app/packages/service-clients/service-search/generated/models/entityFilters.ts+++ b/js/app/packages/service-clients/service-search/generated/models/entityFilters.ts@@ -14,6 +14,7 @@ import type { EmailFilters } from './emailFilters';import type { ForeignEntityFilters } from './foreignEntityFilters';import type { ProjectFilters } from './projectFilters';import type { PropertyFilter } from './propertyFilter';+import type { TagFilterMode } from './tagFilterMode';/*** a bundle of all of the filters for each entity type@@ -39,6 +40,9 @@ export interface EntityFilters {project_filters?: ProjectFilters;/** property-based filters applied across entity types */property_filters?: PropertyFilter[];+ /** How the `tag_option_ids` combine: `any` (default) matches entities+holding at least one selected tag, `all` requires every selected tag. */+ tag_filter_mode?: TagFilterMode;/** tag option ids matched against `properties.values`, OR'd together acrossall tag definitions. Option ids are globally unique, so the match ignoresthe owning definition id (personal and team tags combine into one OR). */diff --git a/js/app/packages/service-clients/service-search/generated/models/index.ts b/js/app/packages/service-clients/service-search/generated/models/index.tsindex f8589274a5..51b773edbd 100644--- a/js/app/packages/service-clients/service-search/generated/models/index.ts+++ b/js/app/packages/service-clients/service-search/generated/models/index.ts@@ -267,6 +267,7 @@ export * from './simpleUnifiedSearchResponseBaseItemOneOfSevenAllOf';export * from './simpleUnifiedSearchResponseBaseItemOneOfSevenAllOfType';export * from './soupProperty';export * from './soupPropertyValue';+export * from './tagFilterMode';export * from './taskFilters';export * from './taskFiltersIncludeCbmAtmNc';export * from './unifiedSearchParams';diff --git a/js/app/packages/service-clients/service-search/generated/models/tagFilterMode.ts b/js/app/packages/service-clients/service-search/generated/models/tagFilterMode.tsnew file mode 100644index 0000000000..b28a4361a6--- /dev/null+++ b/js/app/packages/service-clients/service-search/generated/models/tagFilterMode.ts@@ -0,0 +1,17 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */++/**+ * How multiple `tag_option_ids` combine when filtering.+ */+export type TagFilterMode = (typeof TagFilterMode)[keyof typeof TagFilterMode];++// eslint-disable-next-line @typescript-eslint/no-redeclare+export const TagFilterMode = {+ any: 'any',+ all: 'all',+} as const;diff --git a/js/app/packages/service-clients/service-search/openapi.json b/js/app/packages/service-clients/service-search/openapi.jsonindex a5ddb45e34..a0b727e20a 100644--- a/js/app/packages/service-clients/service-search/openapi.json+++ b/js/app/packages/service-clients/service-search/openapi.json@@ -1657,6 +1657,10 @@},"description": "property-based filters applied across entity types"},+ "tag_filter_mode": {+ "$ref": "#/components/schemas/TagFilterMode",+ "description": "How the `tag_option_ids` combine: `any` (default) matches entities\nholding at least one selected tag, `all` requires every selected tag."+ },"tag_option_ids": {"type": "array","items": {@@ -3724,6 +3728,11 @@}}},+ "TagFilterMode": {+ "type": "string",+ "description": "How multiple `tag_option_ids` combine when filtering.",+ "enum": ["any", "all"]+ },"TaskFilters": {"type": "object","description": "Task-only filters nested under document filters.",diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/entityFilters.ts b/js/app/packages/service-clients/service-storage/generated/schemas/entityFilters.tsindex 63a0fb71df..ec9032c835 100644--- a/js/app/packages/service-clients/service-storage/generated/schemas/entityFilters.ts+++ b/js/app/packages/service-clients/service-storage/generated/schemas/entityFilters.ts@@ -14,6 +14,7 @@ import type { EmailFilters } from './emailFilters';import type { ForeignEntityFilters } from './foreignEntityFilters';import type { ProjectFilters } from './projectFilters';import type { PropertyFilter } from './propertyFilter';+import type { TagFilterMode } from './tagFilterMode';/*** a bundle of all of the filters for each entity type@@ -39,6 +40,9 @@ export interface EntityFilters {project_filters?: ProjectFilters;/** property-based filters applied across entity types */property_filters?: PropertyFilter[];+ /** How the `tag_option_ids` combine: `any` (default) matches entities+holding at least one selected tag, `all` requires every selected tag. */+ tag_filter_mode?: TagFilterMode;/** tag option ids matched against `properties.values`, OR'd together acrossall tag definitions. Option ids are globally unique, so the match ignoresthe owning definition id (personal and team tags combine into one OR). */diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/index.ts b/js/app/packages/service-clients/service-storage/generated/schemas/index.tsindex df085981f0..de2eaed799 100644--- a/js/app/packages/service-clients/service-storage/generated/schemas/index.ts+++ b/js/app/packages/service-clients/service-storage/generated/schemas/index.ts@@ -956,6 +956,7 @@ export * from './soupThreadReplyEditedAt';export * from './string';export * from './successResponse';export * from './syncServiceVersionID';+export * from './tagFilterMode';export * from './taskFilters';export * from './taskFiltersIncludeCbmAtmNc';export * from './teamRole';diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/tagFilterMode.ts b/js/app/packages/service-clients/service-storage/generated/schemas/tagFilterMode.tsnew file mode 100644index 0000000000..be9187f1a4--- /dev/null+++ b/js/app/packages/service-clients/service-storage/generated/schemas/tagFilterMode.ts@@ -0,0 +1,17 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * document_storage_service+ * OpenAPI spec version: 0.1.0+ */++/**+ * How multiple `tag_option_ids` combine when filtering.+ */+export type TagFilterMode = (typeof TagFilterMode)[keyof typeof TagFilterMode];++// eslint-disable-next-line @typescript-eslint/no-redeclare+export const TagFilterMode = {+ any: 'any',+ all: 'all',+} as const;diff --git a/js/app/packages/service-clients/service-storage/generated/zod.ts b/js/app/packages/service-clients/service-storage/generated/zod.tsindex 0a6beb532a..5703c264ba 100644--- a/js/app/packages/service-clients/service-storage/generated/zod.ts+++ b/js/app/packages/service-clients/service-storage/generated/zod.ts@@ -9194,6 +9194,10 @@ export const postItemsSoupBody = zod).optional().describe('property-based filters applied across entity types'),+ tag_filter_mode: zod+ .enum(['any', 'all'])+ .optional()+ .describe('How multiple `tag_option_ids` combine when filtering.'),tag_option_ids: zod.array(zod.string()).optional()diff --git a/js/app/packages/service-clients/service-storage/openapi.json b/js/app/packages/service-clients/service-storage/openapi.jsonindex 383173f32f..e3e10f62a6 100644--- a/js/app/packages/service-clients/service-storage/openapi.json+++ b/js/app/packages/service-clients/service-storage/openapi.json@@ -14166,6 +14166,10 @@},"description": "property-based filters applied across entity types"},+ "tag_filter_mode": {+ "$ref": "#/components/schemas/TagFilterMode",+ "description": "How the `tag_option_ids` combine: `any` (default) matches entities\nholding at least one selected tag, `all` requires every selected tag."+ },"tag_option_ids": {"type": "array","items": {@@ -19300,6 +19304,11 @@}}},+ "TagFilterMode": {+ "type": "string",+ "description": "How multiple `tag_option_ids` combine when filtering.",+ "enum": ["any", "all"]+ },"TaskFilters": {"type": "object","description": "Task-only filters nested under document filters.",diff --git a/rust/cloud-storage/.sqlx/query-2d726fec1915305011d1b97e3baa8b2c37cdd9e6b2ac0fb412705c511021913c.json b/rust/cloud-storage/.sqlx/query-355db98772e35a57aab23852eb018148f74fe07ef799f5aa517613470fa5cb6c.jsonsimilarity index 53%rename from rust/cloud-storage/.sqlx/query-2d726fec1915305011d1b97e3baa8b2c37cdd9e6b2ac0fb412705c511021913c.jsonrename to rust/cloud-storage/.sqlx/query-355db98772e35a57aab23852eb018148f74fe07ef799f5aa517613470fa5cb6c.jsonindex 7adcb2f47f..1afca0ebea 100644--- a/rust/cloud-storage/.sqlx/query-2d726fec1915305011d1b97e3baa8b2c37cdd9e6b2ac0fb412705c511021913c.json+++ b/rust/cloud-storage/.sqlx/query-355db98772e35a57aab23852eb018148f74fe07ef799f5aa517613470fa5cb6c.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT\n c.id as entity_id,\n c.name,\n regexp_replace(\n c.name,\n $7,\n '<macro_em>\\1</macro_em>',\n 'gi'\n ) as name_highlighted,\n c.\"updatedAt\" as updated_at\n FROM \"Chat\" c\n WHERE (c.\"userId\" = $1 OR c.id = ANY($2))\n AND c.\"deletedAt\" IS NULL\n AND c.name ILIKE $3\n AND (\n $5::timestamptz IS NULL\n OR (c.\"updatedAt\", c.id) < ($5, $6)\n )\n AND (\n cardinality($8::text[]) = 0\n OR EXISTS (\n SELECT 1 FROM entity_properties ep\n WHERE ep.entity_id = c.id\n AND ep.entity_type = 'CHAT'\n AND ep.values->'value' ?| $8::text[]\n )\n )\n ORDER BY c.\"updatedAt\" DESC, c.id DESC\n LIMIT $4\n ",+ "query": "\n SELECT\n c.id as entity_id,\n c.name,\n regexp_replace(\n c.name,\n $7,\n '<macro_em>\\1</macro_em>',\n 'gi'\n ) as name_highlighted,\n c.\"updatedAt\" as updated_at\n FROM \"Chat\" c\n WHERE (c.\"userId\" = $1 OR c.id = ANY($2))\n AND c.\"deletedAt\" IS NULL\n AND c.name ILIKE $3\n AND (\n $5::timestamptz IS NULL\n OR (c.\"updatedAt\", c.id) < ($5, $6)\n )\n AND (\n cardinality($8::text[]) = 0\n OR (\n NOT $9::bool\n AND EXISTS (\n SELECT 1 FROM entity_properties ep\n WHERE ep.entity_id = c.id\n AND ep.entity_type = 'CHAT'\n AND ep.values->'value' ?| $8::text[]\n )\n )\n OR (\n $9::bool\n AND NOT EXISTS (\n SELECT 1 FROM unnest($8::text[]) AS want(opt)\n WHERE NOT EXISTS (\n SELECT 1 FROM entity_properties ep\n WHERE ep.entity_id = c.id\n AND ep.entity_type = 'CHAT'\n AND ep.values->'value' ? want.opt\n )\n )\n )\n )\n ORDER BY c.\"updatedAt\" DESC, c.id DESC\n LIMIT $4\n ","describe": {"columns": [{@@ -33,7 +33,8 @@"Timestamptz","Text","Text",- "TextArray"+ "TextArray",+ "Bool"]},"nullable": [@@ -43,5 +44,5 @@false]},- "hash": "2d726fec1915305011d1b97e3baa8b2c37cdd9e6b2ac0fb412705c511021913c"+ "hash": "355db98772e35a57aab23852eb018148f74fe07ef799f5aa517613470fa5cb6c"}diff --git a/rust/cloud-storage/.sqlx/query-5e4006d396a3d256162ec62127d1eb492a3189665b15f395d86f2aafa6508fe9.json b/rust/cloud-storage/.sqlx/query-750cd6a910f8026f27c6a5124e31fd0f2440b9d943424e7fcd99b5a6dda11634.jsonsimilarity index 54%rename from rust/cloud-storage/.sqlx/query-5e4006d396a3d256162ec62127d1eb492a3189665b15f395d86f2aafa6508fe9.jsonrename to rust/cloud-storage/.sqlx/query-750cd6a910f8026f27c6a5124e31fd0f2440b9d943424e7fcd99b5a6dda11634.jsonindex 302b50c815..b3af460f03 100644--- a/rust/cloud-storage/.sqlx/query-5e4006d396a3d256162ec62127d1eb492a3189665b15f395d86f2aafa6508fe9.json+++ b/rust/cloud-storage/.sqlx/query-750cd6a910f8026f27c6a5124e31fd0f2440b9d943424e7fcd99b5a6dda11634.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT\n c.id as entity_id,\n c.name,\n regexp_replace(\n c.name,\n $6,\n '<macro_em>\\1</macro_em>',\n 'gi'\n ) as name_highlighted,\n c.\"updatedAt\" as updated_at\n FROM \"Chat\" c\n WHERE c.id = ANY($1)\n AND c.\"deletedAt\" IS NULL\n AND c.name ILIKE $2\n AND (\n $4::timestamptz IS NULL\n OR (c.\"updatedAt\", c.id) < ($4, $5)\n )\n AND (\n cardinality($7::text[]) = 0\n OR EXISTS (\n SELECT 1 FROM entity_properties ep\n WHERE ep.entity_id = c.id\n AND ep.entity_type = 'CHAT'\n AND ep.values->'value' ?| $7::text[]\n )\n )\n ORDER BY c.\"updatedAt\" DESC, c.id DESC\n LIMIT $3\n ",+ "query": "\n SELECT\n c.id as entity_id,\n c.name,\n regexp_replace(\n c.name,\n $6,\n '<macro_em>\\1</macro_em>',\n 'gi'\n ) as name_highlighted,\n c.\"updatedAt\" as updated_at\n FROM \"Chat\" c\n WHERE c.id = ANY($1)\n AND c.\"deletedAt\" IS NULL\n AND c.name ILIKE $2\n AND (\n $4::timestamptz IS NULL\n OR (c.\"updatedAt\", c.id) < ($4, $5)\n )\n AND (\n cardinality($7::text[]) = 0\n OR (\n NOT $8::bool\n AND EXISTS (\n SELECT 1 FROM entity_properties ep\n WHERE ep.entity_id = c.id\n AND ep.entity_type = 'CHAT'\n AND ep.values->'value' ?| $7::text[]\n )\n )\n OR (\n $8::bool\n AND NOT EXISTS (\n SELECT 1 FROM unnest($7::text[]) AS want(opt)\n WHERE NOT EXISTS (\n SELECT 1 FROM entity_properties ep\n WHERE ep.entity_id = c.id\n AND ep.entity_type = 'CHAT'\n AND ep.values->'value' ? want.opt\n )\n )\n )\n )\n ORDER BY c.\"updatedAt\" DESC, c.id DESC\n LIMIT $3\n ","describe": {"columns": [{@@ -32,7 +32,8 @@"Timestamptz","Text","Text",- "TextArray"+ "TextArray",+ "Bool"]},"nullable": [@@ -42,5 +43,5 @@false]},- "hash": "5e4006d396a3d256162ec62127d1eb492a3189665b15f395d86f2aafa6508fe9"+ "hash": "750cd6a910f8026f27c6a5124e31fd0f2440b9d943424e7fcd99b5a6dda11634"}diff --git a/rust/cloud-storage/item_filters/src/lib.rs b/rust/cloud-storage/item_filters/src/lib.rsindex cb6b8b5371..fa6ffeafd7 100644--- a/rust/cloud-storage/item_filters/src/lib.rs+++ b/rust/cloud-storage/item_filters/src/lib.rs@@ -609,6 +609,18 @@ impl IsEmpty for ProjectFilters {}}+/// How multiple `tag_option_ids` combine when filtering.+#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]+#[serde(rename_all = "snake_case")]+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema, schemars::JsonSchema))]+pub enum TagFilterMode {+ /// Match entities holding at least one of the selected tags (default).+ #[default]+ Any,+ /// Match entities holding every selected tag.+ All,+}+/// a bundle of all of the filters for each entity type#[derive(Debug, Clone, Default, Deserialize, Serialize)]#[cfg_attr(feature = "schema", derive(utoipa::ToSchema, schemars::JsonSchema))]@@ -648,6 +660,10 @@ pub struct EntityFilters {/// the owning definition id (personal and team tags combine into one OR).#[serde(default, skip_serializing_if = "Vec::is_empty")]pub tag_option_ids: Vec<String>,+ /// How the `tag_option_ids` combine: `any` (default) matches entities+ /// holding at least one selected tag, `all` requires every selected tag.+ #[serde(default)]+ pub tag_filter_mode: TagFilterMode,}impl IsEmpty for EntityFilters {@@ -664,6 +680,8 @@ impl IsEmpty for EntityFilters {foreign_entity_filters,property_filters,tag_option_ids,+ // Mode is a modifier on tag_option_ids, not a filter by itself.+ tag_filter_mode: _,} = self;project_filters.is_empty()&& document_filters.is_empty()diff --git a/rust/cloud-storage/name_search/fixtures/chat_tags.sql b/rust/cloud-storage/name_search/fixtures/chat_tags.sqlindex ed69e4f69f..7fd1189e73 100644--- a/rust/cloud-storage/name_search/fixtures/chat_tags.sql+++ b/rust/cloud-storage/name_search/fixtures/chat_tags.sql@@ -1,5 +1,6 @@--- Tag values on chats, keyed to the system Status definition seeded by--- migrations. Only the option ids inside `values` matter to the tag filter.+-- Tag values on chats, keyed to system definitions seeded by migrations.+-- Only the option ids inside `values` matter to the tag filter. The second+-- row lives on a different definition so match-all across rows is exercised.INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)VALUES(@@ -8,4 +9,11 @@ VALUES'CHAT','00000001-0000-0000-0000-000000000002','{"type": "SelectOption", "value": ["cccccccc-0000-0000-0000-000000000001", "cccccccc-0000-0000-0000-000000000002"]}'+ ),+ (+ 'eeeeeeee-0000-0000-0000-000000000002',+ '22222222-2222-2222-2222-222222222222',+ 'CHAT',+ '00000001-0000-0000-0000-000000000003',+ '{"type": "SelectOption", "value": ["cccccccc-0000-0000-0000-000000000003"]}');diff --git a/rust/cloud-storage/name_search/src/chat.rs b/rust/cloud-storage/name_search/src/chat.rsindex cd1f9916b9..f3d882f735 100644--- a/rust/cloud-storage/name_search/src/chat.rs+++ b/rust/cloud-storage/name_search/src/chat.rs@@ -21,6 +21,7 @@ async fn ids_search(search_pattern: String,highlight_pattern: String,tag_option_ids: &[String],+ match_all_tags: bool,limit: u32,cursor: Option<SearchMethodCursor>,) -> Result<PaginatedResult<NameSearchResult>, NameSearchError> {@@ -59,11 +60,26 @@ async fn ids_search()AND (cardinality($7::text[]) = 0- OR EXISTS (- SELECT 1 FROM entity_properties ep- WHERE ep.entity_id = c.id- AND ep.entity_type = 'CHAT'- AND ep.values->'value' ?| $7::text[]+ OR (+ NOT $8::bool+ AND EXISTS (+ SELECT 1 FROM entity_properties ep+ WHERE ep.entity_id = c.id+ AND ep.entity_type = 'CHAT'+ AND ep.values->'value' ?| $7::text[]+ )+ )+ OR (+ $8::bool+ AND NOT EXISTS (+ SELECT 1 FROM unnest($7::text[]) AS want(opt)+ WHERE NOT EXISTS (+ SELECT 1 FROM entity_properties ep+ WHERE ep.entity_id = c.id+ AND ep.entity_type = 'CHAT'+ AND ep.values->'value' ? want.opt+ )+ )))ORDER BY c."updatedAt" DESC, c.id DESC@@ -79,6 +95,7 @@ async fn ids_search(cursor_entity_id,highlight_pattern,tag_option_ids,+ match_all_tags,).fetch_all(db).await@@ -105,6 +122,7 @@ async fn owner_search<'a>(search_pattern: String,highlight_pattern: String,tag_option_ids: &[String],+ match_all_tags: bool,limit: u32,cursor: Option<SearchMethodCursor>,) -> Result<PaginatedResult<NameSearchResult>, NameSearchError> {@@ -139,11 +157,26 @@ async fn owner_search<'a>()AND (cardinality($8::text[]) = 0- OR EXISTS (- SELECT 1 FROM entity_properties ep- WHERE ep.entity_id = c.id- AND ep.entity_type = 'CHAT'- AND ep.values->'value' ?| $8::text[]+ OR (+ NOT $9::bool+ AND EXISTS (+ SELECT 1 FROM entity_properties ep+ WHERE ep.entity_id = c.id+ AND ep.entity_type = 'CHAT'+ AND ep.values->'value' ?| $8::text[]+ )+ )+ OR (+ $9::bool+ AND NOT EXISTS (+ SELECT 1 FROM unnest($8::text[]) AS want(opt)+ WHERE NOT EXISTS (+ SELECT 1 FROM entity_properties ep+ WHERE ep.entity_id = c.id+ AND ep.entity_type = 'CHAT'+ AND ep.values->'value' ? want.opt+ )+ )))ORDER BY c."updatedAt" DESC, c.id DESC@@ -160,6 +193,7 @@ async fn owner_search<'a>(cursor_entity_id,highlight_pattern,tag_option_ids,+ match_all_tags,).fetch_all(db).await@@ -179,7 +213,8 @@ async fn owner_search<'a>(}/// Searches over the user's chats by name. A non-empty `tag_option_ids`-/// restricts results to chats holding any of those property option ids.+/// restricts results to chats holding any of those property option ids, or+/// every one of them when `match_all_tags` is set.#[tracing::instrument(skip(db), err)]#[cfg_attr(not(test),@@ -187,7 +222,7 @@ async fn owner_search<'a>(time = 30,result = true,key = "String",- convert = r#"{ format!("{}-{:?}-{}-{}-{:?}-{}-{}", macro_user_id.as_ref(), chat_ids, term, ids_only, tag_option_ids, limit, cursor.as_ref().and_then(|c| c.as_updated_at()).map(|(id, ts)| format!("{}-{}", id, ts)).unwrap_or_default()) }"#+ convert = r#"{ format!("{}-{:?}-{}-{}-{:?}-{}-{}-{}", macro_user_id.as_ref(), chat_ids, term, ids_only, tag_option_ids, match_all_tags, limit, cursor.as_ref().and_then(|c| c.as_updated_at()).map(|(id, ts)| format!("{}-{}", id, ts)).unwrap_or_default()) }"#))]pub async fn search_chat_names<'a>(@@ -197,6 +232,7 @@ pub async fn search_chat_names<'a>(term: String,ids_only: bool,tag_option_ids: &[String],+ match_all_tags: bool,limit: u32,cursor: Option<SearchMethodCursor>,) -> Result<PaginatedResult<NameSearchResult>, NameSearchError> {@@ -217,6 +253,7 @@ pub async fn search_chat_names<'a>(search_pattern,highlight_pattern,tag_option_ids,+ match_all_tags,limit,cursor,)@@ -229,6 +266,7 @@ pub async fn search_chat_names<'a>(search_pattern,highlight_pattern,tag_option_ids,+ match_all_tags,limit,cursor,)diff --git a/rust/cloud-storage/name_search/src/chat/test.rs b/rust/cloud-storage/name_search/src/chat/test.rsindex 2b86581a65..c1741a8461 100644--- a/rust/cloud-storage/name_search/src/chat/test.rs+++ b/rust/cloud-storage/name_search/src/chat/test.rs@@ -16,8 +16,18 @@ async fn test_search_chat_names_empty_term(pool: Pool<Postgres>) -> anyhow::Resu.map(|l| l.lowercase()).unwrap();- let result =- search_chat_names(&pool, &user_id, &[], "".to_string(), false, &[], 10, None).await;+ let result = search_chat_names(+ &pool,+ &user_id,+ &[],+ "".to_string(),+ false,+ &[],+ false,+ 10,+ None,+ )+ .await;assert!(result.is_err());assert!(matches!(@@ -46,6 +56,7 @@ async fn test_search_chat_names_ids_only_with_empty_ids("project".to_string(),true,&[],+ false,10,None,)@@ -83,6 +94,7 @@ async fn test_search_chat_names_ids_only_mode(pool: Pool<Postgres>) -> anyhow::R"project".to_string(),true,&[],+ false,10,None,)@@ -133,6 +145,7 @@ async fn test_search_chat_names_normal_mode_owned_chats("project".to_string(),false,&[],+ false,10,None,)@@ -180,6 +193,7 @@ async fn test_search_chat_names_case_insensitive(pool: Pool<Postgres>) -> anyhow"PROJECT".to_string(),false,&[],+ false,10,None,)@@ -195,6 +209,7 @@ async fn test_search_chat_names_case_insensitive(pool: Pool<Postgres>) -> anyhow"project".to_string(),false,&[],+ false,10,None,)@@ -210,6 +225,7 @@ async fn test_search_chat_names_case_insensitive(pool: Pool<Postgres>) -> anyhow"PrOjEcT".to_string(),false,&[],+ false,10,None,)@@ -239,6 +255,7 @@ async fn test_search_chat_names_with_shared_chats(pool: Pool<Postgres>) -> anyho"project".to_string(),false,&[],+ false,10,None,)@@ -279,6 +296,7 @@ async fn test_search_chat_names_pagination_limit(pool: Pool<Postgres>) -> anyhow"project".to_string(),false,&[],+ false,2,None,)@@ -319,6 +337,7 @@ async fn test_search_chat_names_pagination_cursor(pool: Pool<Postgres>) -> anyho"project".to_string(),false,&[],+ false,2,None,)@@ -341,6 +360,7 @@ async fn test_search_chat_names_pagination_cursor(pool: Pool<Postgres>) -> anyho"project".to_string(),false,&[],+ false,2,cursor,)@@ -397,6 +417,7 @@ async fn test_search_chat_names_no_results(pool: Pool<Postgres>) -> anyhow::Resu"nonexistent".to_string(),false,&[],+ false,10,None,)@@ -425,6 +446,7 @@ async fn test_search_chat_names_partial_match(pool: Pool<Postgres>) -> anyhow::R"meet".to_string(),false,&[],+ false,10,None,)@@ -469,6 +491,7 @@ async fn test_search_chat_names_user_isolation(pool: Pool<Postgres>) -> anyhow::"User2".to_string(),false,&[],+ false,10,None,)@@ -497,6 +520,7 @@ async fn test_search_chat_names_excludes_soft_deleted(pool: Pool<Postgres>) -> a"project".to_string(),false,&[],+ false,10,None,)@@ -540,6 +564,7 @@ async fn test_search_chat_names_rejects_thread_cursor(pool: Pool<Postgres>) -> a"test".to_string(),false,&[],+ false,10,Some(cursor),)@@ -571,6 +596,7 @@ async fn test_search_chat_names_tag_filter(pool: Pool<Postgres>) -> anyhow::Resu"project".to_string(),false,&tagged,+ false,10,None,)@@ -591,6 +617,7 @@ async fn test_search_chat_names_tag_filter(pool: Pool<Postgres>) -> anyhow::Resu"project".to_string(),false,&unheld,+ false,10,None,)@@ -627,6 +654,7 @@ async fn test_search_chat_names_tag_filter_ids_only(pool: Pool<Postgres>) -> any"project".to_string(),true,&tagged,+ false,10,None,)@@ -640,3 +668,61 @@ async fn test_search_chat_names_tag_filter_ids_only(pool: Pool<Postgres>) -> anyOk(())}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../fixtures", scripts("chat", "chat_tags"))+)]+async fn test_search_chat_names_tag_filter_match_all(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let user_id = MacroUserId::parse_from_str("macro|[email protected]")+ .map(|l| l.lowercase())+ .unwrap();++ // Every option id must be held. The two ids live on different property+ // rows (definitions), so the check must span rows.+ let across_rows = vec![+ "cccccccc-0000-0000-0000-000000000001".to_string(),+ "cccccccc-0000-0000-0000-000000000003".to_string(),+ ];+ let response = search_chat_names(+ &pool,+ &user_id,+ &[],+ "project".to_string(),+ false,+ &across_rows,+ true,+ 10,+ None,+ )+ .await?;++ assert_eq!(response.items.len(), 1);+ assert_eq!(+ response.items[0].entity_id.to_string(),+ "22222222-2222-2222-2222-222222222222"+ );++ // One held plus one unheld id matches nothing under match-all, while the+ // same pair would match under any.+ let with_unheld = vec![+ "cccccccc-0000-0000-0000-000000000001".to_string(),+ "cccccccc-0000-0000-0000-000000000099".to_string(),+ ];+ let response = search_chat_names(+ &pool,+ &user_id,+ &[],+ "project".to_string(),+ false,+ &with_unheld,+ true,+ 10,+ None,+ )+ .await?;++ assert!(response.items.is_empty());++ Ok(())+}diff --git a/rust/cloud-storage/opensearch_client/src/search/chats.rs b/rust/cloud-storage/opensearch_client/src/search/chats.rsindex 9db01afef5..887368c7cd 100644--- a/rust/cloud-storage/opensearch_client/src/search/chats.rs+++ b/rust/cloud-storage/opensearch_client/src/search/chats.rs@@ -57,6 +57,7 @@ pub(crate) struct ChatQueryBuilder {/// The role of the chat messagerole: Vec<String>,tag_option_ids: Vec<String>,+ match_all_tags: bool,}impl ChatQueryBuilder {@@ -65,6 +66,7 @@ impl ChatQueryBuilder {inner: SearchQueryBuilder::new(terms),role: Vec::new(),tag_option_ids: Vec::new(),+ match_all_tags: false,}}@@ -89,6 +91,11 @@ impl ChatQueryBuilder {self}+ pub fn match_all_tags(mut self, match_all_tags: bool) -> Self {+ self.match_all_tags = match_all_tags;+ self+ }+/// Parent/child join query: one `has_child` clause per term, ANDed/// inside `bool.must` so each term must match some message in the/// same chat. Parent metadata filters (user, ids) sit on@@ -118,9 +125,9 @@ impl ChatQueryBuilder {// Access control on parent fields (user_id and/or entity_id).bool_query.filter(self.build_parent_filter()?);- // Tag filter: a single nested clause matching any of the option ids in- // the parent's `properties.values`, with no definition_id constraint.- if let Some(nested) = build_tag_filter(&self.tag_option_ids) {+ // Tag filter: nested clause(s) matching the parent's+ // `properties.values`, with no definition_id constraint.+ if let Some(nested) = build_tag_filter(&self.tag_option_ids, self.match_all_tags) {bool_query.filter(nested);}@@ -241,6 +248,7 @@ pub struct ChatSearchArgs {pub collapse: bool,pub ids_only: bool,pub tag_option_ids: Vec<String>,+ pub match_all_tags: bool,}impl From<ChatSearchArgs> for ChatQueryBuilder {@@ -255,6 +263,7 @@ impl From<ChatSearchArgs> for ChatQueryBuilder {.collapse(args.collapse).ids_only(args.ids_only).tag_option_ids(args.tag_option_ids)+ .match_all_tags(args.match_all_tags)}}diff --git a/rust/cloud-storage/opensearch_client/src/search/documents.rs b/rust/cloud-storage/opensearch_client/src/search/documents.rsindex 104fcc8462..194682e4a7 100644--- a/rust/cloud-storage/opensearch_client/src/search/documents.rs+++ b/rust/cloud-storage/opensearch_client/src/search/documents.rs@@ -83,6 +83,7 @@ pub(crate) struct DocumentQueryBuilder {mode: DocumentSearchMode,property_filters: Vec<PropertyFilterArg>,tag_option_ids: Vec<String>,+ match_all_tags: bool,}impl DocumentQueryBuilder {@@ -93,6 +94,7 @@ impl DocumentQueryBuilder {mode: DocumentSearchMode::default(),property_filters: Vec::new(),tag_option_ids: Vec::new(),+ match_all_tags: false,}}@@ -127,6 +129,11 @@ impl DocumentQueryBuilder {self}+ pub fn match_all_tags(mut self, match_all_tags: bool) -> Self {+ self.match_all_tags = match_all_tags;+ self+ }+/// Parent/child join query: one `has_child` clause per term, ANDed/// inside `bool.must`. Parent metadata filters (owner, ids,/// sub_type) sit on `bool.filter` directly because they live on@@ -171,10 +178,10 @@ impl DocumentQueryBuilder {}}- // Tag filter: a single nested clause matching any of the option ids in- // `properties.values`, with no definition_id constraint. Option ids are- // globally unique, so this ORs tags across definitions in one clause.- if let Some(nested) = build_tag_filter(&self.tag_option_ids) {+ // Tag filter: nested clause(s) matching `properties.values` with no+ // definition_id constraint. Option ids are globally unique, so this+ // ORs (or ANDs, for match-all) tags across definitions.+ if let Some(nested) = build_tag_filter(&self.tag_option_ids, self.match_all_tags) {bool_query.filter(nested);}@@ -376,6 +383,7 @@ pub struct DocumentSearchArgs {pub mode: DocumentSearchMode,pub property_filters: Vec<PropertyFilterArg>,pub tag_option_ids: Vec<String>,+ pub match_all_tags: bool,}impl From<DocumentSearchArgs> for DocumentQueryBuilder {@@ -392,6 +400,7 @@ impl From<DocumentSearchArgs> for DocumentQueryBuilder {.mode(args.mode).property_filters(args.property_filters).tag_option_ids(args.tag_option_ids)+ .match_all_tags(args.match_all_tags)}}diff --git a/rust/cloud-storage/opensearch_client/src/search/documents/test.rs b/rust/cloud-storage/opensearch_client/src/search/documents/test.rsindex 7f8d5a3e32..367c324cb3 100644--- a/rust/cloud-storage/opensearch_client/src/search/documents/test.rs+++ b/rust/cloud-storage/opensearch_client/src/search/documents/test.rs@@ -328,6 +328,46 @@ fn test_build_bool_query_tag_filter_emits_flat_terms() -> anyhow::Result<()> {Ok(())}+#[test]+fn test_build_bool_query_tag_filter_match_all_emits_per_option_musts() -> anyhow::Result<()> {+ let builder = DocumentQueryBuilder::new(vec!["foo".to_string()])+ .match_type("partial")+ .user_id("alice")+ .tag_option_ids(vec![+ "00000001-0000-0000-0003-000000000001".to_string(),+ "00000001-0000-0000-0003-000000000002".to_string(),+ ])+ .match_all_tags(true);++ let json = builder.build_bool_query()?.build().to_json();+ let filter = json["bool"]["filter"].as_array().expect("filter array");++ // Match-all wraps one nested clause per option id inside a bool.must, so+ // an entity must hold every id (possibly across nested property entries).+ let tag_bool: Vec<&serde_json::Value> = filter+ .iter()+ .filter(|f| f["bool"]["must"].is_array())+ .collect();+ assert_eq!(tag_bool.len(), 1, "one bool wrapper for tags: {filter:?}");++ let musts = tag_bool[0]["bool"]["must"].as_array().expect("must array");+ assert_eq!(musts.len(), 2, "one nested clause per option id");+ for (must, option_id) in musts.iter().zip([+ "00000001-0000-0000-0003-000000000001",+ "00000001-0000-0000-0003-000000000002",+ ]) {+ let nested = &must["nested"];+ assert_eq!(nested["path"], "properties");+ assert_eq!(nested["ignore_unmapped"], true);+ assert_eq!(+ nested["query"],+ serde_json::json!({"terms": {"properties.values": [option_id]}})+ );+ }++ Ok(())+}+#[test]fn test_build_bool_query_tag_filter_empty_skipped() -> anyhow::Result<()> {let builder = DocumentQueryBuilder::new(vec!["foo".to_string()])diff --git a/rust/cloud-storage/opensearch_client/src/search/emails.rs b/rust/cloud-storage/opensearch_client/src/search/emails.rsindex d630b397ff..0ab3a1de10 100644--- a/rust/cloud-storage/opensearch_client/src/search/emails.rs+++ b/rust/cloud-storage/opensearch_client/src/search/emails.rs@@ -116,8 +116,10 @@ pub(crate) struct EmailQueryBuilder {importance: Option<bool>,/// When true, only search the subject field (for name-only search mode)subject_only: bool,- /// Tag option ids the thread must carry (OR'd, definition-agnostic)+ /// Tag option ids the thread must carry (definition-agnostic)tag_option_ids: Vec<String>,+ /// Whether the thread must carry every tag option id instead of any+ match_all_tags: bool,}impl EmailQueryBuilder {@@ -134,6 +136,7 @@ impl EmailQueryBuilder {importance: None,subject_only: false,tag_option_ids: Vec::new(),+ match_all_tags: false,}}@@ -199,6 +202,11 @@ impl EmailQueryBuilder {self}+ pub fn match_all_tags(mut self, match_all_tags: bool) -> Self {+ self.match_all_tags = match_all_tags;+ self+ }+pub fn build_bool_query<'a>(&'a self) -> Result<BoolQueryBuilder<'a>> {let mut content_bool_query = self.inner.build_content_bool_query()?;@@ -263,10 +271,10 @@ impl EmailQueryBuilder {content_bool_query.filter(QueryType::terms("labels", self.include_labels.clone()));}- // Tag filter: a single nested clause matching any of the option ids in+ // Tag filter: nested clause(s) matching the option ids in// `properties.values`. Thread-level properties are denormalized onto// every message doc, so filtering per doc filters the thread.- if let Some(nested) = build_tag_filter(&self.tag_option_ids) {+ if let Some(nested) = build_tag_filter(&self.tag_option_ids, self.match_all_tags) {content_bool_query.filter(nested);}@@ -383,6 +391,7 @@ pub struct EmailSearchArgs {pub ids_only: bool,pub subject_only: bool,pub tag_option_ids: Vec<String>,+ pub match_all_tags: bool,}impl From<EmailSearchArgs> for EmailQueryBuilder {@@ -406,6 +415,7 @@ impl From<EmailSearchArgs> for EmailQueryBuilder {.ids_only(args.ids_only).subject_only(args.subject_only).tag_option_ids(args.tag_option_ids)+ .match_all_tags(args.match_all_tags)}}diff --git a/rust/cloud-storage/opensearch_client/src/search/emails/test.rs b/rust/cloud-storage/opensearch_client/src/search/emails/test.rsindex 150200866e..8b7ac3a274 100644--- a/rust/cloud-storage/opensearch_client/src/search/emails/test.rs+++ b/rust/cloud-storage/opensearch_client/src/search/emails/test.rs@@ -120,6 +120,7 @@ fn test_email_search_args_quoted_phrase_uses_phrase_query_in_sqs() -> anyhow::Reids_only: false,subject_only: false,tag_option_ids: vec![],+ match_all_tags: false,}.into();@@ -158,6 +159,7 @@ fn test_email_search_args_build_injects_simple_query_string() -> anyhow::Result<ids_only: false,subject_only: false,tag_option_ids: vec![],+ match_all_tags: false,}.into();diff --git a/rust/cloud-storage/opensearch_client/src/search/projects.rs b/rust/cloud-storage/opensearch_client/src/search/projects.rsindex fb5af112be..f49c8a326d 100644--- a/rust/cloud-storage/opensearch_client/src/search/projects.rs+++ b/rust/cloud-storage/opensearch_client/src/search/projects.rs@@ -25,6 +25,7 @@ impl SearchQueryConfig for ProjectSearchConfig {pub(crate) struct ProjectQueryBuilder {inner: SearchQueryBuilder<ProjectSearchConfig>,tag_option_ids: Vec<String>,+ match_all_tags: bool,}impl ProjectQueryBuilder {@@ -32,6 +33,7 @@ impl ProjectQueryBuilder {Self {inner: SearchQueryBuilder::new(terms),tag_option_ids: Vec::new(),+ match_all_tags: false,}}@@ -40,6 +42,11 @@ impl ProjectQueryBuilder {self}+ pub fn match_all_tags(mut self, match_all_tags: bool) -> Self {+ self.match_all_tags = match_all_tags;+ self+ }+// Copy function signature from SearchQueryBuilderdelegate_methods! {fn match_type(match_type: &str) -> Self;@@ -66,9 +73,9 @@ impl ProjectQueryBuilder {.build_filter_query(ProjectSearchConfig::USER_ID_KEY)?,);- // Tag filter: a single nested clause matching any of the option ids in+ // Tag filter: nested clause(s) matching the option ids in// `properties.values`, with no definition_id constraint.- if let Some(nested) = build_tag_filter(&self.tag_option_ids) {+ if let Some(nested) = build_tag_filter(&self.tag_option_ids, self.match_all_tags) {bool_query.filter(nested);}@@ -101,6 +108,7 @@ pub struct ProjectSearchArgs {pub collapse: bool,pub ids_only: bool,pub tag_option_ids: Vec<String>,+ pub match_all_tags: bool,}impl From<ProjectSearchArgs> for ProjectQueryBuilder {@@ -114,5 +122,6 @@ impl From<ProjectSearchArgs> for ProjectQueryBuilder {.collapse(args.collapse).ids_only(args.ids_only).tag_option_ids(args.tag_option_ids)+ .match_all_tags(args.match_all_tags)}}diff --git a/rust/cloud-storage/opensearch_client/src/search/properties.rs b/rust/cloud-storage/opensearch_client/src/search/properties.rsindex 58ae87fe20..5098ada316 100644--- a/rust/cloud-storage/opensearch_client/src/search/properties.rs+++ b/rust/cloud-storage/opensearch_client/src/search/properties.rs@@ -1,23 +1,38 @@-use opensearch_query_builder::{NestedQuery, QueryType};+use opensearch_query_builder::{BoolQueryBuilder, NestedQuery, QueryType};/// Nested path holding denormalized entity properties on a search doc.pub(crate) const PROPERTIES_PATH: &str = "properties";-/// Build a `nested` query over `properties` matching docs that have any-/// nested entry whose `values` contains one of `option_ids`. There is no-/// `definition_id` constraint: tag option ids are globally unique, so this-/// matches a tag regardless of which definition owns it. Returns `None` when-/// there are no option ids.-pub(crate) fn build_tag_filter<'a>(option_ids: &[String]) -> Option<QueryType<'a>> {+fn nested_values_query<'a>(option_ids: Vec<String>) -> QueryType<'a> {+ NestedQuery::new(+ PROPERTIES_PATH,+ QueryType::terms(format!("{PROPERTIES_PATH}.values"), option_ids),+ )+ .ignore_unmapped(true)+ .into()+}++/// Build a `nested` query over `properties` matching docs against `option_ids`.+/// There is no `definition_id` constraint: tag option ids are globally unique,+/// so this matches a tag regardless of which definition owns it. With+/// `match_all` false a doc matches when any nested entry's `values` contains+/// one of the ids; with `match_all` true every id must be present, expressed+/// as one nested clause per id ANDed inside `bool.must` (the ids may live on+/// different nested entries, e.g. a personal and a team tag definition).+/// Returns `None` when there are no option ids.+pub(crate) fn build_tag_filter<'a>(+ option_ids: &[String],+ match_all: bool,+) -> Option<QueryType<'a>> {if option_ids.is_empty() {return None;}- Some(- NestedQuery::new(- PROPERTIES_PATH,- QueryType::terms(format!("{PROPERTIES_PATH}.values"), option_ids.to_vec()),- )- .ignore_unmapped(true)- .into(),- )+ if !match_all {+ return Some(nested_values_query(option_ids.to_vec()));+ }+ let mut all = BoolQueryBuilder::new();+ for option_id in option_ids {+ all.must(nested_values_query(vec![option_id.clone()]));+ }+ Some(all.build().into())}diff --git a/rust/cloud-storage/opensearch_client/src/search/unified.rs b/rust/cloud-storage/opensearch_client/src/search/unified.rsindex 74f9b5fc35..78952e744d 100644--- a/rust/cloud-storage/opensearch_client/src/search/unified.rs+++ b/rust/cloud-storage/opensearch_client/src/search/unified.rs@@ -84,6 +84,7 @@ impl From<UnifiedSearchArgs> for DocumentSearchArgs {mode: args.document_search_args.mode,property_filters: args.document_search_args.property_filters,tag_option_ids: args.document_search_args.tag_option_ids,+ match_all_tags: args.document_search_args.match_all_tags,}}}@@ -110,6 +111,7 @@ impl From<UnifiedSearchArgs> for EmailSearchArgs {importance: args.email_search_args.importance,subject_only: args.email_search_args.subject_only,tag_option_ids: args.email_search_args.tag_option_ids,+ match_all_tags: args.email_search_args.match_all_tags,}}}@@ -145,6 +147,7 @@ impl From<UnifiedSearchArgs> for ChatSearchArgs {chat_ids: args.chat_search_args.chat_ids,role: args.chat_search_args.role,tag_option_ids: args.chat_search_args.tag_option_ids,+ match_all_tags: args.chat_search_args.match_all_tags,}}}@@ -178,6 +181,7 @@ impl From<UnifiedSearchArgs> for ProjectSearchArgs {ids_only: args.project_search_args.ids_only,project_ids: args.project_search_args.project_ids,tag_option_ids: args.project_search_args.tag_option_ids,+ match_all_tags: args.project_search_args.match_all_tags,}}}@@ -189,6 +193,7 @@ pub struct UnifiedChatSearchArgs {pub role: Vec<String>,pub ids_only: bool,pub tag_option_ids: Vec<String>,+ pub match_all_tags: bool,}#[derive(Debug, Default, Clone)]@@ -200,6 +205,7 @@ pub struct UnifiedDocumentSearchArgs {pub mode: DocumentSearchMode,pub property_filters: Vec<PropertyFilterArg>,pub tag_option_ids: Vec<String>,+ pub match_all_tags: bool,}#[derive(Debug, Default, Clone)]@@ -217,6 +223,7 @@ pub struct UnifiedEmailSearchArgs {pub importance: Option<bool>,pub subject_only: bool,pub tag_option_ids: Vec<String>,+ pub match_all_tags: bool,}#[derive(Debug, Default, Clone)]@@ -243,6 +250,7 @@ pub struct UnifiedProjectSearchArgs {pub project_ids: Vec<String>,pub ids_only: bool,pub tag_option_ids: Vec<String>,+ pub match_all_tags: bool,}/// Possible search result indices for unified searchdiff --git a/rust/cloud-storage/search_service/src/api/search/simple/simple_chat.rs b/rust/cloud-storage/search_service/src/api/search/simple/simple_chat.rsindex 0998ef02ac..9b99aef34c 100644--- a/rust/cloud-storage/search_service/src/api/search/simple/simple_chat.rs+++ b/rust/cloud-storage/search_service/src/api/search/simple/simple_chat.rs@@ -99,6 +99,7 @@ pub(in crate::api::search) async fn filter_chats(}/// Performs the name search over chat names+#[allow(clippy::too_many_arguments)]#[tracing::instrument(skip(db), err)]pub(in crate::api::search::simple) async fn search_names<'a>(db: &Pool<Postgres>,@@ -106,6 +107,7 @@ pub(in crate::api::search::simple) async fn search_names<'a>(filter_chat_response: &FilterChatResponse,term: String,tag_option_ids: &[String],+ match_all_tags: bool,limit: u32,cursor: models_search_cursor::SearchCursorOption,) -> Result<(Vec<SearchHit>, models_search_cursor::SearchCursorOption), SearchError> {@@ -130,6 +132,7 @@ pub(in crate::api::search::simple) async fn search_names<'a>(term,filter_chat_response.ids_only,tag_option_ids,+ match_all_tags,limit,inner_cursor,)diff --git a/rust/cloud-storage/search_service/src/api/search/simple/simple_unified.rs b/rust/cloud-storage/search_service/src/api/search/simple/simple_unified.rsindex d20af8b918..6ebc97d801 100644--- a/rust/cloud-storage/search_service/src/api/search/simple/simple_unified.rs+++ b/rust/cloud-storage/search_service/src/api/search/simple/simple_unified.rs@@ -237,6 +237,7 @@ pub(in crate::api::search) async fn perform_unified_search(// the parent chat doc).let property_filter_args = to_property_filter_args(&req.filters.property_filters);let tag_option_ids = req.filters.tag_option_ids.clone();+ let match_all_tags = req.filters.tag_filter_mode == item_filters::TagFilterMode::All;// Tags are only indexed for the documents, emails, and chats indexes.// With a tag filter active every other source is dropped, so response// pages contain only rows the filter was actually applied to.@@ -326,14 +327,18 @@ pub(in crate::api::search) async fn perform_unified_search(filter_document_response.terms = search_terms.clone();filter_document_response.property_filters = property_filter_args;filter_document_response.tag_option_ids = tag_option_ids.clone();+ filter_document_response.match_all_tags = match_all_tags;filter_channel_response.terms = search_terms.clone();filter_chat_response.terms = search_terms.clone();filter_chat_response.tag_option_ids = tag_option_ids.clone();+ filter_chat_response.match_all_tags = match_all_tags;filter_email_response.terms = email_terms.clone();filter_email_response.tag_option_ids = tag_option_ids.clone();+ filter_email_response.match_all_tags = match_all_tags;filter_call_record_response.terms = search_terms.clone();filter_project_response.terms = search_terms.clone();filter_project_response.tag_option_ids = tag_option_ids;+ filter_project_response.match_all_tags = match_all_tags;// Widen the email access filter to every inbox the caller can reach (their// own plus delegated). Connected secondary inboxes share the owner's@@ -437,6 +442,7 @@ pub(in crate::api::search) async fn perform_unified_search(},name_search_term.clone(),&filter_chat_response.tag_option_ids,+ filter_chat_response.match_all_tags,page_size,chat_cursor_for_search,)