Changed the image tag search query to run more efficiently on pgsql
This commit is contained in:
parent
b01f425a55
commit
183f9bb897
@ -987,6 +987,7 @@ class Image
|
|||||||
global $database;
|
global $database;
|
||||||
|
|
||||||
$positive_tag_id_array = [];
|
$positive_tag_id_array = [];
|
||||||
|
$positive_wildcard_id_array = [];
|
||||||
$negative_tag_id_array = [];
|
$negative_tag_id_array = [];
|
||||||
|
|
||||||
foreach ($tag_conditions as $tq) {
|
foreach ($tag_conditions as $tq) {
|
||||||
@ -998,9 +999,11 @@ class Image
|
|||||||
"),
|
"),
|
||||||
["tag" => Tag::sqlify($tq->tag)]
|
["tag" => Tag::sqlify($tq->tag)]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$tag_count = count($tag_ids);
|
||||||
|
|
||||||
if ($tq->positive) {
|
if ($tq->positive) {
|
||||||
$positive_tag_id_array = array_merge($positive_tag_id_array, $tag_ids);
|
if ($tag_count== 0) {
|
||||||
if (count($tag_ids) == 0) {
|
|
||||||
# one of the positive tags had zero results, therefor there
|
# one of the positive tags had zero results, therefor there
|
||||||
# can be no results; "where 1=0" should shortcut things
|
# can be no results; "where 1=0" should shortcut things
|
||||||
return new Querylet("
|
return new Querylet("
|
||||||
@ -1008,34 +1011,73 @@ class Image
|
|||||||
FROM images
|
FROM images
|
||||||
WHERE 1=0
|
WHERE 1=0
|
||||||
");
|
");
|
||||||
|
} elseif($tag_count==1) {
|
||||||
|
// All wildcard terms that qualify for a single tag can be treated the same as non-wildcards
|
||||||
|
$positive_tag_id_array[] = $tag_ids[0];
|
||||||
|
} else {
|
||||||
|
// Terms that resolve to multiple tags act as an OR within themselves
|
||||||
|
// and as an AND in relation to all other terms,
|
||||||
|
$positive_wildcard_id_array[] = $tag_ids;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Unlike positive criteria, negative criteria are all handled in an OR fashion,
|
||||||
|
// so we can just compile them all into a single sub-query.
|
||||||
$negative_tag_id_array = array_merge($negative_tag_id_array, $tag_ids);
|
$negative_tag_id_array = array_merge($negative_tag_id_array, $tag_ids);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert($positive_tag_id_array || $negative_tag_id_array, @$_GET['q']);
|
$sql = "";
|
||||||
$wheres = [];
|
assert($positive_tag_id_array || $positive_wildcard_id_array || $negative_tag_id_array, @$_GET['q']);
|
||||||
|
if(!empty($positive_tag_id_array) || !empty($positive_wildcard_id_array)) {
|
||||||
|
$inner_joins = [];
|
||||||
if (!empty($positive_tag_id_array)) {
|
if (!empty($positive_tag_id_array)) {
|
||||||
$positive_tag_id_list = join(', ', $positive_tag_id_array);
|
foreach($positive_tag_id_array as $tag) {
|
||||||
$wheres[] = "tag_id IN ($positive_tag_id_list)";
|
$inner_joins[] = "= $tag";
|
||||||
}
|
}
|
||||||
if (!empty($negative_tag_id_array)) {
|
}
|
||||||
|
if(!empty($positive_wildcard_id_array)) {
|
||||||
|
foreach ($positive_wildcard_id_array as $tags) {
|
||||||
|
$positive_tag_id_list = join(', ', $tags);
|
||||||
|
$inner_joins[] = "IN ($positive_tag_id_list)";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$first = array_shift($inner_joins);
|
||||||
|
$sub_query = "SELECT it.image_id FROM image_tags it ";
|
||||||
|
$i = 0;
|
||||||
|
foreach ($inner_joins as $inner_join) {
|
||||||
|
$i++;
|
||||||
|
$sub_query .= " INNER JOIN image_tags it$i ON it$i.image_id = it.image_id AND it$i.tag_id $inner_join ";
|
||||||
|
}
|
||||||
|
if(!empty($negative_tag_id_array)) {
|
||||||
$negative_tag_id_list = join(', ', $negative_tag_id_array);
|
$negative_tag_id_list = join(', ', $negative_tag_id_array);
|
||||||
$wheres[] = "tag_id NOT IN ($negative_tag_id_list)";
|
$sub_query .= " LEFT JOIN image_tags negative ON negative.image_id = it.image_id AND negative.tag_id IN ($negative_tag_id_list) ";
|
||||||
}
|
}
|
||||||
$wheres_str = join(" AND ", $wheres);
|
$sub_query .= "WHERE it.tag_id $first ";
|
||||||
return new Querylet("
|
if(!empty($negative_tag_id_array)) {
|
||||||
|
$sub_query .= " AND negative.image_id IS NULL";
|
||||||
|
}
|
||||||
|
$sub_query .= " GROUP BY it.image_id ";
|
||||||
|
|
||||||
|
$sql = "
|
||||||
SELECT images.*
|
SELECT images.*
|
||||||
FROM images
|
FROM images INNER JOIN (
|
||||||
WHERE images.id IN (
|
$sub_query
|
||||||
SELECT image_id
|
) a on a.image_id = images.id
|
||||||
FROM image_tags
|
";
|
||||||
WHERE $wheres_str
|
} elseif(!empty($negative_tag_id_array)) {
|
||||||
GROUP BY image_id
|
$negative_tag_id_list = join(', ', $negative_tag_id_array);
|
||||||
HAVING COUNT(image_id) >= :search_score
|
$sql = "
|
||||||
)
|
SELECT images.*
|
||||||
", ["search_score"=>count($positive_tag_id_array)]);
|
FROM images LEFT JOIN image_tags negative ON negative.image_id = images.id AND negative.tag_id in ($negative_tag_id_list)
|
||||||
|
WHERE a.image_id IS NULL
|
||||||
|
";
|
||||||
|
} else {
|
||||||
|
throw new SCoreException("No criteria specified");
|
||||||
|
}
|
||||||
|
|
||||||
|
//throw new SCoreException($sql);
|
||||||
|
return new Querylet($sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user