博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
elasticSearch-DSL
阅读量:4356 次
发布时间:2019-06-07

本文共 4439 字,大约阅读时间需要 14 分钟。

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" } }    }}

 

转载于:https://www.cnblogs.com/DennyZhao/p/9444522.html

你可能感兴趣的文章
mysql的数据存储
查看>>
[转载] Activiti Tenant Id 字段释疑
查看>>
[Java 8] (8) Lambda表达式对递归的优化(上) - 使用尾递归 .
查看>>
SQL Server-聚焦移除Bookmark Lookup、RID Lookup、Key Lookup提高SQL查询性能
查看>>
最小权限的挑战
查看>>
jquery 视觉特效(水平滚动图片)
查看>>
SVG笔记
查看>>
linux下使用dd命令写入镜像文件到u盘
查看>>
001---进程
查看>>
视频人脸检测——OpenCV版(三)
查看>>
php获取来访者在搜索引擎搜索某个关键词,进入网站
查看>>
物联网架构成长之路(8)-EMQ-Hook了解、连接Kafka发送消息
查看>>
2018-2019-1 20165234 20165236 实验二 固件程序设计
查看>>
IDEA的GUI连接数据库写入SQL语句的问题总结
查看>>
Xpath在选择器中正确,在代码中返回的是空列表问题
查看>>
leecode第一百九十八题(打家劫舍)
查看>>
【BZOJ 1233】 [Usaco2009Open]干草堆tower (单调队列优化DP)
查看>>
07-3. 数素数 (20)
查看>>
写一个欢迎页node统计接口Py脚本(邮件,附件)-py
查看>>
计算两个日期之间的天数
查看>>