ES 花樣查詢
阿新 • • 發佈:2020-09-10
二話不說上程式碼
#建立索引庫並指定索引型別 PUT /test { "mappings": { "properties": { "name":{ "type": "text" }, "age":{ "type": "integer" }, "birthday":{ "type": "date" } } } } #新增一條文件 PUT /test/_doc/1 { "name":"彼岸舞", "age":12,"birthday":"1999-11-11" } #暴力修改 PUT /test/_doc/1 { "name":"彼岸舞的部落格", "age":12, "birthday":"1999-11-11" } #更新修改 POST /test/_doc/1/_update { "doc":{ "age":11 } } #刪除索引庫 DELETE test #建立索引庫並新增資料 PUT /dance/_doc/1 { "name":"藍貝兒", "age":18, "desc":"不明所以的小迷糊", "tags":["小仙女","小吃貨","時尚達人"] } PUT/dance/_doc/2 { "name":"夏天", "age":26, "desc":"小清新,萌萌的", "tags":["萌萌的","小吃貨","暖男"] } PUT /dance/_doc/3 { "name":"彼岸舞", "age":18, "desc":"走投無路的弟弟", "tags":["技術宅","二次元","碼農"] } PUT /dance/_doc/4 { "name":"彼岸花", "age":18, "desc":"走投無路的妹妹", "tags":["小仙女","妖嬈","小吃貨"] } PUT/dance/_doc/5 { "name":"彼岸草", "age":3, "desc":"走投無路的姐姐", "tags":["御姐","時尚達人","大懶貨"] } #根據ID查詢 GET dance/_doc/_search?q=name:彼岸舞 #指定欄位匹配 GET dance/_doc/_search { "query":{ "match":{ "name":"彼岸" } } } #查詢指定欄位 GET dance/_doc/_search { "query":{ "match":{ "name":"彼岸" } }, "_source":["name","desc"] } #指定欄位排序 GET dance/_doc/_search { "query": { "match": { "name": "彼岸" } }, "sort": [ { "age": { "order": "asc" } } ] } #分頁查詢 GET dance/_doc/_search { "query": { "match": { "name": "彼岸" } }, "from": 0, "size": 1 } #多條件匹配 and關聯 GET dance/_doc/_search { "query": { "bool": { "must": [ { "match": { "name": "彼岸" } }, { "match": { "age": 3 } } ] } } } #多條件匹配 or關聯 GET dance/_doc/_search { "query": { "bool": { "should": [ { "match": { "name": "彼岸舞" } }, { "match": { "age": 18 } } ] } } } #多條件匹配 取非 GET dance/_doc/_search { "query": { "bool": { "must_not": [ { "match": { "age": 18 } } ] } } } #範圍 lt 小於 lte 小於等於 gt 大於 gte 大於等於 #多條件匹配 資料過濾 GET dance/_doc/_search { "query": { "bool": { "must": [ { "match": { "name": "彼岸" } } ], "filter": { "range": { "age": { "lt": 10 } } } } } } #空格分割多條件 GET /_search { "query": { "match": { "tags": "小仙女 小吃貨" } } } #精確查詢 #term 查詢是直接通過倒排索引指定的詞條進行精確查詢 #關於分詞 #term 直接查詢精確的 #match 會使用分詞器(先分析文件,然後通過分析的文件進行查詢) #兩個型別(重點) #text keyword #text 會被分詞器解析 keyword不會被分詞器解析 #建立索引庫指定規則 PUT testdb { "mappings": { "properties": { "name":{ "type": "text" }, "desc":{ "type": "keyword" } } } } #新增測試資料 PUT testdb/_doc/1 { "name":"彼岸舞的部落格園 java name", "desc":"彼岸舞的部落格園 java desc" } PUT testdb/_doc/2 { "name":"彼岸舞的部落格園 java name", "desc":"彼岸舞的部落格園 java desc code" } #嘗試分詞器 GET _analyze { "analyzer": "keyword", "text": "彼岸舞的部落格園 java name" } GET _analyze { "analyzer": "standard", "text": "彼岸舞的部落格園 java name" } #檢索測試 會被分詞 GET testdb/_search { "query": { "term": { "name": "java" } } } #不會被分詞 GET testdb/_search { "query": { "term": { "desc": "彼岸舞的部落格園 java desc code" } } } #高亮查詢 GET testdb/_search { "query": { "match": { "name": "彼岸舞" } }, "highlight": { "fields": { "name": {} } } } #自定義高亮包裹標籤 GET testdb/_search { "query": { "match": { "name": "彼岸舞" } }, "highlight": { "pre_tags": "<span class='key' style='color:red;'>", "post_tags": "</span>", "fields": { "name": {} } } }
自己去試,從上往下
作者:彼岸舞
時間:2020\09\10
內容關於:ElasticSearch
本文來源於網路,只做技術分享,一概不負任何責任