From 713bc497466a364c97c27398c0264699c4db2c9a Mon Sep 17 00:00:00 2001 From: plegall Date: Sat, 13 Feb 2016 18:36:55 +0100 Subject: feature #355, apply search by word on tags we "simulate" a search by tag, but we still perform a "search by words" AND "search by tags", which lead to different result compared to "word on title OR file OR description OR tags" --- include/functions_search.inc.php | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/functions_search.inc.php b/include/functions_search.inc.php index 69c57faa7..9a9842e7d 100644 --- a/include/functions_search.inc.php +++ b/include/functions_search.inc.php @@ -90,7 +90,7 @@ function get_sql_search_clause($search) } } - if (isset($search['fields']['allwords'])) + if (isset($search['fields']['allwords']) and count($search['fields']['allwords']['fields']) > 0) { $fields = array('file', 'name', 'comment'); @@ -98,7 +98,7 @@ function get_sql_search_clause($search) { $fields = array_intersect($fields, $search['fields']['allwords']['fields']); } - + // in the OR mode, request bust be : // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%') // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%')) @@ -199,6 +199,7 @@ function get_sql_search_clause($search) */ function get_regular_search_results($search, $images_where='') { + // echo '
'; print_r($search); echo '
'; global $conf; $forbidden = get_sql_condition_FandF( array @@ -213,6 +214,37 @@ function get_regular_search_results($search, $images_where='') $items = array(); $tag_items = array(); + if (isset($search['fields']['search_in_tags'])) + { + $word_clauses = array(); + foreach ($search['fields']['allwords']['words'] as $word) + { + $word_clauses[] = "name LIKE '%".$word."%'"; + } + + $query = ' +SELECT + id + FROM '.TAGS_TABLE.' + WHERE '.implode(' OR ', $word_clauses).' +;'; + // echo '
'.$query.'
'; + $tag_ids = query2array($query, null, 'id'); + + if (!isset($search['fields']['tags'])) + { + $search['fields']['tags'] = array( + 'words' => $tag_ids, + 'mode' => 'OR', + ); + } + else + { + $search['fields']['tags']['words'] = array_merge($search['fields']['tags']['words'], $tag_ids); + } + // echo '
'; print_r($search); echo '
'; + } + if (isset($search['fields']['tags'])) { $tag_items = get_image_ids_for_tags( -- cgit v1.2.3 From 8897e75fc28679b5003b8abf9ecdb50234c9199f Mon Sep 17 00:00:00 2001 From: plegall Date: Sat, 13 Feb 2016 19:31:26 +0100 Subject: feature #355, OR with other "search by word" results + use $logger instead of old style "echo" for debugging --- include/functions_search.inc.php | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'include') diff --git a/include/functions_search.inc.php b/include/functions_search.inc.php index 9a9842e7d..d8df4eea8 100644 --- a/include/functions_search.inc.php +++ b/include/functions_search.inc.php @@ -199,8 +199,10 @@ function get_sql_search_clause($search) */ function get_regular_search_results($search, $images_where='') { - // echo '
'; print_r($search); echo '
'; - global $conf; + global $conf, $logger; + + $logger->debug(__FUNCTION__, 'search', $search); + $forbidden = get_sql_condition_FandF( array ( @@ -228,21 +230,11 @@ SELECT FROM '.TAGS_TABLE.' WHERE '.implode(' OR ', $word_clauses).' ;'; - // echo '
'.$query.'
'; $tag_ids = query2array($query, null, 'id'); - if (!isset($search['fields']['tags'])) - { - $search['fields']['tags'] = array( - 'words' => $tag_ids, - 'mode' => 'OR', - ); - } - else - { - $search['fields']['tags']['words'] = array_merge($search['fields']['tags']['words'], $tag_ids); - } - // echo '
'; print_r($search); echo '
'; + $search_in_tags_items = get_image_ids_for_tags($tag_ids, 'OR'); + + $logger->debug(__FUNCTION__.' '.count($search_in_tags_items).' items in $search_in_tags_items'); } if (isset($search['fields']['tags'])) @@ -251,6 +243,8 @@ SELECT $search['fields']['tags']['words'], $search['fields']['tags']['mode'] ); + + $logger->debug(__FUNCTION__.' '.count($tag_items).' items in $tag_items'); } $search_clause = get_sql_search_clause($search); @@ -269,6 +263,18 @@ SELECT DISTINCT(id) $query .= $forbidden.' '.$conf['order_by']; $items = array_from_query($query, 'id'); + + $logger->debug(__FUNCTION__.' '.count($items).' items in $items'); + } + + if (isset($search_in_tags_items)) + { + $items = array_unique( + array_merge( + $items, + $search_in_tags_items + ) + ); } if ( !empty($tag_items) ) @@ -276,7 +282,7 @@ SELECT DISTINCT(id) switch ($search['mode']) { case 'AND': - if (empty($search_clause)) + if (empty($search_clause) and !isset($search_in_tags_items)) { $items = $tag_items; } @@ -286,7 +292,6 @@ SELECT DISTINCT(id) } break; case 'OR': - $before_count = count($items); $items = array_unique( array_merge( $items, -- cgit v1.2.3