我有这个搜索代码来搜索Wordpress(MySQL)数据库中图像的描述和替代文本。但是我需要它来搜索一下"更好"我想知道如何改进它。
实施例: 鸟类图像的典型描述是:"大鸟蓝鹭飞行中的大型蓝色飞行飞行"
目前,下面的代码将仅使用这些单词找到匹配的图像描述。但是,如果我结合了大蓝鹭"作为搜索词我什么都没得到。
我认为一些SQL向导可能会看到通过修改代码中的查询方法来扩展搜索功能的一种相当简单的方法。如果你能提供帮助,我将非常感激:)
class WPSSearchImages
{
function __construct() {
//NOP
}
//Searches all gallery photos using a keyword and returns an array of results containing post IDs (ie, image IDs)
static function perform_gallery_keyword_search($keyword)
{
//Handles the keyword search of galleries and photos
global $wpdb;
//Clear the previous search result array if it exists
WPSSession::drop("last_search_results");
WPSSession::drop("last_search_term");
//Perform queries and collect the results
//1) Search image description and alt text
$subquery1 = "$wpdb->posts.post_content LIKE '%$keyword%'"; //Takes care of the post content part
$subquery2 = "$wpdb->postmeta.meta_key = '_wp_attachment_image_alt' AND $wpdb->postmeta.meta_value LIKE '%$keyword%'"; //Takes care of image alt text part
$main_subquery = $subquery1 . " OR " . $subquery2;
$querystr = "
SELECT DISTINCT $wpdb->postmeta.post_id
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = '_wpps_gallery_id'
AND $wpdb->posts.post_type = 'attachment'
AND $main_subquery
ORDER BY $wpdb->postmeta.post_id ASC
";
$results1 = $wpdb->get_results($querystr, ARRAY_A);
//2) Search image caption/excerpt
$subquery1 = "$wpdb->posts.post_excerpt LIKE '%$keyword%'"; //Takes care of the image caption part
$querystr = "
SELECT DISTINCT $wpdb->postmeta.post_id
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = '_wpps_gallery_id'
AND $wpdb->posts.post_type = 'attachment'
AND $subquery1
ORDER BY $wpdb->postmeta.post_id ASC
";
$results2 = $wpdb->get_results($querystr, ARRAY_A);
//3) Search image title
$subquery1 = "$wpdb->posts.post_title LIKE '%$keyword%'"; //Takes care of the image title
$querystr = "
SELECT DISTINCT $wpdb->postmeta.post_id
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = '_wpps_gallery_id'
AND $wpdb->posts.post_type = 'attachment'
AND $subquery1
ORDER BY $wpdb->postmeta.post_id ASC
";
$results3 = $wpdb->get_results($querystr, ARRAY_A);
//4) Search image name
$subquery1 = "$wpdb->posts.post_name LIKE '%$keyword%'"; //Takes care of the image title
$querystr = "
SELECT DISTINCT $wpdb->postmeta.post_id
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = '_wpps_gallery_id'
AND $wpdb->posts.post_type = 'attachment'
AND $subquery1
ORDER BY $wpdb->postmeta.post_id ASC
";
$results4 = $wpdb->get_results($querystr, ARRAY_A);
//Let's merge all result arrays
$merged = array_merge($results1, $results2, $results3, $results4);
if(empty($merged)){
return false;
}
//Extract only the post id and put into array
$s_data = array();
foreach($merged as $res)
{
$s_data[] = $res['post_id'];
}
$unique_results = array_unique($s_data); //Remove duplicates
//Store result array in session variable
if(empty($unique_results)){
return null;
}else{
$searched_photos_array = WPSGalleryItem::getSearchedPhotoItemsArray($unique_results);
WPSSession::set("last_search_results", $searched_photos_array);
WPSSession::set("last_search_term", $keyword);
return $searched_photos_array;
}
}
}
答案 0 :(得分:0)
LIKE
匹配以前导%
开头意味着顺序搜索。如果你能找到避免这种方法的方法,那么你将获得巨大的进步。
我也想知道你的运营商优先权是否正确。我相信整个$main_subquery
应该在括号中:
$main_subquery = "(". $subquery1 . " OR " . $subquery2 .")";