【问题标题】:Elasticsearch on text field of postgres tableElasticsearch在postgres表的文本字段上
【发布时间】:2013-12-22 15:45:03
【问题描述】:

我正在寻找一些关于如何开始满足以下要求的高级建议:

我们有一个运行在 heroku 上的 ruby​​ sinatra API 服务,可以将用户的电子邮件与我们的系统同步。

我们将用户的电子邮件存储在 postgres 数据库中,该数据库分为主题、文本和 html 字段。

我想使用 elasticsearch 搜索这些电子邮件,但搜索只能搜索用户收件箱中的电子邮件。

谁能告诉我如何索引 postgres 电子邮件表以及如何过滤搜索以使其仅限于用户电子邮件的第一步?

电子邮件表的架构是:

CREATE TABLE emails
(
  id serial NOT NULL,
  subject text,
  body text,
  personal boolean,
  sent_at timestamp without time zone,
  created_at timestamp without time zone,
  updated_at timestamp without time zone,
  addresses text,
  account_id integer NOT NULL,
  sender_user_id integer,
  sender_contact_id integer,
  html text,
  folder text,
  draft boolean DEFAULT false,
  check_for_response timestamp without time zone,
  send_time timestamp without time zone,
  send_time_jid text,
  check_for_response_jid text,
  message_id text,
  in_reply_to text,
  CONSTRAINT emails_pkey PRIMARY KEY (id),
  CONSTRAINT emails_account_id_fkey FOREIGN KEY (account_id)
      REFERENCES accounts (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT emails_sender_contact_id_fkey FOREIGN KEY (sender_contact_id)
      REFERENCES contacts (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT emails_sender_user_id_fkey FOREIGN KEY (sender_user_id)
      REFERENCES users (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE
)

【问题讨论】:

  • 电子邮件数据库的架构是什么?
  • @kielni 我已经用架构更新了问题

标签: ruby heroku sinatra elasticsearch


【解决方案1】:

听起来您在搜索时关心的唯一字段是正文、帐户 ID 和文件夹。如果需要,您可以随时添加更多内容(例如,索引日期以启用日期范围搜索可能很有用)。不应分析文件夹名称,以便 Elasicsearch 不会对其应用词干提取,并且您可以执行术语(完全匹配)过滤器以仅检索特定文件夹中的电子邮件。

这是一个仅包含这三个字段的映射:

{
  "email" : {
    "properties" : {
      "account_id" : { "type" : "integer" },
      "body" : { "type" : "string" },
      "folder" : { "type" : "string", "index" : "not_analyzed" },
      "id" : { "type" : "integer" }
    }
  }
}

您可以通过以下方式进行搜索。使用term filters 将结果限制为特定文件夹(“收件箱”)和特定用户(account_id=123)。使用query string 查找特定单词、短语等。

{
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "term": {
              "folder": "Inbox"
            }
          },
          {
            "term": {
              "account_id": 111
            }
          }
        ]
      },
      "query": {
        "query_string": {
          "query": "one"
        }
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    • 2022-12-13
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多