Skip to content

Commit edea991

Browse files
committed
refactor: improve query structure for merge suggestions
1 parent 7f54c5c commit edea991

3 files changed

Lines changed: 37 additions & 46 deletions

File tree

services/apps/merge_suggestions_worker/src/activities/memberMergeSuggestions.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -202,36 +202,34 @@ export async function getMemberMergeSuggestions(
202202
// Query 4: Verified -> Verified fuzzy matches
203203
matches: uniqBy(verifiedFuzzyMatches, 'value'),
204204
builder: ({ value }) => ({
205-
bool: {
206-
should: [
207-
{
208-
match: {
209-
[`nested_identities.string_value`]: {
210-
query: value,
211-
prefix_length: 1,
212-
fuzziness: 'auto',
213-
},
214-
},
215-
},
216-
],
217-
minimum_should_match: 1,
218-
filter: [{ term: { [`nested_identities.bool_verified`]: true } }],
205+
match: {
206+
[`nested_identities.string_value`]: {
207+
query: value,
208+
prefix_length: 1,
209+
fuzziness: 'auto',
210+
},
219211
},
220212
}),
213+
filter: [{ term: { [`nested_identities.bool_verified`]: true } }],
221214
},
222215
]
223216

224-
for (const { matches, builder } of clauseBuilders) {
217+
for (const clauseBuilder of clauseBuilders) {
218+
const { matches, builder, filter } = clauseBuilder
225219
if (matches.length > 0) {
226220
const chunks = chunkArray(matches, CHUNK_SIZE)
227221
for (const chunk of chunks) {
228222
const shouldClauses = chunk.map(builder)
229-
identitiesShould.push({
223+
const chunkQuery: any = {
230224
bool: {
231225
should: shouldClauses,
232226
minimum_should_match: 1,
233227
},
234-
})
228+
}
229+
if (filter) {
230+
chunkQuery.bool.filter = filter
231+
}
232+
identitiesShould.push(chunkQuery)
235233
}
236234
}
237235
}

services/apps/merge_suggestions_worker/src/activities/organizationMergeSuggestions.ts

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
import uniqBy from 'lodash.uniqby'
23

34
import { OrganizationField, findOrgById, queryOrgs } from '@crowd/data-access-layer'
@@ -205,55 +206,46 @@ export async function getOrganizationMergeSuggestions(
205206
// Query 2: Fuzzy matching - find similar values with typos/variations (verified only)
206207
matches: uniqBy(fuzzyIdentities, 'value'),
207208
builder: ({ value }) => ({
208-
bool: {
209-
should: [
210-
{
211-
match: {
212-
[`nested_identities.string_value`]: {
213-
query: value,
214-
prefix_length: 1,
215-
fuzziness: 'auto',
216-
},
217-
},
218-
},
219-
],
220-
minimum_should_match: 1,
221-
filter: [{ term: { [`nested_identities.bool_verified`]: true } }],
209+
match: {
210+
[`nested_identities.string_value`]: {
211+
query: value,
212+
prefix_length: 1,
213+
fuzziness: 'auto',
214+
},
222215
},
223216
}),
217+
filter: [{ term: { [`nested_identities.bool_verified`]: true } }],
224218
},
225219
{
226220
// Query 3: Prefix matching - find values that start with our prefix (verified only)
227221
matches: uniqBy(prefixIdentities, 'value'),
228222
builder: ({ value }) => ({
229-
bool: {
230-
should: [
231-
{
232-
prefix: {
233-
[`nested_identities.string_value`]: {
234-
value,
235-
},
236-
},
237-
},
238-
],
239-
minimum_should_match: 1,
240-
filter: [{ term: { [`nested_identities.bool_verified`]: true } }],
223+
prefix: {
224+
[`nested_identities.string_value`]: {
225+
value,
226+
},
241227
},
242228
}),
229+
filter: [{ term: { [`nested_identities.bool_verified`]: true } }],
243230
},
244231
]
245232

246-
for (const { matches, builder } of clauseBuilders) {
233+
for (const clauseBuilder of clauseBuilders) {
234+
const { matches, builder, filter } = clauseBuilder
247235
if (matches.length > 0) {
248236
const chunks = chunkArray(matches, CHUNK_SIZE)
249237
for (const chunk of chunks) {
250238
const shouldClauses = chunk.map(builder)
251-
identitiesShould.push({
239+
const chunkQuery: any = {
252240
bool: {
253241
should: shouldClauses,
254242
minimum_should_match: 1,
255243
},
256-
})
244+
}
245+
if (filter) {
246+
chunkQuery.bool.filter = filter
247+
}
248+
identitiesShould.push(chunkQuery)
257249
}
258250
}
259251
}

services/apps/merge_suggestions_worker/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,5 @@ export interface ISimilarOrganizationOpensearchResult {
117117
export interface OpenSearchQueryClauseBuilder<T> {
118118
matches: T[]
119119
builder: (match: T) => Record<string, unknown>
120+
filter?: Record<string, unknown>[]
120121
}

0 commit comments

Comments
 (0)