Elasticsearch第三篇:查詢詳解

從第一篇開始,我用的ES版本就是7.8.0的,與低版本略有不一樣,不一樣點能夠參考官方介紹,最大的不一樣就是拋棄 type 這一律念,爲了方便測試,首先創建一個學生成績的索引庫(在創建的同時,規定字段類型,並指定IK中文分詞)閉包

PUT http://localhost:9200/db_student
{
"mappings": { "properties": { "class": { "type": "integer", "store": true, "index":true }, "chinese": { "type": "integer", "store": true, "index":true }, "english": { "type": "integer", "store": true, "index":true }, "math": { "type": "integer", "store": true, "index":true }, "name": { "type": "text", "store": true, "index":true }, "school": { "type": "text", "store": true, "index":true, "analyzer":"ik_max_word" } } } }

爲了方便測試,須要先插入測試數據,以下,一共插入8條記錄app

PUT http://localhost:9200/db_student/_doc/1
{
"chinese":80,
"class":10,
"english":90,
"math":100,
"name":"Vincent",
"school":"華南理工大學"
}

PUT http://localhost:9200/db_student/_doc/2
{
"chinese":80,
"class":11,
"english":85,
"math":90,
"name":"Kitty",
"school":"華南理工大學"
}

PUT http://localhost:9200/db_student/_doc/3
{
"chinese":90,
"class":12,
"english":65,
"math":70,
"name":"Thomas",
"school":"華南師範大學"
}

PUT http://localhost:9200/db_student/_doc/4
{
"chinese":70,
"class":12,
"english":85,
"math":85,
"name":"Lucy",
"school":"華南師範大學"
}

PUT http://localhost:9200/db_student/_doc/5
{
"chinese":60,
"class":12,
"english":95,
"math":95,
"name":"Lily",
"school":"華南農業大學"
}

PUT http://localhost:9200/db_student/_doc/6
{
"chinese":87,
"class":12,
"english":55,
"math":98,
"name":"Coco",
"school":"華南農業大學"
}

PUT http://localhost:9200/db_student/_doc/7
{
"chinese":88,
"class":12,
"english":65,
"math":85,
"name":"Allen",
"school":"中山大學"
}

PUT http://localhost:9200/db_student/_doc/8
{"chinese":77,
"class":12,
"english":45,
"math":89,
"name":"Zack",
"school":"中山大學"
}

 打開 Kibana 能夠看到已經插入的數據,以下post

 

數據已經插入,如今能夠來實現基本的查詢了。測試

一、查詢全部索引庫、全部文檔spa

POST  http://localhost:9200/_search
{
   "query": { "match_all": {} } 
}

二、查詢索引庫 db_student 全部文檔3d

POST  http://localhost:9200/db_student/_search
或者是 http://localhost:9200/db_student/_doc/_search
{ "query": { "match_all": {} } }

三、根據文檔編號 id=1 來獲取文檔code

GET  http://localhost:9200/db_student/_doc/1

四、查詢 class=10 的學生blog

      注意:term 在這裏至關於 = 的邏輯,可是若是是字符串,還能夠是包含的邏輯。排序

POST  http://localhost:9200/db_student/_search
{
   "query": {
       "bool":{
             "must":[
                   {"term":{"class":10}}
             ]
       }
   }   
}

五、And 邏輯查詢,如查詢 class=10 而且 name=vincent 的文檔索引

POST  http://localhost:9200/db_student/_search
{
  "query": {
     "bool":{
         "must":[
             {"term":{"name":"vincent"}},
             {"term":{"class":10}}
         ]
     }
  }
}

六、模糊查詢,例如,查詢 school 包含 「華南」 的文檔

POST  http://localhost:9200/db_student/_search
{
   "query": {
       "bool":{
             "must":[
                   {"match":{"school":"華南"}}
             ]
       }
   }   
}

也能夠是term

POST  http://localhost:9200/db_student/_search
{
   "query": {
       "bool":{
             "must":[
                   {"term":{"school":"華南"}}
             ]
       }
   }   
}

七、範圍查詢,查詢 english 大於等於90,小於等於100的文檔

 注意:from、to 都是閉包的,包含等於

POST   http://localhost:9200/db_student/_search
{
   "query": {
       "bool":{
             "must":[
                   {"range":{"english":{"from":90,"to":99}}}
             ]
       }
   }   
}

還能夠查大於、小於的邏輯,例如查詢  english 大於90的文檔

注意:gt 表示大於, lt 表示小於 ,這二者都不包含等於

POST http://localhost:9200/db_student/_search
{
   "query": {
       "bool":{
             "must":[
                   {"range":{"english":{"gt":90}}}
             ]
       }
   }   
}

八、高亮顯示,例如 name 高亮

POST  http://localhost:9200/db_student/_search
{
   "query": {
      "term":{"name":"vincent"}
   },
   "highlight":{
          "pre_tags" : "<a class='red'>",
          "post_tags" : "</a>",
          "fields" : {
             "name" : {}
        }
   }
}

查詢結果是:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.7917595,
        "hits": [
            {
                "_index": "db_student",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.7917595,
                "_source": {
                    "chinese": 80,
                    "class": 10,
                    "english": 90,
                    "math": 100,
                    "name": "Vincent",
                    "school": "華南理工大學"
                },
                "highlight": {
                    "name": [
                        "<a class='red'>Vincent</a>"
                    ]
                }
            }
        ]
    }
}

九、分頁和排序,先按照 english 倒序,再按 math 升序,每頁3條記錄,取第一頁

POST  http://localhost:9200/db_student/_search
{
    "query": {
        "match_all":{}
    },
    "from": 0, 
    "size": 3, 
    "sort":{ 
        "english" : {"order" : "desc"},
        "math": {"order" : "asc"}
        
    }
}

查詢結果是

{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 8,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "db_student",
                "_type": "_doc",
                "_id": "5",
                "_score": null,
                "_source": {
                    "chinese": 60,
                    "class": 12,
                    "english": 95,
                    "math": 95,
                    "name": "Lily",
                    "school": "華南農業大學"
                },
                "sort": [
                    95,
                    95
                ]
            },
            {
                "_index": "db_student",
                "_type": "_doc",
                "_id": "1",
                "_score": null,
                "_source": {
                    "chinese": 80,
                    "class": 10,
                    "english": 90,
                    "math": 100,
                    "name": "Vincent",
                    "school": "華南理工大學"
                },
                "sort": [
                    90,
                    100
                ]
            },
            {
                "_index": "db_student",
                "_type": "_doc",
                "_id": "4",
                "_score": null,
                "_source": {
                    "chinese": 70,
                    "class": 12,
                    "english": 85,
                    "math": 85,
                    "name": "Lucy",
                    "school": "華南師範大學"
                },
                "sort": [
                    85,
                    85
                ]
            }
        ]
    }
}

 聚合查詢、統計查詢等等, 稍後補上

相關文章
相關標籤/搜索