排名和集群全文搜索

时间:2017-05-18 19:23:56

标签: postgresql

我正在尝试开发排名全文的搜索。我们的想法是从两个不同的表中查询两个不同的列并返回"匹配分数"两者。

我希望将结果聚集在一起。我的意思是,score_question会显示结果ORDER BY DESC,与answer_question相同。我希望结果只显示answer_scorequestion_score是否> = 0.1的行。

我该怎么做?

CREATE OR REPLACE FUNCTION search_questions(psearch text)
  RETURNS TABLE (answerid INTEGER, questionid INTEGER, score_question REAL, score_answer REAL) AS $func$
BEGIN
  return QUERY
  SELECT DISTINCT publications.publicationid as answers ,questions.publicationid as questions, ts_rank_cd(
                                                 to_tsvector('english', questions.title),
                                                 plainto_tsquery('english', psearch)
                                             ) AS score_question, ts_rank_cd(to_tsvector('english', publications.body),
                                                                             plainto_tsquery('english', psearch)) AS score_answer
  FROM questions, publications INNER JOIN answers ON publications.publicationid = answers.publicationid
  WHERE to_tsvector('english', questions.title || '' || publications.body) @@ plainto_tsquery('english', psearch)
  GROUP BY publications.publicationid, questions.publicationid;
END
$func$  LANGUAGE plpgsql;

以下是我的结果

answerid | questionid | score_question | score_answer 
----------+------------+----------------+--------------
       70 |         23 |            0.2 |            0
      116 |         23 |            0.2 |            0
      143 |        150 |            0.1 |            0
      124 |        150 |            0.1 |            0
      130 |         23 |            0.2 |            0
       60 |         32 |              0 |          0.1
       60 |         27 |              0 |          0.1
       60 |        129 |              0 |          0.1
       60 |         23 |            0.2 |          0.1
       60 |        122 |              0 |          0.1
       60 |         31 |              0 |          0.1
      144 |        150 |            0.1 |            0
       53 |         23 |            0.2 |            0
      143 |         23 |            0.2 |            0
      116 |        150 |            0.1 |            0
       54 |         23 |            0.2 |            0
       65 |         23 |            0.2 |            0
       53 |        150 |            0.1 |            0
      144 |         23 |            0.2 |            0
       65 |        150 |            0.1 |            0
       60 |         21 |              0 |          0.1
       54 |        150 |            0.1 |            0
       60 |         10 |              0 |          0.1
       60 |        150 |            0.1 |          0.1
       60 |        141 |              0 |          0.1
      124 |         23 |            0.2 |            0
       60 |         19 |              0 |          0.1
      130 |        150 |            0.1 |            0
       60 |        135 |              0 |          0.1
       70 |        150 |            0.1 |            0
(30 rows)

0 个答案:

没有答案