URI Search
在URL中直接使用查询参数
Request Body Search
使用ElasticSearch 提供的,给予JSON格式的更加完备的 Query Domain Specific Language
指定 查询的索引
语法 | 范围 |
---|---|
/_search | 集群上的所有索引 |
/index1/_search | index1 |
/index1,index2 /_search | index1 和 index2 |
/index*/_search | 搜索以 index 开头的索引 |
使用URI 查询实例
使用 ‘q’ 指定查询字符串,
“query string syntax ” , KV 键值对
curl -XGET "http://elasticsearch:9200/kibana_sample_data_ecommerce/_search?q=customer——firest_name:Eddie "
——> 搜索一个叫做 Eddie
的客户
使用RequestBody
同时支持 POST 和 GET、
curl -XGET
"http://elasticsearch:9200/kibana_sample_data_ecommerce/_search" -H // kibana_sample_data_ecommerce 索引名
'Content-Type:application/json' -d '
{
"query" : { // 查询
"match_all" : {} // match_all 返回所有的文档
}
}’
URI Search 通过 URI query 实现搜索
对指定字段进行查询
GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
{
"profile":true
}
GET /movies/_search?q=title:2012
{
"profile":true
}
- q 指定查询语句,使用query String Syntax
- df 默认字段,不指定时会对所有字段进行查询
- Sort 排序 /from 和 Size 用于分页
- Profile 可以查看查询是如何被执行的
指定字段查询过程
泛查询
// 泛查询
GET /movies/_search?q=2012
{
"profile":true
}
Term v.s Phrase
Beafutiful Mind 等效于 Beautiful OR Mind
“Beautiful Mind” 等效于 Beafutiful ADN MIND Phrase 查询, 还要求前后顺序保持一致
分组和引号
title:(Beautifule Mind)
title: “Beautifule Mind”
布尔操作
使用AND
使用 NOT
使用OR
分组:
+
表示must
-
表示must_not
范围查询
区间表示 : []
闭区间, {}
开区间
- year : {2019 TO 2018}
- year : {* TO 2018}
算数符号
- year : > 2010
- year : (> 2010 && <= 2018)
- year : (+> 2010 +<=2018)
通配符查询(通配符查询效率低,占用内存大,不建议使用,特别是放在最前面。)
- ? 代表一个字符, * 代表 0 个或者 多个字符
- title: mi?d
- title: be*
正则表达式
- title :[bt] oy
模糊匹配 与 近似查询
- title :befutifl~1
- title: “lord rings” ~2
Request Body Search
-
将查询语句通过 HTTP Request Body 发送给ElasticSearch
-
Query DSL
分页
POST /kibana_sample_data_ecommerce/_search
{
"from" : 10,
"size" : 20,
"query" : {
"match_all" : {}
}
}
from 从 0 开始, 默认返回 10 个结果
获取靠后的翻页成本比较高
排序
GET /kibana_sample_data_ecommerce/_search
{
"sort": [
{
"order_date": {
"order": "desc"
}
}
],
"from": 0,
"size": 20,
"query": {
"match_all": {}
}
}
sort 也可以这么来写
GET /kibana_sample_data_ecommerce/_search
{
"sort":[{"order_date":"desc"}],
"query": {
"match_all": {}
}
}
最好在数字型
和 日期型
字段上面排序
因为对于多值类型或分析过的字段排序,系统会选一个值,无法得知该值
_source filter
如果并不是需要查询所有的信息,那么你可以过滤掉那些你不需要的信息
比如:
GET /kibana_sample_data_ecommerce/_search
{
"_source": ["order_date","order_date","category.keyword"]
}
返回结果
{
"_index" : "kibana_sample_data_ecommerce",
"_type" : "_doc",
"_id" : "ywUi6WsBipwyH8yLpMZE",
"_score" : 1.0,
"_source" : {
"order_date" : "2019-07-22T09:28:48+00:00"
}
}
如果_source 没有存储,那么久只返回匹配文档的元数据
** _source 支持使用通配符,_srouce[“name*”,“desc*”]**
GET /kibana_sample_data_ecommerce/_search
{
"_source": ["order_*"]
}
返回
{
"_index" : "kibana_sample_data_ecommerce",
"_type" : "_doc",
"_id" : "ywUi6WsBipwyH8yLpMZE",
"_score" : 1.0,
"_source" : {
"order_date" : "2019-07-22T09:28:48+00:00",
"order_id" : 584677
}
},
脚本字段
GET /kibana_sample_data_ecommerce/_search
{
"script_fields": {
"new_field": {
"script": {
"lang":"painless",
"source": "doc['order_date'].value + 'hello'"
}
}
},
"from": 0,
"size": 20,
"query": {
"match_all": {}
}
}
** 意思就是给文档中的 order_date 的值添加hello但是不修改文档。 **
查询结果
{
"_index" : "kibana_sample_data_ecommerce",
"_type" : "_doc",
"_id" : "ywUi6WsBipwyH8yLpMZE",
"_score" : 1.0,
"fields" : {
"new_field" : [
"2019-07-22T09:28:48.000Zhello"
]
}
},
使用查询表达式 Match
// 使用查询表达式
GET movies/_search
{
"query": {
"match": {
"title": "Last Christms"
}
},
"profile": "true"
}
只需要满足 标题中存在 Last 和 Christms 就可以了。
GET movies/_search
{
"query": {
"match": {
"title": {
"query": "Last Christms",
"operator": "and"
}
}
},
"profile": "true"
}
查询过程
"query" : [
{
"type" : "BooleanQuery",
"description" : "+title:last +title:christms",
},
描述中表示,必须要标题中存在 last 和 christms 才能被查询到
match_phrase查询
使用phrase 查询会考虑 词汇位置,
match查询增加, 不会考虑词汇位置
// match_phrase 查询
GET movies/_search
{
"query": {
"match_phrase": {
"title": {
"query": "one love"
}
}
},
"profile": "true"
}
搜索结果为空
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
使用slop
GET movies/_search
{
"query": {
"match_phrase": {
"title": {
"query": "one love",
"slop": 1
}
}
}
}
查询结果
"hits" : [
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "113829",
"_score" : 5.323583,
"_source" : {
"title" : "One I Love, The",
"genre" : [
"Comedy",
"Drama",
"Romance"
],
"@version" : "1",
"year" : 2014,
"id" : "113829"
}
}
]
Query String Query
类似 URI Query
QueryString
- 类似QueryString,但是会忽略错误的语法,同时只支持部分的查询语法
- 不支持 AND OR NOT, 会当做字符串处理
- Term 之间的默认关系是 OR, 可以指定 Operator
- 支持 部分逻辑
1. + 替代 AND
2. | 替代 OR
3. - 替代 NOT
PUT /users/_doc/1
{
"name" : "Gao Songsong",
"about" : "Java"
}
PUT /users/_doc/2
{
"name" : "Liu Songsong",
"about" : "PHP"
}
POST users/_search
{
"query": {
"query_string": {
"default_field": "name",
"query": "Gao AND Songsong"
}
}
}
POST users/_search
{
"query": {
"query_string": {
"fields": ["namme","about"],
"query": "(Gao AND Songsong) OR (JaVA)"
}
}
}
// Simple Query 默认的是 operator 是 OR
POST users/_search
{
"query": {
"simple_query_string": {
"query": "Gao songsong",
"fields": ["name"],
"default_operator": "AND"
}
}
}