elasticsearch 查询

  

运行搜索的基本方法 : 一个是通过使用 REST request URI  发送搜索参数,另一个是通过使用  REST request body 来发送它们。

 

查看服务器当前内存状况:

http://localhost:9200/_cat/nodes?v&h=http,version,jdk,disk.total,disk.used,disk.avail,disk.used_percent,heap.current,heap.percent,heap.max,ram.current,ram.percent,ram.max,master

 

 REST request URI

 

搜索全部内容

http://39.107.36.109:9200/case_all/_search?q=*&sort=case_id:asc&pretty

 

 

搜索指定关键字

http://39.107.36.109:9200/case_all/_search?q=的&sort=case_id:asc&pretty

 

 

 

 REST request body

全文检索

 将上诉链接(http://39.107.36.109:9200/case_all/_search?q=的&sort=case_id:asc&pretty)转化为request body 为::


POST /case_all/_search HTTP/1.1
Host: 39.107.36.109:9200
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 8f09388b-cff0-c30a-0432-df947bc2bfaa


{
"query": { "match_all": {} },
"sort": [
{ "case_id": "asc" }
],
"size": 10
}


 

 

 

下面的例子做了一个 match_all 并且返回了 11 到 20 的文档。

 

POST /case_all/_search HTTP/1.1
Host: 39.107.36.109:9200
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 3157a0a8-5b62-6863-f714-c5da23fb561d

{
  "query": { "match_all": {} },
  "sort": [
    { "case_id": "asc" }
  ],
  "from":10,
  "size": 10
}

 

 

 

 

 字段检索

POST /case_all/_search HTTP/1.1
Host: 39.107.36.109:9200
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: e69ce996-3809-18af-ad4a-4ca2990b20f5

{
    "query": {
        "match": {
            "title": "上诉人 周"
        }
    },
    "from": 0,
    "size": 10
}

 

 这个例子返回了所有在 title中包含了 term 为 “上诉人” 或 “周” 的账户 :

 

 

 

下面这个例子是 match(match_phrase)的另一种方式,它返回了在 title中所有包含 phrase 为 “上诉人 周” 的账户 :

 1 POST /case_all/_search HTTP/1.1
 2 Host: 39.107.36.109:9200
 3 Content-Type: application/json
 4 Cache-Control: no-cache
 5 Postman-Token: 19e27ce1-b695-48e2-a3f6-dc23ea67094f
 6 
 7 {
 8     "query": {
 9         "match_phrase": {
10             "title": "上诉人 周"
11         }
12     },
13     "from": 0,
14     "size": 10,
15     "highlight":{
16         "pre_tags":["<tag1>","<tag2>"],
17         "post_tags":["</tag1>","</tag2>"],
18         "fields":{
19             "title":{}
20         }
21     }
22 
23 }

 

 

 

bool(ean) query

 

must  相当于and

POST /case_all/_search HTTP/1.1
Host: 39.107.36.109:9200
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: ecc88bc7-54d0-d1db-1214-a04ec7ebdc1f

{
    "query": {
        "bool":{
            "must":[
                {"match":{"title":"上诉人"}},
                {"match":{"title":""}}
                ]
        }
    },
    "from": 0,
    "size": 10,
    "highlight":{
        "pre_tags":["<tag1>","<tag2>"],
        "post_tags":["</tag1>","</tag2>"],
        "fields":{
            "title":{}
        }
    }

}

 

 

 

should 相当于 or

must_not  不出现某某

 

 

 

聚合查询

sql类似为:

 select  case_level as out_level,count(1)  from case_all_jdbc  group by case_level order by count(1) desc

 1 POST /case_all_jdbc/_search HTTP/1.1
 2 Host: 39.107.36.109:9200
 3 Content-Type: application/json
 4 Cache-Control: no-cache
 5 Postman-Token: 21d19abf-ff06-906d-1729-158426d73ea0
 6 
 7 {
 8     "_source":{
 9         "include":["case_level"]
10     },
11     "aggs":{
12         "out_level":{
13             "terms":{
14                 "field":"case_level"
15             }
16         }
17     },
18     "size":0
19     
20 }

 

posted on 2018-06-11 15:43  ziyi_ang  阅读(139)  评论(0编辑  收藏  举报

导航