 	END IF;
 	IF params->'lim' IS NOT NULL THEN lim:=params->>'lim'; END IF;
 	IF params->'ns' IS NOT NULL THEN  ns:=params->>'ns'; END IF;
-	IF params->'metaphone_len' IS NOT NULL THEN mlen:=params->>'metaphone_len'; END IF;
-
-	result := term1.search($1,'=',lim,ns,params); -- exact
+
+
+	result := term1.search_tab($1,'=',lim,ns,params); -- exact
 	IF (result->'error' IS NOT NULL) THEN  -- checks only the first
 		RETURN result;
 	ELSIF (result->'result'->>'n')::int >0 THEN
 		RETURN result;
 	END IF;
 
-	result := term1.search($1,'&',lim,ns,params); -- all words, any order
+	result := term1.search_tab($1,'&',lim,ns,params); -- all words, any order
 	IF (result->'result'->>'n')::int >0 THEN
 		RETURN result;
 	END IF;
-	result := term1.search($1,'p',lim,ns,params); -- prefix
+	result := term1.search_tab($1,'p',lim,ns,params); -- prefix
 	IF (result->'result'->>'n')::int >0 THEN
 		RETURN result;
 	END IF;
 
-	result := term1.search_metaphone($1,'=',lim,ns,mlen,params); -- exact metaphone
+	params := params || jsonb_build_object('metaphone',true); -- enforces metaphone
+	IF params->'metaphone_len' IS NOT NULL THEN mlen:=params->>'metaphone_len'; END IF;
+	
+	result := term1.search_tab($1,'=',lim,ns,mlen,params); -- exact metaphone
 	IF (result->'result'->>'n')::int >0 THEN
 		RETURN result;
 	END IF;
-	result := term1.search_metaphone($1,'&',lim,ns,mlen,params); -- all metaphones, any order
+	result := term1.search_tab($1,'&',lim,ns,mlen,params); -- all metaphones, any order
 	IF (result->'result'->>'n')::int >0 THEN
 		RETURN result;
 	END IF;
-	result := term1.search_metaphone($1,'p',lim,ns,mlen,params); -- prefix
+	result := term1.search_tab($1,'p',lim,ns,mlen,params); -- prefix
 	return result;
 END;
 $f$ LANGUAGE PLpgSQL IMMUTABLE;
+
+
 
 
 ---  ---  ---  ---  ---  ---  ---  ---  --- 

		ELSE 		term_lib.jrpc_ret( jsonb_agg(to_jsonb(t)), p_id )
		END
	INTO r
	FROM term1.search_tab(p) t;
	
	RETURN 	 r;
END;
$f$ LANGUAGE PLpgSQL IMMUTABLE;

---

CREATE FUNCTION term1.search2c_tab(
	--
	-- term1.search_tab() complement to reduce the putput to only canonic forms.
	--
	JSONB -- 1. all input parameters. 
) RETURNS TABLE(id int, term varchar, score int) AS $f$
	SELECT 	max( COALESCE(s.fk_canonic,s.id) ) AS cid,   -- the s.id is the canonic when c.term is null
		COALESCE(c.term,s.term) AS cterm,           -- the s.term is the canonic when c.term is null
		max(s.score) as score
	FROM term1.search_tab($1) s LEFT JOIN term1.term_canonic c ON c.id=s.fk_canonic
	GROUP BY cterm
	ORDER BY score DESC, cterm;
$f$ LANGUAGE SQL IMMUTABLE;

CREATE FUNCTION term1.search2c(JSONB) RETURNS JSONB AS $f$
	-- Wrap function for term1.search2c_tab(), returning standard JSON-RPC.
	SELECT 	jsonb_agg( to_jsonb(t) ) 
	FROM term1.search2c_tab($1) t
$f$ LANGUAGE SQL IMMUTABLE;
 

CREATE FUNCTION term1.search_oldWrap( -- lixo
	--
	-- Overload term1.search(), wrap to enforce use of normal-search.
	--
	text,                      -- 1. query string (usual term or term-fragments)
	text DEFAULT '&', 	   -- 2. '=', '&','|' or free or like-search ('%' or 'p' for prefix)
	int DEFAULT 100,           -- 3. LIMIT
        smallint DEFAULT 1,        -- 4. Namespace
	jsonb DEFAULT NULL	   -- 5. Other parameters (overhiden)
) RETURNS JSONB AS $f$
	SELECT CASE WHEN $5 IS NULL THEN term1.search(oo) ELSE term1.search($5 || oo) END
	FROM (SELECT jsonb_build_object('qs',$1, 'op',$2, 'lim',$3, 'ns',$4, 'metaphone',false) as oo) t;
$f$ LANGUAGE SQL IMMUTABLE;


CREATE FUNCTION term1.search_metaphone(
--
-- Wrap to term1.search(), enforsing use of metaphone-search.
--
	text,                      -- 1. query string (usual term or term-fragments)
	text DEFAULT '&', 	   -- 2. '=', '&','|' or free or like-search ('%' or 'p' for prefix)
	int DEFAULT 100,           -- 3. LIMIT
        smallint DEFAULT 1,        -- 4. Namespace
	int DEFAULT 6,		   -- 5. p_metaphone_len
	jsonb DEFAULT NULL	   -- 6. Other parameters (overhiden)
) RETURNS JSONB AS $f$
	SELECT CASE WHEN $6 IS NULL THEN term1.search(oo) ELSE term1.search($6 || oo) END
	FROM (SELECT jsonb_build_object('qs',$1, 'op',$2, 'lim',$3, 'ns',$4, 'metaphone',true, 'p_metaphone_len',$5) as oo) t;
$f$ LANGUAGE SQL IMMUTABLE;


CREATE FUNCTION term1.find(
--
-- Find a term, or "nearest term", by an heuristic of search strategies. 
-- PS: only didactic illustration, it is not a good algorithm.

CREATE FUNCTION term1.input_ns() RETURNS TRIGGER AS $f$
BEGIN
	NEW.nsid := term_lib.nsmask(array[NEW.nscount]);
  	NEW.kx_regconf := term_lib.lang2regconf(NEW.lang);
	-- optional to CHECK: IF NEW.kx_regconf IS NULL THEN RAISE
  	IF NEW.fk_partOf IS NOT NULL THEN
		NEW.is_base := false;
	END IF; -- else nothing to say, can be both.
