Elasticsearch---DSL搜索實踐

Domain Specific Language
特定領域語言,基於JSON格式的數據查詢,查詢更靈活,有利於複雜查詢
1、普通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
        }
    }
}
View Code

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"   
}
View Code
2、檢索:
普通檢索:

  • 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"
                    }
                }
            ]
        }
    }
    View Code
DSL檢索:
  • match_phrase 查詢同一個字段中幾個詞,能夠跳過其餘詞,slop表示能夠跳過的最大詞數

  • match 條件查詢(會對輸入的字符進行分詞操做、全文檢索):

    • 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%"
                  }
              }
          }
      } 
  • 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:用於查詢後,對結果數據的篩選
實操:查詢帳戶金額大於80元,小於等於155.8元的用戶
gte:大於等於
lte:小於等於
gt:大於
lt:小於
{
    "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"
                      }
                  }
              }
          }
      }
      View Code
      2.插入數據
      POST         /shop2/_doc
      {
          "id": 1001,
          "nickname": "美麗的風景"
      }
      {
          "id": 1002,
          "nickname": "漂亮的小哥哥"
      }
      {
          "id": 1003,
          "nickname": "飛翔的巨鷹"
      }
      {
          "id": 1004,
          "nickname": "完美的天空"
      }
      {
          "id": 1005,
          "nickname": "廣闊的海域"
      }
      View Code
      3.排序     POST    http://192.168.2.223:9200/shop2/_doc/_search
      {
          "sort": [
              {
                  "nickname.keyword": "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歲,是一名畢業生,我在琦䯲星球作演講"
                          ]
                      }
                  }
              ]
          }
      View Code

       

 

Domain Specific Language
特定領域語言,基於JSON格式的數據查詢,查詢更靈活,有利於複雜查詢
1、普通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" }
2、檢索:
  • 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" } } ] } }
 
2、DSL搜索
{ "query": { "match_all": {} } }
{ "query": { "match_all": {} }, "_source": ["id","username","age"] }
{ "query": { "match_all": {} }, "_source": ["id","username","age"], "from": 0, "size": 5 }
{ "query": { "match_phrase": { "desc": { "query": "今天 車流量", "slop": 100 } } } }
{ "query": { "term": { "desc": "學習" } } }
  • terms 對個關鍵字查詢
{ "query": { "terms": { "desc": ["學習","周紅","周江"] } } }
{ "query": { "match": { "desc": "周紅" } }, "_source": ["id","username","age"] }
{ "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%" } } } }
{ "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" ] } } }
{ "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:用於查詢後,對結果數據的篩選
實操:查詢帳戶金額大於80元,小於等於155.8元的用戶
gte:大於等於
lte:小於等於
gt:大於
lt:小於
{ "query": { "match": { "sex": "1" } }, "post_filter": { "range": { "money": { "gte": 60, "lte": 155.8 } } } }
 
{ "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" } ] }
{ "query": { "exists": { "field": "desc" } } }
 
{ "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歲,是一名畢業生,我在琦䯲星球作演講" ] } } ] } }
相關文章
相關標籤/搜索