Elastic Search 6.5.1 API 簡單使用

簡介

Elastic Search 就像一個數據庫,只不過搜索功能比數據庫強大,API 操作起來就是發送 http 請求,類似於數據庫裏面的寫SQL語句(此處只是記錄ES的簡單理解和簡單操作)

比如一個 ElasticSearch 請求

http://47.105.159.23:9200/manage/employee/_search

索引--index-- manage ===== 類似於MySQL裏面的一個數據庫

類型--type-- employee ===== 類似於MySQL裏面的一張表

操作--oper-- _search ====== 類似於MySQL裏面的 select * from employee

插入 Insert

添加操作一般有兩種方式

  1. PUT請求,需要自己攜帶請求ID,將請求的數據保存在此ID下,若此ID原本存在,則將之前的數據覆蓋(不推薦,不友好,我也就不演示)
  2. POST請求,只需要傳遞請求的數據,ES自動生成ID,插入成功後將ID返回
    Request
    POST  http://47.105.159.23:9200/manage/employee/
    Content-Type  application/json
    {
    	"name": "周益",
    	"phone": "18874791111",
    	"sex": 2,
    	"salary": 15000,
    	"post": "IOS工程師",
    	"dept": {
    		"id": "1001",
    		"name": "技術中心"
    	},
    	"join_time": "2018-10-03",
    	"desc": "a IOS boy"
    }
    
    Response
    {
        "_index": "manage",
        "_type": "employee",
        "_id": "nVZ-l2cBof4Qu3GrFc1L",
        "_version": 1,
        "result": "created",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 1
    }

    由於我是使用的 Postman 發送的請求,所以我的請求格式如下,JSON 格式比較直觀,便於理解

查詢 Search

查詢就有很多種了,這裏只記錄最基本最簡單的

  1. 沒有查詢條件(查詢所有)
    GET  http://47.105.159.23:9200/manage/employee/_search

  2. 帶查詢條件

term :一般用戶常量值的匹配,最常用爲數值匹配

GET  http://47.105.159.23:9200/manage/employee/_search
{
    "query": {
    	"term": {
    		"salary": 6000
    	}
    }
}
======Response========
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "oVYCmGcBof4Qu3Gr8c33",
                "_score": 1,
                "_source": {
                    "name": "青萍",
                    "phone": "18874795555",
                    "sex": 2,
                    "salary": 6000,
                    "post": "會計",
                    "dept": {
                        "id": "1002",
                        "name": "財務部門"
                    },
                    "join_time": "2018-08-03",
                    "desc": "a sunny girl"
                }
            }
        ]
    }
}

terms :用於一個字段匹配多個值時使用

GET   http://47.105.159.23:9200/manage/employee/_search
{
    "query": {
    	"terms": {
    		"salary": [6000, 10000]
    	}
    }
}
========Response===========
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nFZul2cBof4Qu3GrDc0v",
                "_score": 1,
                "_source": {
                    "name": "高節",
                    "phone": "18874796310",
                    "sex": 1,
                    "salary": 10000,
                    "post": "Java工程師",
                    "dept": {
                        "id": "1001",
                        "name": "技術中心"
                    },
                    "join_time": "2018-12-03",
                    "desc": "a love of work boy"
                }
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "oVYCmGcBof4Qu3Gr8c33",
                "_score": 1,
                "_source": {
                    "name": "青萍",
                    "phone": "18874795555",
                    "sex": 2,
                    "salary": 6000,
                    "post": "會計",
                    "dept": {
                        "id": "1002",
                        "name": "財務部門"
                    },
                    "join_time": "2018-08-03",
                    "desc": "a sunny girl"
                }
            }
        ]
    }
}

range :範圍查找,一般可用於數值,日期

GET  http://47.105.159.23:9200/manage/employee/_search
{
    "query": {
    	"range": {
    		"join_time": {
    			"lt": "2018-12-02",
    			"gt": "2018-10-02"
    		}
    	}
    }
}
===========Response=================
{
    "took": 21,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nVZ-l2cBof4Qu3GrFc1L",
                "_score": 1,
                "_source": {
                    "name": "周益",
                    "phone": "18874791111",
                    "sex": 2,
                    "salary": 15000,
                    "post": "IOS工程師",
                    "dept": {
                        "id": "1001",
                        "name": "技術中心"
                    },
                    "join_time": "2018-10-03",
                    "desc": "a IOS boy"
                }
            }
        ]
    }
}

match :普通查詢,一般用於文本搜索,以一個漢字或者一個單詞爲最小單位,比如內容爲hello word,輸入hello能夠匹配成功,但是輸入hell則匹配不上

GET  http://47.105.159.23:9200/manage/employee/_search
{
    "query": {
    	"match": {
    		"desc": "boy man"
    	}
    }
}
=============Response==============
{
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 0.6931472,
        "hits": [
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "oFYAmGcBof4Qu3Gr880L",
                "_score": 0.6931472,
                "_source": {
                    "name": "李嶽",
                    "phone": "18874794444",
                    "sex": 2,
                    "salary": 12000,
                    "post": "Android工程師",
                    "dept": {
                        "id": "1001",
                        "name": "技術中心"
                    },
                    "join_time": "2018-04-03",
                    "desc": "a rude man"
                }
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nVZ-l2cBof4Qu3GrFc1L",
                "_score": 0.37881336,
                "_source": {
                    "name": "周益",
                    "phone": "18874791111",
                    "sex": 2,
                    "salary": 15000,
                    "post": "IOS工程師",
                    "dept": {
                        "id": "1001",
                        "name": "技術中心"
                    },
                    "join_time": "2018-10-03",
                    "desc": "a IOS boy"
                }
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "olYDmGcBof4Qu3Grw837",
                "_score": 0.37881336,
                "_source": {
                    "name": "銅川",
                    "phone": "18874796666",
                    "sex": 1,
                    "salary": 16000,
                    "post": "設計UI",
                    "dept": {
                        "id": "1003",
                        "name": "設計中心"
                    },
                    "join_time": "2018-04-03",
                    "desc": "a sunny boy"
                }
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nFZul2cBof4Qu3GrDc0v",
                "_score": 0.3034693,
                "_source": {
                    "name": "高節",
                    "phone": "18874796310",
                    "sex": 1,
                    "salary": 10000,
                    "post": "Java工程師",
                    "dept": {
                        "id": "1001",
                        "name": "技術中心"
                    },
                    "join_time": "2018-12-03",
                    "desc": "a love of work boy"
                }
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nlb-l2cBof4Qu3Gr8M1Y",
                "_score": 0.2876821,
                "_source": {
                    "name": "春哥",
                    "phone": "18874792222",
                    "sex": 1,
                    "salary": 35000,
                    "post": "架構師",
                    "dept": {
                        "id": "1001",
                        "name": "技術中心"
                    },
                    "join_time": "2018-03-03",
                    "desc": "a framework boy"
                }
            }
        ]
    }
}

multi_match :多字段匹配,比如輸入一個關鍵字從姓名和描述中查找

以下會匹配出desc或者name存在beautiful或者高節的記錄

GET  http://47.105.159.23:9200/manage/employee/_search
{
    "query": {
    	"multi_match": {
    		"query": "beautiful 高節",
    		"fields": ["desc", "name"]
    	}
    }
}
==========================
{
    "took": 16,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 2.4079456,
        "hits": [
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nFZul2cBof4Qu3GrDc0v",
                "_score": 2.4079456,
                "_source": {
                    "name": "高節",
                    "phone": "18874796310",
                    "sex": 1,
                    "salary": 10000,
                    "post": "Java工程師",
                    "dept": {
                        "id": "1001",
                        "name": "技術中心"
                    },
                    "join_time": "2018-12-03",
                    "desc": "a love of work boy"
                }
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "n1YAmGcBof4Qu3GrB83o",
                "_score": 0.6931472,
                "_source": {
                    "name": "麗燕",
                    "phone": "18874793333",
                    "sex": 2,
                    "salary": 12000,
                    "post": "測試工程師",
                    "dept": {
                        "id": "1001",
                        "name": "技術中心"
                    },
                    "join_time": "2018-05-03",
                    "desc": "a beautiful girl"
                }
            }
        ]
    }
}

bool :過濾查詢,其中可以包含多個查詢條件,可以通過must,must_not,should連接

must:所有條件必須全部符合(最常用,使用頻率最高)

must_not:一個都不要符合

should:至少有一個符合(若與must公用,must存在滿足時,should可以不匹配成功)

以下查詢性別爲1,且工資大於10000,且 desc 包含 framework 的記錄

GET  http://47.105.159.23:9200/manage/employee/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "sex": 1
                    }
                },
                {
                    "range": {
                        "salary": {
                            "gt": 10000
                        }
                    }
                },
                {
                	"match": {
                		"desc": "framework"
                	}
                }
            ]
        }
    }
}
=================================
{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 2.287682,
        "hits": [
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nlb-l2cBof4Qu3Gr8M1Y",
                "_score": 2.287682,
                "_source": {
                    "name": "春哥",
                    "phone": "18874792222",
                    "sex": 1,
                    "salary": 35000,
                    "post": "架構師",
                    "dept": {
                        "id": "1001",
                        "name": "技術中心"
                    },
                    "join_time": "2018-03-03",
                    "desc": "a framework boy"
                }
            }
        ]
    }
}

分頁:from 起始(默認從0開始),size 讀取記錄數,sort 排序字段(可定義排序字段集合以及排序規則)

以下語句查詢 desc 字段包含 單詞 a 的記錄,從第1條開始查詢,返回5條記錄,首先按照工資降序排序,若工資相同,然後按照入職時間降序排序

GET  http://47.105.159.23:9200/manage/employee/_search
{
    "query": {
        "match": {
            "desc": "a"
        }
    },
    "from": 0,
    "size": 5,
    "sort": [
        {
            "salary": {
                "order": "desc"
            }
        },
        {
        	"join_time": {
        		"order": "desc"
        	}
        }
    ]
}
=====================================
{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 8,
        "max_score": null,
        "hits": [
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nlb-l2cBof4Qu3Gr8M1Y",
                "_score": null,
                "_source": {
                    "name": "春哥",
                    "phone": "18874792222",
                    "sex": 1,
                    "salary": 35000,
                    "post": "架構師",
                    "dept": {
                        "id": "1001",
                        "name": "技術中心"
                    },
                    "join_time": "2018-03-03",
                    "desc": "a framework boy"
                },
                "sort": [
                    35000,
                    1520035200000
                ]
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "o1ZWmGcBof4Qu3GrG81P",
                "_score": null,
                "_source": {
                    "name": "桂祿",
                    "phone": "18874797777",
                    "sex": 1,
                    "salary": 16000,
                    "post": "設計前端",
                    "dept": {
                        "id": "1003",
                        "name": "設計中心"
                    },
                    "join_time": "2018-05-03",
                    "desc": "a front boy"
                },
                "sort": [
                    16000,
                    1525305600000
                ]
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "olYDmGcBof4Qu3Grw837",
                "_score": null,
                "_source": {
                    "name": "銅川",
                    "phone": "18874796666",
                    "sex": 1,
                    "salary": 16000,
                    "post": "設計UI",
                    "dept": {
                        "id": "1003",
                        "name": "設計中心"
                    },
                    "join_time": "2018-04-03",
                    "desc": "a sunny boy"
                },
                "sort": [
                    16000,
                    1522713600000
                ]
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "nVZ-l2cBof4Qu3GrFc1L",
                "_score": null,
                "_source": {
                    "name": "周益",
                    "phone": "18874791111",
                    "sex": 2,
                    "salary": 15000,
                    "post": "IOS工程師",
                    "dept": {
                        "id": "1001",
                        "name": "技術中心"
                    },
                    "join_time": "2018-10-03",
                    "desc": "a IOS boy"
                },
                "sort": [
                    15000,
                    1538524800000
                ]
            },
            {
                "_index": "manage",
                "_type": "employee",
                "_id": "n1YAmGcBof4Qu3GrB83o",
                "_score": null,
                "_source": {
                    "name": "麗燕",
                    "phone": "18874793333",
                    "sex": 2,
                    "salary": 12000,
                    "post": "測試工程師",
                    "dept": {
                        "id": "1001",
                        "name": "技術中心"
                    },
                    "join_time": "2018-05-03",
                    "desc": "a beautiful girl"
                },
                "sort": [
                    12000,
                    1525305600000
                ]
            }
        ]
    }
}

修改 update

根據傳入ID,修改對於的數據,只修改傳入的字段,不傳入的字段不操作

POST  http://47.105.159.23:9200/manage/employee/o1ZWmGcBof4Qu3GrG81P/_update
{
	"doc": {
		"salary": 50000
	}
}
=================
{
    "_index": "manage",
    "_type": "employee",
    "_id": "o1ZWmGcBof4Qu3GrG81P",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

刪除 delete

DELETE 請求,傳入要刪除的ID即可

DELETE http://47.105.159.23:9200/manage/employee/pFZimGcBof4Qu3Gro80s
=====Response======
{
    "_index": "manage",
    "_type": "employee",
    "_id": "pFZimGcBof4Qu3Gro80s",
    "_version": 2,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 6,
    "_primary_term": 1
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章