Elasticsearch 2.20 搜索詳解
來自: http://my.oschina.net/secisland/blog/614690
之前的文章中簡單介紹過搜索,在這里對搜索進行詳細的介紹。在之前的介紹中可以知道,搜索有兩種方式,一種是通過url參數進行搜索,另一種是通過POST請求參數進行搜索。
url參數搜索
請求:GET http://localhost:9200/secilog/log/_search?參數,多個參數用&分來。
參數詳解:
| 參數 | 解釋 |
| q | 查詢字符串,例如:q=syslog |
| df | 當查詢中沒有定義前綴的時候默認使用的字段。 |
| analyzer | 當分析查詢字符串的時候使用的分詞器 |
| lowercase_expanded_terms | 搜索的時候忽略大小寫標志,默認為true |
| analyze_wildcard | 通配符或者前綴查詢是否被分析,默認false |
| default_operator | 默認多個條件的關系,AND或者OR,默認OR |
| lenient | 如果設置為true,字段類型轉換失敗的時候將被忽略,默認為false |
| explain | 在每個返回結果中,將包含評分機制的解釋 |
| _source | 是否包含元數據,同時支持_source_include 和_source_exclude |
| fields | 只放回索引中指定的列,多個列中間用逗號分開 |
| sort | 排序,例如fieldName:asc或者fieldName:desc |
| track_scores | 評分軌跡,當排序的時候,設置為true的時候返回評分的信息 |
| timeout | 超時的時間設置 |
| terminate_after | 在每個分片中查詢的最大條數,如果設置返回結果中會有一個terminated_early字段 |
| from | 開始的記錄數 |
| size | 搜索結果中的條數 |
| search_type | 搜索的類型,可以是dfs_query_then_fetch,query_then_fetch,默認query_then_fetch |
POST請求參數搜索
請求:POST http://localhost:9200/secilog/log/_search
參數在請求頭中。
參數是JSON格式的查詢領域語法(query dsl)。例如:
{
"query" : {
"term" : { "type" : "syslog" }
}
} 返回的結果:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [ {
"_index" : "secilog",
"_type" : "log",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"type" : "syslog",
"message" : "secilog test test test "
}
} ]
}
} 如果需要搜索分頁,可以通過from size組合來進行。from表示從第幾行開始,size表示查詢多少條文檔。from默認為0,size默認為10,例如:
{
"from" : 0, "size" : 10,
"query" : {
"term" : { "type" : "syslog" }
}
} 注意:size的大小不能超過index.max_result_window這個參數的設置,默認為10,000。
排序sort
可以通過一個或者多個字段進行排序。例如:
{
"sort" : [
{"type" :{"order": "asc"}},
{"message" :{"order": "asc"}},
]
} 返回的結果為:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : null,
"hits" : [ {
"_index" : "secilog",
"_type" : "log",
"_id" : "2",
"_score" : null,
"_source" : {
"type" : "file",
"message" : "Another secilog test "
},
"sort" : [ "file", "test" ]
}, {
"_index" : "secilog",
"_type" : "log",
"_id" : "1",
"_score" : null,
"_source" : {
"type" : "syslog",
"message" : "secilog test test test "
},
"sort" : [ "syslog", "test" ]
} ]
}
} 當一個字段的內容有多個值的時候,系統支持一些計算進行排序,包括min,max,sum,avg,median(中間值),例如下面的請求表示order有多個值,取平均值排序:
{
"query" : {
...
},
"sort" : [
{"price" : {"order" : "asc", "mode" : "avg"}}
]} 數據列過濾
數據列過濾允許在查詢的時候不顯示原始數據或者顯示部分原始字段。例如不顯示原始文檔:
{
"_source": false,
"query" : {
"term" : { "type" : "syslog" }
}} 例如顯示部分文檔列:
{
"_source": "obj.*",
"query" : {
"term" : { "type" : "syslog" }
}
} {
"_source": [ "obj1.*", "obj2.*" ],
"query" : {
"term" : { "type" : "syslog" }
}
} 例如可以包含或者排除某些列:
{
"_source": {
"include": [ "obj1.*", "obj2.*" ],
"exclude": [ "*.description" ]
},
"query" : {
"term" : { "type" : "syslog" }
}
} 腳本支持
同樣對搜索也支持腳本,例如:
請求:POST http://localhost:9200/secilog/log/_search?pretty
參數:
{
"query": {
"term": {
"type": "syslog"
}
},
"script_fields": {
"test1": {
"script": "doc['type'].value * 2"
}
}
} 得到返回結果:
{
"took" : 16,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [ {
"_index" : "secilog",
"_type" : "log",
"_id" : "1",
"_score" : 1.0,
"fields" : {
"test1" : [ "syslogsyslog" ]
}
} ]
}
} 賽克藍德(secisland)后續會逐步對Elasticsearch的最新版本的各項功能進行分析,近請期待。也歡迎加入secisland公眾號進行關注。