在Elasticsearch使用URI花式搜索
文章目录
- 通过URI实现搜索
- 指定字段和泛查询
- Term与语句(phrase)查询
- 范围查询和算数符号
- 通配符查询
- 正则表达式
- 模糊匹配与近似查询
- Request Body Search查询
- 使用SearchTemplate查询
通过URI实现搜索
URI Search 是通过在URI上传入参数
定义你的查询条件
,不支持所有的DSL
但是更加便利,常用传入参数如下:
curl -XGET -H "Content-Type:application/json" "http://localhost:9200/<index>/_search?q=<parameter>"
default_operator
设置字符串查询时的操作符,共有三类OR
、AND
、NOT
,在查询时术语term
将可能进行分组
curl -XGET -H "Content-Type:application/json" "http://localhost:9200/<index>/_search?/_search?q=customer_full_name:(Eddie Underwood)&default_operator=AND" -d '{"profile":"true"}'
q
这个参数需要填写查询语句,Lucene
中的字符串查询语法Query string syntax
,在使用时会有指定段查询、泛查询、term分组、语句phrase
查询、范围查询、正则表达式、通配符查询(效率低占内存大,通配符在查询语句前方则存在更大的问题)、模糊匹配与近似查询这类概念。
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley"
df
在q
参数的查询语句中没有指定field则使用df
参数的值指定q
中值所属的参数。
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=Riley&df=customer_last_name"
# 查询customer_last_name为Riley的文档数据
explain
true
或false
是否开启分数计算解释,为查询出来的每一个文档,解释分数计算的过程(IDF计算跟词频相关),默认为false
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley&explain=true"
from
文档开始的偏移量,与size
合用进行分页,但是也存在深度分页的性能问题
,默认为0
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley&from=10"
size
设置命中(his
)的文档返回多少条,默认为10
条
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley&from=10&size=1"
_source
可以设置true
orfalse
,则表示是否返回文档中_source
这个元数据,也可以是字段集合,则表示需要返回原文哪些字段(field
)
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley&_source=false"
# 不返回_source字段即原文
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley&_source=currency,customer_full_name"
# 只返回currency和customer_full_name两个字段
_source_excludes
传入字段名集合,返回文档中需要排除的字段
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley&_source_excludes=manufacturer,products"
# 将返回文档中的manufacturer和products两个字段排除掉
_source_includes
传入字段名集合,返回文档中只需要的字段
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley&_source_includes=customer_full_name"
# 只返回文档中的customer_full_name字段
analyzer
设置q
参数中的查询语句使用的分词器
(中文分词器需要提前安装并重启集群),集群默认使用standard
分词器
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=Mary Bailey&df=customer_last_name&analyzer=simple"
# 此处使用的simple分词器,但是应为Token Filters会将单词转成小写,故有可能查不到
analyze_wildcard
默认情况下,不分析查询字符串中的通配符术语。 将此值设置为true,将尽力分析这些值,默认为false
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=725?4*&df=order_id&analyze_wildcard=false"
sort
stored_fields
返回文档的选择性字段,该字段值存储于原文_source
之外,在指定mappings
是设置在每个字段
# 设置字段是否在store存储
PUT test_demo
{"mappings": {"properties": {"full_name":{"type": "keyword","store": true}}}
}
# 调用API查询full_name字段,命中则只返回full_name字段
curl -XGET 'http://localhost:9200/test_demo/_search?q=customer_last_name:"Mary Bailey"&stored_fields=full_name'
timeout
设置超时时间search_type
搜索类型,目前只有query_then_fetch
和dfs_query_then_fetch
两个可用选项,默认是query_then_fetch
terminate_after
为每个分片最大的检索文档的值,到达后将提前终止
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=elys*&df=user&_source=user&terminate_after=1"
# 最终只命中一条文档
指定字段和泛查询
- 指定字段
可以在q
参数中指定,或者使用df
参数指定查询字段
GET kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley
{"profile": "true" //这个选项打开主要是为能够查看到查询的过程,下列同样如此
}//响应如下,只贴出查询过程部分
{"type" : "TermQuery", //@1"description" : "customer_last_name:riley", //@2"time_in_nanos" : 299386,"breakdown" : { ... }
}
@1 精确查询term
精确查询
@2 精确查询customer_last_name值为riley的文档
2. 泛查询
GET kibana_sample_data_ecommerce/_search?q=Riley
{"profile": "true" //这个选项打开主要是为能够查看到查询的过程,下列同样如此
}//响应如下,只贴出查询过程部分
{"type" : "DisjunctionMaxQuery", //@1"description" : "(products.manufacturer:riley | MatchNoDocsQuery("failed [products.base_unit_price] query, caused by number_format_exception:[For input string: "Riley"]") ", //@2"time_in_nanos" : 299386,"breakdown" : { ... }
}
@1 使用DisjunctionMaxQuery
生成多个子查询的合集查询,是最高的评分作为最终得分
@2 精确查询customer_last_name值为riley的文档
Term与语句(phrase)查询
- Term术语精准查询
- 语句
phrase
查询
GET kibana_sample_data_ecommerce/_search?q="Kamal Richards"&df=customer_full_name
{"profile": "true"
}//响应如下,只贴出查询过程部分
{"type" : "PhraseQuery", //@1"description" : """customer_full_name:"kamal richards"""", //@2"time_in_nanos" : 308390,"breakdown" : { ... }
}
@1 使用的PhraseQuery
语句查询
@2 指定查询customer_full_name
的值为kamal richards
,语句查询要保证查询条件的顺序
范围查询和算数符号
- 范围查询
GET kibana_sample_data_ecommerce/_search?q=taxful_total_price:[30.00 TO 43.0]
{"profile": "true"
}//响应如下,只贴出查询过程部分
{"type" : "IndexOrDocValuesQuery", //@1"description" : "taxful_total_price:[30.0 TO 43.0]", //@2"time_in_nanos" : 286535,"breakdown" : {...}
}
- 算数符号进行范围查询
GET kibana_sample_data_ecommerce/_search?q=order_date:(>=2020-03-27 AND <2020-03-28)
{"profile": "true"
}//
{"type" : "BooleanQuery", //@1"description" : "+order_date:[1585267200000 TO 9223372036854775807] +order_date:[-9223372036854775808 TO 1585353599999]", //@2"time_in_nanos" : 616947,"breakdown" : : {...},"children" : [ //@3{"type" : "IndexOrDocValuesQuery", //@4"description" : "order_date:[1585267200000 TO 9223372036854775807]", //@5"time_in_nanos" : 227389,"breakdown" : {...}},{"type" : "IndexOrDocValuesQuery","description" : "order_date:[-9223372036854775808 TO 1585353599999]","time_in_nanos" : 137528,"breakdown" : {...}}]
}
@1 使用BooleanQuery
对查询进行分组
@2 对查询的描述,必须
order_date满足[1585267200000 TO 9223372036854775807]
和[-9223372036854775808 TO 1585353599999]
两个条件同时满足
@3 本次查询包含的子查询,即BooleanQuery
将条件分成了两个IndexOrDocValuesQuery
查询
@4
@5
通配符查询
正则表达式
模糊匹配与近似查询
Request Body Search查询
使用SearchTemplate查询
允许你使用mustache language
对现有模板进行填充
GET shakespeare/_search/template
POST kibana_sample_data_ecommerce/_search
{"profile": true,"query": {"match_all": {}}
}
GET kibana_sample_data_ecommerce/_search
{"profile": true,"query": {"match_phrase": {"FIELD": "PHRASE"}}
}GET kibana_sample_data_ecommerce/_search?q=customer_full_name:(Eddie Underwood)&default_operator=AND
{"profile":"true"
}GET kibana_sample_data_ecommerce/_search/template?pretty
{"source": {"query": {"match_phrase": {"{{my_field}}": "{{my_value}}"}},"size": "{{my_size}}"},"params": {"my_routing":"pVeQmXABN95HgAacawWq","my_field": "customer_full_name","my_value": "Eddie Underwood","my_size": 5}
}POST /_scripts/my_search_template?pretty
{"script": {"lang": "mustache","source": {"query": {"match_phrase": {"{{my_field}}": "{{my_value}}"}}}}
}GET _scripts/my_search_templateGET kibana_sample_data_ecommerce/_search/template
{"id": "my_search_template","params": {"my_field": "customer_full_name","my_value": "Eddie Underwood"}
}GET _render/template
{"source": "{ \"query\": { \"terms\": {{#toJson}}statuses{{/toJson}} }}","params": {"statuses" : {"status": [ "pending", "published" ]}}
}
//验证一个搜索模板
GET _render/template
{"id": "my_search_template","params": {"my_field": "customer_full_name","my_value": "Eddie Underwood"}
}GET /_search/template?pretty
{"id": "my_search_template","params": {"my_field": "customer_full_name","my_value": "Eddie Underwood"},"explain": true
}
在Elasticsearch使用URI花式搜索相关推荐
- 在ElasticSearch之下(图解搜索的故事)
http://www.cnblogs.com/richaaaard/p/5226334.html 摘要 先自上而下,后自底向上的介绍ElasticSearch的底层工作原理,试图回答以下问题: 为什么 ...
- 【ElasticSearch】大数据搜索选开源还是商业软件?ElasticSearch 对比 Splunk
1.概述 转载:大数据搜索选开源还是商业软件?ElasticSearch 对比 Splunk述 本文就架构,功能,产品线,概念等方面就ElasticSearch和Splunk做了一下全方位的对比,希望 ...
- ElasticSearch 2 (16) - 深入搜索系列之近似度匹配
ElasticSearch 2 (16) - 深入搜索系列之近似度匹配 摘要 标准的全文搜索使用TF/IDF处理文档.文档里的每个字段或一袋子词.match 查询可以告诉我们哪个袋子里面包含我们搜索的 ...
- 用Elasticsearch构建电商搜索平台(有赞)
随着互联网数据规模的爆炸式增长,如何从海量的历史,实时数据中快速获取有用的信息,变得越来越有挑战性. 电商数据系统主要类型 一个中等的电商平台,每天都要产生百万条原始数据,上亿条用户行为数据.一般来说 ...
- 如何使用Elasticsearch构建强大的搜索和分析应用程序(2023年最新ES新手教程)
1.Elasticsearch 非常强大的开源搜索引擎,可以帮助我们从海量数据中快速找到需要的内容 什么是elasticsearch 一个开源的分布式搜索引擎,可以用来实现搜索.日志统计.分析.系统监 ...
- 使用ElasticSearch进行可扩展搜索
当我上高中时, google只是一个名词,代表了一个非常庞大的数字. 今天,我们有时将google用作动词,与在线浏览和搜索同义,并且我们也用它来指代同名公司. 通常会调用" Papa Go ...
- 使用ElasticSearch进行文档搜索
1.ElasticSearch 概念特点 全文搜索是对非结构化数据的一种搜索方式,所谓非结构化数据是指相对于结构化数据(如数据库)来说长度不固定或无固定格式的数据,例如文档.邮件等. 对非结构化数据的 ...
- 使用Elasticsearch快速搭建食谱搜索系统
搜索是一个网站的基础功能,一个好的搜索系统可以直接促进页面访问量的提升,目前流行的搜索系统方案都是基于开源的Elasticsearch和Solr搭建.本文以食谱搜索场景为例,介绍如何利用阿里云Elas ...
- elasticsearch系列四:搜索详解(搜索API、Query DSL)
一.搜索API 1. 搜索API 端点地址 从索引tweet里面搜索字段user为kimchy的记录 GET /twitter/_search?q=user:kimchy 从索引tweet,user里 ...
最新文章
- javascript捕获ocx事件
- 软件成本度量方法及CMMI V2.0,你Get到了吗?
- sklearn识别单张自定义手写数字图片
- python怎么写中文至excel_[ Python爬虫实战 ] python 操作excel以及解决中文报错 - pytorch中文网...
- 记录一下在mac上做一个usb linux安装盘
- python tabula 使用方法_Python中os.walk()的使用方法
- 【Vue学习】—Vue UI组件库(二十八)
- Ablative analysis(消融分析)
- switch case异常处理机制
- HardSoft-Viewer SQL
- zone watermark水位控制
- pdf editor android,MaxiPDF PDF editor builder
- foxit pdf editor linux,Foxit PDF Editor
- ZOJ 3880 Demacia of the Ancients
- 计算机考研落榜了怎么办,如果,你考研落榜了怎么办?
- 兑换记录html页面,兑换码记录.html
- SpringBoot后台搭建-创建restful接口,使用mybatisPlus实现分页
- 算法基础知识——二叉树
- 弘扬文明之风,彰显正能量!中山公园职工平凡岗位上的不凡坚守!
- 现代企业管理-管理概论
热门文章
- 诚之和:李子柒向资本发起“反击战”
- 牛客网SQL 进阶篇刷题
- 《实时控制软件设计》第一次编程作业
- 2020再谈软件测试人员的核心竞争力
- 向服务器获取同步信息失败,主域服务器和辅助域服务器数据同步失败
- linux中uboot作用,uboot的作用和启动方式
- Kaggle word2vec NLP 教程 第三部分:词向量的更多乐趣
- chp2-2-2_fmm_word_seg通过最大正向匹配算法对句子进行切分
- IBM斥资340亿美元完成收购红帽;亚马逊云计算Q2营收数据新鲜出炉;甲骨文推出Oracle专用自治数据库云……...
- PentestBOX教程