ES中的DSL语句操作

ES中的查询分为URI Search、Request Body Search。
URI Search - 在URL中使用查询参数。
Request Body Search - 使用JSON格式的入参作为查询条件。DSL语句就是基于Request Body Search查询类型的

查询索引相关的用法

语法 范围
/_search 集群上所有的索引
/index1/_search index1索引
/index1,index2/_search index1和index2索引
/index*/_search 以index开头的索引

查询索引下的文档

## 无Request body 查询
GET /orders/_search


## 有Request body 查询索引下的文档
GET /orders/_search
{
  "query": {
    "match_all": {}
  }
}

基于关键词查询

在ES中默认Text数据类型可以分词,其他数据类型都不能进行分词
Text数据类型 默认ES使用的是标准分词器,标准分词器对中文是单字分词,对英文是单词分词
对不能分词的数据类型查询,要输入整个关键词,对能分词的数据类型查询,可以输入想要查询的词

## description的数据类型是Text,且是英文的,就可以用单个分词去查询
GET /my_index/_search
{
  "query": {
    "term": {
      "description": {
        "value": "low"
      }
    }
  }
}

## user的数据类型是keyword,不能进行分词,所以只能输入整个keyword
GET /my_index/_search
{
  "query": {
    "term": {
      "user": {
        "value": "Tom"
      }
    }
  }
}

范围查询

GET /my_index/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 200,
        "lte": 1000
      }
    }
  }
}

前缀查询 用来检索含有指定前缀的关键词

GET /my_index/_search
{
  "query": {
    "prefix": {
      "title": {
        "value": "ka"
      }
    }
  }
}

通配符查询,?代表一个字符,*代表有多个字符

## ?代表后边有1个字符,进行通配查询
GET /my_index/_search
{
  "query": {
    "wildcard": {
      "user": {
        "value": "To?"
      }
    }
  }
}

## *代表后边有多个字符,进行通配查询
GET /my_index/_search
{
  "query": {
    "wildcard": {
      "user": {
        "value": "J*"
      }
    }
  }
}

多个ID查询

GET /my_index/_search
{
  "query": {
    "ids": {
      "values": [1,2,5]
    }
  }
}

fuzzy 模糊查询

当搜索关键词长度为2 , 不允许存在模糊查询
当搜索关键词长度为3-5 , 允许1次模糊查询
当搜索关键词长度大于5 , 允许最大2次模糊查询

## my_index索引的descrition中并没有hongxian关键词,只有hongmian
## 因关键词长度大于5,允许进行模糊查询
GET /my_index/_search
{
  "query":{
    "fuzzy": {
      "description": "hongxian"
    }
  }
}

bool 布尔查询

must 相当于 and 同时成立
should 相当于 or 成立一个就行
must_not 相当于 ! 不能满足任何一个

GET /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "price": {
              "value": "795"
            }
          }
        }
      ]
    }
  }
}

高亮查询【highlight】

高亮查询不是在原文档上进行修改的,是另外存放了一个地方
可以自己手动指定高亮查询的前缀和后缀形式

## 普通高亮查询
GET /my_index/_search
{
  "query":{
    "term": {
      "description": {
        "value": "hongmian"
      }
    }
  },
  "highlight": {
    "fields": {
      "*":{ }
    }
  }
}

## 高亮查询[highlight] 指定高亮前缀和后缀
GET /my_index/_search
{
  "query":{
    "term": {
      "description": {
        "value": "hongmian"
      }
    }
  },
  "highlight": {
    "pre_tags":["<span style='color:red;'>"],
    "post_tags": ["</span>"],
    "require_field_match": "false", ## 是否需要字段匹配,默认true 。false会查询其他字段
    "fields": {
      "*":{ }
    }
    
  }
}

分页查询

分页查询,指定返回条数,ES默认是返回10条
分页查询,也可指定从多少条开始返回,默认从0开始计数

## 指定分页查询中返回前3条
GET /my_index/_search
{
  "query": {
    "query_string": {
      "default_field": "description",
      "query": "is"
    }
  },
  "size": 3
}

## 指定分页查询,从第2条开始返回,返回3条,查询结果是2,3,4
GET /my_index/_search
{
  "query": {
    "query_string": {
      "default_field": "description",
      "query": "is"
    }
  },
  "from":2,
  "size": 3
}

排序

GET /my_index/_search
{
  "query": {
    "query_string": {
      "default_field": "description",
      "query": "is"
    }
  },
  "sort":[
    {
     "price":{
        "order":"desc"
     }
    }
  ]
}

返回指定字段

GET /my_index/_search
{
  "query": {
    "query_string": {
      "default_field": "description",
      "query": "is"
    }
  },
  "_source": ["id","title","description"]
}

查询条数

GET /my_index/_count
{
  "query": {
    "query_string": {
      "default_field": "description",
      "query": "is"
    }
  }
}

通过指定字段去重复查询

GET /my_index/_search
{
  "query": {
    "query_string": {
      "default_field": "description",
      "query": "is"
    }
  },
  "collapse": {
    "field": "price"
  }
}

根据查询条件更新某个文档

## script是固定脚本写法,ctx._source 是获取的行
POST /my_index/_update_by_search
{
	"query":{
  	"term":{
    	"title":"hongmian"
		}
	},
	"script":{
  	"source":"ctx._source['description']='hongmian product from huizhou'"
	}
}

根据查询条件删除某个文档

POST /my_index/_delete_by_query
{
    "query":{
        "term":{
        	"title":"hongmian"
    		}
    }
}

posted @ 2022-10-16 11:34  Tenic  阅读(2205)  评论(0编辑  收藏  举报