Elasticsearch入门4 - 通过 query 实现简单搜索

例如

curl -X GET "localhost:9200/twitter/_search?q=user:kimchy&pretty"

q指定查询语句,使用Query String Syntax
df默认字段,不指定时,会对所有字段进行查询
profile显示查询如何执行的
analyzer 指定分析器
参数说明可在下面链接中查看
https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-uri-request.html

movies数据导入请看:
es+kibana+logstash快速安装上手

查询title字段包含2012的结果,不加df的话,会对所有字段进行查询q=2012&df=title也等价于q=title:2012
GET /movies/_search?q=2012&df=title
{
  "profile": "true"
}
在profile
"profile" : {
    "shards" : [
      {
        "id" : "[Ko-M9qn6S_eVTQodS2IElA][movies][0]",
        "searches" : [
          {
            "query" : [
              {
                "type" : "TermQuery",
                "description" : "title:2012",
                "time_in_nanos" : 1664736,

分组
GET /movies/_search?q=title:(2012 2013)
{
  "profile": "true"
}

  "profile" : {
    "shards" : [
      {
        "id" : "[Ko-M9qn6S_eVTQodS2IElA][movies][0]",
        "searches" : [
          {
            "query" : [
              {
                "type" : "BooleanQuery",
                "description" : "title:2012 title:2013",	# 查询title为2012,或者2013
布尔操作

AND OR NOT 或者 && // !

注意,必须大写

AND

GET /movies/_search?q=title:(beautiful AND mind)
{
  "profile": "true"
}

            "query" : [
              {
                "type" : "BooleanQuery",
                "description" : "+title:beautiful +title:mind",

NOT

GET /movies/_search?q=title:(beautiful not mind)
{
  "profile": "true"
}

            "query" : [
              {
                "type" : "BooleanQuery",
                "description" : "title:beautiful title:not title:mind",
范围查询
  • 区间表示:
    • year:[* TO 2018]
GET /movies/_search?q=year: [* TO  2018]
算数符号
  • year: > 2010
  • year:(>=2010 AND <2019)
GET /movies/_search?q=year: (>=2010 AND <2019)
通配符查询

通配符查询效率低,占用内存大,不建议使用

  • ?代表一个字符,* 代表0或多个字符
    • title: mi?d
    • title: be*
GET /movies/_search?q=title:(Grumpier Old M?>)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值