1 Star 0 Fork 2

封捷/atguigu-project-new-221126

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
代码:基础.txt 6.24 KB
一键复制 编辑 原始数据 按行查看 历史
jerry 提交于 2023-03-21 16:38 . 2023年3月21日 代码 补充
# 查询index中所有document
GET /db_hr/_search
# 查询指定id的单个document
GET /db_hr/_doc/1
# DSL:指定更详细的查询条件
GET /db_hr/_search
{
"query": {
"match": {
"emp_name": "tom"
}
}
}
# 插入测试数据
POST db_song/_doc
{
"song_name":"give me an apple",
"song_singer":"tom",
"song_album":"apple pie",
"song_lyrics":"i want to give you an apple",
"song_price":10
}
POST db_song/_doc
{
"song_name":"happy tears",
"song_singer":"jerry",
"song_album":"apple farmer",
"song_lyrics":"i will go to London",
"song_price":20
}
POST db_song/_doc
{
"song_name":"tears of happy",
"song_singer":"jerry",
"song_album":"apple farmer",
"song_lyrics":"i will go to NewYork",
"song_price":30
}
POST db_song/_doc
{
"song_name":"fly me",
"song_singer":"bigger one",
"song_album":"two star",
"song_lyrics":"i put an apple on your table",
"song_price":40
}
GET /db_song/_search
# DSL:查询全部
# 从DSL语法角度来说,第一行是请求方式和请求地址,第一行下面是一个JSON格式的请求体
GET /db_song/_search
{
"query": {
"match_all": {}
}
}
# DSL:关键词匹配一个字段查询
GET /db_song/_search
{
"query": {
"match": {
"song_name": "apple"
}
}
}
# DSL:关键词匹配多个字段
# 被命中的document的字段中,和关键词匹配的越多,在搜索结果中得分越高,说明匹配度越高
GET /db_song/_search
{
"query": {
"multi_match": {
"query": "apple",
"fields": [
"song_name",
"song_album",
"song_singer",
"song_lyrics"
]
}
}
}
# DSL查询:前缀匹配
# 分词结果以指定字符串开头的能匹配上
GET /db_song/_search
{
"query": {
"prefix": {
"song_name": {
"value": "app"
}
}
}
}
# DSL查询:关键词不分词
# 原始数据:happy tears
# 原始数据分词结果:[happy][tears]
# 匹配方式:用happy tears和分词结果匹配,发现它和happy、tears都匹配不上
GET /db_song/_search
{
"query": {
"term": {
"song_name": {
"value": "happy tears"
}
}
}
}
# 对比match
GET /db_song/_search
{
"query": {
"match": {
"song_name": "happy tears"
}
}
}
# DSL查询:多个不分词的关键词
GET /db_song/_search
{
"query": {
"terms": {
"song_name": [
"happy",
"tears"
]
}
}
}
# DSL查询:范围查询
# gte: 大于等于,greater than equal
# lte: 小于等于,less than equal
# gt: 大于,greater than
# lt: 小于,less than
GET /db_song/_search
{
"query": {
"range": {
"song_price": {
"gte": 20,
"lte": 30
}
}
}
}
GET /db_song/_search
{
"query": {
"range": {
"song_price": {
"gt": 20,
"lt": 40
}
}
}
}
GET /db_song/_search
# DSL查询:bool查询
# bool查询中的must
# must的值是一个数组,数组中每一个对象是一个查询条件,各个条件之间是“且”的关系
GET /db_song/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"song_name": "apple"
}
},
{
"match": {
"song_lyrics": "want"
}
}
]
}
}
}
# should的值是一个数组,数组中每一个对象是一个查询条件,各个条件之间是“或”的关系
GET /db_song/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"song_name": "apple"
}
},
{
"match": {
"song_lyrics": "London"
}
}
]
}
}
}
# must_not:各个条件取反,取反之后是“且”的关系
GET /db_song/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"song_name": "apple"
}
},
{
"match": {
"song_lyrics": "London"
}
}
]
}
}
}
# DSL查询:filter相当于不计算得分的must
GET /db_song/_search
{
"query": {
"bool": {
"filter": [
{
"match": {
"song_name":"apple"
}
},
{
"match": {
"song_lyrics":"want"
}
}
]
}
}
}
# DSL查询:组合各种语法
GET /db_song/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"song_name": "apple"
}
}
],
"filter": [
{
"range": {
"song_price": {
"gte": 10,
"lte": 50
}
}
}
]
}
}
}
# 指定查询结果中显示哪些字段
GET /db_hr/_search
{
"query": {
"match": {
"emp_name": "adler"
}
},
"_source": [
"emp_name",
"emp_age",
"emp_salary"
]
}
# 高亮效果
# 默认效果:围绕关键词加em标签
GET /db_hr/_search
{
"query": {
"match": {
"emp_name": "adler"
}
},
"highlight": {
"fields": {
"emp_name": {}
}
}
}
# 指定高亮效果
GET /db_hr/_search
{
"query": {
"match": {
"emp_name": "adler"
}
},
"highlight": {
"fields": {
"emp_name": {
"pre_tags": "<span style='color:red;'>",
"post_tags": "</span>"
}
}
}
}
# 分页:常规分页
# from和页码之间需要进行换算
# from=(pageNo-1)*pageSize
# size=pageSize
GET /db_hr/_search
{
"from": 5,
"size": 5
}
# 分页:滚动分页
GET /db_hr/_search?scroll=5m
{
"size": 2
}
GET /_search/scroll
{
"scroll": "5m",
"scroll_id": "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFFNBSW1Bb2NCNTA5a2g1SUNMTEJTAAAAAAAADm8WcThUX01NdUxRY0dBQ2FhdTZ4RXVrdw=="
}
# 排序
# 如果涉及多个字段,则先根据第一个字段排序
# 在第一个字段值重复的范围内,再根据第二个字段排序
# 以此类推
GET db_hr/_search
{
"query": {
"match": {
"emp_name": "robert"
}
},
"sort": [
{
"emp_age": {
"order": "desc"
}
},
{
"emp_salary": {
"order": "asc"
}
}
]
}
# 模糊查询
# 原始文档:adler
GET /db_hr/_search
{
"query": {
"fuzzy": {
"emp_name": {
"value": "ader",
"fuzziness": "auto:2,5"
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/heavy_code_industry/atguigu-project-new-221126.git
[email protected]:heavy_code_industry/atguigu-project-new-221126.git
heavy_code_industry
atguigu-project-new-221126
atguigu-project-new-221126
master

搜索帮助