Elasticsearch的Search API

Elasticsearch的Search API
1. API类型
- URI Search:在URL中使用查询参数
- Request Body Search:使用Elasticsearch提供的,基于JSON格式的更加完备的Query Domain Specific Language(DSL)
2. API查询范围
语法范围
/_search集群上所有的索引
/index1/_searchindex1
/index1,index2/_searchindex1和index2
/index*/_search以index开头的索引
3. URI查询
- 使用“q”,指定查询字符串

curl-XGET
"http://elasticsearch:9200/kibana_sample_data_ecommerce/_search?q=customer_first_name:Eddie"
4. Request Body
curl -XGET
"http://elasticsearch:9200/kibana_sample_data_ecommerce/_search" -H
'Content-Type:application/json' -d '
{
	"query": {
		"match_all": {}
	}
}'
5. URL Search详解
GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
{
  "profile": "true"
}

# q指定查询语句,使用Query String Syntax
# df默认字段,不指定时,会对所有字段进行查询
# sort排序/from和size用于分页
# profile可以查看查询是如何被执行的
1. 指定字段和泛查询
# 指定字段查询
GET /movies/_search?q=title:2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
{
  "profile": "true"
}

# 泛查询
GET /movies/_search?q=2012
{
  "profile": "true"
}
2. Term和Phrase
# Beautiful Mind等效于Beautiful OR Mind
# “Beautiful Mind”,等效于Beautiful AND Mind。Phrase查询,还要求前后顺序保持一致

# 使用引号,Phrase查询,必须同时存在Beautiful Mind,且顺序保持一致
GET /movies/_search?q=title:"Beautiful Mind"
{
  "profile": "true"
}

# 不使用引号,Mind为泛查询,查询title中包含Beautiful,其余字段包含Mind
GET /movies/_search?q=title:Beautiful Mind
{
  "profile": "true"
}
3. 分组与引号
# title:(Beautiful AND Mind)
# title="Beautiful Mind"

# 分组,bool查询,查询title中包含Beautiful或者Mind的,默认是OR
GET /movies/_search?q=title:(Beautiful Mind)
{
  "profile": "true"
}
4. 布尔操作
# AND / OR / NOT 或者 && / || / !
# 必须大写(title:(A NOT B))
# + 表示must
# - 表示must_not
# title:(+A -B)

# 分组使用AND
GET /movies/_search?q=title:(Beautiful AND Mind)
{
  "profile": "true"
}

# 分组使用NOT
GET /movies/_search?q=title:(Beautiful NOT Mind)
{
  "profile": "true"
}

# 分组查询
GET /movies/_search?q=title:(+Beautiful -Mind)
{
  "profile": "true"
}

# 分组查询,%2B表示+
GET /movies/_search?q=title:(Beautiful %2BMind)
{
  "profile": "true"
}
5. 范围查询
# 区间表示:[]闭区间,{}开区间
# year:{2019 TO 2018}	year:[* TO 2018]
# 算数符号:year:>2010

# 范围查询
GET /movies/_search?q=year:>=1980
{
  "profile": "true"
}

# 区间
GET /movies/_search?q=year:[* TO 2018]
{
  "profile": "true"
}
6. 通配符与正则查询
# 通配符查询(通配符查询效率低,占用内存大,不建议使用。特别是放在最前面)
	# ?代表1个字符,*代表0到多个字符(title:mi?d title:bea*)
# 模糊匹配与近似查询
	# title:befutifl~1	title:"lord rings"~2
	
# 通配符查询
GET /movies/_search?q=title:b*
{
  "profile": "true"
}

# 模糊匹配与近似度匹配,查询与beautifl差1个字符
GET /movies/_search?q=title:beautifl~1
{
  "profile": "true"
}

# 查询包含Lord Rings,并且之间相隔2个词
GET /movies/_search?q=title:"Lord Rings"~2
{
  "profile": "true"
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yanko24

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值