天天看點

Elasticsearch---DSL搜尋實踐

Elasticsearch---DSL搜尋實踐

Domain Specific Language

特定領域語言,基于JSON格式的資料查詢,查詢更靈活,有利于複雜查詢

一、普通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:用于查詢後,對結果資料的篩選

實操:查詢賬戶金額大于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"
                }
            }
        }
    }
}           
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歲,是一名畢業生,我在琦䯲星球做演講"
                    ]
                }
            }
        ]
    }
}           

繼續閱讀