DSL:
query_string
match
match_phrase
match_phrase_prefix
multi_match
simple_query_string
term
terms
bool(must,should,must_not)
match
filter
多索引查询
主要参数说明
聚合aggs
query:
query主要是将要查询的内容进行内部分词后匹配然后获取 or的结果。比如:query “中国人民” 则会查询 内容中有“中”“国”“人”“民”“中国”“人民”“国人”“中国人”“中国人民”
在查询中可使用 OR 或关联 AND 并关联 ***进行模糊匹配
"query": "kill OR a" "query": "kill AND a" "query": ""bird""
注意查询的单词一律用小写,不能用大写,大写内容有特殊含义
query_string 最基本的查询:
全文检索:
POST http://192.168.201.105:9200/_search {"query": {"query_string": { "query": "kill" }} }
fields指定属性内检索
POST http://192.168.201.105:9200/_search {"query": {"query_string": { "fields": [ "title" ], "query": "kill" }} }
match 匹配查询:
"match" : {[filed] :[text]}
POST http://192.168.201.105:9200/_search {"query": {"match": { "title": "kill" }} }
match 的text中还可以这样写:
POST http://192.168.201.105:9200/_search {"query": {"match": { "title": { "query": "bird" } }} }
match_phrase 短句查询:
POST http://192.168.201.105:9200/_search {"query": {"match_phrase": { "title": "to kill a" }} }
match_phrase_prefix 短句最后不完全字符查询,也就是我们有时知道前缀的模糊查询:
POST http://192.168.201.105:9200/_search {"query": {"match_phrase_prefix": { "title": "to ki" }} }
multi_match 多复合查询:
多个可能的值以空格隔开以下就为 title中为 kill 或者 bill的内容。
POST http://192.168.201.105:9200/_search {"query": {"multi_match": { "query": "kill Bill", "fields": ["title"] }} }
如果我们想要对检索的内容进行分级显示:
就要添加type: "best_fields" 以完全匹配最高,
type: "most_fields" 以最多匹配最高
type: "cross_fields" 分词在不同的属性里
simple_query_string 简单query查询:
POST http://192.168.201.105:9200/_search {"query": {"simple_query_string": { "query": "kill Bill", "fields": ["title"] }} }
term 不分词查询:
即如果现在有中国人则 term会完全在分词上去查找“中国人”这个分词,而不会去理会中国,或国人。
post _search { "query": { "term": { "content": { "value": "中国人" } } } }
terms 也是不分词查询,但是支持多个结果以或的形式存在。
post _search { "query": { "terms": { "content": [ "中国人", "中国" ] } } }
bool查询(must,should,must_not,filter)
bool可实现联合查询,must必须存在,should可能存在,must_not 一定不存在
must:
post /myindex/_search { "query": { "bool": { "must": [ {"term": {"title1": "世界"}}, {"term": {"content": "中国"}} ] } } }
should:
post /myindex/_search { "query": { "bool": { "should": [ {"term": {"title1": "world"}}, {"term": {"content": "中国"}} ] } } }
must_not:
post /myindex/_search { "query": { "bool": { "must_not": [ {"term": {"title1": "world"}} ] } } }
match:
post /myindex/_search { "query": { "bool": { "must": [ {"match": {"title1": "world"}} ] } } }
filter:
post /myindex/_search { "query": { "bool": { "must": [ {"match": {"title1": "world"}} ], "filter":[ {"match": {"content": "china"}},
{"range":{"title":{"gte":"2018-7-1"}}} ] } } }
多索引查询:
当我们想对多个索引下的内容进行查询时
>post http://ip:port/index1,index2,index3,...../_search // 检索index1,index2,index3...索引下的数据
>post http://ip:port/_search //检索所有
>post http://ip:port/_all/_search //检索所有
还可以使用通配符的形式 (*)模糊匹配,(+)另外包括, (-)排除掉
>post http://ip:port/*index,-myindex,+mytest/_search //匹配所有以index结尾,排除掉myindex,包括mytest
日期索引格式的数字支持,先不记录,后面用到再写。
在匹配中有可能所要查找的索引不存在而引发查询报错。为此,需要加参数忽略索引不存在的情况:
主要参数说明:
?ignore_unavailable=true //运行索引不存在
allow_no_indices=true //允许带通配符索引不存在
expand_wildcards=true //允许通配符索引在关闭的情况下访问不报错
human = true // 将输出的结果适合于人阅读,数字会进行人为化,比如2000,变为2k
pretty = true //结果美化,便于查看
version = [versionNo] //版本控制
op_type = [create] //限制操作类型,即此处只允许新建,如果已经存在则不插入也不更新,防止对数据冲击
parent = [id] // 定义父文档的id,约束查询范围
timeout = [3m] // 默认是1m(分钟),可修改时间
fields = [fieldName],[att1] // 指定要输出的json对象的属性
q = [fieldName]:[value] // 快速属性值查找
sort=fieldName:asc // 排序
size=15 //默认是10
aggs 聚合
如同sql一样,可以对查询的结果进行聚合
有平均avg,最大max,最小min,但这些聚合用于数字类型
聚合东西比较多,用到的时候再说吧。
post /myindex/_search { "aggs" : { "avg_grade" : { "avg" : { "field" : "age" } } } }