From a2e705bd92e085007fb0313b163f194497bcb5bf Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Wed, 12 Jul 2023 10:38:11 +0200 Subject: [PATCH 1/5] fixed: error with parent joins in thirdparty api search query --- .../Controller/resquery.bad.sql | 21 +++++++++++++++++++ .../Controller/resquery.fixed.sql | 21 +++++++++++++++++++ .../Search/ThirdPartyApiSearch.php | 2 +- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/Bundle/ChillMainBundle/Controller/resquery.bad.sql create mode 100644 src/Bundle/ChillMainBundle/Controller/resquery.fixed.sql diff --git a/src/Bundle/ChillMainBundle/Controller/resquery.bad.sql b/src/Bundle/ChillMainBundle/Controller/resquery.bad.sql new file mode 100644 index 000000000..1033ec28c --- /dev/null +++ b/src/Bundle/ChillMainBundle/Controller/resquery.bad.sql @@ -0,0 +1,21 @@ +SELECT +'tparty' AS key, jsonb_build_object('id', tparty.id) AS metadata, GREATEST( +STRICT_WORD_SIMILARITY(LOWER(UNACCENT('areams')), tparty.canonicalized), +STRICT_WORD_SIMILARITY(LOWER(UNACCENT('areams')), parent.canonicalized) +) + GREATEST( +(tparty.canonicalized LIKE '%s' || LOWER(UNACCENT('areams')) || '%')::int, +(parent.canonicalized LIKE '%s' || LOWER(UNACCENT('areams')) || '%')::int +) + COALESCE((LOWER(UNACCENT(cmpc.label)) LIKE '%' || LOWER(UNACCENT('areams')) || '%')::int * 0.3, 0) + COALESCE((LOWER(UNACCENT(cmpc_p.label)) LIKE '%' || LOWER(UNACCENT('areams')) || '%')::int * 0.3, 0) + 1 AS pertinence +FROM chill_3party.third_party AS tparty +LEFT JOIN chill_main_address cma ON cma.id = tparty.address_id +LEFT JOIN chill_main_postal_code cmpc ON cma.postcode_id = cmpc.id +LEFT JOIN chill_3party.third_party AS parent ON tparty.parent_id = parent.id +LEFT JOIN chill_main_address cma_p ON parent.address_id = cma_p.id +LEFT JOIN chill_main_postal_code cmpc_p ON cma_p.postcode_id = cmpc.id +WHERE (tparty.active IS TRUE) AND (((LOWER(UNACCENT('areams')) <<% tparty.canonicalized OR +tparty.canonicalized LIKE '%' || LOWER(UNACCENT('areams')) || '%') +OR +(LOWER(UNACCENT('areams')) <<% parent.canonicalized OR +parent.canonicalized LIKE '%' || LOWER(UNACCENT('areams')) || '%')) +AND tparty.active IS TRUE and (parent.active IS TRUE OR parent IS NULL)) +ORDER BY pertinence DESC LIMIT 50 OFFSET 0; diff --git a/src/Bundle/ChillMainBundle/Controller/resquery.fixed.sql b/src/Bundle/ChillMainBundle/Controller/resquery.fixed.sql new file mode 100644 index 000000000..dbb55f187 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Controller/resquery.fixed.sql @@ -0,0 +1,21 @@ +SELECT +'tparty' AS key, jsonb_build_object('id', tparty.id) AS metadata, GREATEST( +STRICT_WORD_SIMILARITY(LOWER(UNACCENT('areams')), tparty.canonicalized), +STRICT_WORD_SIMILARITY(LOWER(UNACCENT('areams')), parent.canonicalized) +) + GREATEST( +(tparty.canonicalized LIKE '%s' || LOWER(UNACCENT('areams')) || '%')::int, +(parent.canonicalized LIKE '%s' || LOWER(UNACCENT('areams')) || '%')::int +) + COALESCE((LOWER(UNACCENT(cmpc.label)) LIKE '%' || LOWER(UNACCENT('areams')) || '%')::int * 0.3, 0) + COALESCE((LOWER(UNACCENT(cmpc_p.label)) LIKE '%' || LOWER(UNACCENT('areams')) || '%')::int * 0.3, 0) + 1 AS pertinence +FROM chill_3party.third_party AS tparty +LEFT JOIN chill_main_address cma ON cma.id = tparty.address_id +LEFT JOIN chill_main_postal_code cmpc ON cma.postcode_id = cmpc.id +LEFT JOIN chill_3party.third_party AS parent ON tparty.parent_id = parent.id +LEFT JOIN chill_main_address cma_p ON parent.address_id = cma_p.id +LEFT JOIN chill_main_postal_code cmpc_p ON cma_p.postcode_id = cmpc_p.id +WHERE (tparty.active IS TRUE) AND (((LOWER(UNACCENT('areams')) <<% tparty.canonicalized OR +tparty.canonicalized LIKE '%' || LOWER(UNACCENT('areams')) || '%') +OR +(LOWER(UNACCENT('areams')) <<% parent.canonicalized OR +parent.canonicalized LIKE '%' || LOWER(UNACCENT('areams')) || '%')) +AND tparty.active IS TRUE and (parent.active IS TRUE OR parent IS NULL)) +ORDER BY pertinence DESC LIMIT 50 OFFSET 0; diff --git a/src/Bundle/ChillThirdPartyBundle/Search/ThirdPartyApiSearch.php b/src/Bundle/ChillThirdPartyBundle/Search/ThirdPartyApiSearch.php index 86c0fa9db..42b98622f 100644 --- a/src/Bundle/ChillThirdPartyBundle/Search/ThirdPartyApiSearch.php +++ b/src/Bundle/ChillThirdPartyBundle/Search/ThirdPartyApiSearch.php @@ -75,7 +75,7 @@ class ThirdPartyApiSearch implements SearchApiInterface LEFT JOIN chill_main_postal_code cmpc ON cma.postcode_id = cmpc.id LEFT JOIN chill_3party.third_party AS parent ON tparty.parent_id = parent.id LEFT JOIN chill_main_address cma_p ON parent.address_id = cma_p.id - LEFT JOIN chill_main_postal_code cmpc_p ON cma_p.postcode_id = cmpc.id') + LEFT JOIN chill_main_postal_code cmpc_p ON cma_p.postcode_id = cmpc_p.id') ->andWhereClause('tparty.active IS TRUE'); $strs = explode(' ', $pattern); From f3829d3390ae7e22939f02a6bb71db305ebe060e Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Wed, 12 Jul 2023 10:50:17 +0200 Subject: [PATCH 2/5] adapt query to simplify join clauses (lightly improve perfs) --- .../Controller/resquery.good.sql | 19 +++++++++++++++++++ .../Search/ThirdPartyApiSearch.php | 10 +++------- 2 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Controller/resquery.good.sql diff --git a/src/Bundle/ChillMainBundle/Controller/resquery.good.sql b/src/Bundle/ChillMainBundle/Controller/resquery.good.sql new file mode 100644 index 000000000..5877b04f8 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Controller/resquery.good.sql @@ -0,0 +1,19 @@ +SELECT +'tparty' AS key, jsonb_build_object('id', tparty.id) AS metadata, GREATEST( +STRICT_WORD_SIMILARITY(LOWER(UNACCENT('areams')), tparty.canonicalized), +STRICT_WORD_SIMILARITY(LOWER(UNACCENT('areams')), parent.canonicalized) +) + GREATEST( +(tparty.canonicalized LIKE '%s' || LOWER(UNACCENT('areams')) || '%')::int, +(parent.canonicalized LIKE '%s' || LOWER(UNACCENT('areams')) || '%')::int +) + COALESCE((LOWER(UNACCENT(cmpc.label)) LIKE '%' || LOWER(UNACCENT('areams')) || '%')::int * 0.3, 0) + 1 AS pertinence +FROM chill_3party.third_party AS tparty +LEFT JOIN chill_3party.third_party AS parent ON tparty.parent_id = parent.id +LEFT JOIN chill_main_address cma ON cma.id = COALESCE(parent.address_id, tparty.address_id) +LEFT JOIN chill_main_postal_code cmpc ON cma.postcode_id = cmpc.id +WHERE (((LOWER(UNACCENT('areams')) <<% tparty.canonicalized OR +tparty.canonicalized LIKE '%' || LOWER(UNACCENT('areams')) || '%') +OR +(LOWER(UNACCENT('areams')) <<% parent.canonicalized OR +parent.canonicalized LIKE '%' || LOWER(UNACCENT('areams')) || '%')) +AND tparty.active IS TRUE and (parent.active IS TRUE OR parent IS NULL)) +ORDER BY pertinence DESC, tparty.id ASC LIMIT 500 OFFSET 0; diff --git a/src/Bundle/ChillThirdPartyBundle/Search/ThirdPartyApiSearch.php b/src/Bundle/ChillThirdPartyBundle/Search/ThirdPartyApiSearch.php index 42b98622f..bb5303143 100644 --- a/src/Bundle/ChillThirdPartyBundle/Search/ThirdPartyApiSearch.php +++ b/src/Bundle/ChillThirdPartyBundle/Search/ThirdPartyApiSearch.php @@ -71,12 +71,9 @@ class ThirdPartyApiSearch implements SearchApiInterface ->setSelectKey('tparty') ->setSelectJsonbMetadata("jsonb_build_object('id', tparty.id)") ->setFromClause('chill_3party.third_party AS tparty - LEFT JOIN chill_main_address cma ON cma.id = tparty.address_id - LEFT JOIN chill_main_postal_code cmpc ON cma.postcode_id = cmpc.id LEFT JOIN chill_3party.third_party AS parent ON tparty.parent_id = parent.id - LEFT JOIN chill_main_address cma_p ON parent.address_id = cma_p.id - LEFT JOIN chill_main_postal_code cmpc_p ON cma_p.postcode_id = cmpc_p.id') - ->andWhereClause('tparty.active IS TRUE'); + LEFT JOIN chill_main_address cma ON cma.id = COALESCE(parent.address_id, tparty.address_id) + LEFT JOIN chill_main_postal_code cmpc ON cma.postcode_id = cmpc.id'); $strs = explode(' ', $pattern); $wheres = []; @@ -102,8 +99,7 @@ class ThirdPartyApiSearch implements SearchApiInterface (parent.canonicalized LIKE '%s' || LOWER(UNACCENT(?)) || '%')::int ) + " . // take postcode label into account, but lower than the canonicalized field - "COALESCE((LOWER(UNACCENT(cmpc.label)) LIKE '%' || LOWER(UNACCENT(?)) || '%')::int * 0.3, 0) + " . - "COALESCE((LOWER(UNACCENT(cmpc_p.label)) LIKE '%' || LOWER(UNACCENT(?)) || '%')::int * 0.3, 0)"; + "COALESCE((LOWER(UNACCENT(cmpc.label)) LIKE '%' || LOWER(UNACCENT(?)) || '%')::int * 0.3, 0)"; $pertinenceArgs[] = [$str, $str, $str, $str, $str, $str]; } } From efee2d8b44369e6546d13afa19d58b8a43d086db Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Wed, 12 Jul 2023 10:53:12 +0200 Subject: [PATCH 3/5] cleaning --- .../Controller/resquery.bad.sql | 21 ------------------- .../Controller/resquery.fixed.sql | 21 ------------------- .../Controller/resquery.good.sql | 19 ----------------- 3 files changed, 61 deletions(-) delete mode 100644 src/Bundle/ChillMainBundle/Controller/resquery.bad.sql delete mode 100644 src/Bundle/ChillMainBundle/Controller/resquery.fixed.sql delete mode 100644 src/Bundle/ChillMainBundle/Controller/resquery.good.sql diff --git a/src/Bundle/ChillMainBundle/Controller/resquery.bad.sql b/src/Bundle/ChillMainBundle/Controller/resquery.bad.sql deleted file mode 100644 index 1033ec28c..000000000 --- a/src/Bundle/ChillMainBundle/Controller/resquery.bad.sql +++ /dev/null @@ -1,21 +0,0 @@ -SELECT -'tparty' AS key, jsonb_build_object('id', tparty.id) AS metadata, GREATEST( -STRICT_WORD_SIMILARITY(LOWER(UNACCENT('areams')), tparty.canonicalized), -STRICT_WORD_SIMILARITY(LOWER(UNACCENT('areams')), parent.canonicalized) -) + GREATEST( -(tparty.canonicalized LIKE '%s' || LOWER(UNACCENT('areams')) || '%')::int, -(parent.canonicalized LIKE '%s' || LOWER(UNACCENT('areams')) || '%')::int -) + COALESCE((LOWER(UNACCENT(cmpc.label)) LIKE '%' || LOWER(UNACCENT('areams')) || '%')::int * 0.3, 0) + COALESCE((LOWER(UNACCENT(cmpc_p.label)) LIKE '%' || LOWER(UNACCENT('areams')) || '%')::int * 0.3, 0) + 1 AS pertinence -FROM chill_3party.third_party AS tparty -LEFT JOIN chill_main_address cma ON cma.id = tparty.address_id -LEFT JOIN chill_main_postal_code cmpc ON cma.postcode_id = cmpc.id -LEFT JOIN chill_3party.third_party AS parent ON tparty.parent_id = parent.id -LEFT JOIN chill_main_address cma_p ON parent.address_id = cma_p.id -LEFT JOIN chill_main_postal_code cmpc_p ON cma_p.postcode_id = cmpc.id -WHERE (tparty.active IS TRUE) AND (((LOWER(UNACCENT('areams')) <<% tparty.canonicalized OR -tparty.canonicalized LIKE '%' || LOWER(UNACCENT('areams')) || '%') -OR -(LOWER(UNACCENT('areams')) <<% parent.canonicalized OR -parent.canonicalized LIKE '%' || LOWER(UNACCENT('areams')) || '%')) -AND tparty.active IS TRUE and (parent.active IS TRUE OR parent IS NULL)) -ORDER BY pertinence DESC LIMIT 50 OFFSET 0; diff --git a/src/Bundle/ChillMainBundle/Controller/resquery.fixed.sql b/src/Bundle/ChillMainBundle/Controller/resquery.fixed.sql deleted file mode 100644 index dbb55f187..000000000 --- a/src/Bundle/ChillMainBundle/Controller/resquery.fixed.sql +++ /dev/null @@ -1,21 +0,0 @@ -SELECT -'tparty' AS key, jsonb_build_object('id', tparty.id) AS metadata, GREATEST( -STRICT_WORD_SIMILARITY(LOWER(UNACCENT('areams')), tparty.canonicalized), -STRICT_WORD_SIMILARITY(LOWER(UNACCENT('areams')), parent.canonicalized) -) + GREATEST( -(tparty.canonicalized LIKE '%s' || LOWER(UNACCENT('areams')) || '%')::int, -(parent.canonicalized LIKE '%s' || LOWER(UNACCENT('areams')) || '%')::int -) + COALESCE((LOWER(UNACCENT(cmpc.label)) LIKE '%' || LOWER(UNACCENT('areams')) || '%')::int * 0.3, 0) + COALESCE((LOWER(UNACCENT(cmpc_p.label)) LIKE '%' || LOWER(UNACCENT('areams')) || '%')::int * 0.3, 0) + 1 AS pertinence -FROM chill_3party.third_party AS tparty -LEFT JOIN chill_main_address cma ON cma.id = tparty.address_id -LEFT JOIN chill_main_postal_code cmpc ON cma.postcode_id = cmpc.id -LEFT JOIN chill_3party.third_party AS parent ON tparty.parent_id = parent.id -LEFT JOIN chill_main_address cma_p ON parent.address_id = cma_p.id -LEFT JOIN chill_main_postal_code cmpc_p ON cma_p.postcode_id = cmpc_p.id -WHERE (tparty.active IS TRUE) AND (((LOWER(UNACCENT('areams')) <<% tparty.canonicalized OR -tparty.canonicalized LIKE '%' || LOWER(UNACCENT('areams')) || '%') -OR -(LOWER(UNACCENT('areams')) <<% parent.canonicalized OR -parent.canonicalized LIKE '%' || LOWER(UNACCENT('areams')) || '%')) -AND tparty.active IS TRUE and (parent.active IS TRUE OR parent IS NULL)) -ORDER BY pertinence DESC LIMIT 50 OFFSET 0; diff --git a/src/Bundle/ChillMainBundle/Controller/resquery.good.sql b/src/Bundle/ChillMainBundle/Controller/resquery.good.sql deleted file mode 100644 index 5877b04f8..000000000 --- a/src/Bundle/ChillMainBundle/Controller/resquery.good.sql +++ /dev/null @@ -1,19 +0,0 @@ -SELECT -'tparty' AS key, jsonb_build_object('id', tparty.id) AS metadata, GREATEST( -STRICT_WORD_SIMILARITY(LOWER(UNACCENT('areams')), tparty.canonicalized), -STRICT_WORD_SIMILARITY(LOWER(UNACCENT('areams')), parent.canonicalized) -) + GREATEST( -(tparty.canonicalized LIKE '%s' || LOWER(UNACCENT('areams')) || '%')::int, -(parent.canonicalized LIKE '%s' || LOWER(UNACCENT('areams')) || '%')::int -) + COALESCE((LOWER(UNACCENT(cmpc.label)) LIKE '%' || LOWER(UNACCENT('areams')) || '%')::int * 0.3, 0) + 1 AS pertinence -FROM chill_3party.third_party AS tparty -LEFT JOIN chill_3party.third_party AS parent ON tparty.parent_id = parent.id -LEFT JOIN chill_main_address cma ON cma.id = COALESCE(parent.address_id, tparty.address_id) -LEFT JOIN chill_main_postal_code cmpc ON cma.postcode_id = cmpc.id -WHERE (((LOWER(UNACCENT('areams')) <<% tparty.canonicalized OR -tparty.canonicalized LIKE '%' || LOWER(UNACCENT('areams')) || '%') -OR -(LOWER(UNACCENT('areams')) <<% parent.canonicalized OR -parent.canonicalized LIKE '%' || LOWER(UNACCENT('areams')) || '%')) -AND tparty.active IS TRUE and (parent.active IS TRUE OR parent IS NULL)) -ORDER BY pertinence DESC, tparty.id ASC LIMIT 500 OFFSET 0; From 1409a3b23af53b9744cca5fd1dadc60874b82951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 13 Jul 2023 10:27:29 +0200 Subject: [PATCH 4/5] update changelog --- .changes/unreleased/Fixed-20230713-102640.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Fixed-20230713-102640.yaml diff --git a/.changes/unreleased/Fixed-20230713-102640.yaml b/.changes/unreleased/Fixed-20230713-102640.yaml new file mode 100644 index 000000000..e731e5252 --- /dev/null +++ b/.changes/unreleased/Fixed-20230713-102640.yaml @@ -0,0 +1,6 @@ +kind: Fixed +body: | + Correct bug in thirdparty API search query: simplify address joins clause for child and parent kind +time: 2023-07-13T10:26:40.503796155+02:00 +custom: + Issue: "126" From 28c41aaf8543dac4a3f3da0eb195c87199e19c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 13 Jul 2023 10:32:56 +0200 Subject: [PATCH 5/5] fix the number of parameters in the query --- src/Bundle/ChillThirdPartyBundle/Search/ThirdPartyApiSearch.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bundle/ChillThirdPartyBundle/Search/ThirdPartyApiSearch.php b/src/Bundle/ChillThirdPartyBundle/Search/ThirdPartyApiSearch.php index bb5303143..1d4e12074 100644 --- a/src/Bundle/ChillThirdPartyBundle/Search/ThirdPartyApiSearch.php +++ b/src/Bundle/ChillThirdPartyBundle/Search/ThirdPartyApiSearch.php @@ -100,7 +100,7 @@ class ThirdPartyApiSearch implements SearchApiInterface ) + " . // take postcode label into account, but lower than the canonicalized field "COALESCE((LOWER(UNACCENT(cmpc.label)) LIKE '%' || LOWER(UNACCENT(?)) || '%')::int * 0.3, 0)"; - $pertinenceArgs[] = [$str, $str, $str, $str, $str, $str]; + $pertinenceArgs[] = [$str, $str, $str, $str, $str]; } }