需求
由於 kibana3 中,不支持直接在請求的 url 中設置搜索的 type (是不是我不知道???)。
為了支持特定 type 的搜索,所以我設置了個下每個 panel 的查詢語句,讓它增加一個:
"query_string": {"query": " _type:\"my_type\" "}
結果今天在查一個 bug 的時候,發現這樣有一個坑,,,
問題
由於URL請求的路徑並沒不能指定 type ,所以每一次的搜索,依然會查詢整個 index,只是在獲取結果時候,再 query 了一次 "_type" 字段。
如果在同一個 index 下,存在不同 type 中,某個字段類型不一致的情況,那將可能導致搜索不到想要的結果。(因為不同的 type 有不同的 _mapping)
示例1:我在一個字段第一次存的時候,filed1 存為了 string 類型,而又新建了另一個 type,且 filed1 字段類型變為了 date,
后來在對這個字段進行時間 range 過濾操作的時候,發現總是匹配不到想要的結果,hits 總是空數組,
URL:http://localhost:9200/index/_search { "query": { "filtered": { "query": { "bool": { "should": [{ "query_string": { "query": "_type:\"my_type\"" } }] } }, "filter": { "bool": { "must": [{ "range": { "過期時間": { "from": 1860000665, "to": 2550091665 } } }] } } } }, "from": 0 }
結果1:
{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "failed": 0 }, "hits": { "total": 0, "max_score": 1, "hits": [] ......
但是,完全相同的查詢語句,如果在 URL 中指定 type,那么過濾就 OK 了,,,
示例2:
URL: http://200.200.194.155:9200/index/my_type/_search { "query": { "filtered": { "query": { "bool": { "should": [{ "query_string": { "query": "_type:\"my_type1\"" } }] } }, "filter": { "bool": { "must": [{ "range": { "過期時間": { "from": 1861665, "to": 25500008799861665 } } }] } } } }, "from": 0 }
結果2:
{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "failed": 0 }, "hits": { "total": 67, "max_score": 1, "hits": [ { ......
解決
確保相同字段的數據類型一致,,,
比如上面的問題,我刪除了該字段類型為 string 的那個 type 就完全 OK 了。