空搜索
最基本的搜索是空搜索潘酗,他沒有指定任何的查詢條件挠铲,只返回集群索引中的所有文檔在:
curl -i -XGET "localhost:9200/_search"
響應(yīng)內(nèi)容類似于這樣:
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 1877
{
"took" : 38,
"timed_out" : false,
"_shards" : {
"total" : 10,
"successful" : 10,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 1.0,
"hits" : [
{
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_score" : 1.0,
"_source" : {
"title" : "My first blog entry",
"text" : "I am starting to get the hang of this...",
"date" : "2014/01/02"
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about" : "I like to build cabinets",
"interests" : [
"forestry"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"first_name" : "Jane",
"last_name" : "Fir",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
},
{
"_index" : "website",
"_type" : "blog",
"_id" : "X7cGE2UB2XL2Aw5Pme58",
"_score" : 1.0,
"_source" : {
"title" : "My second blog entry",
"text" : "Still trying this out...",
"date" : "2014/01/01"
}
}
]
}
}
在所有索引的所有類型中搜索
curl -i -XGET "localhost:9200/_search"
在索引gb的所有類型中搜索
curl -i -XGET "localhost:9200/gb/_search"
在索引gb和us的所有類型中搜索
curl -i -XGET "localhost:9200/gb,us/_search"
在以g或u開頭的索引的所有類型中搜索
curl -i -XGET "localhost:9200/g*,u*/_search"
在索引gb的user類型中進(jìn)行搜索
curl -i -XGET "localhost:9200/gb/user/_search"
在索引gb和us的類型為user和tweet中搜索
curl -i -XGET "localhost:9200/gb,us/user,tweet/_search"
在所有索引的類型為user和tweet中搜索
curl -i -XGET "localhost:9200/_all/user,tweet/_search"
分頁(yè)
和SQL使用LIMIT關(guān)鍵字返回只有一頁(yè)的結(jié)果一樣西雀,ElasticSearch接受 from 和 size 參數(shù):
**size:每頁(yè)展示的結(jié)果數(shù)磺送,默認(rèn)10车份;
**from:從0開始跳過(guò)的結(jié)果數(shù)谋减,默認(rèn)0;
如果你想每頁(yè)顯示5個(gè)結(jié)果扫沼,頁(yè)碼從1到3出爹,那請(qǐng)求如下:
curl -i -XGET "localhost:9200/_search?size=5"
curl -i -XGET "localhost:9200/_search?size=5&from=5"
curl -i -XGET "localhost:9200/_search?size=5&from=10"
簡(jiǎn)易搜索
有兩種搜索模式:一種是"簡(jiǎn)易版"的查詢字符串將所有參數(shù)通過(guò)查詢字符串定義,另一種版本使用JSON完整的表示請(qǐng)求體缎除,這種富搜索語(yǔ)言叫做結(jié)構(gòu)化查詢語(yǔ)句(DSL)
查詢字符串搜索對(duì)于在命令行下運(yùn)行點(diǎn)對(duì)點(diǎn)查詢特別有用严就。例如這個(gè)語(yǔ)句查詢所有類型為 employee 并在 last_name 字段中為 "Fir"字符的文檔:
curl -i -XGET "localhost:9200/_all/employee/_search?q=last_name:Fir"
下一個(gè)語(yǔ)句查找last_name字段中包含"Fir"和age字段中包含"32"的結(jié)果:
curl -i -XGET "localhost:9200/_all/employee/_search?q=+last_name:Fir+age:32"
"+" 前綴表示語(yǔ)句匹配條件必須被滿足。類似的 "-" 前綴表示條件必須不被滿足器罐。所有條件如果沒有 + 或 -表示可選的梢为。