Domain Specific Language
特定領域語言,基于JSON格式的資料查詢,查詢更靈活,有利于復雜查詢
更好的閱讀體驗:http://www.zhouhong.icu/post/137
一、普通url路徑引數搜索
-
資料準備
1.建立名字為 shop 的索引
2.手動建立mappings
POST http://192.168.2.223:9200/shop/_mapping
{
"properties": {
"id": {
"type": "long"
},
"age": {
"type": "integer"
},
"username": {
"type": "keyword"
},
"nickname": {
"type": "text",
"analyzer": "ik_max_word"
},
"money": {
"type": "float"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word"
},
"sex": {
"type": "byte"
},
"birthday": {
"type": "date"
},
"face": {
"type": "text",
"index": false
}
}
}
3.添加資料
POST http://192.168.2.223:9200/shop/_doc/1001
{
"id": 1011,
"age": 31,
"username": "sprder",
"nickname": "皮特帕克",
"money": 180.8,
"desc": "它是一個超級英雄",
"sex": 1,
"birthday": "1989-08-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1008,
"age": 19,
"username": "zhoujiang",
"nickname": "周江",
"money": 1056.8,
"desc": "周江大學畢業后,進了阿里",
"sex": 1,
"birthday": "1995-06-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1007,
"age": 19,
"username": "msgame",
"nickname": "gamexbox",
"money": 1056.8,
"desc": "明天去進貨,最近微軟處理很多游戲機,還要買xbox游戲卡帶",
"sex": 1,
"birthday": "1985-05-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1003,
"age": 20,
"username": "bigFace",
"nickname": "飛翔的巨鷹",
"money": 66.8,
"desc": "周江和導游坐飛機去海外旅游,去了新馬泰和歐洲",
"sex": 1,
"birthday": "1996-01-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1002,
"age": 19,
"username": "zhouhong",
"nickname": "周紅",
"money": 77.8,
"desc": "今天上下班都很堵,車流量很大",
"sex": 1,
"birthday": "1993-01-24",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1012,
"age": 31,
"username": "super hero",
"nickname": "super hero",
"money": 188.8,
"desc": "BatMan, GreenArrow, SpiderMan, IronMan... are all Super Hero",
"sex": 1,
"birthday": "1980-08-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1010,
"age": 30,
"username": "tata",
"nickname": "隔壁老王",
"money": 100.8,
"desc": "隔壁老外去國外出差,帶給我很多好吃的",
"sex": 1,
"birthday": "1988-07-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1009,
"age": 22,
"username": "shaonian",
"nickname": "騷年輪",
"money": 96.8,
"desc": "騷年在大學畢業后,考研究生去了",
"sex": 1,
"birthday": "1998-07-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1006,
"age": 19,
"username": "zhouhong",
"nickname": "我叫周紅",
"money": 156.8,
"desc": "我叫周紅,今年20歲,是一名畢業生,我在琦?星球做演講",
"sex": 1,
"birthday": "1993-04-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1005,
"age": 25,
"username": "gotoplay",
"nickname": "ps游戲機",
"money": 155.8,
"desc": "今年生日,女友送了我一臺play station游戲機,非常好玩,非常不錯",
"sex": 1,
"birthday": "1989-03-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1004,
"age": 22,
"username": "flyfish",
"nickname": "水中魚",
"money": 55.8,
"desc": "昨天周紅在學校的池塘里,看到有很多魚在游泳",
"sex": 0,
"birthday": "1988-02-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1001,
"age": 18,
"username": "zhoujiang",
"nickname": "周江",
"money": 88.8,
"desc": "周江在大學學習java和前端",
"sex": 0,
"birthday": "1992-12-24",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
4、普通檢索:
-
http://192.168.2.223:9200/shop/_search?q=desc:周紅&q=age:20
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shop",
"_type": "_doc",
"_id": "1003",
"_score": 1,
"_source": {
"id": 1003,
"age": 20,
"username": "bigFace",
"nickname": "飛翔的巨鷹",
"money": 66.8,
"desc": "周江和導游坐飛機去海外旅游,去了新馬泰和歐洲",
"sex": 1,
"birthday": "1996-01-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
}
]
}
}
二、DSL搜索
-
match_all 檢索
-
查詢所有:POST http://192.168.2.223:9200/shop/_doc/_search
{
"query": {
"match_all": {}
}
}
-
查詢部分欄位 : POST http://192.168.2.223:9200/shop/_doc/_search
{
"query": {
"match_all": {}
},
"_source": ["id","username","age"]
}
-
分頁查詢: POST http://192.168.2.223:9200/shop/_doc/_search
{
"query": {
"match_all": {}
},
"_source": ["id","username","age"],
"from": 0,
"size": 5
}
-
match_phrase 查詢同一個欄位中幾個詞,可以跳過其他詞,slop表示可以跳過的最大詞數
-
POST http://192.168.2.223:9200/shop/_doc/_search
{
"query": {
"match_phrase": {
"desc": {
"query": "今天 車流量",
"slop": 100
}
}
}
}
-
term 單個條件查詢(不會進行分詞、將輸入字符當做關鍵字查找,精確匹配)
-
POST http://192.168.2.223:9200/shop/_doc/_search
{
"query": {
"term": {
"desc": "學習"
}
}
}
-
terms 對多個關鍵字查詢
{
"query": {
"terms": {
"desc": ["學習","周紅","周江"]
}
}
}
-
match 條件查詢(會對輸入的字符進行分詞操作、全文檢索)
-
POST http://192.168.2.223:9200/shop/_doc/_search
{
"query": {
"match": {
"desc": "周紅"
}
},
"_source": ["id","username","age"]
}
-
POST http://192.168.2.223:9200/shop/_doc/_search operator 為 “or”表示:欄位只要有一個分詞就可以查出來,operator 為 “and”表示:欄位必須包含分詞分出來的所有欄位,
{
"query": {
"match": {
"desc": {
"query": "周紅",
"operator": "and"
}
}
},
"_source": ["id","username","age"]
}
-
POST http://192.168.2.223:9200/shop/_doc/_search
- ?minimum_should_match: 最低匹配精度,至少有[分詞后的詞語個數]x百分百,得出一個資料值取整,舉個例子:當前屬性設定為70,若一個用戶查詢檢索內容分詞后有10個詞語,那么匹配度按照 10x70%=7,則desc中至少需要有7個詞語匹配,就展示;若分詞后有8個,則 8x70%=5.6,則desc中至少需要有5個詞語匹配,就展示,
- minimum_should_match 也能設定具體的數字,表示拆分出來的詞在一個欄位中個數
{
"query": {
"match": {
"desc": {
"query": "女友生日送我好玩的xbox游戲機",
"minimum_should_match": "60%"
}
}
}
}
-
POST http://192.168.2.223:9200/shop/_doc/_search 根據檔案 ids 進行查找
{
"query": {
"ids": {
"type": "_doc",
"values": ["1001","1005","1006"]
}
},
"_source": ["id","username","desc"]
}
-
multi_match 對多個欄位進行檢索
-
POST http://192.168.2.223:9200/shop/_doc/_search ^10 表示權重,權重,為某個欄位設定權重,權重越高,檔案相關性得分就越高,通暢來說搜索商品名稱要比商品簡介的權重更高,
{
"query": {
"multi_match": {
"query": "游戲",
"fields": [
"desc^10","nickname"
]
}
}
}
-
bool 查詢
-
POST http://192.168.2.223:9200/shop/_doc/_search
-
must :多個條件全部要滿足, should:或者的意思,滿足一個條件即可,must_not :除了滿足所有條件剩下的資料,
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "游戲",
"fields": ["desc","nickname"]
}
},
{
"term": {
"age": "19"
}
}
]
}
}
}
-
post_filter 過濾器
-
POST http://192.168.2.223:9200/shop/_doc/_search
- 對搜索出來的結果進行資料過濾,不會到es庫里去搜,不會去計算檔案的相關度分數,所以過濾的性能會比較高,過濾器可以和全文搜索結合在一起使用,
- post_filter元素是一個頂層元素,只會對搜索結果進行過濾,不會計算資料的匹配度相關性分數,不會根據分數去排序,query則相反,會計算分數,也會按照分數去排序,
- 使用場景:
- ?query:根據用戶搜索條件檢索匹配記錄
- post_filter:用于查詢后,對結果資料的篩選
{
"query": {
"match": {
"sex": "1"
}
},
"post_filter": {
"range": {
"money": {
"gte": 60,
"lte": 155.8
}
}
}
}
-
sort 排序功能
-
POST http://192.168.2.223:9200/shop/_doc/_search 先以money排序再以age排序,注意:只能對整形排序,不能對文本型別排序,
{
"query": {
"match": {
"sex": "1"
}
},
"sort": [
{
"money": "asc"
},
{
"age": "asc"
}
]
}
-
對文本排序
-
需要對排序欄位加一個附加屬性,型別選擇為keyword
1.創建索引
POST /shop2/_mapping
{
"properties": {
"id": {
"type": "long"
},
"nickname": {
"type": "text",
"analyzer": "ik_max_word",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
2.插入資料
POST /shop2/_doc
{
"id": 1001,
"nickname": "美麗的風景"
}
{
"id": 1002,
"nickname": "漂亮的小哥哥"
}
{
"id": 1003,
"nickname": "飛翔的巨鷹"
}
{
"id": 1004,
"nickname": "完美的天空"
}
{
"id": 1005,
"nickname": "廣闊的海域"
}
3.排序 POST http://192.168.2.223:9200/shop2/_doc/_search
{
"sort": [
{
"nickname.keyword": "desc"
}
]
}
-
exists 判斷欄位是否存在
-
POST http://192.168.2.223:9200/shop/_doc/_search
{
"query": {
"exists": {
"field": "desc"
}
}
}
-
highlight 關鍵字高亮顯示
-
POST http://192.168.2.223:9200/shop/_doc/_search
{
"query": {
"match": {
"desc": "周紅"
}
},
"highlight": {
"pre_tags": ["<span>"],
"post_tags": ["</span>"],
"fields": {
"desc": {}
}
}
}
結果:默認為em標簽,上面設定為自定義的<span>標簽,對頁面 em/span 標簽做一個顏色設定就可以實作高亮顯示了,
{
"took": 110,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.1329247,
"hits": [
{
"_index": "shop",
"_type": "_doc",
"_id": "1004",
"_score": 1.1329247,
"_source": {
"id": 1004,
"age": 22,
"username": "flyfish",
"nickname": "水中魚",
"money": 55.8,
"desc": "昨天周紅在學校的池塘里,看到有很多魚在游泳",
"sex": 0,
"birthday": "1988-02-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
},
"highlight": {
"desc": [
"昨天<em>周紅</em>在學校的池塘里,看到有很多魚在游泳"
]
}
},
{
"_index": "shop",
"_type": "_doc",
"_id": "1006",
"_score": 0.9585575,
"_source": {
"id": 1006,
"age": 19,
"username": "zhouhong",
"nickname": "我叫周紅",
"money": 156.8,
"desc": "我叫周紅,今年20歲,是一名畢業生,我在琦?星球做演講",
"sex": 1,
"birthday": "1993-04-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
},
"highlight": {
"desc": [
"我叫<em>周紅</em>,今年20歲,是一名畢業生,我在琦?星球做演講"
]
}
}
]
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/262432.html
標籤:其他
