Elasticsearch 提供了JSON风格的特定领域语言,你可以用来执行自己的查询
1.query索引中所有文档
curl 'localhost:9200/bank/_search?pretty' -d '
{
"query":{"match_all":{}}
}
返回结果:
{
"took" : 63,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : 1.0,
"hits" : [ {
"_index" : "bank",
"_type" : "account",
"_id" : "25",
"_score" : 1.0,
"_source" : {
"account_number" : 25,
"balance" : 40540,
"firstname" : "Virginia",
"lastname" : "Ayala",
"age" : 39,
"gender" : "F",
"address" : "171 Putnam Avenue",
"employer" : "Filodyne",
"email" : "virginiaayala@filodyne.com",
"city" : "Nicholson",
"state" : "PA"
}
}, {
"_index" : "bank",
"_type" : "account",
"_id" : "44",
"_score" : 1.0,
"_source" : {
"account_number" : 44,
"balance" : 34487,
"firstname" : "Aurelia",
"lastname" : "Harding",
"age" : 37,
"gender" : "M",
"address" : "502 Baycliff Terrace",
"employer" : "Orbalix",
"email" : "aureliaharding@orbalix.com",
"city" : "Yardville",
"state" : "DE"
}
},
....
} ]
}
}
ruby代码:
require "elasticsearch"
$elastic = Elasticsearch::Client.new hosts: [ 'localhost:9200' ], randomize_hosts: true
res = $elastic.search index:'bank',body:{query:{match_all:{}}}
puts res
2.elasticsearch 默认返回文档的前十条数据,在查询得时候可以指定想要返回的文档数量
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"size": 1
}'
只返回索引中第一条文档
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : 1.0,
"hits" : [ {
"_index" : "bank",
"_type" : "account",
"_id" : "25",
"_score" : 1.0,
"_source" : {
"account_number" : 25,
"balance" : 40540,
"firstname" : "Virginia",
"lastname" : "Ayala",
"age" : 39,
"gender" : "F",
"address" : "171 Putnam Avenue",
"employer" : "Filodyne",
"email" : "virginiaayala@filodyne.com",
"city" : "Nicholson",
"state" : "PA"
}
} ]
}
}
ruby代码:
require "elasticsearch"
$elastic = Elasticsearch::Client.new hosts: [ 'localhost:9200' ], randomize_hosts: true
res = $elastic.search index:'bank',body:{query:{match_all:{}},size:1}
puts res
3.返回索引中第11-20条文档(from: 开始位置 size:从开始位置返回的文档数)
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}'
返回结果
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : 1.0,
"hits" : [ {
"_index" : "bank",
"_type" : "account",
"_id" : "227",
"_score" : 1.0,
"_source" : {
"account_number" : 227,
"balance" : 19780,
"firstname" : "Coleman",
"lastname" : "Berg",
"age" : 22,
"gender" : "M",
"address" : "776 Little Street",
"employer" : "Exoteric",
"email" : "colemanberg@exoteric.com",
"city" : "Eagleville",
"state" : "WV"
}
},
.....(省略9条)
} ]
}
}
ruby代码:
require "elasticsearch"
$elastic = Elasticsearch::Client.new hosts: [ 'localhost:9200' ], randomize_hosts: true
res = $elastic.search index:'bank',body:{query:{match_all:{}},from:10,size:10}
puts res
4.按照年龄大小排序,返回年龄最大的Top3
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"sort": { "age": { "order": "desc" } },
"size":3
}'
返回结果:
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : null,
"hits" : [ {
"_index" : "bank",
"_type" : "account",
"_id" : "948",
"_score" : null,
"_source" : {
"account_number" : 948,
"balance" : 37074,
"firstname" : "Sargent",
"lastname" : "Powers",
"age" : 40,
"gender" : "M",
"address" : "532 Fiske Place",
"employer" : "Accuprint",
"email" : "sargentpowers@accuprint.com",
"city" : "Umapine",
"state" : "AK"
},
"sort" : [ 40 ]
}, {
"_index" : "bank",
"_type" : "account",
"_id" : "40",
"_score" : null,
"_source" : {
"account_number" : 40,
"balance" : 33882,
"firstname" : "Pace",
"lastname" : "Molina",
"age" : 40,
"gender" : "M",
"address" : "263 Ovington Court",
"employer" : "Cytrak",
"email" : "pacemolina@cytrak.com",
"city" : "Silkworth",
"state" : "OR"
},
"sort" : [ 40 ]
}, {
"_index" : "bank",
"_type" : "account",
"_id" : "468",
"_score" : null,
"_source" : {
"account_number" : 468,
"balance" : 18400,
"firstname" : "Foreman",
"lastname" : "Fowler",
"age" : 40,
"gender" : "M",
"address" : "443 Jackson Court",
"employer" : "Zillactic",
"email" : "foremanfowler@zillactic.com",
"city" : "Wakarusa",
"state" : "WA"
},
"sort" : [ 40 ]
} ]
}
}
ruby代码:
require "elasticsearch"
$elastic = Elasticsearch::Client.new hosts: [ 'localhost:9200' ], randomize_hosts: true
res = $elastic.search index:'bank',body:{query:{match_all:{}},sort:{"age":{order:"desc"}}}
puts res