From 455077550b02f1291f29917297a6a5fc14100404 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Thu, 29 Jul 2021 17:32:54 -0500 Subject: [PATCH] Include contact.ticket_count as tickets in index --- contacts/index_settings.json | 3 +++ indexer.go | 2 +- indexer_test.go | 4 ++++ testdb.sql | 25 +++++++++++++------------ 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/contacts/index_settings.json b/contacts/index_settings.json index 8abbe5e..9706745 100644 --- a/contacts/index_settings.json +++ b/contacts/index_settings.json @@ -150,6 +150,9 @@ "status": { "type": "keyword" }, + "tickets": { + "type": "integer" + }, "language": { "type": "keyword", "normalizer": "lowercase" diff --git a/indexer.go b/indexer.go index 1a8856d..be30942 100644 --- a/indexer.go +++ b/indexer.go @@ -384,7 +384,7 @@ func MapIndexAlias(elasticURL string, alias string, newIndex string) error { const contactQuery = ` SELECT org_id, id, modified_on, is_active, row_to_json(t) FROM ( SELECT - id, org_id, uuid, name, language, status, is_active, created_on, modified_on, last_seen_on, + id, org_id, uuid, name, language, status, ticket_count AS tickets, is_active, created_on, modified_on, last_seen_on, EXTRACT(EPOCH FROM modified_on) * 1000000 as modified_on_mu, ( SELECT array_to_json(array_agg(row_to_json(u))) diff --git a/indexer_test.go b/indexer_test.go index 14545b9..40b6296 100644 --- a/indexer_test.go +++ b/indexer_test.go @@ -97,6 +97,10 @@ func TestIndexing(t *testing.T) { assertQuery(t, client, physicalName, elastic.NewMatchQuery("org_id", "1"), []int64{1, 2, 3, 4}) + assertQuery(t, client, physicalName, elastic.NewMatchQuery("tickets", 2), []int64{1}) + assertQuery(t, client, physicalName, elastic.NewMatchQuery("tickets", 1), []int64{2, 3}) + assertQuery(t, client, physicalName, elastic.NewRangeQuery("tickets").Gt(0), []int64{1, 2, 3}) + // created_on range query assertQuery(t, client, physicalName, elastic.NewRangeQuery("created_on").Gt("2017-01-01"), []int64{1, 6, 8}) diff --git a/testdb.sql b/testdb.sql index 5914f8b..99f379b 100644 --- a/testdb.sql +++ b/testdb.sql @@ -12,7 +12,8 @@ CREATE TABLE contacts_contact ( name character varying(128), language character varying(3), uuid character varying(36) NOT NULL, - fields jsonb + fields jsonb, + ticket_count integer NOT NULL ); CREATE SEQUENCE contacts_contact_id_seq @@ -87,42 +88,42 @@ ALTER SEQUENCE contacts_contactgroup_contacts_id_seq OWNED BY contacts_contactgr -- fcab2439-861c-4832-aa54-0c97f38f24ab - home_district (district) -- a551ade4-e5a0-4d83-b185-53b515ad2f2a - home_ward (ward) -INSERT INTO contacts_contact(id, is_active, created_by_id, created_on, modified_by_id, modified_on, last_seen_on, org_id, status, name, language, uuid, fields) VALUES +INSERT INTO contacts_contact(id, is_active, created_by_id, created_on, modified_by_id, modified_on, last_seen_on, org_id, status, name, language, uuid, fields, ticket_count) VALUES ( 1, TRUE, -1, '2017-11-10 21:11:59.890662+00', -1, '2017-11-10 21:11:59.890662+00', '2020-08-04 21:11', 1, 'A', NULL, 'eng', 'c7a2dd87-a80e-420b-8431-ca48d422e924', - '{ "17103bb1-1b48-4b70-92f7-1f6b73bd3488": {"text": "the rock"}}' + '{ "17103bb1-1b48-4b70-92f7-1f6b73bd3488": {"text": "the rock"}}', 2 ), ( 2, TRUE, -1, '2015-03-26 10:07:14.054521+00', -1, '2015-03-26 10:07:14.054521+00', '2020-08-03 13:11', 1, 'S', NULL, NULL, '7a6606c7-ff41-4203-aa98-454a10d37209', - '{ "05bca1cd-e322-4837-9595-86d0d85e5adb": {"text": "11", "number": 11 }}' + '{ "05bca1cd-e322-4837-9595-86d0d85e5adb": {"text": "11", "number": 11 }}', 1 ), ( 3, TRUE, -1, '2015-03-26 13:04:58.699648+00', -1, '2015-03-26 13:04:58.699648+00', '2018-05-04 21:11', 1, 'B', NULL, NULL, '29b45297-15ad-4061-a7d4-e0b33d121541', - '{ "05bca1cd-e322-4837-9595-86d0d85e5adb": {"text": "9", "number": 9 }, "e0eac267-463a-4c00-9732-cab62df07b16": { "text": "2018-04-06T18:37:59+00:00", "datetime": "2018-04-06T18:37:59+00:00"}}' + '{ "05bca1cd-e322-4837-9595-86d0d85e5adb": {"text": "9", "number": 9 }, "e0eac267-463a-4c00-9732-cab62df07b16": { "text": "2018-04-06T18:37:59+00:00", "datetime": "2018-04-06T18:37:59+00:00"}}', 1 ), ( 4, TRUE, -1, '2015-03-27 07:39:28.955051+00', -1, '2015-03-27 07:39:28.955051+00', '2015-12-31 23:59', 1, 'A', 'John Doe', NULL, '51762bba-01a2-4c4e-b5cd-b182d0405cd4', - '{ "e0eac267-463a-4c00-9732-cab62df07b16": { "text": "2030-04-06T18:37:59+00:00", "datetime": "2030-04-06T18:37:59+00:00"}}' - ), + '{ "e0eac267-463a-4c00-9732-cab62df07b16": { "text": "2030-04-06T18:37:59+00:00", "datetime": "2030-04-06T18:37:59+00:00"}}', 0 +), ( 5, TRUE, -1, '2015-10-30 19:42:27.001837+00', -1, '2015-10-30 19:42:27.001837+00', '2020-08-04 21:11', 2, 'A', 'Ajodinabiff Dane', NULL, '3e814add-e614-41f7-8b5d-a07f670a698f', - '{ "22d11697-edba-4186-b084-793e3b876379": { "text": "USA > Washington", "state": "USA > Washington"} }' + '{ "22d11697-edba-4186-b084-793e3b876379": { "text": "USA > Washington", "state": "USA > Washington"} }', 0 ), ( 6, TRUE, -1, '2017-11-10 21:11:59.890662+00', -1, '2017-11-10 21:11:59.890662+00', '2020-08-04 21:00', 2, 'A', 'Joanne Stone', NULL, '7051dff0-0a27-49d7-af1f-4494239139e6', - '{ "22d11697-edba-4186-b084-793e3b876379": { "text": "USA > Colorado", "state": "USA > Colorado"} }' + '{ "22d11697-edba-4186-b084-793e3b876379": { "text": "USA > Colorado", "state": "USA > Colorado"} }', 0 ), ( 7, TRUE, -1, '2015-03-27 13:39:43.995812+00', -1, '2015-03-27 13:39:43.995812+00', NULL, 2, 'A', NULL, NULL, 'b46f6e18-95b4-4984-9926-dded047f4eb3', - '{ "fcab2439-861c-4832-aa54-0c97f38f24ab": { "text": "USA > Washington > King-Côunty", "district": "USA > Washington > King-Côunty"} }' + '{ "fcab2439-861c-4832-aa54-0c97f38f24ab": { "text": "USA > Washington > King-Côunty", "district": "USA > Washington > King-Côunty"} }', 0 ), ( 8, TRUE, -1, '2017-11-10 21:11:59.890662+00', -1, '2017-11-10 21:11:59.890662+00', NULL, 2, 'A', NULL, NULL, '9195c8b7-6138-4d84-ac56-5192cc3d8ceb', - '{ "a551ade4-e5a0-4d83-b185-53b515ad2f2a": { "text": "USA > Washington > King-Côunty > Central District", "ward": "USA > Washington > King-Côunty > Central District"} }' + '{ "a551ade4-e5a0-4d83-b185-53b515ad2f2a": { "text": "USA > Washington > King-Côunty > Central District", "ward": "USA > Washington > King-Côunty > Central District"} }', 0 ), ( 9, TRUE, -1, '2016-08-22 14:20:05.690311+00', -1, '2016-08-22 14:20:05.690311+00', NULL, 2, 'A', NULL, NULL, '2b8bd28d-43e0-4c34-a4bb-0f10b11fdb8a', - '{ "fcab2439-861c-4832-aa54-0c97f38f24ab": { "text": "USA > Colorado > King", "district": "USA > Colorado > King"} }' + '{ "fcab2439-861c-4832-aa54-0c97f38f24ab": { "text": "USA > Colorado > King", "district": "USA > Colorado > King"} }', 0 ); INSERT INTO contacts_contacturn(id, contact_id, scheme, org_id, priority, path, display, identity) VALUES