本文仅用于快速查询DSL语句,具体使用方式请查看上篇笔记
[TOC]
1. 索引操作
常见的mapping属性
属性 | 类别 | 说明 |
---|---|---|
type | ||
字符类 | text(可分词)、keyword(精确值) | |
数值类 | long、integer、short、byte、double、float | |
布尔 | boolean | |
日期 | date | |
对象 | object | |
index | 是否创建索引 | |
analyzer | 使用哪种分词器 | |
properties | 该字段的子字段 |
1.1. 新建索引
PUT /{索引库名}
{
"mappings": {
"properties": {
"字段名":{
"type": "text",
"analyzer": "ik_smart"
},
"字段名2":{
"type": "keyword",
"index": "false"
},
"字段名3":{
"properties": {
"子字段": {
"type": "keyword"
}
}
},
// ...略
}
}
}
示例:
PUT /demo
{
"mappings":{
"properties":{
"info":{
"type":"text",
"analyzer":"ik_smart"
},
"email":{
"type":"keyword",
"index":"false"
},
"name":{
"properties":{
"firstName":{
"type":"keyword"
},
"lastName":{
"type":"keyword"
}
}
}
}
}
}
1.2. 删除索引
DELETE /{索引库名}
1.3. 添加索引字段
PUT /{索引库名}/_mapping
{
"properties":{
"新字段名":{
"type":"integer"
}
}
}
示例:
PUT /demo/_mapping
{
"properties":{
"age":{
"type":"integer"
}
}
}
1.4. 查询索引
GET /索引库名
2. 文档操作
2.1. 新增文档
POST /{索引库名}/_doc/文档id
{
"字段1": "值1",
"字段2": "值2",
"字段3": {
"子属性1": "值3",
"子属性2": "值4"
},
// ...
}
示例:
POST /demo/_doc/1
{
"info":"欢迎来到小默的博客",
"email":"mosfield@xiaomo.link",
"name":{
"firstName":"云",
"lastName":"赵"
}
}
2.2. 删除文档
DELETE /{索引库名}/_doc/id值
2.3. 修改文档
2.3.1. 全量修改
PUT /{索引库名}/_doc/文档id
{
"字段1": "值1",
"字段2": "值2",
// ... 略
}
示例:
PUT /demo/_doc/1
{
"info":"欢迎来到小默的博客",
"email":"mosfield@xiaomo.link",
"name":{
"firstName":"云",
"lastName":"赵"
}
}
2.3.2. 增量修改
POST /{索引库名}/_update/文档id
{
"doc": {
"字段名": "新的值",
}
}
示例:
POST /demo/_update/1
{
"doc":{
"email":"ZhaoYun@xiaomo.link"
}
}
2.4. 查询文档
GET /{索引库名称}/_doc/{id}
3. 文档查询
3.1. 查询所有文档
GET /{索引库名}/_search
{
"query": {
"match_all": {}
}
}
3.2. 全文检索
3.2.1. 从一个字段中检索一个词
GET /{索引库名}/_search
{
"query": {
"match": {
"字段名": "具体要查询的词"
}
}
}
3.2.2. 从多个字段中检索一个词
GET /{索引库名}/_search
{
"query": {
"multi_match": {
"query": "具体要查询的词",
"fields": ["字段名1", " 字段名2"]
}
}
}
3.3. 查询keyword
GET /{索引库名}/_search
{
"query": {
"term": {
"字段名": {
"value": "具体要查询的keyword"
}
}
}
}
3.4. 范围查询
GET /{索引库名}/_search
{
"query": {
"range": {
"字段名" {
"gte": 10, // 大于等于
"lte": 20 // 小于等于
}
}
}
}
3.5. 地理坐标查询
GPS坐标中是(纬度, 经度)
3.5.1. 矩形范围查询
GET /{索引库名}/_search
{
"query":{
"geo_bounding_box":{
"字段名":{
"top_left":{ // 左上点
"lat":31.1, // 纬度
"lon":121.5 // 经度
},
"bottom_right":{ // 右下点
"lat":30.9, // 纬度
"lon":121.7 // 经度
}
}
}
}
}
3.5.2. 圆形距离查询
GET /{索引库名}/_search
{
"query":{
"geo_distance":{
"distance":"距离km",
"FIELD":"31.21,121.5"
}
}
}
3.6. 布尔查询
- must:必须匹配每个子查询,类似“与” and
- should:选择性匹配子查询,类似“或” or
- must_not:必须不匹配,不参与算分,类似“非”
- filter:必须匹配,不参与算分
示例
GET /hotel/_search
{
"query": {
"bool": {
"must": [
{"term": {"city": "上海" }}
],
"should": [
{"term": {"brand": "皇冠假日" }},
{"term": {"brand": "华美达" }}
],
"must_not": [
{ "range": { "price": { "lte": 500 } }}
],
"filter": [
{ "range": {"score": { "gte": 45 } }}
]
}
}
}
3.7. 算分函数
GET /{索引库名}/_search
{
"query": {
"function_score": {
"query": { .... }, // 原始查询,可以是普通的query,也可以把这里的query替换成布尔查询
"functions": [ // 算分函数
{
"filter": { // 满足的条件,品牌必须是如家
"term": {
"brand": "如家"
}
},
"weight": 2 // 算分权重为2
}
],
"boost_mode": "sum" // 加权模式,求和
}
}
}
示例
GET /item/_search
{
"query": {
"function_score": {
"query": {
"match": {
"category": "拉杆箱"
}
},
"functions": [
{
"filter": {
"term": {
"brand": "小米"
}
},
"weight": 2
}
],
"boost_mode": "multiply"
}
}
}
4. 结果处理
4.1. 排序
4.1.1. 普通字段排序
GET /{索引库名}/_search
{
"query": {...},
"sort": [
{
"字段名": "desc" // 排序字段、排序方式ASC、DESC
}
]
}
4.1.2. 地理坐标排序
GET /{索引库名}/_search
{
"query": {...},
"sort": [
{
"_geo_distance" : {
"字段名" : "101,101", // 纬度, 经度
"order" : "asc", // 排序方式
"unit" : "km" // 排序的距离单位
}
}
]
}
4.2. 分页
4.2.1. 基本分页
GET /{索引库名}/_search
{
"query": {...},
"from": 0, // 开始位置下标(从0开始)
"size": 10, // 偏移量
"sort": [
{"字段名": "asc"}
]
}
4.2.2. 深度分页
GET /{索引库名}/_search
{
"query":{...},
"sort": [
{
"字段1": "asc"
},
{
"字段2":"asc"
}
],
"size":100,
"search_after":[// 上一页最后一条记录的sort
135,
"2316304"
]
}
4.3. 高亮
GET /{索引库名}/_search
{
"query": {
"match": {
"字段名": "查询的值" // 查询条件,高亮一定要使用全文检索查询 text
}
},
"highlight": {
"fields": { // 指定要高亮的字段
"字段名": {
"pre_tags": "<em>", // 用来标记高亮字段的前置标签
"post_tags": "</em>" // 用来标记高亮字段的后置标签
}
}
}
}
Comments NOTHING