From: Tomas Vondra <tv@fuzzy.cz>
To: pgsql-sql@postgresql.org
Subject: how to transform list to table and evaluate an expression on it?
Date: Sat, 07 Jan 2006 17:03:35 +0100
Message-ID: <43BFE657.7050303@fuzzy.cz> (raw)
Hello,
in several apps running over postgres 8.0/8.1 I've used following
"full-text" search engine - imagine there are two tables:
1) Documents - with "docId", "title", "abstract" and "body" columns
2) Index - with "docId", "word" and "score"
It's very easy to build the Index from the Documents table - for each
document (row in the Documents table) a list of words is built, and the
number of occurences is used as the score (there are different
possibilities, of course).
When searching for a string, at first I search for documents with at
least one of the words in the string. So for example when searching for
"apples oranges", at first I do something like
SELECT docId, word, score FROM Index where word IN ('apples','oranges');
Then I fetch all these results to PHP, and in PHP, I transform this
"list" of words into two-dimensional associative array with "document
ID" as a key at first level and words at second level.
$results = array(
$docId1 => array(
word1 => score,
word2 => score,
...
)
$docId2 => array(
word1 => score,
word2 => score,
...
)
...
);
And this array is processed (in PHP) to get the final score - each OR
connective is translated as MAX() and each AND connective is translated
as MIN() function (on each element of the array).
For example the "apples AND oranges" would be translated as
MIN(score for apples, score for oranges)
Then I have a list of (docId,score) pairs, so I can fetch details for
the documents I want (the ones with the highest score, for example).
QUESTIONS & PROBLEMS
--------------------
What I really don't like on the "algorithm" is the need to process the
data outside of the database - fetch them into the PHP, process them and
query the database again to get the final result. It's not fast, it's
not shared for different parts of the projects written in different
languages, etc.
I'd like to transform all this processing into PL/pgSQL (or maybe
PL/Perl or something else) function, but I don't know
1) How to create a "table" in the form
documtent_id | word_1 | word_2 | ... | word_n
-------------------------------------------------
1345 | 11 | 12 | ... | 2
1202 | 1 | 0 | ... | 12
. . . . .
. . . . .
1129 | 20 | 1 | ... | 0
from the query
SELECT docId, word, score FROM Index WHERE word IN (word_1,..,word_n)
2) How to evaluate the function derived from the 'search string' on this
table, but this probably will work as an EXECUTE statement or
something like that.
Maybe there's a completely different solution to all of this, which I
can't see.
Thanks for your advices
Tomas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: tv@fuzzy.cz
Subject: Re: how to transform list to table and evaluate an expression on it?
In-Reply-To: <43BFE657.7050303@fuzzy.cz>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox