elasticSearch-DSL
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" } }
}
}
elasticSearch-DSL的更多相关文章
- Elasticsearch DSL中Query与Filter的不同
Elasticsearch支持很多查询方式,其中一种就是DSL,它是把请求写在JSON里面,然后进行相关的查询. 举个DSL例子 GET _search { "query": { ...
- [elk]elasticsearch dsl语句
例子1 统计1,有唱歌兴趣的 2,按年龄分组 3,求每组平均年龄 4,按平均年龄降序排序 sql转为dsl例子 # 每种型号车的颜色数 > 1的 SELECT model,COUNT(DISTI ...
- elasticsearch DSL查询
总结一个DSL的用法吧,语法网上查去,只记录一点心得,也是研究了半天,太麻烦了 先附上python代码 #!/usr/bin/env python # _*_ coding:utf-8 _*_ fro ...
- Elasticsearch DSL 常用语法介绍
课程环境 CentOS 7.3 x64 JDK 版本:1.8(最低要求),主推:JDK 1.8.0_121 Elasticsearch 版本:5.2.0 相关软件包百度云下载地址(密码:0yzd):h ...
- ElasticSearch DSL 查询
公号:码农充电站pro 主页:https://codeshellme.github.io DSL(Domain Specific Language)查询也叫做 Request Body 查询,它比 U ...
- Elasticsearch DSL语句之连接查询
传统数据库支持的full join(全连接)查询方式. 这种方式在Elasticsearch中使用时非常昂贵的.因此,Elasticsearch提供两种操作可以支持水平扩展 更多内容请参考Elasti ...
- ES 20 - 查询Elasticsearch中的数据 (基于DSL查询, 包括查询校验match + bool + term)
目录 1 什么是DSL 2 DSL校验 - 定位不合法的查询语句 3 match query的使用 3.1 简单功能示例 3.1.1 查询所有文档 3.1.2 查询满足一定条件的文档 3.1.3 分页 ...
- ElasticSearch实战系列二: ElasticSearch的DSL语句使用教程---图文详解
前言 在上一篇中介绍了ElasticSearch集群和kinaba的安装教程,本篇文章就来讲解下 ElasticSearch的DSL语句使用. ElasticSearch DSL 介绍 Elastic ...
- Elasticsearch+Logstash+Kibana教程
参考资料 累了就听会歌吧! Elasticsearch中文参考文档 Elasticsearch官方文档 Elasticsearch 其他——那些年遇到的坑 Elasticsearch 管理文档 Ela ...
- ElasticSearch大数据分布式弹性搜索引擎使用
阅读目录: 背景 安装 查找.下载rpm包 .执行rpm包安装 配置elasticsearch专属账户和组 设置elasticsearch文件所有者 切换到elasticsearch专属账户测试能否成 ...
随机推荐
- vm 虚拟机选择启动项
1. 每次狂按鼠标和ESC而且要试验N次,找了一下解决办法 在你的虚拟机里面找到一个 .vmx文件(虚拟机初始化文件) 加入 bios.bootDelay = "5000"(延迟 ...
- js读取iframe里的元素
父层 <div id="SubStepNav" class="SubStepNav"> <iframe src="aaa.html& ...
- golang 常量的用法
1.Golang常量的用法 //常量的用法 var num int num =9 //1.常量声明的时候必须赋值 const tax int = 0 //2.常量值是无法修改的 //tax = 10 ...
- T-SQL 局部变量和全局变量
局部变量 use StudentManageDB go --声明学号变量 ) --查询李铭的信息 set @stuname='李铭' select StudentId,StudentName,Gend ...
- Map相关问题
<!--加载地图开始--> <!DOCTYPE html><html><head> <meta charset="UTF-8" ...
- 在线学习和在线凸优化(online learning and online convex optimization)—凸化方法4
一些在线预测问题可以转化到在线凸优化框架中.下面介绍两种凸化技术: 一些在线预测问题似乎不适合在线凸优化框架.例如,在线分类问题中,预测域(predictions domain)或损失函数不是凸的.我 ...
- Linux安装jsvc,及Linux服务开发
在linux上以服务的方式启动java程序,需要提前安装jsvc.linux是利用daemon(jsvc)构建java守护进程. 编译 daemon 安装JSVC 1 下载文件,http://comm ...
- 第5章 IP地址和子网划分(1)_IP格式和子网掩码
1. 二进制和十进制 (1)二进制与十进制的对应关系 ①128为数轴的中点,最高位为1.其后的数,二进制最高位均为1.其前面的数二进制最高位均为0. ②192为128-255中间的数,最高两位为1.2 ...
- (转)C#.NET WINFORM应用程序中控制应用程序只启动一次
原文地址 :http://www.cnblogs.com/emanlee/archive/2009/08/31/1557379.html using System; using System.Thre ...
- 通过mapreduce把mysql的一张表的数据导到另外一张表中
怎么安装hadoop集群我在这里就不多说了,我这里安装的是三节点的集群 先在主节点安装mysql 启动mysql 登录mysql 创建数据库,创建表格,先把数据加载到表格 t ,表格t2是空的 mys ...