2013-12-09 64 views
1

我使用的jdbc river,我可以創建以下指標:elasticsearch - 複雜的查詢

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

它有助於獲得一些用於複製問題的示例數據(理想情況下,它是一個可立即執行的curl命令列表)。噢,如果你還沒有分開測試三個部分。這可能會縮小問題的範圍 – towr

回答

0

事實證明,您需要使用type_mapping節來指定字段在jdbc河中不被分析,正常映射節點將被忽略。

下面是如何橫空出世:

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" 
     } 
    } 
    } 
}