Skip to content

Commit

Permalink
feat: or-1972 extract duplicate query into methods
Browse files Browse the repository at this point in the history
  • Loading branch information
QuintenGreenstack committed Dec 5, 2023
1 parent b28e033 commit 016f042
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Vereniging;

Expand Down Expand Up @@ -35,151 +34,98 @@ await _client
s => s
.Query(
q => q.Bool(
b => b.Must(must => must
.Match(m => m
.Field(f => f.Naam)
.Query(naam)
.Analyzer(DuplicateDetectionDocumentMapping
.DuplicateAnalyzer)
.Fuzziness(
Fuzziness
.Auto) // Assumes this analyzer applies lowercase and asciifolding
.MinimumShouldMatch("90%") // You can adjust this percentage as needed
)).Filter(f => f
.Bool(fb => fb
.Should( // Use should within a filter context for municipalities
gemeentes.Select(gemeente =>
new Func<QueryContainerDescriptor<
DuplicateDetectionDocument>, QueryContainer>(
qc => qc
.Nested(n => n
.Path(p => p.Locaties)
.Query(nq => nq
.Match(m => m
.Field(
f => f
.Locaties
.First()
.Gemeente)
.Query(
gemeente)
)
)
)
)
).ToArray().Append(
postalCodesQuery => postalCodesQuery
.Nested(n => n
.Path(p => p.Locaties)
.Query(nq => nq
.Terms(t => t
.Field(
f => f.Locaties
.First()
.Postcode)
.Terms(postcodes)
)
)
))
)
.MinimumShouldMatch(
1) // At least one of the location conditions must match
),
descriptor => descriptor.Term(queryDescriptor => queryDescriptor.Field(document => document.IsVerwijderd)
.Value(false))
)
b => b.Must(
MatchOpNaam(naam),
IsNietGestopt
)
.Filter(MatchOpPostcodeOfGemeente(gemeentes, postcodes)
)
)
));

return searchResponse.Documents.Select(ToDuplicateVereniging)
.ToArray();
}

private static QueryContainer MatchNaam(QueryContainerDescriptor<DuplicateDetectionDocument> mu, VerenigingsNaam naam)
private static Func<QueryContainerDescriptor<DuplicateDetectionDocument>, QueryContainer> MatchOpPostcodeOfGemeente(
string[] gemeentes,
string[] postcodes)
{
return mu
.Match(
m => m
.Field(
f => f
.Naam)
.Query(
naam)
.Analyzer(
DuplicateDetectionDocumentMapping
.DuplicateAnalyzer)
.Fuzziness(
Fuzziness
.Auto));
return f =>
f.Bool(fb =>
fb
.Should( // Use should within a filter context for municipalities
MatchOpGemeente(gemeentes)
.Append(
MatchOpPostcode(postcodes))
)
.MinimumShouldMatch(
1) // At least one of the location conditions must match
);
}

private static QueryContainer MatchGemeente(QueryContainerDescriptor<DuplicateDetectionDocument> mu, string[] gemeentes)
private static QueryContainer IsNietGestopt(QueryContainerDescriptor<DuplicateDetectionDocument> descriptor)
{
return mu
.Nested(
n => n
.Path(
p => p
.Locaties)
.Query(
nq
=> nq
.Match(
m =>
FuzzyMatchOpNaam(
m,
path
: f
=> f
.Locaties
.First()
.Gemeente,
string
.Join(
separator
: " ",
gemeentes))
)
)
);
return descriptor.Term(queryDescriptor => queryDescriptor.Field(document => document.IsGestopt)
.Value(false));
}

private static QueryContainer MatchPostcode(QueryContainerDescriptor<DuplicateDetectionDocument> mu, string[] postcodes)
private static Func<QueryContainerDescriptor<DuplicateDetectionDocument>, QueryContainer> MatchOpPostcode(string[] postcodes)
{
return mu
.Nested(
n => n
.Path(
p => p
.Locaties)
.Query(
nq
=> nq
.Terms(
t => t
.Field(
f => f
.Locaties
.First()
.Postcode)
.Terms(
postcodes)
)
)
return postalCodesQuery => postalCodesQuery
.Nested(n => n
.Path(p => p.Locaties)
.Query(nq => nq
.Terms(t => t
.Field(
f => f.Locaties
.First()
.Postcode)
.Terms(postcodes)
)
)
);
}

private static MatchQueryDescriptor<DuplicateDetectionDocument> FuzzyMatchOpNaam(
MatchQueryDescriptor<DuplicateDetectionDocument> m,
Expression<Func<DuplicateDetectionDocument, string>> path,
string query)
=> m
.Field(path)
.Query(query)
.Analyzer(DuplicateDetectionDocumentMapping
.DuplicateAnalyzer)
.Fuzziness(Fuzziness.Auto) // Assumes this analyzer applies lowercase and asciifolding
.MinimumShouldMatch("90%");
private static IEnumerable<Func<QueryContainerDescriptor<DuplicateDetectionDocument>, QueryContainer>> MatchOpGemeente(
string[] gemeentes)
{
return gemeentes.Select(gemeente =>
new Func<QueryContainerDescriptor<
DuplicateDetectionDocument>, QueryContainer>(
qc => qc
.Nested(n => n
.Path(p => p.Locaties)
.Query(nq => nq
.Match(m => m
.Field(
f => f
.Locaties
.First()
.Gemeente)
.Query(
gemeente)
)
)
)
)
);
}

private static Func<QueryContainerDescriptor<DuplicateDetectionDocument>, QueryContainer> MatchOpNaam(VerenigingsNaam naam)
{
return must => must
.Match(m => m
.Field(f => f.Naam)
.Query(naam)
.Analyzer(DuplicateDetectionDocumentMapping
.DuplicateAnalyzer)
.Fuzziness(
Fuzziness
.Auto) // Assumes this analyzer applies lowercase and asciifolding
.MinimumShouldMatch("90%") // You can adjust this percentage as needed
);
}

private static DuplicaatVereniging ToDuplicateVereniging(DuplicateDetectionDocument document)
=> new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task Handle(EventEnvelope<FeitelijkeVerenigingWerdGeregistreerd> me
KorteNaam = message.Data.KorteNaam,
Locaties = message.Data.Locaties.Select(Map).ToArray(),
HoofdactiviteitVerenigingsloket = MapHoofdactiviteitVerenigingsloket(message.Data.HoofdactiviteitenVerenigingsloket),
IsVerwijderd = false,
IsGestopt = false,
}
);

Expand All @@ -38,7 +38,7 @@ public async Task Handle(EventEnvelope<AfdelingWerdGeregistreerd> message)
KorteNaam = message.Data.KorteNaam,
Locaties = message.Data.Locaties.Select(Map).ToArray(),
HoofdactiviteitVerenigingsloket = MapHoofdactiviteitVerenigingsloket(message.Data.HoofdactiviteitenVerenigingsloket),
IsVerwijderd = false,
IsGestopt = false,
}
);

Expand All @@ -52,7 +52,7 @@ public async Task Handle(EventEnvelope<VerenigingMetRechtspersoonlijkheidWerdGer
KorteNaam = message.Data.KorteNaam,
Locaties = Array.Empty<DuplicateDetectionDocument.Locatie>(),
HoofdactiviteitVerenigingsloket = Array.Empty<string>(),
IsVerwijderd = false,
IsGestopt = false,
}
);

Expand Down Expand Up @@ -97,7 +97,7 @@ public async Task Handle(EventEnvelope<VerenigingWerdGestopt> message)
message.VCode,
new DuplicateDetectionDocument
{
IsVerwijderd = true,
IsGestopt = true,
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public record DuplicateDetectionDocument
public string VerenigingsTypeCode { get; set; } = null!;
public string KorteNaam { get; set; } = null!;
public string[] HoofdactiviteitVerenigingsloket { get; set; } = Array.Empty<string>();
public bool IsVerwijderd { get; set; }
public bool IsGestopt { get; set; }

public record Locatie : ILocatie
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static TypeMappingDescriptor<DuplicateDetectionDocument> Get(TypeMappingD
.Name(document => document.HoofdactiviteitVerenigingsloket))
.Boolean(
propertyDescriptor => propertyDescriptor
.Name(document => document.IsVerwijderd))
.Name(document => document.IsGestopt))
.Nested<DuplicateDetectionDocument.Locatie>(
propertyDescriptor => propertyDescriptor
.Name(document => document.Locaties)
Expand Down

0 comments on commit 016f042

Please sign in to comment.