2

I'm using the following text search configuration:

CREATE TEXT SEARCH DICTIONARY tsd
  (TEMPLATE = pg_catalog.SIMPLE);
  
CREATE TEXT SEARCH CONFIGURATION tsc_test
  (COPY = german);
ALTER TEXT SEARCH CONFIGURATION tsc_test
  ALTER MAPPING FOR asciihword, asciiword, hword, hword_asciipart, hword_part, word
  WITH tsd;

I'm trying to perform full text search on a table, but I can't figure out how to define the text search configuration correctly. My problem is, that

SELECT to_tsvector('tsc_test', 'foo bar');

and

SELECT to_tsvector('tsc_test', 'foo_bar');

results in the same tsvector object, because the _ (and others, like #(){}, etc.) are interpreted as space symbols (see SELECT ts_debug ('tsc_test', 'foo_bar');). I need them to be distinct, however.

Is there a way to adjust my text search configuration so it treats only spaces as word separators and not anything else?

1
  • You probably need to write your own parser for that.
    – mustaccio
    Oct 7, 2021 at 13:14

1 Answer 1

2

There is no really good way to do that.

A workaround may be to replace everything that is not a space or a character with – say – a digit:

CREATE FUNCTION transmogrify(text) RETURNS text
   LANGUAGE sql IMMUTABLE STRICT AS
$$SELECT regexp_replace($1, '[^[:alpha:] ]', '0', 'g')$$;

SELECT to_tsvector('german', transmogrify('foo_bar'))
    @@ to_tsquery('german', transmogrify('foo'));

 ?column? 
══════════
 f
(1 row)

SELECT to_tsvector('german', transmogrify('foo_bar'))
    @@ to_tsquery('german', transmogrify('foo_bar'));

 ?column? 
══════════
 t
(1 row)

Of course that will cause trouble if there are zeros in your text, but as I said, there is no perfectly satisfying way to do this.

1
  • Thanks! Fortunately, the replacement shouldn't cause any issues in my specific case.
    – karpfen
    Oct 7, 2021 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.