Question

can't seem to figure out the syntax for populating a hstore with a value of composite type -- note: I do not want to convert a record to a hstore.

select hstore('hello => ROW(1,2)');

I know it's something simple; However, google is not my friend today.

use case : custom inverted index.

The data is modelling an inverted index of lexemes, the composite data types are various probabilities related to the lexemes which I will use to implement document clustering. Does anyone know a better way of doing this ? I'm open to using an external system if it allows attaching attributes to key->posting pairs in the inverted index.

I'd use something external if it had solid support for what I am trying to do, I suspect that sticking 3-10k lexemes per tuple and then doing batch processing on them is gonna be nasty as the whole hstore will have to be parsed and converted.

At the moment my lexemes are in the range of 50-1k per tuple, mostly in the low hundreds, and i'm just doing it for my research algorithms. But there has to be a better way of doing this.

Was it helpful?

Solution

Strings in hstore are double-quoted. hstore only supports text values, nothing else, so you must store other types as their text representations:

SELECT hstore('k => "(1,2)"');

eg:

regress=> SELECT (hstore('k => "(1,2)"')) -> 'k';
 ?column? 
----------
 (1,2)
(1 row)

This means you have to cast the values to use them, eg:

regress=> CREATE TYPE pair AS (a integer, b integer);
CREATE TYPE
regress=> SELECT ((hstore('k => "(1,2)"')) -> 'k')::pair;
 pair  
-------
 (1,2)
(1 row)

or using arrays instead to avoid the composite type:

regress=> SELECT ((hstore('k => "{1,2}"')) -> 'k')::integer[];
 int4  
-------
 {1,2}
(1 row)

Arrays are indexed from 1 with the [] operator, eg [1].

This is generally horrendously inefficient because the values must be parsed and converted every single time. It's not really practical to suggest alternatives when you haven't said much about the nature of your problem and why you want hstore in the first place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top