2016-08-22 3 views
0

"이름", "설명", "준비"라는 이름의 열이있는 "레시피"테이블이 있습니다. 내가 한 전체 텍스트 검색을 사용하려면 다음PostgreSQL 9.5 Coalesce

ALTER TABLE recipes ADD COLUMN recipes_searchtext TSVECTOR; 

CREATE INDEX idx_recipes_searchtext_gin ON recipes USING GIN(recipes_searchtext); 
UPDATE recipes SET recipes_searchtext = 
setweight(to_tsvector('german',name), 'A') || 
setweight(to_tsvector('german',description), 'B') || 
setweight(to_tsvector('german',preparation), 'C'); 

내가 PostgreSQL의 새로운 해요,하지만 지금까지 내가 몇 가지 테스트 후 볼 수 있듯이이 나를 위해 잘 작동합니다.

하지만 NULL 값을 처리하기 위해 "COALESCE"에 대해 읽었습니다. 그래서 나는 시도 :

UPDATE recipes SET recipes_searchtext = 
setweight(to_tsvector('german',COALESCE(name), 'A')) || 
setweight(to_tsvector('german',COALESCE(description), 'B')) || 
setweight(to_tsvector('german',COALESCE(preparation), 'C')); 

오류 메시지에

누군가가 나에게 내가 뭘하는지 힌트를 줄시겠습니까 알 수없는

Funktion의 to_tsvector (알 수없는 문자가 변화를 초래 무엇을 잘못 되었습니까?

감사합니다.

+2

인 경우 'COALESCE (name)'가 적합하지 않은 경우 'Nothing'을 반환합니다. 유용하게 쓰려면 coalesce()는 하나 이상의 인수를 필요로합니다.이 경우에는 아마도'COALESCE (name, '')'입니다. [그러나 당신이 합병()을 필요로한다면 나는 여기에] – joop

답변

1

잘못된 위치에 괄호가있는 것 같습니다.

to_tsvector은 최대 2 개의 인수를 취하지 만 현재 가지고있는 방식은 3 개입니다.

이 그것을 수정해야합니다 :

UPDATE recipes SET recipes_searchtext = 
setweight(to_tsvector('german',COALESCE(name)), 'A') || 
setweight(to_tsvector('german',COALESCE(description)), 'B') || 
setweight(to_tsvector('german',COALESCE(preparation)), 'C'); 

그러나, 나는 당신의 질의 차이를 볼 수 없습니다. coalesce은 모든 인수가 null이면 null을 반환합니다. 다음과 같이 기본값을 제공 할 수 있습니다. coalesce(name, 'Nothing')namenull

+0

고맙습니다. 이게 잘 작동 해. – Dirk