Elasticsearch系列-搜索

  • 指定查询的索引
  • URl查询
    • Query String Syntax
  • Request Body查询
    • sort排序
    • from,size分页
    • _source 过滤字段
    • match表达式
    • match_phrase 分词表达式
    • query string表达式
    • simple query string

指定查询的索引

语法 范围
/_search 集群上所有索引
/index1/_search index1
/index1,index2/_search index1和index2
/index*/_search 以index开头的索引

URl查询

查询参数:

  • q: 指定查询语句,使用 Query String Syntax
  • df: 默认字段,不指定时,会对所有字段进行查询
  • sort:排序
  • from 和size: 用于分页
  • profile: 可以查看查询是如何执行的

Query String Syntax

  • 指定字段:q=title:2012
  • 泛查询:q=2012
  • Term&Phrase:
    • Beautiful Mind 等效于Beautiful OR Mind
    • “Beautiful Mind” 等效于Beautiful AND Mind。Phrase查询,要求前后顺序保持一致
  • 分组与引号:
    • title:(Beautiful AND Mind)
    • title=“Beautiful Mind”
  • 布尔操作:
    • AND OR NOT 或者 && || !

      • 必须大写
      • title:(matrix NOT reloaded)
  • 分组
    • +表示must
    • -表示must_not
    • title:(+matrix -reloaded)
  • 范围查询:区间表示:[]闭区间,{}开区间
    • year:{2019 TO 2018]
    • year:[* TO 2018]
  • 算数符号:> >= < <= !
  • 通配符查询:?代表一个字符,*代表0或多个字符
    • title:mi?d
    • title:be*
  • 正则表达式:title:[bt]oy
  • 模糊匹配与近似查询:
    • title:befutifl~1
    • title:“lord rings”~2

相关示例(通过kibana开发工具):

//全文搜索,只返回前10条数据
GET /movies/_search//全文搜索,会匹配所有字段
GET /movies/_search?q=2012
{"profile": "true"
}//全文搜索,会匹配所有字段
GET /movies/_search?q="2012"
{"profile": "true"
}//匹配title字段
GET /movies/_search?q=title:2012
{"profile": "true"
}
//匹配title字段
GET /movies/_search?q=2012&df=title
{"profile": "true"
}//匹配title字段是 "2012 2011" 不会做分词匹配
GET /movies/_search?q=title:"2012 2011"
{"profile": "true"
}//匹配title字段是2012或者2011的数据  OR必须大写
GET /movies/_search?q=title:(2012 OR 2011)
{"profile": "true"
}//匹配title字段是 2012并且包含2011 AND必须大写
GET /movies/_search?q=title:(2012 AND 2011)
{"profile": "true"
}//匹配title字段是2012 和 所有字段匹配2011
GET /movies/_search?q=title:2012 2011
{"profile": "true"
}//通配符匹配ti开头的字段是2012
GET /movies/_search?q=2012&df=ti*
{"profile": "true"
}//通配符匹配title字段是201开头
GET /movies/_search?q=title:201*
{"profile": "true"
}//通配符匹配ti开头的字段是2012 通配符前面添加 "\"做转义
GET /movies/_search?q=ti\*:2012
{"profile": "true"
}//匹配title字段不为null的数据
GET /movies/_search?q=_exists_:title
{"profile": "true"
}//匹配title字段为kony的数据
GET /movies/_search?q=title:kony
{"profile": "true"
}//匹配title字段为kony的数据 并允许有两位字符误差 "~" 默认运行两位误差
GET /movies/_search?q=title:kony~
{"profile": "true"
}//匹配title字段为koey的数据 并允许有一位字符误差 "~" 默认运行两位误差
GET /movies/_search?q=title:koey~1
{"profile": "true"
}//匹配year >= 2011 并且 <=2013的数据
GET /movies/_search?q=year:(>=2011 AND <=2013)
{"profile": "true"
}//匹配year 2012以上的信息
GET /movies/_search?q=year:[2012 TO *]
{"profile": "true"
}//匹配year >= 2011 并且 <=2013的数据
GET /movies/_search?q=year:[2012 TO 2014]
{"profile": "true"
}//title字段必须含有matrix 并且必须不包含reloaded
GET /movies/_search?q=title:(+matrix AND -reloaded)
{"profile": "true"
}//多字段查询
GET /movies/_search?q=(title:bingo OR id:150092)
{"profile": "true"
}//多字段查询
GET /movies/_search?q=(title:bingo AND id:150092)
{"profile": "true"
}

Request Body查询

sort排序

GET /movies/_search
{"sort": [{"year": "desc"},{"title.keyword": "asc"}]
}

from,size分页

from从0开始,size默认10条

GET /movies/_search
{"from": 0,"size": 20
}

_source 过滤字段

GET /movies/_search
{"_source": "title"
}
//source只返回title,year字段
GET /movies/_search
{"_source": ["title","year"]
}

match表达式

//查询title字段同时含有charley moon值
GET /movies/_search
{"profile": "true", "query": {"match": {"title": {"query": "charley moon","operator": "and"}}}
}

match_phrase 分词表达式

GET /movies/_search
{"from": 0, "size": 20, "profile": "true","query": {"match_phrase": {"title": {"query": "mind murder","slop": 1}}}
}

query string表达式

query 字段赋值参照Query String Syntax

GET /movies/_search
{"profile": "true","query": {"query_string": {"default_field": "title","query": "charley moon","default_operator": "AND"}}
}GET /movies/_search
{"profile": "true","query": {"query_string": {"default_field": "title","query": "charley AND moon"}}
}

simple query string

query 字段赋值参照Query String Syntax

GET /movies/_search
{"profile": "true","query": {"simple_query_string": {"query": "charley moon","fields": ["title"],"default_operator": "AND"}}
}GET /movies/_search
{"profile": "true","query": {"simple_query_string": {"query": "+charley -moon","fields": ["title"],"default_operator": "AND"}}
}

Elasticsearch系列-搜索操作相关推荐

  1. elasticsearch实战三部曲之三:搜索操作

    elasticsearch实战三部曲之三:搜索操作 2019年01月13日 21:35:18 博陵精骑 阅读数:1367 标签: elasticsearch 更多 个人分类: elasticsearc ...

  2. 无法从elasticsearch节点检索版本信息_【Elasticsearch 7 搜索之路】(一)什么是 Elasticsearch?...

    本篇文章对 Elasticsearch 做了基本介绍,在后续将通过专栏的方式持续更新,本系列以 Elasticsearch7 作为主要的讲解版本,欢迎各位大佬指正,共同学习进步涨工资! 一般涉及大型数 ...

  3. ElasticSearch系列——Kibana,核心概念

    ElasticSearch系列--Kibana,核心概念 Kibana 下载地址 Windows安装 修改配置文件 启动Kibana 验证 ES核心概念 Index索引 Mapping映射 Docum ...

  4. ElasticSearch系列18:Mapping 设计指南

     点击上方"方才编程",即可关注我! 本文导读 ElasticSearch 的 mapping 该如何设计,才能保证检索的高效?想要回答这个问题,就需要全面系统地掌握 mappin ...

  5. 【ElasticSearch系列连载】3. 如何安装符合生产环境要求的ES集群

    [ElasticSearch系列连载]3. 如何安装符合生产环境要求的ES集群 通过本文,将会循序渐进地了解到ES的若干部署方案,以及相关的基础操作与配置. 上一节介绍的一键安装方式,可以快速启动一个 ...

  6. SpringBoot ElasticSearch 全文搜索

    2019独角兽企业重金招聘Python工程师标准>>> 一.pom.xml配置 SpringBoot版本1.5.6https://blog.csdn.net/kingice1014/ ...

  7. 基于Java、Kafka、ElasticSearch的搜索框架的设计与实现

    Jkes是一个基于Java.Kafka.ElasticSearch的搜索框架.Jkes提供了注解驱动的JPA风格的对象/文档映射,使用rest api用于文档搜索. 项目主页:https://gith ...

  8. 【elasticsearch系列】windows安装kibana

    目录 环境 安装 配置文件 启动 环境 下载kibana版本,要与elasticsearch版本保持一致: 中文社区下载:https://elasticsearch.cn/download/ 安装 下 ...

  9. 【Elasticsearch】十九种Elasticsearch字符串搜索方式

    1.概述 十九种Elasticsearch字符串搜索方式 刚开始接触Elasticsearch的时候被Elasticsearch的搜索功能搞得晕头转向,每次想在Kibana里面查询某个字段的时候,查出 ...

最新文章

  1. 一个用Spring Boot做的垃圾分类小程序,你不拿来学习一下?
  2. 在unity2d同屏显示9千人
  3. 如何使用用window.open()
  4. python垃圾分类准确率计算公式_准确率(Accuracy), 精确率(Precision), 召回率(Recall)和F1-Measure...
  5. 浅析Secondary NameNode与namenode
  6. 大数据知识点汇总---Redis,Spark,Kafka,Hive,Mysql,Hbase,Hadoop...
  7. 数学建模学习笔记之相关系数
  8. 用opencv实现连连看外挂
  9. 纪念日或悼念人网页变灰仅需一行代码
  10. 纯CSS中的可视数据库库
  11. [zt]软件研发的6sigma案例解析
  12. zzulioj:1153: 简易版最长序列
  13. html中如何修改表格标题栏,如何设置css中表格标题caption标签的位置
  14. 一个女人努力工作的意义
  15. Unity 内置着色器下载方法
  16. 利用setTimeout实现setInterval
  17. 韦神!北大数学系韦东奕爆红!拒绝哈佛offer,留任北大!
  18. 四、node系列之购物车的业务逻辑
  19. 务实java基础之集合总结
  20. 使用Ventoy安装Deepin系统出现Error verification failed 0x1A Security violation错误

热门文章

  1. Linux安装CentOS系统
  2. Hive-编写UDF函数(详细教程~~~)
  3. 52、疏散楼梯的设计要求
  4. 前端自动化打包部署服务器
  5. BUUCTF_Crypto题目题解记录1
  6. mysql qps tps 监控_Mysql库TPS,QPS实时监控脚本
  7. 农产品电商app开发的主要价值与功能分析
  8. 扒开ARM中断控制器的底裤来看看!
  9. shell md5sum命令
  10. JavaScript 中的设计模式