Elasticsearch查询

  有两种基本的方式来运行搜索:一种是在REST请求的URI中发送搜索参数,另一种是将搜索参数发送到REST请求体中。请求体方法的表达能力更好,并且你可以使用更加可读的JSON格式来定义搜索。下面我们用请求体的方式做介绍:

1、查询全部

 curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
             {
               "query": { "match_all": {} }
             }'

2、指定查询返回结果个数

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
         {
           "query": { "match_all": {} },
           "size": 1
         }'

3、指定查询返回结果范围

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
         {
           "query": { "match_all": {} },
           "from": 10,
           "size": 10
         }'

4、指定查询返回结果排序规则

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
         {
           "query": { "match_all": {} },
           "sort": { "balance": { "order": "desc" } }
         }'

5、指定查询返回结果字段

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
         {
           "query": { "match_all": {} },
           "_source": ["account_number", "balance"]
         }'

6、指定查询账户编号为20的文档

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
         {
           "query": { "match": { "account_number": 20 } }
         }'

7、指定查询地址中包含“mill”的所有账户

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
         {
           "query": { "match": { "address": "mill" } }
         }'

8、指定查询地址中包含“mill”或者包含“lane”的账户

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
         {
           "query": { "match": { "address": "mill lane" } }
         }' 

9、指定查询地址中包含“mill lane”的账户

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
         {
           "query": { "match_phrase": { "address": "mill lane" } }
         }'
posted @ 2017-12-05 14:34  穷开心y  阅读(257)  评论(0编辑  收藏  举报