如何使用子查询在Sphinx的多值属性(MVA)中查询类型

时间:2016-05-13 18:22:51

标签: postgresql subquery search-engine sphinx

我的数据库是PostgreSQL,我想使用Sphinx搜索引擎来索引我的数据;

如何使用sql_attr_multi获取关系数据?

postgresql中的表格,模式是:

crm=# \d orders
                                   Table "orders"
     Column      |            Type             |               Modifiers
-----------------+-----------------------------+----------------------------------------
 id              | bigint                      | not null
 trade_id        | bigint                      | not null
 item_id         | bigint                      | not null
 price           | numeric(10,2)               | not null
 total_amount    | numeric(10,2)               | not null
 subject         | character varying(255)      | not null default ''::character varying
 status          | smallint                    | not null default 0
 created_at      | timestamp without time zone | not null default now()
 updated_at      | timestamp without time zone | not null default now()
Indexes:
    "orders_pkey" PRIMARY KEY, btree (id)
    "orders_trade_id_idx" btree (trade_id)




crm=# \d trades
                                     Table "trades"
        Column         |            Type             |               Modifiers
-----------------------+-----------------------------+---------------------------------------
 id                    | bigint                      | not null
 operator_id           | bigint                      | not null
 customer_id           | bigint                      | not null
 category_ids          | bigint[]                    | not null
 total_amount          | numeric(10,2)               | not null
 discount_amount       | numeric(10,2)               | not null
 created_at            | timestamp without time zone | not null default now()
 updated_at            | timestamp without time zone | not null default now()
Indexes:
    "trades_pkey" PRIMARY KEY, btree (id)

Sphinx的配置是:

source trades_src
{
  type        = pgsql
  sql_host    = 10.10.10.10
  sql_user    = ******
  sql_pass    = ******
  sql_db      = crm
  sql_port    = 5432

  sql_query   = \
    SELECT id, operator_id, customer_id, category_ids, total_amount, discount_amount, \
         date_part('epoch',created_at) AS created_at, \
         date_part('epoch',updated_at) AS updated_at \
      FROM public.trades;

  #attributes
  sql_attr_bigint = operator_id
  sql_attr_bigint = customer_id

  sql_attr_float  = total_amount
  sql_attr_float  = discount_amount


  sql_attr_multi = bigint category_ids from field category_ids

  #sql_attr_multi = bigint order_ids from query; SELECT id FROM orders
  #how can i add where condition is the query for orders? eg. WHERE trade_id = ?


  sql_attr_timestamp  = created_at
  sql_attr_timestamp  = updated_at
}

我在category_ids字段上使用MVA (multi-valued attributes),它是Postgresql中的ARRAY类型。

但我不知道如何在order_ids上定义MVA。它将通过子查询?

1 个答案:

答案 0 :(得分:1)

从狮身人面像论坛复制....

sql_attr_multi = bigint order_ids from query; SELECT trade_id,id FROM orders ORDER BY trade_id

查询的第一列是sphinx' document_id' (即主id中的sql_query

第二列是要插入该文档的MVA数组的值。

(可能并不严格需要ORDER BY,但如果由document_id IIRC订购,则sphinx处理数据的速度要快得多。