ElasticSearch
基于ElasticSearch6.*的基本操作,只涉及基本使用,不包含集群搭建、部署等
擴展后期可能會單獨發一些,歡迎關注
文章目錄
- ElasticSearch
- 創建索引
- 洗掉索引
- 新增資料
- 更新資料
- 洗掉
- 獲得資料
- DSL搜索
- 復雜查詢(例如:大于30且為男性)
- 全文檢索
- 高亮顯示
- 聚合(分組)
- 指定欄位查詢
- 批量獲得
- 批量操作添加
- 分頁
- 映射
- 結構化查詢
- 中文分詞
- 全文搜索
- 權重
創建索引
請求地址 http://localhost:9200/name/ #name為創建索引的名字
PUT /name #put請求,右面是名字
{
"settings": {
"index": {
"number_of_shards": "2", #分片書
"number_of_replicas": "0" #副本數
}
}
}
洗掉索引
請求地址 http://localhost:9200/name/
DELETE /name #DELETE請求,右面是名字
新增資料
http://localhost:9200/索引/型別/id
例:http://localhost:9200/test/user/0001
POST /name/type/id
body:
{
"id":1,
"name":"李華",
"age":20,
"sex":"男"
}
更新資料
- 第一種覆寫
http://localhost:9200/test/user/0001
PUT /name/type/id
body:
{
"id":1,
"name":"李華1",
"age":20,
"sex":"男"
}
直接覆寫
- 第二種區域修改
http://localhost:9200/test/user/0001/_update #注意這里加了_update
POST /name/type/id
body:
{
"doc":{
"sex":"男"
}
}
洗掉
DELETE /name/type/id
直接執行
獲得資料
http://localhost:9200/test/user/0001/
GET /name/type/id
獲取多條
http://localhost:9200/test/user/_search #默認回傳10條,多了需要做分頁
按需索引
http://localhost:9200/test/user/_search?q=age:20
DSL搜索
http://localhost:9200/test/user/_search
POST /name/type/_search
{
"query":{
"match":{
"age":20
}
}
}
復雜查詢(例如:大于30且為男性)
http://localhost:9200/test/user/_search
POST /name/type/_search
{
"query": { #查找
"bool": {
"filter": { #過濾
"range": { #范圍
"age": { #age屬性
"gt": 30 #大于30
}
}
},
"must": { #滿足
"match": {
"sex": "男"
}
}
}
}
}
符號標識 代表含義
gte 大于或等于
gt 大于
lte 小于或等于
lt 小于
全文檢索
http://localhost:9200/test/user/_search
POST /name/type/_search
{
"query":{
"match":{
"name":"張三 李四"
}
}
}
高亮顯示
http://localhost:9200/test/user/_search
POST /name/type/_search
{
"query": {
"match": {
"name": "李華1"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
會多回傳這么一段
"highlight":{
"name":[
"<em>汪</em><em>京</em><em>1</em>"
]
}
聚合(分組)
http://localhost:9200/test/user/_search
POST /name/type/_search
以年齡聚合
{
"aggs": {
"all_interests": {
"terms": {
"field": "age"
}
}
}
}
多回傳了
{
"aggregations": {
"all_interests": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 18,
"doc_count": 1
},
{
"key": 20,
"doc_count": 1
}
]
}
}
}
指定欄位查詢
http://localhost:9200/test/user/0001?_source=id,name
GET /name/type/id?_source=id,name
回傳
{
"_index": "test",
"_type": "user",
"_id": "0001",
"_version": 1,
"found": true,
"_source": {
"name": "李華1",
"id": 1
}
}
加_source去掉頭
http://localhost:9200/test/user/0001/_source?_source=id,name
{
"name": "李華1",
"id": 1
}
判斷檔案是否存在
http://localhost:9200/test/user/0002
HEAD /name/type/id
批量操作
批量獲得
http://localhost:9200/test/user/_mget #批量獲得
POST /name/type/_mget
{
"ids":["id1","id2"]
}
回傳結果
{
"docs": [
{
"_index": "test",
"_type": "user",
"_id": "0001",
"_version": 1,
"found": true,
"_source": {
"id": 1,
"name": "李華1",
"age": 20,
"sex": "男"
}
},
{
"_index": "test",
"_type": "user",
"_id": "1111",
"found": false #沒找到
}
]
}
批量操作添加
http://localhost:9200/test/user/_bulk
POST /name/type/_bulk
創建
{"create": {"_index": "test", "_type": "user", "_id": "0003"}}
{"id": "0003", "name": "李華3", "age": 23, "sex": "男"}
{"create": {"_index": "test", "_type": "user", "_id": "0004"}}
{"id": "0004", "name": "李華4", "age": 23, "sex": "女"}
{"create": {"_index": "test", "_type": "user", "_id": "0005"}}
{"id": "0005", "name": "李華5", "age": 23, "sex": "男"} #這必須要來個回車
洗掉
{"delete": {"_index": "test", "_type": "user", "_id": "0005"}}
性能優化,批量運算元量不是越多越好,操作任務會被放在記憶體中,請妥善尋找最佳大小
分頁
size:結果數,默認10
from:跳過開始的結果數,默認為0 #就是跳過多少個后開始查
就是從那條開始讀,讀幾條實作分頁
GET /_search?size=5 查5條資料
GET /_search?size=5&from=5 跳過5條,就是第二頁
GET /_search?size=5&from=10 跳過10條,就是第三頁
映射
| 型別 | 表示的資料 |
|---|---|
| String | string,text,keyword |
| Whole number | byte,short,integer,long |
| Floating point | float,double |
| Boolean | boolean |
| Date | date |
Text:會分詞,然后進行索引
? 支持模糊、精確查詢
? 不支持聚合
keyword:不進行分詞,直接索引
? 支持模糊、精確查詢
? 支持聚合
請求地址 http://localhost:9200/name/ #name為創建索引的名字
PUT /name #put請求,右面是名字
{
"settings": {
"index": {
"number_of_shards": "2", #分片書
"number_of_replicas": "0" #副本數
}
},
"mappings": {
"person": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"mail": {
"type": "keyword"
},
"hobby": {
"type": "text"
"analyzer": "ik_max_word" #指定分詞器
}
}
}
}
}
GET /name/_mapping
{
"test1": {
"mappings": {
"person": {
"properties": {
"age": {
"type": "integer"
},
"hobby": {
"type": "text"
},
"mail": {
"type": "keyword"
},
"name": {
"type": "text"
}
}
}
}
}
}
結構化查詢
#term 精確查詢
主要用于精確匹配那些值,比如數字,日期,布林值或not_analyzed的字串(未經分析的文本資料型別)
POST /name/type/_search
{
"query":{
"term":{
"age":18
}
}
}
#terms 多匹配精確查詢
terms跟term有點類似,單terms允許指定多個匹配條件,如果某個欄位指定了多個值,那么檔案需要一起做匹配
{
"term":{
"tag":["search","full_text","nosql"]
}
}
{
"query":{
"terms":{
"age":[18,20] #注意:匹配而不是范圍查詢!
}
}
}
#range 范圍查詢
范圍查詢
{
"query":{
"range":{
"age":{
"gte:17", # 大于等于17
"lt":30 # 小于30
}
}
}
}
gte:大于等于
gt:大于
lte:小于等于
lt:小于
boost:設定查詢的推動值(boost),默認為1.0
`時間范圍查詢`
#exists 存在欄位查詢
exists查詢可以用于查詢檔案中是否包含指定欄位或沒有某個`欄位`,類似于sql陳述句中的is_null條件
POST localhost/_search #不加索引名稱也可以
{
"query":{
"exists":{ #必須包含某個欄位
"field": "age"
}
}
}
#match 標準查詢
match查詢是一個標準查詢,不管你需要全文本查詢還是精確查詢基本上都要用到他,
如果你使用match查詢一個全文本欄位,它會在真正查詢之前用分析器match一下查詢欄位
如果你傳入的是數字布爾日期或者not_analyzed字串時,它將為你提供準確查詢
POST /_search #可以指定索引
{
"query":{
"match":{
"hobby":"fate"
}
}
}
#bool 布爾查詢
POST /_search
bool查詢可以用來合并多個條件查詢結果的布爾邏輯,他包含以下運算子:
`must`:多個查詢條件的完全匹配 相當于 and
`must_not`:多個查詢條件的相反匹配 相當于 not
`should`:至少有一個查詢條件匹配 相當于 or
{
"query": {
"bool": {
"must": {
"term": {
"age": "18" #滿足年齡是18就可以回傳
}
},
"must_not": {
"term": {
"name": "李華" #名字不能是李華 絕對不能是李華
}
},
"should": [{
"match": {
"hobby": "fate" ,#愛好是fate分詞查詢
}
},
{
"match": {
"mail": "12345678912@163.com" #郵箱是這個 兩個滿足一個就行
}
}
]
}
}
}
單獨說一下時間的range查詢
日期欄位的范圍
當范圍查詢運行在日期型別的欄位上時,可以通過日期數學指定范圍,
GET _search { "query": { "range" : { "date" : { "gte" : "now-1d/d", "lt" : "now/d" } } } } 1234567891011日期數學與舍入
當使用日期數學將日期舍入到最近的天、月、小時等時,舍入后的日期依賴于范圍的邊界是否被包含,向上舍入移動到舍入范圍的最后一毫秒,向下舍入移動到舍入范圍的第一毫秒,
- gt:大于舍入的日期,2014-11-18||/M變為2014-11-30T23:59:59.999,即不包括整個月,
- gte:大于等于舍入的日期,2014-11-18||/M變為2014-11-01,即包括整個月,
- lt:小于舍入的日期,204-11-18||/M變為2014-11-01,即不包括整個月,
- lte:小于等于舍入的日期,2014-11-18||/M變為2014-11-30T23:59:59.999,即包含整個月,
范圍查詢中的日期格式
默認使用設定在日期欄位中的format引數決議格式化日期,但是,此引數可以通過在范圍查詢中設定format引數來進行覆寫,GET _search { "query": { "range" : { "born" : { "gte": "01/01/2012", "lte": "2013", "format": "dd/MM/yyyy||yyyy" } } } } 123456789101112范圍查詢中的時區
通過在日期值中指定時區或使用time_zone引數指定時區,可以轉換日期時區,GET _search { "query": { "range" : { "timestamp" : { "gte": "2015-01-01 00:00:00", "lte": "now", "time_zone": "+01:00" } } } } 123456789101112note:gte引數的日期值將會被轉化為2014-12-31T23:00:00 UTC
note:now不會被time_zone引數影響(日期必須存盤為UTC)參考:https://blog.csdn.net/qq_32165041/article/details/83714203
過濾查詢
#查詢年齡未18歲user 型別通過restful指定
POST /name/type/_search
{
"query":{
"bool":{
"filter":{
"term":{
"age":18
}
}
}
}
}
#注意:
- 一條查詢陳述句會計算每個檔案與查詢陳述句之間的相關性,并給出評分,然后按評分排序
- 過濾查詢會快取資料,一般過濾查詢性能要高于普通查詢
- `建議:做精確查詢的時候盡量使用過濾查詢,因為過濾查詢可以快取資料`
中文分詞
#指定分詞器進行分詞
POST /_analyze
{
"analyze":"standard",
"text":"fate動漫"
}
`回傳`
{
"tokens":[
{
"token": "fate",
"start_offset": 0,
"end_offset": 4,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "動",
"start_offset": 4,
"end_offset": 5,
"type": "<IDEOGRAPHIC>",
"position": 1
},
{
"token": "漫",
"start_offset": 5,
"end_offset": 6,
"type": "<IDEOGRAPHIC>",
"position": 2
}
]
}
`在做一個測驗` #指定索引庫,指定欄位的text
POST /name/_analyze
{
"analyzer":"standard",
"field":"hobby",
"text":"fate動漫"
}
#使用ik分詞器分詞 涉及ik分詞器的安裝,自行百度
POST /_analyze
{
"analyzer":"ik_max_word",
"text":"中華人民共和國"
}
結果
tokens":[
{"token": "中華人民共和國", "start_offset": 0, "end_offset": 7, "type": "CN_WORD",…},
{"token": "中華人民", "start_offset": 0, "end_offset": 4, "type": "CN_WORD",…},
{"token": "中華", "start_offset": 0, "end_offset": 2, "type": "CN_WORD",…},
{"token": "華人", "start_offset": 1, "end_offset": 3, "type": "CN_WORD",…},
{"token": "人民共和國", "start_offset": 2, "end_offset": 7, "type": "CN_WORD",…},
{"token": "人民", "start_offset": 2, "end_offset": 4, "type": "CN_WORD",…},
{"token": "共和國", "start_offset": 4, "end_offset": 7, "type": "CN_WORD",…},
{"token": "共和", "start_offset": 4, "end_offset": 6, "type": "CN_WORD",…},
{"token": "國", "start_offset": 6, "end_offset": 7, "type": "CN_CHAR",…}
]
全文搜索
- 相關性
- 分詞
測驗步驟:
1.創建索引,指定text型別
2.創建檔案 #較占篇幅,自行百度或者看上面的批量操作示例
3.單詞搜索
POST /name/type/_search
{
"query":{
"match":{
"hobby":"雞蛋,飯" #可以單個也可以多個,用,號分割,滿足其中一個就行
}
},
"highlight":{ #高亮
"fields":{
"hobby":{}
}
}
}
4.多詞搜索
POST /name/type/_search
{
"query":{
"match":{
"hobby":{
"query":"西紅柿 面", #用空格分隔
"operator":"and" #表示and ,必須滿足連個條件
}
}
},
"highlight":{
"fields":{
"hobby":{}
}
}
}
##注意:and和or都是100%匹配率,因此我們可以自行設定匹配度
{
"query":{
"match":{
"hobby":{
"query":"西紅柿 面", #用空格分隔
"minimum_should_match":"40%" #設定40%匹配度
}
}
},
"highlight":{
"fields":{
"hobby":{}
}
}
}
#組合搜索
POST /name/type/_search
`必須有西紅柿,不能包含蓋飯,有湯匹配度更高`
{
"query":{
"bool":{
"must":{
"match":{
"hobby":"西紅柿"
}
},
"must_not":{
"match":{
"hobby":"蓋飯"
}
},
"should":[
{
"match":{
"hobby":"湯"
}
}
]
}
},
"highlight":{
"fields":{
"hobby":{}
}
}
}
權重
`必須包含西紅柿和雞蛋,如果是面就給10權重,飯就給2權重`
{
"query": {
"bool": {
"must": {
"match": {
"hobby": {
"query": "西紅柿 雞蛋",
"operator": "and"
}
}
},
"should": [
{
"match": {
"hobby": {
"query": "面",
"boost": 10 #設定權重
}
}
},
{
"match": {
"hobby": {
"query": "飯",
"boost": 2
}
}
}
]
}
},
"highlight": {
"fields": {
"hobby": {}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/256344.html
標籤:其他
上一篇:在vs2017下使用MYNTEYE S SDK 2.5.0小覓雙目攝像頭,永久設定包含目錄和庫目錄
下一篇:什么是序列化和反序列化?
