ElasticSearch(7.2.2)-搜索的簡單使⽤

  • 2019 年 10 月 30 日
  • 筆記

版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。

本文鏈接:https://blog.csdn.net/weixin_42528266/article/details/102799417

準備⼯作
  • 刪掉nba索引
    • DELETE localhost:9200/nba
  • 新建⼀個索引,並且指定mapping
    • PUT localhost:9200/nba
{  	"mappings": {  		"properties": {  			"name": {  				"type": "text"  			},  			"team_name": {  				"type": "text"  			},  			"position": {  				"type": "text"  			},  			"play_year": {  				"type": "long"  			},  			"jerse_no": {  				"type": "keyword"  			}  		}  	}  }
新增document
  • PUT localhost:9200/nba/_doc/1
{  	 "name": "哈登",  	 "team_name": "⽕箭",  	 "position": "得分後衛",  	 "play_year": 10,  	 "jerse_no": "13"  }
  • PUT localhost:9200/nba/_doc/2
{  	 "name": "庫⾥",  	 "team_name": "勇⼠",  	 "position": "控球後衛",  	 "play_year": 10,  	 "jerse_no": "30"  }
  • PUT localhost:9200/nba/_doc/3
{  	 "name": "詹姆斯",  	 "team_name": "湖⼈",  	 "position": "⼩前鋒",  	 "play_year": 15,  	 "jerse_no": "23"  }
term(詞條)查詢和full text(全⽂)查詢
  • 詞條查詢:詞條查詢不會分析查詢條件,只有當詞條和查詢字元串完全匹配時,才匹配搜索。
  • 全⽂查詢:ElasticSearch引擎會先分析查詢字元串,將其拆分成多個分詞,只要已分析的欄位中包含詞條的任意⼀個,或全部包含,就匹配查詢條件,返回該⽂檔;如果不包含任意⼀個分詞,表示沒有任何⽂檔匹配查詢條件
單條term查詢
  • POST localhost:9200/nba/_search
{  	"query": {  		"term": {  			"jerse_no": "23"  		}  	}  }
多條term查詢
  • POST localhost:9200/nba/_search
{  	"query": {  		"terms": {  			"jerse_no": [  				"23",  				"13"  			]  		}  	}  }
{  	"took": 21,  	"timed_out": false,  	"_shards": {  		"total": 1,  		"successful": 1,  		"skipped": 0,  		"failed": 0  	},  	"hits": {  		"total": {  			"value": 2,  			"relation": "eq"  		},  		"max_score": 1,  		"hits": [{  				"_index": "nba",  				"_type": "_doc",  				"_id": "1",  				"_score": 1,  				"_source": {  					"name": "哈登",  					"team_name": "⽕箭",  					"position": "得分後衛",  					"play_year": 10,  					"jerse_no": "13"  				}  			},  			{  				"_index": "nba",  				"_type": "_doc",  				"_id": "3",  				"_score": 1,  				"_source": {  					"name": "詹姆斯",  					"team_name": "湖⼈",  					"position": "⼩前鋒",  					"play_year": 15,  					"jerse_no": "23"  				}  			}  		]  	}  }
match_all
  • POST localhost:9200/nba/_search
{  	"query": {  		"match_all": {}  	},  	"from": 0,  	"size": 10  }
{  	"took": 9,  	"timed_out": false,  	"_shards": {  		"total": 1,  		"successful": 1,  		"skipped": 0,  		"failed": 0  	},  	"hits": {  		"total": {  			"value": 3,  			"relation": "eq"  		},  		"max_score": 1,  		"hits": [{  				"_index": "nba",  				"_type": "_doc",  				"_id": "1",  				"_score": 1,  				"_source": {  					"name": "哈登",  					"team_name": "⽕箭",  					"position": "得分後衛",  					"play_year": 10,  					"jerse_no": "13"  				}  			},  			{  				"_index": "nba",  				"_type": "_doc",  				"_id": "2",  				"_score": 1,  				"_source": {  					"name": "庫⾥",  					"team_name": "勇⼠",  					"position": "控球後衛",  					"play_year": 10,  					"jerse_no": "30"  				}  			},  			{  				"_index": "nba",  				"_type": "_doc",  				"_id": "3",  				"_score": 1,  				"_source": {  					"name": "詹姆斯",  					"team_name": "湖⼈",  					"position": "⼩前鋒",  					"play_year": 15,  					"jerse_no": "23"  				}  			}  		]  	}  }
match
  • POST localhost:9200/nba/_search
{  	"query": {  		"match": {  			"position": "後衛"  		}  	}  }
{  	"took": 89,  	"timed_out": false,  	"_shards": {  		"total": 1,  		"successful": 1,  		"skipped": 0,  		"failed": 0  	},  	"hits": {  		"total": {  			"value": 2,  			"relation": "eq"  		},  		"max_score": 0.90630186,  		"hits": [{  				"_index": "nba",  				"_type": "_doc",  				"_id": "1",  				"_score": 0.90630186,  				"_source": {  					"name": "哈登",  					"team_name": "⽕箭",  					"position": "得分後衛",  					"play_year": 10,  					"jerse_no": "13"  				}  			},  			{  				"_index": "nba",  				"_type": "_doc",  				"_id": "2",  				"_score": 0.90630186,  				"_source": {  					"name": "庫⾥",  					"team_name": "勇⼠",  					"position": "控球後衛",  					"play_year": 10,  					"jerse_no": "30"  				}  			}  		]  	}  }
multi_match
  • POST localhost:9200/nba/_update/2
{  	"doc": {  		"name": "庫⾥",  		"team_name": "勇⼠",  		"position": "控球後衛",  		"play_year": 10,  		"jerse_no": "30",  		"title": "the best shooter"  	}  }
  • POST localhost:9200/nba/_search
{  	"query": {  		"multi_match": {  			"query": "shooter",  			"fields": ["title", "name"]  		}  	}  }
{  	"query": {  		"multi_match": {  			"query": "shooter",  			"fields": ["*title", "name"]  		}  	}  }
match_phrase
  • post localhost:9200/nba/_search
{  	"query": {  		"match_phrase": {  			"position": "得分後衛"  		}  	}  }
{  	"took": 4,  	"timed_out": false,  	"_shards": {  		"total": 1,  		"successful": 1,  		"skipped": 0,  		"failed": 0  	},  	"hits": {  		"total": {  			"value": 1,  			"relation": "eq"  		},  		"max_score": 3.0384295,  		"hits": [{  			"_index": "nba",  			"_type": "_doc",  			"_id": "1",  			"_score": 3.0384295,  			"_source": {  				"name": "哈登",  				"team_name": "⽕箭",  				"position": "得分後衛",  				"play_year": 10,  				"jerse_no": "13"  			}  		}]  	}  }
match_phrase_prefix
  • POST localhost:9200/nba/_update/3
{  	"doc": {  		"name": "詹姆斯",  		"team_name": "湖⼈",  		"position": "⼩前鋒",  		"play_year": 15,  		"jerse_no": "23",  		"title": "the best small forward"  	}  }
  • POST localhost:9200/nba/_search
{  	"query": {  		"match_phrase_prefix": {  			"title": "the best s"  		}  	}  }