通过完全匹配订购全文搜索THEN前缀匹配

时间:2016-09-26 19:58:25

标签: postgresql full-text-search

如何确保我的全文搜索结果按完全匹配和那些前缀匹配排序?

SELECT ticker, name, ts_rank(document, to_tsquery('english', 'MAT:*')) AS rank
FROM (
  SELECT *, setweight(to_tsvector('english', ticker), 'A') || setweight(to_tsvector('english', name), 'B') AS document
  FROM ( VALUES
     ('MATI-R' , 'MATICHON PCL.NVDR')
    ,('MATCH-R', 'MATCHING MAXIMIZE SLN. NVDR')
    ,('MATV'   , 'MATAV-CABLE SYS.MEDIA SPN.ADR 1:2 DEAD - DELIST.03/07/06')
    ,('MAT'    , 'MATISSE HOLDINGS DEAD - 03/10/06')
    ,('MAT'    , 'MATTEL')
  ) data (ticker,name)
) ss ORDER BY rank DESC

我在to_tsquery('english', 'MAT:A & MAT:*B')上尝试了一些类似 ticker | name | rank ---------+----------------------------------------------------------+---------- MATI-R | MATICHON PCL.NVDR | 1.45903 MATCH-R | MATCHING MAXIMIZE SLN. NVDR | 1.27665 MATV | MATAV-CABLE SYS.MEDIA SPN.ADR 1:2 DEAD - DELIST.03/07/06 | 1.09427 MAT | MATISSE HOLDINGS DEAD - 03/10/06 | 0.851098 MAT | MATTEL | 0.851098 的建议,但似乎没有人给我我想要的订单。当前输出是

 ticker  |                           name                           |   rank
---------+----------------------------------------------------------+----------
 MAT     | MATTEL                                                   | ??
 MAT     | MATISSE HOLDINGS DEAD - 03/10/06                         | ??
 MATCH-R | MATCHING MAXIMIZE SLN. NVDR                              | ??
 MATI-R  | MATICHON PCL.NVDR                                        | ??
 MATV    | MATAV-CABLE SYS.MEDIA SPN.ADR 1:2 DEAD - DELIST.03/07/06 | ??

当我想要更像

的东西时
        private final SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);          

        ...

        btn1_row1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {

                sp.play(sndTest, 1, 1, 0, 0, 1);

            } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {

                sp.stop(sndTest); // It requires a Stream ID, not a Sound ID
            }
            return false;
        }
    });

1 个答案:

答案 0 :(得分:2)

使用LIKEILIKE

SELECT ticker, name, ts_rank(document, to_tsquery('english', 'MAT:*')) AS rank
FROM (
  SELECT *, setweight(to_tsvector('english', ticker), 'A') || setweight(to_tsvector('english', name), 'B') AS document
  FROM ( VALUES
     ('MATI-R' , 'MATICHON PCL.NVDR')
    ,('MATCH-R', 'MATCHING MAXIMIZE SLN. NVDR')
    ,('MATV'   , 'MATAV-CABLE SYS.MEDIA SPN.ADR 1:2 DEAD - DELIST.03/07/06')
    ,('MAT'    , 'MATISSE HOLDINGS DEAD - 03/10/06')
    ,('MAT'    , 'MATTEL')
  ) data (ticker,name)
) ss ORDER BY name LIKE concat('%', ticker, '%') desc, rank DESC

 ticker  |                           name                           |   rank   
---------+----------------------------------------------------------+----------
 MAT     | MATISSE HOLDINGS DEAD - 03/10/06                         | 0.851098
 MAT     | MATTEL                                                   | 0.851098
 MATI-R  | MATICHON PCL.NVDR                                        |  1.45903
 MATCH-R | MATCHING MAXIMIZE SLN. NVDR                              |  1.27665
 MATV    | MATAV-CABLE SYS.MEDIA SPN.ADR 1:2 DEAD - DELIST.03/07/06 |  1.09427
(5 rows)