1 介紹
主要介紹索引請求的基礎(chǔ)API操作,使用postman進行請求,接口請求的前綴地址統(tǒng)一為elasticsearch 部署IP地址+端口號(例如 http://192.168.51.4:9200 。
統(tǒng)一請求地址:
POST /search_demo/_doc/_search
2 multi_match
multi_match 滿足使用 match 在多個字段中進行查詢的需求
傳遞JSON數(shù)據(jù)
{
"query": {
"multi_match": {
"query": "組合",
"fields": [
"desc","nickname"
]
}
},
"_source": [
"id",
"nickname",
"desc"
]
}
請求結(jié)果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 2.2874916,
"hits": [
{
"_index": "search_demo",
"_type": "_doc",
"_id": "1007",
"_score": 2.2874916,
"_source": {
"nickname": "老男孩",
"id": 1007,
"desc": "確實是個很好的組合,筷子 筷子"
}
}
]
}
}
3 boost
boost 權(quán)重,為某個字段設(shè)置權(quán)重,權(quán)重越高,文檔相關(guān)性得分就越高。通常來說,搜索商品名稱要比商品簡介的權(quán)重要高。
傳遞JSON數(shù)據(jù)
{
"query": {
"multi_match": {
"query": "好的",
"fields": [
"desc","nickname*10"
]
}
},
"_source": [
"id",
"nickname",
"desc"
]
}
請求結(jié)果
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 9,
"relation": "eq"
},
"max_score": 3.1980762,
"hits": [
{
"_index": "search_demo",
"_type": "_doc",
"_id": "1004",
"_score": 3.1980762,
"_source": {
"nickname": "紅帽子",
"id": 1004,
"desc": "好的系統(tǒng)必須擁有穩(wěn)定的系統(tǒng)結(jié)構(gòu)"
}
},
{
"_index": "search_demo",
"_type": "_doc",
"_id": "1005",
"_score": 3.0979095,
"_source": {
"nickname": "switch游戲機",
"id": 1005,
"desc": "好的游戲,才會有人購買,比如塞爾達"
}
},
{
"_index": "search_demo",
"_type": "_doc",
"_id": "1003",
"_score": 0.37556386,
"_source": {
"nickname": "渦輪增壓",
"id": 1003,
"desc": "極限的速度是需要渦輪增壓的"
}
},
{
"_index": "search_demo",
"_type": "_doc",
"_id": "1012",
"_score": 0.36424035,
"_source": {
"nickname": "youzi",
"id": 1012,
"desc": "永遠(yuǎn)的神"
}
},
{
"_index": "search_demo",
"_type": "_doc",
"_id": "1002",
"_score": 0.35254776,
"_source": {
"nickname": "進擊的巨人",
"id": 1002,
"desc": "艾倫是會變成真正的巨人的"
}
},
{
"_index": "search_demo",
"_type": "_doc",
"_id": "1011",
"_score": 0.27665582,
"_source": {
"nickname": "皮特",
"id": 1011,
"desc": "皮特的姓氏好像是彼得"
}
},
{
"_index": "search_demo",
"_type": "_doc",
"_id": "1007",
"_score": 0.2639615,
"_source": {
"nickname": "老男孩",
"id": 1007,
"desc": "確實是個很好的組合,筷子 筷子"
}
},
{
"_index": "search_demo",
"_type": "_doc",
"_id": "1009",
"_score": 0.252381,
"_source": {
"nickname": "露西",
"id": 1009,
"desc": "露西是一只很聰明的cat"
}
},
{
"_index": "search_demo",
"_type": "_doc",
"_id": "1001",
"_score": 0.18093815,
"_source": {
"nickname": "飛翔的荷蘭號",
"id": 1001,
"desc": "我在p2pi網(wǎng)站解決項目中遇到的問題,學(xué)習(xí)到了很多知識"
}
}
]
}
}
nickname^10 代表搜索提升10倍的相關(guān)性,也就是說用戶搜索的時候,其實以這個nickname為主,desc為輔,nickname的匹配相關(guān)度高只要提升權(quán)重比例就可以。
4 布爾查詢
可以實現(xiàn)多重組合查詢
- must:查詢必須匹配搜索條件,譬如 and
- should:查詢匹配滿足一個條件,譬如 or
- must_not:不匹配搜索條件,一個都不滿足,譬如 not in
單個查詢
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "好的",
"fields": ["desc","nickname"]
}
},
{
"term": {
"sex": 0
}
},
{
"term": {
"birthday": "1992-12-24"
}
}
]
}
},
"_source": [
"id",
"sex",
"nickname",
"desc"
]
}
組合查詢
{
"query": {
"bool": {
"must": [
{
"match": {
"desc": "好的"
}
},
{
"match": {
"nickname": "好的"
}
}
],
"should": [
{
"match": {
"sex": 1
}
}
],
"must_not":[
{
"term": {
"birthday": "1993-01-24"
}
}
]
}
},
"_source": [
"id",
"sex",
"nickname",
"desc",
"birthday"
]
}
為指定詞語加權(quán)
{
"query": {
"bool": {
"should": [
{
"match": {
"desc": {
"query": "好的",
"boost": 2
}
}
},
{
"match": {
"desc": {
"query": "男孩",
"boost": 20
}
}
}
]
}
},
"_source": [
"id",
"sex",
"nickname",
"desc",
"birthday"
]
}
5 相關(guān)信息
- 博文不易,辛苦各位猿友點個關(guān)注和贊,感謝