sql >> データベース >  >> RDS >> Mysql

あいまい検索をサポートする、実装が最も簡単なサイト検索アプリケーションは何ですか?

    ewemliの答えは正しい方向ですが、フルテキストを置き換えるのではなく、フルテキストとサウンデックスのマッピングを組み合わせる必要があります。そうしないと、LIKEクエリが非常に遅くなる可能性があります。

    create table with_soundex (
      id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
      original TEXT,
      soundex TEXT,
      FULLTEXT (soundex)
    );
    
    insert into with_soundex (original, soundex) values 
    
    ('add some test cases', CONCAT_WS(' ', soundex('add'), soundex('some'), soundex('test'), soundex('cases'))),
    ('this is some text', CONCAT_WS(' ', soundex('this'), soundex('is'), soundex('some'), soundex('text'))),
    ('one more test case', CONCAT_WS(' ', soundex('one'), soundex('more'), soundex('test'), soundex('case'))),
    ('just filling the index', CONCAT_WS(' ', soundex('just'), soundex('filling'), soundex('the'), soundex('index'))),
    ('need one more example', CONCAT_WS(' ', soundex('need'), soundex('one'), soundex('more'), soundex('example'))),
    ('seems to need more', CONCAT_WS(' ', soundex('seems'), soundex('to'), soundex('need'), soundex('more')))
    ('some helpful cases to consider', CONCAT_WS(' ', soundex('some'), soundex('helpful'), soundex('cases'), soundex('to'), soundex('consider')))
    
    select * from with_soundex where match(soundex) against (soundex('test'));
    +----+---------------------+---------------------+
    | id | original            | soundex             |
    +----+---------------------+---------------------+
    |  1 | add some test cases | A300 S500 T230 C000 | 
    |  2 | this is some text   | T200 I200 S500 T230 | 
    |  3 | one more test case  | O500 M600 T230 C000 | 
    +----+---------------------+---------------------+
    
    select * from with_soundex where match(soundex) against (CONCAT_WS(' ', soundex('test'), soundex('some')));
    +----+--------------------------------+---------------------------+
    | id | original                       | soundex                   |
    +----+--------------------------------+---------------------------+
    |  1 | add some test cases            | A300 S500 T230 C000       | 
    |  2 | this is some text              | T200 I200 S500 T230       | 
    |  3 | one more test case             | O500 M600 T230 C000       | 
    |  7 | some helpful cases to consider | S500 H414 C000 T000 C5236 | 
    +----+--------------------------------+---------------------------+
    

    これにより、インデックスを最大限に活用しながら(soundexアルゴリズムの制限内で)かなり良い結果が得られます(「%foo」のようなクエリは、テーブルのすべての行をスキャンする必要があります)。

    フレーズ全体ではなく、各単語でsoundexを実行することの重要性に注意してください。 SQLに実行させるのではなく、各単語に対して独自のバージョンのsoundexを実行することもできますが、その場合、アルゴリズム間に違いがある場合に備えて、保存と取得の両方で実行するようにしてください(たとえば、MySQLのアルゴリズムは制限されません標準の4文字 )



    1. HTML入力フィールドへのクエリおよびエコーデータ

    2. PostgreSQLはJSON配列として結果セットを返しますか?

    3. ZendDbを使用したネストされた選択

    4. postgresqlデータベースに画像を挿入します