Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 (日本語) » SQL Anywhere サーバ - SQL の使用法 » データのクエリと変更 » データのクエリ » 全文検索のタイプ

 

単語とフレーズの検索

単語のリストの全文検索を実行する場合は、フレーズ内の単語でないかぎり、単語の順序は重要ではありません。単語がフレーズ内にある場合、データベース・サーバは、出現する順序と相対位置が指定されたものと厳密に一致する単語を検索します。

単語検索やフレーズ検索を実行する際、単語長の設定を超過するため、またはストップリストに含まれているために単語がクエリから削除された場合、予期しない数のローが返される可能性があります。これは、単語をクエリから削除することは探索条件を変更することと同じであるためです。たとえば、'"grown cotton"' というフレーズを検索し、grown がストップリストに入っている場合、cotton を含むインデックス付けされたローがすべて返されます。

CONTAINS 句の文法でキーワードと見なされる単語は、フレーズに含まれている場合のみ検索できます。

単語検索

デモ・データベースでは、MarketingInformation テーブルの Description カラムに MarketingTextIndex というテキスト・インデックスが作成されています。次の文は、MarketingInformation テーブルの Description カラムを問い合わせて、Description カラムの値に cotton という単語が含まれるローを返します。

SELECT ID, Description 
   FROM MarketingInformation
   WHERE CONTAINS ( Description, 'cotton' );
ID Description
906 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Visor</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Lightweight 100% organically grown 「cotton」 construction. Shields against sun and precipitation.cotton Metallic ions in the fibers inhibit bacterial growth, and help neutralize odor.</span></p></body></html>
908 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Sweatshirt</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Lightweight 100% organically grown 「cotton」 hooded sweatshirt with taped neck seams. Comes pre-washed for softness and to lessen shrinkage.</span></p></body></html>
909 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Sweatshirt</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Top-notch construction includes durable topstitched seams for strength with low-bulk, resilient rib-knit collar, cuffs and bottom. An 80% 「cotton」/20% polyester blend makes it easy to keep them clean.</span></p></body></html>
910 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Shorts</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>These quick-drying 「cotton」 shorts provide all day comfort on or off the trails. Now with a more comfortable and stretchy fabric and an adjustable drawstring waist.</span></p></body></html>

次の例は、MarketingInformation テーブルを問い合わせて、各ローについて、Description カラムの値に cotton という単語が含まれるかどうかを示す 1 つの値を返します。

SELECT ID, IF CONTAINS ( Description, 'cotton' )
      THEN 1
      ELSE 0
      ENDIF AS Results
   FROM MarketingInformation;
ID Results
901 0
902 0
903 0
904 0
905 0
906 1
907 0
908 1
909 1
910 1

次の例は、MarketingInformation テーブルを問い合わせて、Description カラムに cotton という単語が含まれる項目を検索し、一致ごとのスコアを表示します。

SELECT ID, ct.score, Description
   FROM MarketingInformation CONTAINS ( MarketingInformation.Description, 'cotton' ) as ct
   ORDER BY ct.score DESC;
ID score Description
908 0.9461597363521859 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Sweatshirt</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Lightweight 100% organically grown 「cotton」 hooded sweatshirt with taped neck seams. Comes pre-washed for softness and to lessen shrinkage.</span></p></body></html>
910 0.9244136988525732 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Shorts</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>These quick-drying 「cotton」 shorts provide all day comfort on or off the trails. Now with a more comfortable and stretchy fabric and an adjustable drawstring waist.</span></p></body></html>
906 0.9134171046194403 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Visor</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Lightweight 100% organically grown 「cotton」 construction. Shields against sun and precipitation. Metallic ions in the fibers inhibit bacterial growth, and help neutralize odor.</span></p></body></html>
909 0.8856420222728282 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Sweatshirt</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Top-notch construction includes durable topstitched seams for strength with low-bulk, resilient rib-knit collar, cuffs and bottom. An 80% 「cotton」/20% polyester blend makes it easy to keep them clean.</span></p></body></html>

クエリの FROM 句で CONTAINS を使用した場合のスコア結果の詳細については、全文検索結果のスコア付けを参照してください。

フレーズ検索

フレーズの全文検索を実行する場合、フレーズを二重引用符で囲みます。指定の順序と相対位置の単語が含まれる場合、カラムは一致します。

単語がフレーズ内に含まれている場合を除き、AND や FUZZY などの CONTAINS キーワードを、検索する単語として指定することはできません (単一単語のフレーズは検索可能)。たとえば、NOT は CONTAINS キーワードですが、次の文は実行できます。CONTAINS キーワードと特殊文字のリストについては、CONTAINS 探索条件を参照してください。

SELECT * FROM table-name CONTAINS ( Remarks, '"NOT"' );

フレーズの中で使用される特殊文字は、特殊文字として解釈されません (アスタリスクを除く)。

フレーズは、近接検索の引数としては使用できません。

次の文は、MarketingInformation テーブルの Description カラムを問い合わせて、"grown cotton" というフレーズを検索し、一致ごとのスコアを表示します。

SELECT ID, ct.score, Description
   FROM MarketingInformation CONTAINS ( MarketingInformation.Description, '"grown cotton"' ) as ct
   ORDER BY ct.score DESC;
ID score Description
908 1.6619019465461564 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Sweatshirt</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Lightweight 100% organically grown 「cotton」 hooded sweatshirt with taped neck seams. Comes pre-washed for softness and to lessen shrinkage.</span></p></body></html>
906 1.6043904700786786 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Visor</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Lightweight 100% organically grown 「cotton」 construction. Shields against sun and precipitation. Metallic ions in the fibers inhibit bacterial growth, and help neutralize odor.</span></p></body></html>