elasticsearch - 复杂查询

时间:2013-12-09 17:31:40

标签: elasticsearch elasticsearch-jdbc-river

我正在使用jdbc river,我可以创建以下索引:

curl -XPUT 'localhost:9201/_river/email/_meta' -d '{
    "type" : "jdbc",
    "jdbc" : {
        "strategy":"simple", 
        "poll":"10",
        "driver" : "org.postgresql.Driver",
        "url" : "jdbc:postgresql://localhost:5432/api_development",
        "username" : "paulcowan",
        "password" : "",
        "sql" : "SELECT id, subject, body, personal, sent_at, read_by, account_id, sender_user_id, sender_contact_id, html, folder, draft FROM emails"
    },
    "index" : {
        "index" : "email",
        "type" : "jdbc"
    },
    "mappings" : {
        "email" : {
      "properties" : {
        "account_id" : { "type" : "integer" },
        "subject" : { "type" : "string" },
        "body" : { "type" : "string" },
        "html" : { "type" : "string" },
        "folder" : { "type" : "string", "index" : "not_analyzed" },
        "id" : { "type" : "integer" }
      }
      }
    }
}'

我可以像这样使用curl运行基本查询:

curl -XGET 'http://localhost:9201/email/jdbc/_search?pretty&q=fullcontact'

我回来了结果

但我想要做的是将结果限制为特定的电子邮件account_id和特定的电子邮件,我运行以下查询:

curl -XGET 'http://localhost:9201/email/jdbc/_search' -d '{
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "term": {
              "folder": "INBOX"
            }
          },
          {
            "term": {
              "account_id": 1
            }
          }
        ]
      },
      "query": {
        "query_string": {
          "query": "fullcontact*"
        }
      }
    }
  }
}'

我得到以下结果:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

有谁能告诉我我的查询有什么问题?

1 个答案:

答案 0 :(得分:0)

事实证明,你需要使用type_mapping部分来指定jdbc河中的not_analyzed字段,忽略普通的映射节点。

以下是结果:

curl -XPUT 'localhost:9200/_river/your_index/_meta' -d '{
    "type" : "jdbc",
    "jdbc" : {
        "strategy":"simple", 
        "poll":"10",
        "driver" : "org.postgresql.Driver",
        "url" : "jdbc:postgresql://localhost:5432/api_development",
        "username" : "user",
        "password" : "your_password",
        "sql" : "SELECT field_one, field_two, field_three, the_rest FROM blah"
    },
    "index" : {
        "index" : "your_index",
        "type" : "jdbc",
    "type_mapping": "{\"your_index\" : {\"properties\" : {\"field_two\":{\"type\":\"string\",\"index\":\"not_analyzed\"}}}}"
    }
}'

奇怪或烦人的是,type_mapping部分采用json编码的字符串而不是普通的json节点:

我可以通过运行来检查映射:

# check mappings
curl -XGET 'http://localhost:9200/your_index/jdbc/_mapping?pretty=true'

应该提供类似的内容:

{
  "jdbc" : {
    "properties" : {
      "field_one" : {
        "type" : "long"
      },
      "field_two" : {
        "type" : "string",
        "index" : "not_analyzed",
        "omit_norms" : true,
        "index_options" : "docs"
      },
      "field_three" : {
        "type" : "string"
      }
    }
  }
}