ElasticSearch基礎入門(二)基本查詢、結果過濾
- 一、基本查詢
- 1. 查詢所有(match_all)
- 2. 匹配查詢(match)
- 3. 多欄位查詢(multi_match)
- 4.詞條匹配(term)
- 5. 多詞條精確匹配(terms)
- 二、結果過濾
- 1.直接在_source后面指定欄位
- 2.指定includes和excludes
一、基本查詢
基本語法
GET /索引庫名/_search
{
"query":{
"查詢型別":{
"查詢條件":"查詢條件值"
}
}
}
"query"代表一個查詢物件,里面可以有不同的查詢屬性,
- 查詢型別:
- 例如:
match_all,match,term,range等等
- 例如:
- 查詢條件:查詢條件會根據型別的不同,寫法也有差異,
1. 查詢所有(match_all)
GET /youshop/_search
{
"query": {
"match_all": {}
}
}
結果如下:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "youshop",
"_type": "goods",
"_id": "1",
"_score": 1,
"_source": {
"title": "iphone xs max",
"price": 8848,
"images": "www.iphone.com",
"saleable": false
}
},
{
"_index": "youshop",
"_type": "goods",
"_id": "2",
"_score": 1,
"_source": {
"title": "iphone xs max",
"price": 8848,
"images": "www.iphone.com",
"saleable": true
}
}
]
}
}
- took:查詢花費時間,單位是毫秒
- time_out:是否超時
- _shards:分片資訊
- hits:搜索結果總覽物件
- total:搜索到的總條數
- max_score:所有結果中檔案得分的最高分
- hits:搜索結果的檔案物件陣列,每個元素是一條搜索到的檔案資訊
- _index:索引庫
- _type:檔案型別
- _id:檔案id
- _score:檔案得分
- _source:檔案的源資料
2. 匹配查詢(match)
match型別查詢,會把所查詢欄位的值進行分詞,然后查詢,分詞之后詞條之間的關系是or,
GET /索引庫名/_search
{
"query": {
"match": {
"title": "小米手機"
}
}
}
在本例中,小米手機會被拆分為小米和手機兩個詞條,match采用的默認關系是or,因此,搜索結果中只要包含這兩個詞條中的任何一個都可以,結果如下,小米手機以及小米電視都會被匹配到
"hits": [
{
"_index": "youshop",
"_type": "goods",
"_id": "4",
"_score": 1.5408844,
"_source": {
"title": "小米手機",
"images": "www.xiaomi.com",
"price": 2899,
"saleable": true
}
},
{
"_index": "youshop",
"_type": "goods",
"_id": "3",
"_score": 0.49917623,
"_source": {
"title": "小米電視",
"images": "www.xiaomidianshi.com",
"price": 3899,
"saleable": true
}
}
]
本例中如果想要單純的匹配到小米手機這個檔案,而不想找到小米電視,我們可以將詞條間的默認關系or修改為and
GET /youshop/_search
{
"query":{
"match": {
"title": {
"query": "小米手機",
"operator": "and"
}
}
}
}
現在,搜索結果就只剩下小米手機了!
"hits": [
{
"_index": "youshop",
"_type": "goods",
"_id": "4",
"_score": 1.5408844,
"_source": {
"title": "小米手機",
"images": "www.xiaomi.com",
"price": 2899,
"saleable": true
}
}
]
- or和and之間?
在 or 與 and 間二選一有點過于非黑即白, 如果用戶給定的條件分詞后有 5 個查詢詞項,想查找只包含其中 4 個詞的檔案,該如何處理?將 operator 運算子引數設定成 and 只會將此檔案排除,
有時候這正是我們期望的,但在全文搜索的大多數應用場景下,我們既想包含那些可能相關的檔案,同時又排除那些不太相關的,換句話說,我們想要處于中間某種結果,
match 查詢支持 minimum_should_match 最小匹配引數, 這讓我們可以指定必須匹配的詞項數用來表示一個檔案是否相關,我們可以將其設定為某個具體數字,更常用的做法是將其設定為一個百分數,因為我們無法控制用戶搜索時輸入的單詞數量,
GET /youshop/_search
{
"query":{
"match":{
"title":{
"query":"小米曲面電視",
"minimum_should_match": "75%"
}
}
}
}
本例中,搜索陳述句可以分為3個詞,如果使用and關系,需要同時滿足3個詞才會被搜索到,這里我們采用最小品牌數:75%,那么也就是說只要匹配到總詞條數量的75%即可,這里3*75% 約等于2,所以只要包含2個詞條就算滿足條件了,
3. 多欄位查詢(multi_match)
multi_match與match類似,但它可以在多個欄位中查詢
GET /youshop/_search
{
"query": {
"multi_match": {
"query": "小米",
"fields": ["title", "subTitle"]
}
}
}
從下圖的查詢結果中可以看到,副標題的包含小米的檔案也被搜索到了,
"hits": [
{
"_index": "youshop",
"_type": "goods",
"_id": "1",
"_score": 1.0,
"_source": {
"title": "iphone xs max",
"price": 8848.00,
"images": "www.iphone.com",
"saleable": false
}
},
{
"_index": "youshop",
"_type": "goods",
"_id": "3",
"_score": 1.0,
"_source": {
"title": "小米電視",
"images": "www.xiaomidianshi.com",
"price": 3899,
"saleable": true
}
},
{
"_index": "youshop",
"_type": "goods",
"_id": "4",
"_score": 1.0,
"_source": {
"title": "小米手機",
"images": "www.xiaomi.com",
"price": 2899,
"saleable": true
}
},
{
"_index": "youshop",
"_type": "goods",
"_id": "5",
"_score": 1.0,
"_source": {
"title": "紅米手機",
"subTitle": "小米旗下",
"price": 8848.00,
"images": "www.iphone.com",
"saleable": true
}
}
]
4.詞條匹配(term)
term查詢被用于精確值匹配,這些精確值可能是數字、時間、bool或者那些未分詞的字串,
GET /youshop/_search
{
"query": {
"term": {
"price": {
"value": "3899"
}
}
}
}
索引庫中的價格為3899的檔案將被匹配到,
"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "youshop",
"_type": "goods",
"_id": "3",
"_score": 1.0,
"_source": {
"title": "小米電視",
"images": "www.xiaomidianshi.com",
"price": 3899,
"saleable": true
}
}
]
5. 多詞條精確匹配(terms)
terms 查詢和 term 查詢一樣,但它允許你指定多值進行匹配,如果這個欄位包含了指定值中的任何一個值,那么這個檔案滿足條件,
GET /youshop/_search
{
"query":{
"terms":{
"price":[2699.00,2899.00,3899.00]
}
}
}
二、結果過濾
默認情況下,elasticsearch在搜索的結果中,會把檔案中保存在_source的所有欄位都回傳,
如果我們只想獲取其中的部分欄位,我們可以添加_source的過濾,
1.直接在_source后面指定欄位
GET /yosuhop/_search
{
"_source": ["title","price"],
"query": {
"term": {
"price": 2699
}
}
}
2.指定includes和excludes
我們也可以通過:
- includes:來指定想要顯示的欄位
- excludes:來指定不想要顯示的欄位
二者都是可選的,
GET /heima/_search
{
"_source": {
"includes":["title","price"]
},
"query": {
"term": {
"price": 2699
}
}
}
GET /heima/_search
{
"_source": {
"excludes": ["images"]
},
"query": {
"term": {
"price": 2699
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/263423.html
標籤:其他
上一篇:高頻面試題:Redis的使用場景
下一篇:web頁面錄制與回放全堆疊小專案
