Synonym DICTIONARY for `simple` language is not working

断了今生、忘了曾经 提交于 2020-02-08 02:39:05

问题


I am supposing that we can use this old recipe and only replace the language german to language simple (that is the "no language")... But it is not working.

  CREATE TEXT SEARCH DICTIONARY my_synonym (
     TEMPLATE = synonym,
     SYNONYMS = synonym_sample
     -- default /usr/share/postgresql/12/tsearch_data/synonym_sample.syn
  );
  CREATE TEXT SEARCH CONFIGURATION my_synonym(COPY='simple'); -- need it??
  ALTER TEXT SEARCH CONFIGURATION my_synonym
     ALTER MAPPING FOR asciiword, asciihword, hword_asciipart, word, hword, hword_part
     WITH my_synonym   -- make sense??
  ;

 SELECT to_tsvector('my_synonym', 'Postgresql') 
      @@ to_tsquery('my_synonym', 'Postgresql'); -- true


    SELECT to_tsvector('my_synonym', 'Postgresql') 
         @@ to_tsquery('my_synonym', 'pgsql'); -- false. NOTICE:  
    -- text-search query contains only stop words or doesn't contain lexemes, ignored
    SELECT to_tsvector('my_synonym', 'Postgresql pgsql') 
         @@ to_tsquery('my_synonym', 'pgsql'); -- false. Same NOTICE.

The synonym_sample.syn is described in the current Guide's 12.6.3. Synonym Dictionary section. It converts "pgsql" abbreviation into "postgres" word... So many problems

  1. fail, not work, not translated "pgsql" into "postgres".
  2. ignored "pgsql" (!)
  3. generates a NOTICE that not makes sense

回答1:


Once you are done doing synonym replacements, you need to pass the resulting lexemes along to a stemmer. That occurs in both examples you link to, but does not occur in your example. You can use "simple" as a dummy stemmer which just passes its output along without doing any actual stemming.

ALTER TEXT SEARCH CONFIGURATION my_synonym
     ALTER MAPPING FOR asciiword, asciihword, hword_asciipart, word, hword, hword_part
     WITH my_synonym, simple;



回答2:


I agree with the answer by @jjanes. You need to pass a result of your synonym dictionary to the simple dictionary.

Here is the results if you don't pass a result to the simple dictionary:

SELECT to_tsvector('my_synonym', 'Postgresql'), to_tsquery('my_synonym', 'pgsql');
 to_tsvector | to_tsquery
-------------+------------
 'pgsql':1   |

And here is the results if you pass it:

SELECT to_tsvector('my_synonym', 'Postgresql'), to_tsquery('my_synonym', 'pgsql');
 to_tsvector | to_tsquery
-------------+------------
 'pgsql':1   | 'pgsql'

But there is another approach in case if you don't want to use other dictionaries other than synonym. You can use dict_xsyn contrib module. Here is its documentation: https://www.postgresql.org/docs/current/dict-xsyn.html

Just do the following:

CREATE EXTENSION dict_xsyn;
ALTER TEXT SEARCH DICTIONARY xsyn(
    RULES='xsyn_sample', KEEPORIG=false, MATCHSYNONYMS=true);
CREATE TEXT SEARCH CONFIGURATION xsyn(COPY='simple');
ALTER TEXT SEARCH CONFIGURATION xsyn
    ALTER MAPPING FOR
        asciiword, asciihword, hword_asciipart, word, hword, hword_part
    WITH xsyn;

Here I don't use the simple dictionary. Creating dict_xsyn extension also creates the xsyn dictionary. But it doesn't create a configuration, so you need to create appropriate configuration.

You also need to configure the xsyn dictionary with appropriate options or to create new one. My xsyn_sample.rules file is:

supernova sn sne 1987a
postgresql pgsql

And here is the results:

SELECT to_tsvector('xsyn', 'Postgresql'), to_tsquery('xsyn', 'pgsql');
 to_tsvector | to_tsquery
-------------+------------
 'pgsql':1   | 'pgsql'
SELECT to_tsvector('xsyn', 'Postgresql') @@ to_tsquery('xsyn', 'pgsql');
 ?column?
----------
 t


来源:https://stackoverflow.com/questions/60082663/synonym-dictionary-for-simple-language-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!