elasticsearch-高级查询

高级查询

ES中提供了一种强大的检索数据方式,这中检索方式称之为 Query DSL 。Query DSL是利用Res API传递json格式的请求体(RequestBody)数据与ES进行交互,这种方式的 丰富查询语法 让ES检索变得更强大,更简洁。

语法
# GET /索引名/_doc/_search {json格式请求体数据}
# GET /索引名/_search {json格式请求体数据}
查询所有
# query DSL 查询所有 match_all
GET /products/_doc/_search
{
  "query":{
    "match_all": {}
  }
}

GET /products/_search
{
  "query": {
    "match_all": {}
  }
}

在这里插入图片描述

关键词查询(term)

# term 关键词查询
# keyword integer double date 不分词
# text 默认ES标准分词器 中文单字分词 英文单词分词
# 1.在 ES 中,除了 text 类型分词,其余类型均不分词
# 2.在 ES 中默认使用标准分词器 中文单字分词 英文单词分词
GET /products/_search
{
  "query":{
    "term": {
      "price": {
        "value": 1.5
      }
    }
  }
}

1.在 ES 中,除了 text 类型分词,其余类型均不分词
2.在 ES 中默认使用标准分词器 中文单字分词 英文单词分词

范围查询 range
GET /products/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 1,
        "lte": 2.5
      }
    }
  }
}

在这里插入图片描述

前缀查询 prefix
GET /products/_search
{
  "query": {
    "prefix": {
      "title": {
        "value": "冰"
      }
    }
  }
}

在这里插入图片描述

通配符查询 wildcard

? 用来匹配一个任意字符 * 用来匹配多个任意字符

GET /products/_search
{
  "query": {
    "wildcard": {
      "title": {
        "value": "冰??梨"
      }
    }
  }
}


GET /products/_search
{
  "query": {
    "wildcard": {
      "title": {
        "value": "*梨"
      }
    }
  }
}

在这里插入图片描述

ids 查询 docId 查询
{
  "query": {
    "ids": {
      "values": ["5","6","7","8","10"]
    }
  }
}

在这里插入图片描述

模糊查询 fuzzy

用来查询含有制定关键字的文档

  • fuzzy 最大模糊错误必须在 0-2 之间
  • 搜索关键词长度为 2 ,不允许存在模糊
  • 搜索关键词长度为3-5,允许一次模糊
  • 搜索关键词长度大于5,允许最大2次模糊

GET /products/_search
{
  "query": {
    "fuzzy": {
      "title": "卫龙"
      }
    }
  }
}

GET /products/_search
{
  "query": {
    "fuzzy": {
      "title": "冰糖雪花"
      }
    }
  }
}

在这里插入图片描述

布尔查询 bool

bool关键字:用来组合多个条件实现复杂查询

  • must 相当于 && 同时成立
  • should 相当于 || 成立一个就行
  • must_not 相当于! 不能满足任何一个
GET /products/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "ids": {
            "values": [7]
          }
        },
        {
          "term": {
            "title": {
              "value": "冰糖雪梨"
            }
          }
        }
      ]
    }
  }
}


GET /products/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "ids": {
            "values": [7]
          }
        },
        {
          "term": {
            "title": {
              "value": "冰糖"
            }
          }
        }
      ]
    }
  }
}


GET /products/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "ids": {
            "values": [7]
          }
        },
        {
          "term": {
            "title": {
              "value": "milk"
            }
          }
        }
      ]
    }
  }
}

多字段查询 multi_match

如果字段类型分词,将查询条件分词之后进行查询该字段;如果该字段不分词,会将查询条件作为整体进行查询

因为 title 字段是 keyword 类型,所以不分词,进行整体查询,将 冰糖 看做整体条件
description 字段是 text 类型,按照标准分词后吗,再对 description 字段查询


GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "冰糖",
      "fields": ["title","description"]
    }
  }
}

在这里插入图片描述

默认字段分词查询 query_string

查询字段分词就将查询条件分词查询 ,查询字段不分词将查询条件不分词查询

GET /products/_search
{
  "query": {
    "query_string": {
      "default_field": "description",
      "query": "梨花"
    }
  }
}
高亮查询 highlight

highlight关键字,可以让符合条件的文档中的关键词高亮
pre_tags: 高亮前缀
post_tags: 高亮后缀
高亮不会修改原始内容,即 _source 字段中的内容,会将高亮内容放在 highlight 属性中,所以最终取得时候,需要取 hightlight 中的内容

GET /products/_search
{
  "query": {
    "term": {
      "description": {
        "value": "吃"
      }
    }
  },
  "highlight": {
    "pre_tags": ["<span style='color:red'>"],
    "post_tags": ["</span>"], 
    "fields": {
      "*": {}
    }
  }
}

在这里插入图片描述

分页查询 from size

size 默认为10
from 起始位置


GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "size": 2,
  "from": 2
}

指定字段排序 sort

GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "size": 2,
  "from": 2,
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

在这里插入图片描述

返回指定字段
GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "size": 2,
  "from": 2,
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ],
  "_source": ["title","price"]
}

在这里插入图片描述

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值