1

Let's say I get the following table when I do select name, alternative_name from persons;

          name            |           alternative_name
--------------------------+----------------------------------
 Johnny A                 | John the first
 Johnny B                 | The second John

Now with this query select name from persons where to_tsvector(name || alternative_name) @@ to_tsquery('John');:

          name            |           alternative_name
--------------------------+----------------------------------
 Johnny A                 | John the first

Shouldn't I get both? How can I do a full text search on both the name and columns where I get all rows that match the search query?

Edit: Yes, there is indeed a typo here. It is to_tsquery

3

1 Answer 1

1

you concat without space:

t=# with c(n,a) as (values('Johnny A','John the first'),('Johny B','The second John'))
select * from c
where to_tsvector(n || a) @@ to_tsquery('John')
;
    n    |        a
---------+-----------------
 Johny B | The second John
(1 row)

so first haystack becomes Johnny AJohn the first, thus lexeme do not match, try:

t=# with c(n,a) as (values('Johnny A','John the first'),('Johny B','The second John'))
select * from c
where to_tsvector(n ||' '|| a) @@ to_tsquery('John')
;
    n     |        a
----------+-----------------
 Johnny A | John the first
 Johny B  | The second John
(2 rows)
Sign up to request clarification or add additional context in comments.

3 Comments

I mentioned that in comment but that's obviously not the problem according to the results OP is displaying (assuming that's the real data, query and results).
@DenysSéguret yes - he has the "opposite result" - but I assumed his code is not copy paste after seeing to_query. Fair point though - I should have make clear it before answering.
@DenysSéguret : and you are correct in assuming that this is not real data.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.