- 카테고리:
문자열 및 이진 함수 (전체 텍스트 검색)
SEARCH¶
Searches character data (text) in specified columns from one or more tables, including fields in VARIANT, OBJECT, and ARRAY columns. A text analyzer breaks the text into tokens, which are discrete units of text, such as words or numbers. A default analyzer is applied if you don’t specify one.
이 함수를 사용하는 방법에 대한 자세한 내용은 전체 텍스트 검색 사용하기 섹션을 참조하십시오.
구문¶
SEARCH( <search_data>, '<search_string>'
[ , ANALYZER => '<analyzer_name>' ]
[ , SEARCH_MODE => { 'OR' | 'AND' | 'PHRASE' | 'EXACT' } ] )
필수 인자¶
search_data검색하려는 데이터는 문자열 리터럴, 열 이름 또는 VARIANT 열의 필드에 대한 경로 를 쉼표로 구분된 목록으로 표현됩니다. 검색 데이터는 단일 리터럴 문자열이 될 수도 있는데, 이는 함수를 테스트할 때 유용할 수 있습니다.
와일드카드 문자(
*)를 지정할 수 있는데, 여기서*는 함수 범위에 있는 모든 테이블에 있는 모든 적격 열로 확장됩니다. 적격 열은 VARCHAR(텍스트), VARIANT, ARRAY, OBJECT 데이터 타입을 갖는 열입니다. VARIANT, ARRAY 및 OBJECT 데이터는 검색을 위해 텍스트로 변환됩니다.함수에 와일드카드를 전달할 때 와일드카드를 테이블의 이름이나 별칭으로 한정할 수 있습니다. 예를 들어,
mytable이라는 테이블의 모든 열을 전달하려면 다음을 지정하십시오.(mytable.*)
필터링을 위해 ILIKE 및 EXCLUDE 키워드를 사용할 수도 있습니다.
ILIKE는 지정된 패턴과 일치하는 열 이름을 필터링합니다. 패턴은 하나만 허용됩니다. 예:
(* ILIKE 'col1%')
EXCLUDE는 지정된 열과 일치하지 않는 열 이름을 걸러냅니다. 예:
(* EXCLUDE col1) (* EXCLUDE (col1, col2))
이러한 키워드를 사용할 경우 한정자가 유효합니다. 다음 예에서는 ILIKE 키워드를 사용하여 테이블
mytable의 패턴col1%와 일치하는 모든 열을 필터링합니다.(mytable.* ILIKE 'col1%')
ILIKE 및 EXCLUDE 키워드는 단일 함수 호출에서 결합할 수 없습니다.
ILIKE 및 EXCLUDE 키워드에 대한 자세한 내용은 SELECT 의 “매개 변수” 섹션을 참조하십시오.
테이블을 조인하거나 UNION 세트 연산자를 사용함으로써 여러 테이블이 범위에 있는 경우 2개 이상의 테이블에서 열을 검색할 수 있습니다. 조인이나 UNION 쿼리의 출력에서 모든 열을 검색하려면 다음과 같이 정규화되지 않은
*와일드카드를 사용할 수 있습니다.SELECT * FROM t AS T1 JOIN t AS T2 USING (col1) WHERE SEARCH((*), 'string');
테이블을 조인할 때 특정 열을 검색하려면 열 이름(예:
table2.colname)을 한정해야 할 수도 있습니다. 다음과 같이 정규화된*와일드카드를 사용할 수도 있습니다.SELECT * FROM t AS T1 JOIN t AS T2 USING (col1) WHERE SEARCH((T2.*), 'string');
하지만 함수에 대해
*또는table.*을 두 번 이상 지정할 수 없습니다. 이전 조인 예에서는SEARCH((T1.*, T2.*), 'string')을 지정할 수 없습니다. 이 구문은 오류를 반환합니다.*,table.*또는 여러 항목이 나열된 경우search_data인자에 괄호가 필요합니다. 예:SEARCH((col1, col2, col3), 'string') SEARCH((t1.*), 'string') SEARCH((*), 'string')
If parentheses aren’t used to separate multiple items, commas are parsed as separators between function arguments.
예상되는 오류 사례의 예 섹션도 참조하십시오.
열 이름, 콜론이나 점, 점으로 구분된 하위 필드를 지정하여 VARIANT 데이터의 필드를 검색할 수 있습니다. 예를 들어
colname:fieldname.subfieldname과 같습니다. 이러한 열에 필드를 지정하는 방법에 대한 자세한 내용은 반정형 데이터 탐색하기 섹션을 참조하십시오.'search_string'하나 이상의 검색어가 포함된 VARCHAR 문자열입니다. 이 인자는 리터럴 문자열이어야 하며, 열 이름은 지원되지 않습니다. 전체 문자열을 작은따옴표 한 쌍으로 묶어 지정합니다. 개별 용어나 문구를 따옴표로 묶지 지정하지는 마십시오. 예를 들어 다음을 사용하십시오.
'blue red green'다음을 사용하지 마십시오.
'blue' 'red' 'green'The list of terms can be disjunctive or conjunctive when
ORorANDis set for the SEARCH_MODE argument. However, when the'NO_OP_ANALYZER'is used, the query string is matched exactly as it is, with no tokenization and no disjunctive or conjunctive semantics.Searches aren’t case sensitive, except when the
'NO_OP_ANALYZER'is used, so a search for the term'ONCE'against the string'Once upon a time'returns TRUE.SEARCH_MODE 인자에 대해
OR또는AND`가 설정되면 검색된 데이터에서 검색어의 존재 여부와 관련하여 검색어의 순서는 중요하지 않습니다. SEARCH_MODE 인자에 대해 :code:`PHRASE또는 :code:`EXACT`가 설정되면 검색어의 순서는 검색된 데이터와 정확히 일치해야 합니다.
선택적 인자¶
ANALYZER => 'analyzer_name'텍스트 분석기의 이름입니다. 이름은 작은따옴표로 묶어야 합니다.
The analyzer breaks the search terms (and the text from the column being searched) into tokens. The matching semantics (disjunctive, conjunctive, phrase, or exact) for tokens extracted from the search string and tokens extracted from the columns or fields being searched depends on the value of the SEARCH_MODE argument.
The analyzer tokenizes a string by breaking it where it finds certain delimiters. These delimiters aren’t included in the resulting tokens, and empty tokens aren’t extracted.
이 매개 변수는 다음 값 중 하나를 허용합니다.
'DEFAULT_ANALYZER'- Breaks text into tokens based on the following delimiters:문자
유니코드 코드
설명
U+0020공백
[U+005B왼쪽 대괄호
]U+005D오른쪽 대괄호
;U+003B세미콜론
<U+003C보다 작음 기호
>U+003E보다 큼 기호
(U+0028왼쪽 괄호
)U+0029오른쪽 괄호
{U+007B왼쪽 중괄호
}U+007D오른쪽 중괄호
|U+007C세로선
!U+0021느낌표
,U+002C쉼표
'U+0027아포스트로피
"U+0022따옴표
*U+002A별표
&U+0026앰퍼샌드
?U+003F물음표
+U+002B더하기 기호
/U+002F슬래시
:U+003A콜론
=U+003D등호
@U+0040@ 기호
.U+002E마침표
-U+002D하이픈
$U+0024달러 기호
%U+0025퍼센트 기호
\U+005C백슬래시
_U+005F밑줄
\nU+000A줄 바꿈
\rU+000D캐리지 리턴
\tU+0009가로 탭
'UNICODE_ANALYZER'- Tokenizes based on Unicode segmentation rules that treat spaces and certain punctuation characters as delimiters. These internal rules are designed for natural language searches in many different languages. For example, the default analyzer treats periods in IP addresses and apostrophes in contractions as delimiters, but the Unicode analyzer doesn’t. See 분석기를 사용하여 검색 동작 조정하기.유니코드 텍스트 분할 알고리즘에 대한 자세한 내용은 https://unicode.org/reports/tr29/를 참조하십시오.
'NO_OP_ANALYZER'- Tokenizes neither the data nor the query string. A search term must exactly match the full text in a column or field, including case sensitivity; otherwise, the SEARCH function returns FALSE. Even if the query string looks like it contains multiple tokens — for example,'sky blue'— the column or field must equal the entire query string exactly. In this case, only'sky blue'is a match;'sky'and'blue'aren’t matches.
다양한 분석기의 동작에 대한 자세한 내용은 검색어 토큰화 방식 섹션을 참조하십시오.
SEARCH_MODE => { 'OR' | 'AND' | 'PHRASE' | 'EXACT' }The semantics used by the search. Set this argument to one of the following values:
'OR'- The function uses disjunctive semantics. There is a match if any of the tokens extracted from the columns or fields being searched match any of the tokens extracted from the search string. For example, if thesearch_stringvalue is'blue red green', the function returns TRUE for a row that containsblueORredORgreenin any of the columns or fields being searched.'AND'- The function uses conjunctive semantics. There is a match if the tokens extracted from at least one of the columns or fields being searched matches all of the tokens extracted from the search string. The matching tokens must all be in one column or field; they can’t be spread across multiple columns or fields. For example, if thesearch_stringvalue is'blue red green', the function returns TRUE for a row that containsblueANDredANDgreenin at least one of the columns or fields being searched.'PHRASE'- 이 함수는 구문 일치 의미 체계를 사용합니다. 검색 중인 열 또는 필드 중 하나 이상*에서 추출한 토큰이 토큰의 순서 및 인접성을 포함하여 검색 문자열에서 추출된 *모든 토큰과 일치하는 경우 일치하는 항목이 있는 것입니다.일치하는 의미 체계는 다음과 같은 차이점을 제외하면 연결형 의미 체계와 동일합니다.
'EXACT'- 이 함수는 정확한 일치 의미 체계를 사용합니다. 검색 중인 열 또는 필드 중 하나 이상*에서 추출한 토큰이 구분 기호를 포함하여 검색 문자열에서 추출된 *모든 토큰과 정확히 일치하는 경우 일치하는 항목이 있는 것입니다.일치 규칙은 다음과 같은 차이점을 제외하면 구문 검색 의미 체계와 동일합니다.
토큰 사이의 구분 기호 문자열은 정확히 일치해야 합니다. 예를 들어,
search_string값이'blue,red,green'`이면 이 함수는 검색 중인 열 또는 필드 중 하나 이상에서 :code:`blue,red,green`을 포함하는 행에 대해 TRUE를 반환합니다. 이 함수는 :code:`blue|red|green또는 :code:`blue, red, green`와 같은 변형에 FALSE를 반환합니다.구분 기호가
search_string값의 첫 번째 또는 마지막 문자인 경우 구분 기호는 일치를 위해 문자처럼 처리됩니다. 따라서 첫 번째 및 마지막 구분 기호의 왼쪽과 오른쪽에 있는 구분 기호는 일치 결과를 가져올 수 있습니다. 예를 들어,search_string값이'[blue]'`이면 이 함수는 :code:`foo [blue] bar,[[blue]]및=[blue].`에 대해 TRUE를 반환하지만 :code:`(blue)또는 :code:`foo blue bar`에 대해서는 그렇지 않습니다.
모든 검색 모드에서 문자열은 왼쪽과 오른쪽에서 구분 기호로 구분해야 합니다. 예를 들어,
search_string값이'blue,red,green'`이면 이 함수는 :code:-blue,red,green;`에 대해 TRUE를 반환합니다.darkblue,red,green또는 :code:`blue,red,greenish`에 대해 이 함수는 FALSE를 반환합니다.UNICODE_ANALYZER`를 사용하는 경우 정확한 일치 의미 체계는 지원되지 않습니다. 정확한 일치 의미 체계에서 :code:`DEFAULT_ANALYZER또는 :code:`NO_OP_ANALYZER`를 사용할 수 있지만 일반적으로 이러한 검색 의미 체계는 :code:`DEFAULT_ANALYZER`에 가장 적합합니다.:code:`DEFAULT_ANALYZER`에서 정확한 일치 의미 체계를 사용하는 검색은 다음과 같은 방식으로 동등 검색 또는 :code:`NO_OP_ANALYZER`를 사용하는 전체 텍스트 검색과는 다르게 동작합니다.
동등 검색은 열 값이 조건자(대문자 및 소문자 포함)와 정확히 동일하고 검색 문자열의 발생 주위에 추가 텍스트가 없는 행과 일치합니다.
:code:`NO_OP_ANALYZER`를 사용한 전체 텍스트 검색은 대문자와 소문자를 구분하고 추가 텍스트를 허용하지 않는다는 점에서 동등 검색과 유사합니다.
:code:`DEFAULT_ANALYZER`와 정확한 일치 의미 체계를 사용한 검색은 열 값을 토큰화합니다. 토큰 구분 기호로 구분된 경우 검색 문자열 발생의 왼쪽과 오른쪽에 추가 토큰을 허용합니다. 검색에서는 대소문자를 구분하지 않습니다.
기본값:
'OR'
반환¶
BOOLEAN을 반환합니다.
search_string토큰이 SEARCH_MODE 인자에 지정된 시맨틱에 따라search_data토큰과 일치하는 경우 값은 TRUE 입니다.이러한 인자 중 하나가 NULL이면 NULL을 반환합니다.
그렇지 않으면 FALSE를 반환합니다.
사용법 노트¶
SEARCH 함수는 VARCHAR, VARIANT, ARRAY 및 OBJECT 데이터에 대해서만 작동합니다.
search_data인자에 이러한 데이터 타입의 데이터가 포함되지 않은 경우 함수는 오류를 반환합니다.search_data인자에 지원되는 데이터 타입과 지원되지 않는 데이터 타입의 데이터가 모두 포함된 경우, 함수는 지원되는 데이터 타입의 데이터를 검색하고 지원되지 않는 데이터 타입의 데이터는 자동으로 무시합니다. 예는 예상되는 오류 사례의 예 을 참조하십시오.ALTER TABLE 명령을 사용하여 SEARCH 함수 호출의 대상이 되는 열에 FULL_TEXT 검색 최적화를 추가할 수 있습니다. 예:
ALTER TABLE lines ADD SEARCH OPTIMIZATION ON FULL_TEXT(play, character, line);
자세한 내용은 FULL_TEXT 검색 최적화 사용하기 섹션을 참조하십시오.
검색어 토큰화 방식¶
다음 표에는 사용되는 분석기에서 적용하는 규칙에 따라 입력 검색어가 토큰으로 분할되는 방식을 보여주는 몇 가지 예가 나와 있습니다. 표에서 쉼표는 토큰이 분할된 위치를 나타냅니다(있는 경우).
검색어 |
토큰: DEFAULT_ANALYZER |
토큰: UNICODE_ANALYZER |
NO_OP_ANALYZER(분할되지 않음) |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
예¶
The following examples show different ways to use the SEARCH function, starting with simple usage and progressing to more complex use cases:
여러 예에서 쿼리를 실행하려면 먼저 SEARCH 함수에 대한 샘플 데이터를 만듭니다.
리터럴에 대한 일치¶
SEARCH 함수의 가장 간단한 예는 문자열 리터럴에서 TRUE 또는 FALSE를 테스트하는 것입니다. 첫 번째 예에서는 첫 번째 인자와 두 번째 인자의 리터럴이 일치하므로 TRUE 를 반환하는데, 비교 시 대/소문자를 구분하지 않습니다.
SELECT SEARCH('king','KING');
+-----------------------------+
| SEARCH('KING','KING') |
|-----------------------------|
| True |
+-----------------------------+
두 번째 예에서는 토큰 32 가 첫 번째 인자에 지정된 리터럴 5.1.33 에 나타나지 않으므로 FALSE 를 반환합니다.
SELECT SEARCH('5.1.33','32');
+-----------------------------+
| SEARCH('5.1.33','32') |
|-----------------------------|
| False |
+-----------------------------+
열 참조에 대한 일치¶
이 예에서는 테이블의 열을 첫 번째 인자로 사용합니다. 검색어 중 하나(king)가 character 열에 존재하므로 이 함수는 TRUE를 반환합니다. SEARCH_MODE 인자의 기본값이 'OR' 이므로 용어 목록은 분리형입니다. (이 예와 뒤에 나오는 몇몇 예에서 사용되는 테이블에 대한 자세한 내용은 Creating the sample data for SEARCH 섹션을 참조하십시오.)
SELECT SEARCH(character, 'king queen'), character
FROM lines
WHERE line_id=4;
+---------------------------------+---------------+
| SEARCH(CHARACTER, 'KING QUEEN') | CHARACTER |
|---------------------------------+---------------|
| True | KING HENRY IV |
+---------------------------------+---------------+
다음 예제는 이전 예제와 유사하지만 SEARCH_MODE 인자가 'AND' 로 설정되어 있으므로 검색 시맨틱이 접속형입니다. 검색어 중 하나(king)만 character 열에 존재하므로 이 함수는 FALSE 를 반환합니다. queen 이라는 용어가 검색 데이터에 나타나지 않습니다.
SELECT SEARCH(character, 'king queen', SEARCH_MODE => 'AND'), character
FROM lines
WHERE line_id=4;
+-------------------------------------------------------+---------------+
| SEARCH(CHARACTER, 'KING QUEEN', SEARCH_MODE => 'AND') | CHARACTER |
|-------------------------------------------------------+---------------|
| False | KING HENRY IV |
+-------------------------------------------------------+---------------+
한 열에 대한 WHERE 절 검색¶
다음 쿼리에서는 SEARCH 함수를 사용하여 line 열에 wherefore 라는 단어가 포함된 행을 찾습니다.
SELECT *
FROM lines
WHERE SEARCH(line, 'wherefore')
ORDER BY character LIMIT 5;
+---------+----------------------+------------+----------------+-----------+-----------------------------------------------------+
| LINE_ID | PLAY | SPEECH_NUM | ACT_SCENE_LINE | CHARACTER | LINE |
|---------+----------------------+------------+----------------+-----------+-----------------------------------------------------|
| 100109 | Troilus and Cressida | 31 | 2.1.53 | ACHILLES | Why, how now, Ajax! wherefore do you thus? How now, |
| 16448 | As You Like It | 2 | 2.3.6 | ADAM | And wherefore are you gentle, strong and valiant? |
| 24055 | The Comedy of Errors | 14 | 5.1.41 | AEMELIA | Be quiet, people. Wherefore throng you hither? |
| 99330 | Troilus and Cressida | 30 | 1.1.102 | AENEAS | How now, Prince Troilus! wherefore not afield? |
| 92454 | The Tempest | 150 | 2.1.343 | ALONSO | Wherefore this ghastly looking? |
+---------+----------------------+------------+----------------+-----------+-----------------------------------------------------+
여러 열에 대한 WHERE 절 검색¶
다음 쿼리에서는 SEARCH 함수를 사용하여 play 열, character 열 또는 두 열 모두에 king 이라는 단어가 포함된 행을 찾습니다. 첫 번째 인자에는 괄호가 필요합니다.
SELECT play, character
FROM lines
WHERE SEARCH((play, character), 'king')
ORDER BY play, character LIMIT 10;
+---------------------------+-----------------+
| PLAY | CHARACTER |
|---------------------------+-----------------|
| All's Well That Ends Well | KING |
| Hamlet | KING CLAUDIUS |
| Hamlet | KING CLAUDIUS |
| Henry IV Part 1 | KING HENRY IV |
| Henry IV Part 1 | KING HENRY IV |
| King John | CHATILLON |
| King John | KING JOHN |
| King Lear | GLOUCESTER |
| King Lear | KENT |
| Richard II | KING RICHARD II |
+---------------------------+-----------------+
테이블의 모든 적격 열에 대한 와일드카드 검색¶
이 예에서 보듯이 * 문자(또는 table.*)를 SEARCH 함수의 첫 번째 인자로 사용할 수 있습니다. 검색은 선택하는 테이블의 모든 적격 열(이 경우에는 lines 테이블)에서 수행됩니다.
lines 테이블에는 검색 기능에서 지원되는 데이터 타입을 가진 열이 4개 있습니다. 결과는 검색된 4개 열 중 하나 이상에 king 이 나타나는 행으로 구성됩니다. 이러한 열 중 하나인 act_scene_line 의 경우 함수가 일치 항목을 찾지 못했지만 다른 세 열에는 모두 일치 항목이 있습니다. SEARCH_MODE 인자의 기본값은 'OR' 입니다.
SELECT play, character, line, act_scene_line
FROM lines
WHERE SEARCH((lines.*), 'king')
ORDER BY act_scene_line LIMIT 10;
+-----------------+-----------------+----------------------------------------------------+----------------+
| PLAY | CHARACTER | LINE | ACT_SCENE_LINE |
|-----------------+-----------------+----------------------------------------------------+----------------|
| Pericles | LODOVICO | This king unto him took a fere, | 1.0.21 |
| Richard II | KING RICHARD II | Old John of Gaunt, time-honour'd Lancaster, | 1.1.1 |
| Henry VI Part 3 | WARWICK | I wonder how the king escaped our hands. | 1.1.1 |
| King John | KING JOHN | Now, say, Chatillon, what would France with us? | 1.1.1 |
| King Lear | KENT | I thought the king had more affected the Duke of | 1.1.1 |
| Henry IV Part 1 | KING HENRY IV | So shaken as we are, so wan with care, | 1.1.1 |
| Henry IV Part 1 | KING HENRY IV | Which, like the meteors of a troubled heaven, | 1.1.10 |
| King Lear | GLOUCESTER | so often blushed to acknowledge him, that now I am | 1.1.10 |
| Cymbeline | First Gentleman | Is outward sorrow, though I think the king | 1.1.10 |
| King John | CHATILLON | To this fair island and the territories, | 1.1.10 |
+-----------------+-----------------+----------------------------------------------------+----------------+
필터링에 ILIKE 및 EXCLUDE 키워드를 사용할 수도 있습니다. 이러한 키워드에 대한 자세한 내용은 SELECT 섹션을 참조하십시오.
이 검색에서는 ILIKE 키워드를 사용하여 문자열 line 으로 끝나는 열에서만 검색합니다. 따라서 이 함수는 line 및 act_scene_line 열에서 검색합니다.
SELECT play, character, line, act_scene_line
FROM lines
WHERE SEARCH((lines.* ILIKE '%line'), 'king')
ORDER BY act_scene_line LIMIT 10;
+-----------------+-----------------+--------------------------------------------------+----------------+
| PLAY | CHARACTER | LINE | ACT_SCENE_LINE |
|-----------------+-----------------+--------------------------------------------------+----------------|
| Pericles | LODOVICO | This king unto him took a fere, | 1.0.21 |
| Henry VI Part 3 | WARWICK | I wonder how the king escaped our hands. | 1.1.1 |
| King Lear | KENT | I thought the king had more affected the Duke of | 1.1.1 |
| Cymbeline | First Gentleman | Is outward sorrow, though I think the king | 1.1.10 |
+-----------------+-----------------+--------------------------------------------------+----------------+
이 검색에서는 EXCLUDE 키워드를 사용하므로 함수가 character 열의 데이터를 검색하지 않습니다.
SELECT play, character, line, act_scene_line
FROM lines
WHERE SEARCH((lines.* EXCLUDE character), 'king')
ORDER BY act_scene_line LIMIT 10;
+-----------------+-----------------+----------------------------------------------------+----------------+
| PLAY | CHARACTER | LINE | ACT_SCENE_LINE |
|-----------------+-----------------+----------------------------------------------------+----------------|
| Pericles | LODOVICO | This king unto him took a fere, | 1.0.21 |
| Henry VI Part 3 | WARWICK | I wonder how the king escaped our hands. | 1.1.1 |
| King John | KING JOHN | Now, say, Chatillon, what would France with us? | 1.1.1 |
| King Lear | KENT | I thought the king had more affected the Duke of | 1.1.1 |
| Cymbeline | First Gentleman | Is outward sorrow, though I think the king | 1.1.10 |
| King Lear | GLOUCESTER | so often blushed to acknowledge him, that now I am | 1.1.10 |
| King John | CHATILLON | To this fair island and the territories, | 1.1.10 |
+-----------------+-----------------+----------------------------------------------------+----------------+
SELECT 목록에서의 와일드카드 검색¶
다음 예에서 보듯이, SELECT 목록에서 * 문자(또는 table.*)를 사용할 수 있습니다.
다음 검색은 선택하는 테이블의 모든 적격 열(이 경우에는 lines 테이블)에서 수행됩니다. 검색된 4개 열 중 하나 이상에 king 이 나타나면 검색 결과에 TRUE 가 반환됩니다. SEARCH_MODE 인자의 기본값은 'OR' 입니다.
SELECT SEARCH((*), 'king') result, *
FROM lines
ORDER BY act_scene_line LIMIT 10;
+--------+---------+---------------------------+------------+----------------+-----------------+--------------------------------------------------------+
| RESULT | LINE_ID | PLAY | SPEECH_NUM | ACT_SCENE_LINE | CHARACTER | LINE |
|--------+---------+---------------------------+------------+----------------+-----------------+--------------------------------------------------------|
| True | 75787 | Pericles | 178 | 1.0.21 | LODOVICO | This king unto him took a fere, |
| True | 43494 | King John | 1 | 1.1.1 | KING JOHN | Now, say, Chatillon, what would France with us? |
| True | 49031 | King Lear | 1 | 1.1.1 | KENT | I thought the king had more affected the Duke of |
| True | 78407 | Richard II | 1 | 1.1.1 | KING RICHARD II | Old John of Gaunt, time-honour'd Lancaster, |
| False | 67000 | A Midsummer Night's Dream | 1 | 1.1.1 | THESEUS | Now, fair Hippolyta, our nuptial hour |
| True | 4 | Henry IV Part 1 | 1 | 1.1.1 | KING HENRY IV | So shaken as we are, so wan with care, |
| False | 12664 | All's Well That Ends Well | 1 | 1.1.1 | COUNTESS | In delivering my son from me, I bury a second husband. |
| True | 9526 | Henry VI Part 3 | 1 | 1.1.1 | WARWICK | I wonder how the king escaped our hands. |
| False | 52797 | Love's Labour's Lost | 1 | 1.1.1 | FERDINAND | Let fame, that all hunt after in their lives, |
| True | 28487 | Cymbeline | 3 | 1.1.10 | First Gentleman | Is outward sorrow, though I think the king |
+--------+---------+---------------------------+------------+----------------+-----------------+--------------------------------------------------------+
필터링에 ILIKE 및 EXCLUDE 키워드를 사용할 수도 있습니다. 이러한 키워드에 대한 자세한 내용은 SELECT 섹션을 참조하십시오.
이 검색에서는 ILIKE 키워드를 사용하여 문자열 line 으로 끝나는 열에서만 검색합니다. 따라서 이 함수는 line 및 act_scene_line 열에서 검색합니다.
SELECT SEARCH(* ILIKE '%line', 'king') result, play, character, line
FROM lines
ORDER BY act_scene_line LIMIT 10;
+--------+---------------------------+-----------------+--------------------------------------------------------+
| RESULT | PLAY | CHARACTER | LINE |
|--------+---------------------------+-----------------+--------------------------------------------------------|
| True | Pericles | LODOVICO | This king unto him took a fere, |
| False | King John | KING JOHN | Now, say, Chatillon, what would France with us? |
| True | King Lear | KENT | I thought the king had more affected the Duke of |
| False | Richard II | KING RICHARD II | Old John of Gaunt, time-honour'd Lancaster, |
| False | A Midsummer Night's Dream | THESEUS | Now, fair Hippolyta, our nuptial hour |
| False | Henry IV Part 1 | KING HENRY IV | So shaken as we are, so wan with care, |
| False | All's Well That Ends Well | COUNTESS | In delivering my son from me, I bury a second husband. |
| True | Henry VI Part 3 | WARWICK | I wonder how the king escaped our hands. |
| False | Love's Labour's Lost | FERDINAND | Let fame, that all hunt after in their lives, |
| True | Cymbeline | First Gentleman | Is outward sorrow, though I think the king |
+--------+---------------------------+-----------------+--------------------------------------------------------+
이 검색에서는 EXCLUDE 키워드를 사용하므로 함수가 play 또는 line 열의 데이터를 검색하지 않습니다.
SELECT SEARCH(* EXCLUDE (play, line), 'king') result, play, character, line
FROM lines
ORDER BY act_scene_line LIMIT 10;
+--------+---------------------------+-----------------+--------------------------------------------------------+
| RESULT | PLAY | CHARACTER | LINE |
|--------+---------------------------+-----------------+--------------------------------------------------------|
| False | Pericles | LODOVICO | This king unto him took a fere, |
| True | King John | KING JOHN | Now, say, Chatillon, what would France with us? |
| False | King Lear | KENT | I thought the king had more affected the Duke of |
| True | Richard II | KING RICHARD II | Old John of Gaunt, time-honour'd Lancaster, |
| False | A Midsummer Night's Dream | THESEUS | Now, fair Hippolyta, our nuptial hour |
| True | Henry IV Part 1 | KING HENRY IV | So shaken as we are, so wan with care, |
| False | All's Well That Ends Well | COUNTESS | In delivering my son from me, I bury a second husband. |
| False | Henry VI Part 3 | WARWICK | I wonder how the king escaped our hands. |
| False | Love's Labour's Lost | FERDINAND | Let fame, that all hunt after in their lives, |
| False | Cymbeline | First Gentleman | Is outward sorrow, though I think the king |
+--------+---------------------------+-----------------+--------------------------------------------------------+
조인된 테이블의 적격 열에 대한 와일드카드 검색¶
이 예에서는 자동차 모델에 대한 정보가 포함된 2개의 작은 테이블을 사용합니다. 문자 열이 테이블 t1 에는 2개 있고, 테이블 t2 에는 3개 있습니다. 다음과 같이 테이블을 만들고 로드할 수 있습니다.
CREATE OR REPLACE TABLE t1 (col1 INT, col2 VARCHAR(20), col3 VARCHAR(20));
INSERT INTO t1 VALUES
(1,'Mini','Cooper'),
(2,'Mini','Cooper S'),
(3,'Mini','Countryman'),
(4,'Mini','Countryman S');
CREATE OR REPLACE TABLE t2 (col1 INT, col2 VARCHAR(20), col3 VARCHAR(20), col4 VARCHAR(20));
INSERT INTO t2 VALUES
(1,'Mini','Cooper', 'Convertible'),
(2,'Mini','Cooper S', 'Convertible'),
(3,'Mini','Countryman SE','ALL4'),
(4,'Mini','Countryman S','ALL4');
t1.* 및 t2.* 에 대한 검색을 고려할 때 다음 두 쿼리의 결과는 서로 다릅니다. t1 에서는 2개의 열만 검색에 적합하지만, t2 에서는 3개의 열이 적합합니다.
SELECT * FROM t1 JOIN t2 USING(col1)
WHERE SEARCH((t1.*),'s all4');
+------+------+--------------+------+--------------+-------------+
| COL1 | COL2 | COL3 | COL2 | COL3 | COL4 |
|------+------+--------------+------+--------------+-------------|
| 2 | Mini | Cooper S | Mini | Cooper S | Convertible |
| 4 | Mini | Countryman S | Mini | Countryman S | ALL4 |
+------+------+--------------+------+--------------+-------------+
SELECT * FROM t1 JOIN t2 USING(col1)
WHERE SEARCH((t2.*),'s all4');
+------+------+--------------+------+---------------+-------------+
| COL1 | COL2 | COL3 | COL2 | COL3 | COL4 |
|------+------+--------------+------+---------------+-------------|
| 2 | Mini | Cooper S | Mini | Cooper S | Convertible |
| 3 | Mini | Countryman | Mini | Countryman SE | ALL4 |
| 4 | Mini | Countryman S | Mini | Countryman S | ALL4 |
+------+------+--------------+------+---------------+-------------+
UNION 하위 쿼리 출력에 대한 와일드카드 검색¶
다음 예에서는 이전 예와 동일한 2개의 테이블을 사용합니다. 이 경우, 검색은 하위 쿼리의 결과로 반환되는 테이블인 t3 의 모든 적격 열에 적용됩니다. 하위 쿼리는 t1 및 t2 (5개 행)에서 처음 3개 열의 UNION을 계산합니다. 이 검색에서는 UNION 결과에서 일치하는 2개 행이 반환됩니다.
SELECT *
FROM (
SELECT col1, col2, col3 FROM t1
UNION
SELECT col1, col2, col3 FROM t2
) AS T3
WHERE SEARCH((T3.*),'s');
+------+------+--------------+
| COL1 | COL2 | COL3 |
|------+------+--------------|
| 2 | Mini | Cooper S |
| 4 | Mini | Countryman S |
+------+------+--------------+
여러 검색 문자열과 일치하는 행 찾기¶
다음 예에서는 SEARCH_MODE 인자를 사용하여 두 검색어가 같은 열에 함께 있을 때 일치하는 항목을 찾는 접속형 시맨틱을 지정합니다. 접속형 시맨틱을 사용하려면 SEARCH_MODE 인자를 'AND' 로 설정합니다.
SELECT act_scene_line, character, line
FROM lines
WHERE SEARCH(line, 'Rosencrantz Guildenstern', SEARCH_MODE => 'AND')
AND act_scene_line IS NOT NULL;
+----------------+------------------+-----------------------------------------------------------+
| ACT_SCENE_LINE | CHARACTER | LINE |
|----------------+------------------+-----------------------------------------------------------|
| 2.2.1 | KING CLAUDIUS | Welcome, dear Rosencrantz and Guildenstern! |
| 2.2.35 | KING CLAUDIUS | Thanks, Rosencrantz and gentle Guildenstern. |
| 2.2.36 | QUEEN GERTRUDE | Thanks, Guildenstern and gentle Rosencrantz: |
| 2.2.241 | HAMLET | Guildenstern? Ah, Rosencrantz! Good lads, how do ye both? |
| 4.6.27 | HORATIO | where I am. Rosencrantz and Guildenstern hold their |
| 5.2.60 | HORATIO | So Guildenstern and Rosencrantz go to't. |
| 5.2.389 | First Ambassador | That Rosencrantz and Guildenstern are dead: |
+----------------+------------------+-----------------------------------------------------------+
접속사 시맨틱을 사용할 때는 같은 열에 있는 두 검색어에 모두 일치하는 항목이 있어야 합니다. 예를 들어 다음 쿼리는 KING 및 Rosencrantz 라는 용어가 검색 데이터의 어떤 행에서도 같은 열에 나타나지 않기 때문에 아무런 결과도 반환하지 않습니다.
SELECT act_scene_line, character, line
FROM lines
WHERE SEARCH(line, 'KING Rosencrantz', SEARCH_MODE => 'AND')
AND act_scene_line IS NOT NULL;
+----------------+-----------+------+
| ACT_SCENE_LINE | CHARACTER | LINE |
|----------------+-----------+------|
+----------------+-----------+------+
SEARCH_MODE 인자를 'OR' 로 설정하여 분리형 시맨틱(기본값)을 사용하는 유사한 쿼리는 검색 데이터에서 일치하는 항목을 찾습니다.
SELECT act_scene_line, character, line
FROM lines
WHERE SEARCH(line, 'KING Rosencrantz', SEARCH_MODE => 'OR')
AND act_scene_line IS NOT NULL;
+----------------+------------------+-----------------------------------------------------------+
| ACT_SCENE_LINE | CHARACTER | LINE |
|----------------+------------------+-----------------------------------------------------------|
| 1.1.1 | WARWICK | I wonder how the king escaped our hands. |
| 1.1.10 | First Gentleman | Is outward sorrow, though I think the king |
| 2.2.1 | KING CLAUDIUS | Welcome, dear Rosencrantz and Guildenstern! |
| 2.2.35 | KING CLAUDIUS | Thanks, Rosencrantz and gentle Guildenstern. |
| 2.2.36 | QUEEN GERTRUDE | Thanks, Guildenstern and gentle Rosencrantz: |
| 2.2.241 | HAMLET | Guildenstern? Ah, Rosencrantz! Good lads, how do ye both? |
| 4.6.27 | HORATIO | where I am. Rosencrantz and Guildenstern hold their |
| 5.2.60 | HORATIO | So Guildenstern and Rosencrantz go to't. |
| 5.2.389 | First Ambassador | That Rosencrantz and Guildenstern are dead: |
| 1.1.1 | KENT | I thought the king had more affected the Duke of |
| 1.0.21 | LODOVICO | This king unto him took a fere, |
+----------------+------------------+-----------------------------------------------------------+
구문 일치 및 정확한 일치 의미 체계를 사용하여 행 찾기¶
유사하지만 약간 다른 사용 사례에 구문 일치 및 정확히 일치 의미 체계를 사용할 수 있습니다.
단어와 단어의 순서가 정확히 일치해야 하지만 구분 기호와 단어 사이의 간격에 차이가 있을 수 있는 경우 구문 일치 의미 체계를 사용합니다. 구문 일치 의미 체계를 사용하려면 SEARCH_MODE 인자를 :code:`’PHRASE’`로 설정하세요.
단어, 단어 순서, 단어 사이의 구분 기호, 단어 사이의 간격이 정확히 일치해야 하는 경우 정확한 일치 의미 체계를 사용합니다. 정확한 일치 의미 체계를 사용하려면 SEARCH_MODE 인자를 :code:`’EXACT’`로 설정하세요.
다음 예에서는 구문 일치 의미 체계를 사용하여 더 긴 텍스트 문자열에서 정확히 일치하는 텍스트를 찾지만, 검색 텍스트에는 구분 기호가 다르고 단어 사이에 추가 공백이 있습니다.
SELECT act_scene_line, character, line
FROM lines
WHERE SEARCH(line, 'Why - how now: Ajax!', SEARCH_MODE => 'PHRASE');
+----------------+-----------+-----------------------------------------------------+
| ACT_SCENE_LINE | CHARACTER | LINE |
|----------------+-----------+-----------------------------------------------------|
| 2.1.53 | ACHILLES | Why, how now, Ajax! wherefore do you thus? How now, |
+----------------+-----------+-----------------------------------------------------+
다음 예는 정확한 일치 의미 체계를 사용하여 더 긴 텍스트 문자열에서 정확히 일치하는 텍스트를 찾는 것을 제외하고는 이전 예와 동일합니다.
SELECT act_scene_line, character, line
FROM lines
WHERE SEARCH(line, 'Why, how now, Ajax!', SEARCH_MODE => 'EXACT');
+----------------+-----------+-----------------------------------------------------+
| ACT_SCENE_LINE | CHARACTER | LINE |
|----------------+-----------+-----------------------------------------------------|
| 2.1.53 | ACHILLES | Why, how now, Ajax! wherefore do you thus? How now, |
+----------------+-----------+-----------------------------------------------------+
구문 일치 및 정확한 일치 의미 체계의 일반적인 사용 사례에는 이메일 주소, URLs 및 전화 번호 찾기가 포함됩니다. 다음 예에서는 샘플 데이터의 한 행이 있는 테이블을 만듭니다.
CREATE OR REPLACE TABLE phrase_exact_search_samples (
email VARCHAR,
url VARCHAR,
phone VARCHAR);
INSERT INTO phrase_exact_search_samples VALUES (
'john.robert.doe@mycompany.com',
'http://mycompany.com/product/id-12345.67',
'800-555-0100');
The following example runs a query that uses conjunctive semantics to search the email data by setting the SEARCH_MODE
argument to 'AND' in the first search, phrase-match semantics in the second search, and exact-match semantics
in the third search:
SELECT email AS search_data,
SEARCH(email, 'doe.john.robert@mycompany.com', SEARCH_MODE => 'AND') AS conjunctive_search,
SEARCH(email, 'doe.john.robert@mycompany.com', SEARCH_MODE => 'PHRASE') AS phrase_search,
SEARCH(email, 'doe.john.robert@mycompany.com', SEARCH_MODE => 'EXACT') AS exact_search
FROM phrase_exact_search_samples;
출력은 다음 결과를 보여줍니다.
용어
john,robert및doe``가 검색 문자열 및 검색 데이터에서 다른 순서로 되어 있더라도 ``AND검색은 TRUE를 반환합니다.검색어가 검색 문자열의 순서와 일치하지 않기 때문에
PHRASE및EXACT검색은 FALSE를 반환합니다.
+-------------------------------+--------------------+---------------+--------------+
| SEARCH_DATA | CONJUNCTIVE_SEARCH | PHRASE_SEARCH | EXACT_SEARCH |
|-------------------------------+--------------------+---------------+--------------|
| john.robert.doe@mycompany.com | True | False | False |
+-------------------------------+--------------------+---------------+--------------+
The following example runs a query that uses conjunctive semantics to search the email data by setting
the SEARCH_MODE argument to 'AND' in the first search, phrase-match semantics in the second search, and exact-match
semantics in the third search:
SELECT email AS search_data,
SEARCH(email, 'john.doe@mycompany.com', SEARCH_MODE => 'AND') AS conjunctive_search,
SEARCH(email, 'john.doe@mycompany.com', SEARCH_MODE => 'PHRASE') AS phrase_search,
SEARCH(email, 'john.doe@mycompany.com', SEARCH_MODE => 'EXACT') AS exact_search
FROM phrase_exact_search_samples;
출력은 다음 결과를 보여줍니다.
AND검색은 추가 토큰 ``robert``가 검색 데이터에 삽입되어 있더라도 TRUE를 반환합니다.PHRASE및EXACT검색은 이러한 검색 의미 체계가 검색 데이터에 추가 토큰이 삽입되어 있을 때 일치하는 항목을 찾지 못하기 때문에 FALSE를 반환합니다.
+-------------------------------+--------------------+---------------+--------------+
| SEARCH_DATA | CONJUNCTIVE_SEARCH | PHRASE_SEARCH | EXACT_SEARCH |
|-------------------------------+--------------------+---------------+--------------|
| john.robert.doe@mycompany.com | True | False | False |
+-------------------------------+--------------------+---------------+--------------+
다음 예는 첫 번째 검색에서 구문 검색 의미 체계를 사용하여 이메일 데이터를 검색하고, 두 번째 검색에서는 정확한 일치 의미 체계를 사용하는 쿼리를 실행합니다.
SELECT email AS search_data,
SEARCH(email, 'john-robert-doe@mycompany.com', SEARCH_MODE => 'PHRASE') AS phrase_search,
SEARCH(email, 'john-robert-doe@mycompany.com', SEARCH_MODE => 'EXACT') AS exact_search
FROM phrase_exact_search_samples;
출력 결과는 검색 문자열에서 이메일 주소의 구분 기호가 john, robert 및 doe 사이의 마침표 대신 하이픈임에도 불구하고 PHRASE 검색이 TRUE를 반환함을 보여줍니다. 정확한 일치 의미 체계를 사용하면 검색 문자열의 구분 기호가 검색 데이터와 정확히 일치해야 하기 때문에 EXACT 검색은 FALSE를 반환합니다.
+-------------------------------+---------------+--------------+
| SEARCH_DATA | PHRASE_SEARCH | EXACT_SEARCH |
|-------------------------------+---------------+--------------|
| john.robert.doe@mycompany.com | True | False |
+-------------------------------+---------------+--------------+
다음 예는 첫 번째 검색에서는 구문 검색 의미 체계를 사용하여 URL 데이터를 검색하고 두 번째 검색에서는 정확한 일치 의미 체계를 사용하여 쿼리를 실행합니다.
SELECT url AS search_data,
SEARCH(url, 'http://mycompany.com/product/id-12345_67', SEARCH_MODE => 'PHRASE') AS phrase_search,
SEARCH(url, 'http://mycompany.com/product/id-12345_67', SEARCH_MODE => 'EXACT') AS exact_search
FROM phrase_exact_search_samples;
출력 결과는 검색 문자열에서 URL의 구분 기호가 제품 ID의 마침표 대신 밑줄임에도 불구하고 PHRASE 검색이 TRUE를 반환함을 보여줍니다. EXACT 검색은 FALSE를 반환합니다.
+------------------------------------------+---------------+--------------+
| SEARCH_DATA | PHRASE_SEARCH | EXACT_SEARCH |
|------------------------------------------+---------------+--------------|
| http://mycompany.com/product/id-12345.67 | True | False |
+------------------------------------------+---------------+--------------+
다음 예는 첫 번째 검색에서는 구문 검색 의미 체계를 사용하여 전화번호 데이터를 검색하고, 두 번째 검색에서는 정확한 일치 의미 체계를 사용하는 쿼리를 실행합니다.
SELECT phone AS search_data,
SEARCH(phone, '800.555.0100', SEARCH_MODE => 'PHRASE') AS phrase_search,
SEARCH(phone, '800.555.0100', SEARCH_MODE => 'EXACT') AS exact_search
FROM phrase_exact_search_samples;
출력 결과는 검색 문자열에서 전화번호의 구분 기호가 하이픈 대신 마침표임에도 불구하고 PHRASE 검색이 TRUE를 반환함을 보여줍니다. EXACT 검색은 FALSE를 반환합니다.
+--------------+---------------+--------------+
| SEARCH_DATA | PHRASE_SEARCH | EXACT_SEARCH |
|--------------+---------------+--------------|
| 800-555-0100 | True | False |
+--------------+---------------+--------------+
다음 예는 WHERE 절에 SEARCH 함수를 사용하여 phrase_exact_search_samples 테이블을 쿼리합니다. 먼저, 테이블에 다른 행을 삽입합니다.
INSERT INTO phrase_exact_search_samples VALUES (
'jane.smith@mycompany.com',
'http://mycompany.com/product/id-89012.34',
'800-555-0199');
다음 예에서는 테이블 데이터에서 전화번호 ``800-555-0100``과 정확히 일치하는 항목을 검색합니다.
SELECT *
FROM phrase_exact_search_samples
WHERE SEARCH(phone, '800-555-0100', SEARCH_MODE => 'EXACT');
+-------------------------------+------------------------------------------+--------------+
| EMAIL | URL | PHONE |
|-------------------------------+------------------------------------------+--------------|
| john.robert.doe@mycompany.com | http://mycompany.com/product/id-12345.67 | 800-555-0100 |
+-------------------------------+------------------------------------------+--------------+
다음 예는 이전 예와 동일하지만, 800 또는 ``555``가 포함된 모든 전화번호가 일치하도록 정확한 일치 의미 체계 대신 분리형 의미 체계를 사용합니다.
SELECT *
FROM phrase_exact_search_samples
WHERE SEARCH(phone, '800-555-0100', SEARCH_MODE => 'OR');
+-------------------------------+------------------------------------------+--------------+
| EMAIL | URL | PHONE |
|-------------------------------+------------------------------------------+--------------|
| john.robert.doe@mycompany.com | http://mycompany.com/product/id-12345.67 | 800-555-0100 |
| jane.smith@mycompany.com | http://mycompany.com/product/id-89012.34 | 800-555-0199 |
+-------------------------------+------------------------------------------+--------------+
조인에서 VARIANT 및 VARCHAR 데이터 검색하기¶
다음 예에서는 두 테이블 car_rentals 와 car_sales 의 열에 검색을 적용했을 때 두 테이블의 조인을 보여줍니다. car_sales 테이블에는 VARIANT 데이터가 포함되어 있습니다. car_sales 테이블과 해당 데이터는 반정형 데이터 쿼리하기 아래에 설명되어 있습니다. 다음 SQL 문은 car_rentals 테이블을 생성하고 그 안에 데이터를 삽입합니다.
CREATE OR REPLACE TABLE car_rentals(
vehicle_make VARCHAR(30),
dealership VARCHAR(30),
salesperson VARCHAR(30));
INSERT INTO car_rentals VALUES
('Toyota', 'Tindel Toyota', 'Greg Northrup'),
('Honda', 'Valley View Auto Sales', 'Frank Beasley'),
('Tesla', 'Valley View Auto Sales', 'Arturo Sandoval');
쿼리를 실행합니다.
SELECT SEARCH((r.vehicle_make, r.dealership, s.src:dealership), 'Toyota Tesla')
AS contains_toyota_tesla, r.vehicle_make, r.dealership,s.src:dealership
FROM car_rentals r JOIN car_sales s
ON r.SALESPERSON=s.src:salesperson.name;
+-----------------------+--------------+------------------------+--------------------------+
| CONTAINS_TOYOTA_TESLA | VEHICLE_MAKE | DEALERSHIP | S.SRC:DEALERSHIP |
|-----------------------+--------------+------------------------+--------------------------|
| True | Toyota | Tindel Toyota | "Tindel Toyota" |
| False | Honda | Valley View Auto Sales | "Valley View Auto Sales" |
+-----------------------+--------------+------------------------+--------------------------+
두 번째 예에서는 동일한 데이터에 대해 서로 다른 검색어가 사용됩니다.
SELECT SEARCH((r.vehicle_make, r.dealership, s.src:dealership), 'Toyota Honda')
AS contains_toyota_honda, r.vehicle_make, r.dealership, s.src:dealership
FROM car_rentals r JOIN car_sales s
ON r.SALESPERSON =s.src:salesperson.name;
+-----------------------+--------------+------------------------+--------------------------+
| CONTAINS_TOYOTA_HONDA | VEHICLE_MAKE | DEALERSHIP | S.SRC:DEALERSHIP |
|-----------------------+--------------+------------------------+--------------------------|
| True | Toyota | Tindel Toyota | "Tindel Toyota" |
| True | Honda | Valley View Auto Sales | "Valley View Auto Sales" |
+-----------------------+--------------+------------------------+--------------------------+
분석기를 사용하여 검색 동작 조정하기¶
The following examples show how to adjust the behavior of the SEARCH function by specifying a
non-default analyzer: 'UNICODE_ANALYZER' or 'NO_OP_ANALYZER'.
The first example uses the 'NO_OP_ANALYZER' to test whether the string 1.2.500 matches the exact contents
of the act_scene_line column for any row in the lines table. Two rows qualify for the search.
SELECT line_id, act_scene_line FROM lines
WHERE SEARCH(act_scene_line, '1.2.500', ANALYZER=>'NO_OP_ANALYZER');
+---------+----------------+
| LINE_ID | ACT_SCENE_LINE |
|---------+----------------|
| 91998 | 1.2.500 |
| 108464 | 1.2.500 |
+---------+----------------+
If you remove 'NO_OP_ANALYZER' as an argument to the function for this example, the search returns a large
number of rows. The default analyzer treats 1, 2, and 500 as distinct tokens; therefore, the function
returns TRUE for all of the rows where 1, 2, or 500 exist in any order or combination.
If you change this query to include only the prefix 1.2 for the second argument, the default analyzer
returns TRUE, but the 'UNICODE_ANALYZER' and 'NO_OP_ANALYZER' both return FALSE. The default analyzer treats
periods in these values as delimiters, but the Unicode analyzer doesn’t.
The following two queries show another effect of using the 'UNICODE_ANALYZER' instead of the default analyzer. The
first query, using the 'UNICODE_ANALYZER', returns only one row. The extra single quote in the second
argument is there to escape the single quote for the apostrophe. See 작은따옴표로 묶인 문자열 상수.
SELECT DISTINCT(play)
FROM lines
WHERE SEARCH(play, 'love''s', ANALYZER=>'UNICODE_ANALYZER');
+----------------------+
| PLAY |
|----------------------|
| Love's Labour's Lost |
+----------------------+
기본 분석기가 아포스트로피 문자를 구분 기호로 처리하므로 기본 분석기를 사용하는 두 번째 쿼리는 4개의 행을 반환합니다. 문자 “s”를 토큰으로 포함하는 모든 문자열은 검색에 적합합니다. 이 예에서 함수는 “아포스트로피 s”('s)를 포함하는 모든 문자열에 대해 TRUE를 반환합니다.
SELECT DISTINCT(play) FROM lines WHERE SEARCH(play, 'love''s');
+---------------------------+
| PLAY |
|---------------------------|
| All's Well That Ends Well |
| Love's Labour's Lost |
| A Midsummer Night's Dream |
| The Winter's Tale |
+---------------------------+
예상되는 오류 사례의 예¶
다음 예에서는 예상되는 구문 오류를 반환하는 쿼리를 보여줍니다.
다음 예시 코드는 5 가 search_string 인자에 대해 지원되는 데이터 타입이 아니므로 실패합니다.
SELECT SEARCH(line, 5) FROM lines;
001045 (22023): SQL compilation error:
argument needs to be a string: '1'
다음 예시 코드는 search_data 인자에 대해 지원되는 데이터 타입의 열이 지정되지 않았으므로 실패합니다.
SELECT SEARCH(line_id, 'dream') FROM lines;
001173 (22023): SQL compilation error: error line 1 at position 7: Expected non-empty set of columns supporting full-text search.
다음 예시 코드는 search_data 인자에 대해 지원되는 데이터 타입의 열이 지정되었으므로 성공합니다. 이 함수는 line_id 열이 지원되는 데이터 타입이 아니라 무시합니다.
SELECT SEARCH((line_id, play), 'dream') FROM lines
ORDER BY play LIMIT 5;
+----------------------------------+
| SEARCH((LINE_ID, PLAY), 'DREAM') |
|----------------------------------|
| True |
| True |
| False |
| False |
| False |
+----------------------------------+
다음 예시 코드는 첫 번째 인자에 대해 여러 문자열 리터럴이 괄호 없이 나열되어 있어 인자가 일치하지 않으므로 실패합니다.
SELECT SEARCH('docs@snowflake.com', 'careers@snowflake.com', '@');
001881 (42601): SQL compilation error: Expected 1 named argument(s), found 0
다음 예시 코드는 첫 번째 인자에 대해 여러 열 이름이 괄호 없이 나열되어 있어 인자가 너무 많으므로 실패합니다.
SELECT SEARCH(play,line,'king', ANALYZER=>'UNICODE_ANALYZER') FROM lines;
000939 (22023): SQL compilation error: error line 1 at position 7
too many arguments for function [SEARCH(LINES.PLAY, LINES.LINE, 'king', 'UNICODE_ANALYZER')] expected 3, got 4
다음 예시 코드는 열 이름이 검색 문자열 인자로 허용되지 않으므로 실패합니다.
SELECT SEARCH(line, character) FROM lines;
001015 (22023): SQL compilation error:
argument 2 to function SEARCH needs to be constant, found 'LINES.CHARACTER'
Creating the sample data for SEARCH¶
이 섹션의 일부 예제에서는 셰익스피어 희곡에 등장하는 텍스트가 포함된 테이블을 쿼리합니다. 각 텍스트 줄은 테이블의 단일 행에 저장됩니다. 다른 열에는 희곡 제목, 등장인물의 이름 등이 표시됩니다. lines 테이블의 구조는 다음과 같습니다.
DESCRIBE TABLE lines;
+----------------+---------------+--------+-------+-
| name | type | kind | null? |
|----------------+---------------+--------+-------+-
| LINE_ID | NUMBER(38,0) | COLUMN | Y |
| PLAY | VARCHAR(50) | COLUMN | Y |
| SPEECH_NUM | NUMBER(38,0) | COLUMN | Y |
| ACT_SCENE_LINE | VARCHAR(10) | COLUMN | Y |
| CHARACTER | VARCHAR(30) | COLUMN | Y |
| LINE | VARCHAR(2000) | COLUMN | Y |
+----------------+---------------+--------+-------+-
예를 들어, 이 테이블의 한 줄은 다음과 같습니다.
SELECT * FROM lines
WHERE line_id=34230;
+---------+--------+------------+----------------+-----------+--------------------------------------------+
| LINE_ID | PLAY | SPEECH_NUM | ACT_SCENE_LINE | CHARACTER | LINE |
|---------+--------+------------+----------------+-----------+--------------------------------------------|
| 34230 | Hamlet | 19 | 3.1.64 | HAMLET | To be, or not to be, that is the question: |
+---------+--------+------------+----------------+-----------+--------------------------------------------+
이 섹션의 예시 코드를 실행하려면 다음 명령을 실행하여 이 테이블을 만드십시오.
CREATE OR REPLACE TABLE lines(
line_id INT,
play VARCHAR(50),
speech_num INT,
act_scene_line VARCHAR(10),
character VARCHAR(30),
line VARCHAR(2000)
);
INSERT INTO lines VALUES
(4,'Henry IV Part 1',1,'1.1.1','KING HENRY IV','So shaken as we are, so wan with care,'),
(13,'Henry IV Part 1',1,'1.1.10','KING HENRY IV','Which, like the meteors of a troubled heaven,'),
(9526,'Henry VI Part 3',1,'1.1.1','WARWICK','I wonder how the king escaped our hands.'),
(12664,'All''s Well That Ends Well',1,'1.1.1','COUNTESS','In delivering my son from me, I bury a second husband.'),
(15742,'All''s Well That Ends Well',114,'5.3.378','KING','Your gentle hands lend us, and take our hearts.'),
(16448,'As You Like It',2,'2.3.6','ADAM','And wherefore are you gentle, strong and valiant?'),
(24055,'The Comedy of Errors',14,'5.1.41','AEMELIA','Be quiet, people. Wherefore throng you hither?'),
(28487,'Cymbeline',3,'1.1.10','First Gentleman','Is outward sorrow, though I think the king'),
(33522,'Hamlet',1,'2.2.1','KING CLAUDIUS','Welcome, dear Rosencrantz and Guildenstern!'),
(33556,'Hamlet',5,'2.2.35','KING CLAUDIUS','Thanks, Rosencrantz and gentle Guildenstern.'),
(33557,'Hamlet',6,'2.2.36','QUEEN GERTRUDE','Thanks, Guildenstern and gentle Rosencrantz:'),
(33776,'Hamlet',67,'2.2.241','HAMLET','Guildenstern? Ah, Rosencrantz! Good lads, how do ye both?'),
(34230,'Hamlet',19,'3.1.64','HAMLET','To be, or not to be, that is the question:'),
(35672,'Hamlet',7,'4.6.27','HORATIO','where I am. Rosencrantz and Guildenstern hold their'),
(36289,'Hamlet',14,'5.2.60','HORATIO','So Guildenstern and Rosencrantz go to''t.'),
(36640,'Hamlet',143,'5.2.389','First Ambassador','That Rosencrantz and Guildenstern are dead:'),
(43494,'King John',1,'1.1.1','KING JOHN','Now, say, Chatillon, what would France with us?'),
(43503,'King John',5,'1.1.10','CHATILLON','To this fair island and the territories,'),
(49031,'King Lear',1,'1.1.1','KENT','I thought the king had more affected the Duke of'),
(49040,'King Lear',4,'1.1.10','GLOUCESTER','so often blushed to acknowledge him, that now I am'),
(52797,'Love''s Labour''s Lost',1,'1.1.1','FERDINAND','Let fame, that all hunt after in their lives,'),
(55778,'Love''s Labour''s Lost',405,'5.2.971','ADRIANO DE ARMADO','Apollo. You that way: we this way.'),
(67000,'A Midsummer Night''s Dream',1,'1.1.1','THESEUS','Now, fair Hippolyta, our nuptial hour'),
(69296,'A Midsummer Night''s Dream',104,'5.1.428','PUCK','And Robin shall restore amends.'),
(75787,'Pericles',178,'1.0.21','LODOVICO','This king unto him took a fere,'),
(78407,'Richard II',1,'1.1.1','KING RICHARD II','Old John of Gaunt, time-honour''d Lancaster,'),
(91998,'The Tempest',108,'1.2.500','FERDINAND','Were I but where ''tis spoken.'),
(92454,'The Tempest',150,'2.1.343','ALONSO','Wherefore this ghastly looking?'),
(99330,'Troilus and Cressida',30,'1.1.102','AENEAS','How now, Prince Troilus! wherefore not afield?'),
(100109,'Troilus and Cressida',31,'2.1.53','ACHILLES','Why, how now, Ajax! wherefore do you thus? How now,'),
(108464,'The Winter''s Tale',106,'1.2.500','CAMILLO','As or by oath remove or counsel shake')
;