Skip to content

Instantly share code, notes, and snippets.

@sebastjan-hribar
Created March 2, 2021 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastjan-hribar/fc4728104d15d087a0938b144d4fc68b to your computer and use it in GitHub Desktop.
Save sebastjan-hribar/fc4728104d15d087a0938b144d4fc68b to your computer and use it in GitHub Desktop.
# Aggregation
SELECT "id", "language_id", "content", "created_at", "updated_at",
SIMILARITY("content", 'I''m light as a frog') AS "similarity" FROM "segments"
WHERE (("language_id" = 5) AND (similarity("content", 'I''m light as a frog') > 0.45))
ORDER BY SIMILARITY("content", 'I''m light as a frog')
DESC
SELECT "translation_records"."id", "translation_records"."source_segment_id",
"translation_records"."target_segment_id", "translation_records"."domain_id",
"translation_records"."style_id", "translation_records"."project_name",
"translation_records"."created_at", "translation_records"."updated_at",
"translation_records"."language_combination",
"translation_records"."uid" FROM "translation_records"
INNER JOIN "segments" ON ("segments"."id" = "translation_records"."source_segment_id")
WHERE ("translation_records"."source_segment_id" IN (113, 115)) ORDER BY "translation_records"."id"
# Returned
[
{
:id=>113, :language_id=>5, :content=>"I'm light as a bird.", :created_at=>2021-02-28 14:11:17 UTC,
:updated_at=>2021-02-28 14:11:17 UTC, :similarity=>0.583333,
:translation_records=>
[
{
:id=>57, :source_segment_id=>113, :target_segment_id=>114, :domain_id=>2, :style_id=>4,
:project_name=>"Rjavina", :created_at=>2021-02-28 14:11:17 UTC,
:updated_at=>2021-02-28 14:11:17 UTC, :language_combination=>"5_4", :uid=>"113_114_5_4_2_4"
}
]
},
{
:id=>115, :language_id=>5, :content=>"I'm light as a dodo.", :created_at=>2021-02-28 16:16:21 UTC,
:updated_at=>2021-02-28 16:16:21 UTC, :similarity=>0.583333,
:translation_records=>
[
{
:id=>58, :source_segment_id=>115, :target_segment_id=>116, :domain_id=>2, :style_id=>4,
:project_name=>"Rjavina", :created_at=>2021-02-28 16:16:21 UTC,
:updated_at=>2021-02-28 16:16:21 UTC, :language_combination=>"5_4", :uid=>"115_116_5_4_2_4"
}
]
}
]
---
# Without aggregation
SELECT "id", "language_id", "content", "created_at", "updated_at",
SIMILARITY("content", 'I''m light as a frog') AS "similarity" FROM "segments"
WHERE (("language_id" = 5) AND (similarity("content", 'I''m light as a frog') > 0.45))
ORDER BY SIMILARITY("content", 'I''m light as a frog')
DESC
# Returned
[
#<Segment:0x000055aab0009898
@attributes=
{
:id=>113, :language_id=>5, :content=>"I'm light as a bird.",
:created_at=>2021-02-28 14:11:17 UTC, :updated_at=>2021-02-28 14:11:17 UTC
}>,
#<Segment:0x000055aaaffcf788
@attributes=
{
:id=>115, :language_id=>5, :content=>"I'm light as a dodo.",
:created_at=>2021-02-28 16:16:21 UTC, :updated_at=>2021-02-28 16:16:21 UTC
}>
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment