1 - inline 查询
在Restful API中,连同查询参数一起,指定查询关键字:
curl -XGET 'Http://localhost:9200/ebook/book/_search?q=Elastic&prettry=true
2 - 查询 DSL
在使用Restful API检索文档的时候,使用复杂的查询条件,比如多条件查询(复合查询),更改积分权重等:
curl -XGET 'http://localhost:9200/ebook/book/_search?pretty=true' -d '{
"query": {
"query_string":{ "query":"title:crime"}
}
}
(以下查询都是基于Kibana提交,省却了curl的一系列参数设置)
2.1 - match_all:
不适用任何索引关键字,查询所有的文档,类似于sql的select *
GET 'ebooks/POST/_search'
{
"query":{
"match_all":{}
}
}
2.2 - term query :
GET ebooks/POST/_search
{
"query":{
"term":{
"content":"fire"
}
}
}
下面这个查询,要注意大小写。假如redemption首字大写,那么就会查询不到。而原文中,首字是大写的。初步判断是因为在查询的时候,没有指定一个合适的analyser.
GET ebooks/POST/_search
{
"query":{
"term":{
"content":"redemption"
}
}
}
以下查询,首字大写,并没有查获任何的匹配文档。
GET ebooks/POST/_search
{
"query":{
"term":{
"content":"Redemption"
}
}
}
尝试着给查询字段增加一个analyzer:
GET ebooks/POST/_search
{
"query":{
"term":{
"content":{
"value":"Redemption",
"analyzer":"simple"
}
}
}
}
报错:
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "[term] query does not support [analyzer]",
"line": 6,
"col": 20
}
],
"type": "parsing_exception",
"reason": "[term] query does not support [analyzer]",
"line": 6,
"col": 20
},
"status": 400
}
term是词干的意思,就是已经被提炼和分析过的原生字,可以想象成为原子级别的查询。在term query里面,不需要再做任何的分析,就只能是全字符串匹配。所以碰到这种情况,需要使用分析器的例子,就要使用match查询:
GET ebooks/POST/_search
{
"query":{
"match":{
"content":"Redemption"
}
}
}
}
2.3 boolean query:
boolean查询,条件组合查询。当有多个条件表达式组合时候,可以组合起来判别最终的条件:
,"should":[
{"match":{"content":"redemption"}}
,{"match":{"content":"there"}}
]
下面两段脚本,如果没有指定should的minimum_number_should_match,则结果是一样的:
must是在should应用之后,再执行:表示一定要符合的条件; 而should可以根据其中一或者多个匹配表达式,来查找文档的;
GET ebooks/POST/_search
{
"query":{
"bool":{
"must":[{"match":{"content":"there"}}
,{"match":{"content":"redemption"}}]
,"should":[
{"match":{"content":"redemption"}}
,{"match":{"content":"there"}}
]
}
}
}
GET ebooks/POST/_search
{
"query":{
"bool":{
"must":[{"match":{"content":"there"}}
,{"match":{"content":"redemption"}}]
}
}
}
除了使用must,还可以使用boolean的minimum_number_should_match
GET ebooks/POST/_search
{
"query":{
"bool":{
"must":[{"match":{"content":"there"}}
,{"match":{"content":"redemption"}}]
,"should":[
{"match":{"content":"redemption"}}
,{"match":{"content":"there"}}
]
}
}
}
2.4 wildcard query
, ? 是两个在查询条件中使用的通配符。 代表了多个字符通配,?仅仅代表一个通配字符
GET ebooks/POST/_search
{
"query":{
"wildcard":{
"content":{"value":"langol*"}
}
}
}
没有想到的是,bool和wildcard能够联合成符合查询。
GET ebooks/POST/_search
{
"query":{
"bool":{
"must_not":{"wildcard":{
"content":"langol*"
}}
}
}
}
2.5 range query
GET ebook/book/_search
{
"query":{
"range":{
"charpter":{
"gte":0
,"lte":5
}
}
}
}
欢迎关注个人微信公众号【有关SQL】