首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ElasticSearch(7.2.2)-搜索的简单使⽤

ElasticSearch(7.2.2)-搜索的简单使⽤

作者头像
cwl_java
修改2019-10-31 17:55:57
3810
修改2019-10-31 17:55:57
举报
文章被收录于专栏:cwl_Javacwl_Java

版权声明:本文为博主原创文章,遵循 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"
		}
	}
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 准备操作
  • 新增document
  • term(词条)查询和full text(全⽂)查询
  • 单条term查询
  • 多条term查询
  • match_all
  • match
  • multi_match
  • match_phrase
  • match_phrase_prefix
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档