代码拉取完成,页面将自动刷新
# 查询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"
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。