高级查询
Query DSL <Domain Specified Language>
语法
# GET /索引名/_doc/_search {json格式请求体数据}
简化
# GET /索引名/_search {json格式请求体数据}
测试数据
# 查询所有 match_all
# GET /products/_doc/_search
GET /products/_search
{
"query": {
"match_all": {}
}
}
# 查询映射信息
GET /products/_mapping
关键词查询[term]
# keyword integer price date 类型 不分词
# text 标准分词器 中文 单字分词,英文 单次分词。
# 在 Es 中除了 text 分词,其余都不分词,默认标准分词器
GET /products/_search
{
"query": {
"term": {
"description": {
"value": "还"
}
}
}
}
GET /products/_search
{
"query": {
"term": {
"title": {
"value": "iphone14"
}
}
}
}
范围查询[range]
GET /products/_search
{
"query": {
"range": {
"price": {
"gte": 1,
"lte": 100000
}
}
}
}
前缀查询[prefix]
GET /products/_search
{
"query": {
"prefix": {
"description": {
"value": "iph"
}
}
}
}
通配符查询[wildcard] ? 匹配一个 * 匹配多个
GET /products/_search
{
"query": {
"wildcard": {
"description": {
"value": "iphon?"
}
}
}
}
多id查询[ids]
GET /products/_search
{
"query": {
"ids": {
"values": [1, 2, 3, 4]
}
}
}
模糊查询[fuzzy]
# 关键词<=2 不允许模糊 3-5只允许1次模糊 >5最多允许两次模糊
GET /products/_search
{
"query": {
"fuzzy": {
"title": "iphone15"
}
}
}
布尔查询[bool] 组合多个条件实现查询
GET /products/_search
{
"query": {
"bool": {
"must": [
{
"ids": {
"values": [2]
}
},
{
"term": {
"description": {
"value": "iphone"
}
}
}
]
}
}
}
多字段查询[multi_match]
# !字段类型分词 将查询条件进行分词之后进行查询该字段 如果该字段不分词就会将查询条件作为整体进行查询
GET /products/_search
{
"query": {
"multi_match": {
"query": "16 14",
"fields": ["title", "description"]
}
}
}
默认字段分词查询[query_string]
GET /products/_search
{
"query": {
"query_string": {
"default_field": "description",
"query": "iphone 16"
}
},
"highlight": {
"fields": {
"description": {}
}
},
"from": 0,
"size": 10,
"sort": [
{
"price": {
"order": "asc"
}
}
],
"_source": ["title", "description"]
}
# 高亮字段必须能分词