postgresql: full text search: how to understand ts_rank calculation
Asked Answered
F

0

11

I am using postgresql 9.6. I am trying to understand ts_rank

I have a phrase "one two four" and my search query is 'one two three' So i am trying to get the ts_rank for all possible combinations as below.

select 
ts_rank(to_tsvector('one two four'), to_tsquery('one')) AS one,
ts_rank(to_tsvector('one two four'), to_tsquery('two')) AS two,
ts_rank(to_tsvector('one two four'), to_tsquery('three')) AS three,
ts_rank(to_tsvector('one two four'), to_tsquery('one | two | three')) AS oneortwoorthree,
ts_rank(to_tsvector('one two four'), to_tsquery('one & two & three')) AS oneandtwoandthree,
ts_rank(to_tsvector('one two four'), to_tsquery('(one & two)')) AS mix2_1,
ts_rank(to_tsvector('one two four'), to_tsquery('(two & three)')) AS mix2_2,
ts_rank(to_tsvector('one two four'), to_tsquery('(one & three)')) AS mix2_3,
ts_rank(to_tsvector('one two four'), to_tsquery('(one & two) | three')) AS mix3_1,
ts_rank(to_tsvector('one two four'), to_tsquery('(two & three) | one')) AS mix3_2,
ts_rank(to_tsvector('one two four'), to_tsquery('(one & three) | two')) AS mix3_3

I get the following results:

one: 0.0607927
two: 0.0607927
three: 0
oneortwoorthree: 0.0405285
oneandtwoandthree: 0.0991032
mix2_1: 0.0991032
mix2_2: 1e-20
mix2_3: 1e-20
mix3_1: 0.0405285
mix3_2: 0.0405285
mix3_3: 0.0405285

Question 1: I am expecting

ts_rank(to_tsvector('one two four'), to_tsquery('one & two & three')) AS oneandtwoandthree = 0 Because one & two & three are not present.

Question 2: How the rank for

ts_rank(to_tsvector('one two four'), to_tsquery('one | two | three')) AS oneortwoorthree = 0.0405285

and

ts_rank(to_tsvector('one two four'), to_tsquery('(one & two) | three')) AS mix3_1, = 0.0405285

are same

Question 3:

I am expecting ts_rank for

ts_rank(to_tsvector('one two four'), to_tsquery('one | two | three')) AS oneortwoorthree = 0.0405285

should be greater than

ts_rank(to_tsvector('one two four'), to_tsquery('one')) AS one = 0.0607927

whereas it is inverse. There is high chances that any of the three words can exist in the phrase than a single word.

Fenestrated answered 9/2, 2018 at 4:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.