Permalink
Browse files

Allow searching on texts like 'id:1234' to find a specific id.

(cherry picked from commit bbf2bb9)
  • Loading branch information...
1 parent 59f0228 commit 9a2892e41a3524541335dcacfdd9a5601c01ad49 @mworrell mworrell committed with arjan May 15, 2012
Showing with 51 additions and 25 deletions.
  1. +39 −16 modules/mod_search/mod_search.erl
  2. +12 −9 modules/mod_search/support/search_query.erl
@@ -35,7 +35,8 @@
-export([
observe_search_query/2,
observe_module_activate/2,
- to_tsquery/2
+ to_tsquery/2,
+ find_by_id/2
]).
-include("zotonic.hrl").
@@ -363,27 +364,32 @@ search({upcoming, [{cat, Cat}]}, OffsetLimit, Context) ->
search({autocomplete, [{text,QueryText}]}, OffsetLimit, Context) ->
search({autocomplete, [{cat,[]}, {text,QueryText}]}, OffsetLimit, Context);
search({autocomplete, [{cat,Cat}, {text,QueryText}]}, _OffsetLimit, Context) ->
- TsQuery = to_tsquery(QueryText, Context),
- case TsQuery of
- A when A == undefined orelse A == [] ->
- #search_result{};
+ case z_string:trim(QueryText) of
+ "id:" ++ S ->
+ find_by_id(S, Context);
_ ->
- #search_sql{
- select="r.id, ts_rank_cd(pivot_tsv, query, 32) AS rank",
- from="rsc r, to_tsquery($2, $1) query",
- where=" query @@ pivot_tsv",
- order="rank desc",
- args=[TsQuery, z_pivot_rsc:pg_lang(Context#context.language)],
- cats=[{"r", Cat}],
- tables=[{rsc,"r"}]
- }
+ TsQuery = to_tsquery(QueryText, Context),
+ case TsQuery of
+ A when A == undefined orelse A == [] ->
+ #search_result{};
+ _ ->
+ #search_sql{
+ select="r.id, ts_rank_cd(pivot_tsv, query, 32) AS rank",
+ from="rsc r, to_tsquery($2, $1) query",
+ where=" query @@ pivot_tsv",
+ order="rank desc",
+ args=[TsQuery, z_pivot_rsc:pg_lang(Context#context.language)],
+ cats=[{"r", Cat}],
+ tables=[{rsc,"r"}]
+ }
+ end
end;
search({fulltext, [{cat,Cat},{text,QueryText}]}, OffsetLimit, Context) when Cat == undefined orelse Cat == [] orelse Cat == <<>> ->
search({fulltext, [{text,QueryText}]}, OffsetLimit, Context);
search({fulltext, [{text,QueryText}]}, _OffsetLimit, Context) ->
- case QueryText of
+ case z_string:trim(QueryText) of
A when A == undefined orelse A == "" orelse A == <<>> ->
#search_sql{
select="r.id, 1 AS rank",
@@ -392,6 +398,8 @@ search({fulltext, [{text,QueryText}]}, _OffsetLimit, Context) ->
args=[],
tables=[{rsc,"r"}]
};
+ "id:" ++ S ->
+ find_by_id(S, Context);
_ ->
TsQuery = to_tsquery(QueryText, Context),
#search_sql{
@@ -405,7 +413,7 @@ search({fulltext, [{text,QueryText}]}, _OffsetLimit, Context) ->
end;
search({fulltext, [{cat,Cat},{text,QueryText}]}, _OffsetLimit, Context) ->
- case QueryText of
+ case z_string:trim(QueryText) of
A when A == undefined orelse A == "" orelse A == <<>> ->
#search_sql{
select="r.id, 1 AS rank",
@@ -414,6 +422,8 @@ search({fulltext, [{cat,Cat},{text,QueryText}]}, _OffsetLimit, Context) ->
cats=[{"r", Cat}],
tables=[{rsc,"r"}]
};
+ "id:" ++ S ->
+ find_by_id(S, Context);
_ ->
TsQuery = to_tsquery(QueryText, Context),
#search_sql{
@@ -527,3 +537,16 @@ to_tsquery(Text, Context) ->
re:replace(TsQ2, "'", "", [global])
end.
+
+%% @doc Find a resource by id or name
+find_by_id(S, Context) ->
+ case m_rsc:rid(S, Context) of
+ undefined ->
+ #search_result{};
+ RscId ->
+ #search_result{
+ result=[RscId],
+ page=1,
+ total=1
+ }
+ end.
@@ -35,11 +35,15 @@ search(Query, Context) ->
from="rsc rsc",
tables=[{rsc, "rsc"}]},
Query1 = filter_empty(Query),
- R = parse_query(Query1, Context, Start),
- %% add any sort terms
- R1 = parse_sort(Query1, R),
- %% add default sorting
- add_order("-rsc.id", R1).
+ case parse_query(Query1, Context, Start) of
+ [] -> #search_result{};
+ #search_result{} = Result -> Result;
+ R ->
+ %% add any sort terms
+ R1 = parse_sort(Query1, R),
+ %% add default sorting
+ add_order("-rsc.id", R1)
+ end.
@@ -350,10 +354,9 @@ parse_query([{custompivot, Table}|Rest], Context, Result) ->
%% text=...
%% Perform a fulltext search
parse_query([{text, Text}|Rest], Context, Result) ->
- Text1 = z_string:trim(Text),
- case Text1 of
- [] ->
- parse_query(Rest, Context, Result);
+ case z_string:trim(Text) of
+ "id:"++ S -> mod_search:find_by_id(S, Context);
+ [] -> parse_query(Rest, Context, Result);
_ ->
TsQuery = mod_search:to_tsquery(Text, Context),
{QArg, Result1} = add_arg(TsQuery, Result),

0 comments on commit 9a2892e

Please sign in to comment.