Elasticsearch 基本语法 及组合查询

基本语法

#查询
GET /school/student/_search  #查询数据表中的所有内容
GET /school/_search          #查询数据库中的所有内容
GET /school/student/1        #查询数据表中编号为1的数据

#新增
PUT /school/student/3
{
  "name" : "王五",
  "age" : 50,
  "sex" : "男",
  "phone" : "13112345679",
  "address" : "上海静安"
}

#修改,其中编号为2的数据已存在
PUT /school/student/2
{
  "name" : "李四",
  "age" : 45,
  "sex" : "女",
  "phone" : "13112345670",
  "address" : "北京海淀"
}

#删除
DELETE /school/student/3

#条件查询
GET /school/student/_mget   #根据多个Id查询
{                           #对应sql: select * from student where id in (1,3)
  "ids":[1,3]
}

GET /school/student/_search?q=age:30 #查询age为30的数据
GET /school/student/_search?q=age[35 TO 50] #查询age为30至40间的数据 
GET /school/student/_search?q=age:>=45 #查询age大于等于45的

#分页
GET /school/student/_search?from=0&size=1 #from-起始下标,size-页面大小
GET /school/student/_search?q=age:<=45&from=0&size=2 #条件查询加分页

#字段选取
GET /school/student/_search?_source=name,age
GET /school/student/_search?q=age:<=45&_source=name,age,phone #条件查询加字段选取

#排序
GET /school/student/_search?sort=age:desc
GET /school/student/_search?q=age:<=45&sort=age:desc

#组合查询
GET /school/student/_search?q=age:<=45&sort=age:desc&from=0&size=2&_source=name,sex




组合查询

类型:
1. 核心数据类型
(1)字符串类型:text, keyword
(2)数字类型:long, integer, short, byte, double, float, half_float, scaled_float
(3)日期:date
(4)日期纳秒:date_nanos
(5)布尔型:boolean
(6)Binary:binary
(7)Range: integer_range, float_range, long_range, double_range, date_range
2. 复杂数据类型
(1)Object: object(for single JSON objects)
(2)Nested: nested (for arrays of JSON objects)

命令:

get /school/students/_mapping #查询表格结构

put /news/_mapping/article
{
  "article" : {
    "properties": {
      "articleId": {
        "type": "integer"
      },
       "title": {
        "type": "text",   #文本会被分词,模糊搜索:相当于可以使用like %keyword%
        "analyzer": "ik_smart",        #输入建索引时的分词器,不指明分词器会将每一个字作为一个词
        "search_analyzer": "ik_smart"  #查询时对查询语句进行分词
      },
      "content": {
        "type": "text",
        "analyzer": "ik_smart",
        "search_analyzer": "ik_smart"
      },
      "image": {
        "type": "keyword" #作为整体看待,不会切分为词,精确匹配:相当于=keyword
      },
      "category": {
        "type": "object"
      },
      "createDate": {
        "type": "date"
      },
      "status": {
        "type": "keyword"
      }
    }
  }
}

#select * from article where title like '%拜登%'
get /news/article/_search
{
  "query": {
    "match": {
      "title": "拜登"
    }
  }
}

#select * from article where title like '%拜登%' or content like '%拜登%'
get /news/article/_search
{
  "query": {
    "bool": {
      "should": [   #should=or must=and
        {
          "match": {
            "title": "拜登"
          }
        },
        {
          "match": {
            "content": "拜登"
          }
        }
      ]
    }
  }
}

#select * from article where (title like '%拜登%' or content like '%拜登%') and categoryId = 1
get /news/article/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "拜登"
          }
        },
        {
          "match": {
            "content": "拜登"
          }
        }
      ],
      "filter": {        #对返回结果进行过滤(效率比must高,因为不需要打分)
        "term": {        #term为精确匹配
          "category.categoryId": 1
        }
      }
    }
  }
}


get /news/article/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "拜登"
          }
        },
        {
          "match": {
            "content": "拜登"
          }
        }
      ],
      "filter": [   #多个条件是and的关系,多个条件同时成立
        {
          "term": {
            "category.categoryId": 1
          }
        }
        ,
        {
          "range": {
            "createDate": {
              "gte": "2020-08-01",
              "lte": "2020-08-10"
            }
          }
        }
      ]
    }
  }
}


get /news/article/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "拜登"
          }
        },
        {
          "match": {
            "content": "拜登"
          }
        }
      ],
      "filter": {
        "bool": {
          "should": [ #多个条件是or的关系,多个条件有一个成立即可
            {
              "range": {
                "createDate": {
                  "gte": "2020-08-01",
                  "lte": "2020-08-10"
                }
              }
            },
            {
              "term": {
                "category.categoryId": 1
              }
            }
          ]
        }
      }
    }
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值