Elasticsearch基本语法
match和match_phrase区别
match: 索引中只要有任意一个匹配拆分后词就可以出现在结果中,只是匹配度越高的排越前面
match_phrase: 索引中必须同时匹配拆分后词就可以出现在结果中
ex:
GET /product_index/product/_search
{
"query": {
"match_phrase": {
"product_name": "PHILIPS toothbrush"
}
}
}
product_name必须同时包含PHILIPS和toothbrush才会返回。
match的另一些用法
满足分词结果中所有的词,而不是像上面,任意一个就可以的。
GET /product_index/product/_search
{
"query": {
"match": {
"product_name": {
"query": "PHILIPS toothbrush",
"operator": "and"
}
}
}
}
只要命中50%的分词就返回
GET /test_index/test/_search
{
"query": {
"match": {
"product_name": {
"query": "java 程序员 书 推荐",
"minimum_should_match": "50%"
}
}
}
}
multi_match: 查询a和b字段中,只要有c关键字的就出现
GET /test_index/test/_search
{
"query": {
"multi_match": {
"query": "c",
"fields": [
"a",
"b"
]
}
}
}
multi_match 跨多个 field 查询,表示查询分词必须出现在相同字段中
GET /product_index/product/_search
{
"query": {
"multi_match": {
"query": "PHILIPS toothbrush",
"type": "cross_fields",
"operator": "and",
"fields": [
"product_name",
"product_desc"
]
}
}
}
match_phrase + slop
- 在说 slop 的用法之前,需要先说明原数据是:大吉大利,被分词后至少有:大 吉 大 利 四个 term。
- match_phrase 的用法我们上面说了,按理说查询的词必须完全匹配才能查询到,吉利 很明显是不完全匹配的。
- 但是有时候我们就是要这种不完全匹配,只要求他们尽可能靠谱,中间有几个单词是没啥问题的,那就可以用到 slop。slop = 2 表示中间如果间隔 2 个单词以内也算是匹配的结果()。
- 实也不能称作间隔,应该说是移位,查询的关键字分词后移动多少位可以跟 doc 内容匹配,移动的次数就是 slop。所以 吉利 其实也是可以匹配到 doc 的,只是 slop = 1 才行。
GET /product_index/product/_search
{
"query": {
"match_phrase": {
"product_name" : {
"query" : "吉利",
"slop" : 1
}
}
}
}
term用法
term 一般用在不分词字段上的,因为它是完全匹配查询,如果要查询的字段是分词字段就会被拆分成各种分词结果,和完全查询的内容就对应不上了。
所以自己设置 mapping 的时候有些不分词的时候就最好设置不分词。
其实 Elasticsearch 5.X 之后给 text 类型的分词字段,又默认新增了一个子字段 keyword,这个字段的类型就是 keyword,是不分词的,默认保留 256 个字符。假设 product_name 是分词字段,那有一个 product_name.keyword 是不分词的字段,也可以用这个子字段来做完全匹配查询。
terms 用法
类似于数据库的 in
GET /product_index/product/_search
{
"query": {
"constant_score": {
"filter": {
"terms": {
"product_name": [
"toothbrush",
"shell"
]
}
}
}
}
}
query和filter区别
GET /product_index/_search
{
"query": {
"bool": {
"must": [
{
"terms": {
"product_name": [
"PHILIPS",
"toothbrush"
]
}
},
{
"range": {
"price": {
"gt": 12.00
}
}
}
]
}
}
}
GET /product_index/product/_search
{
"query": {
"constant_score": {
"filter": {
"range": {
"price": {
"gte": 30.00
}
}
}
}
}
}
从搜索结果上看:
- filter,只查询出搜索条件的数据,不计算相关度分数
- query,查询出搜索条件的数据,并计算相关度分数,按照分数进行倒序排序
从性能上看:
- filter(性能更好,无排序),无需计算相关度分数,也就无需排序,内置的自动缓存最常使用查询结果的数据
- query(性能较差,有排序),要计算相关度分数,按照分数进行倒序排序,没有缓存结果的功能
- filter 和 query 一起使用可以兼顾两者的特性,所以看你业务需求
should 有一个特殊性,如果组合查询中没有 must 条件,那么 should 中必须至少匹配一个。我们也还可以通过 minimum_should_match 来限制它匹配更多个。
GET /product_index/product/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"product_name": "java"
}
},
{
"match": {
"product_name": "程序员"
}
},
{
"match": {
"product_name": "书"
}
},
{
"match": {
"product_name": "推荐"
}
}
],
"minimum_should_match": 3
}
}
}
should有一个特殊性,如果组合查询中没有 must 条件,那么 should 中必须至少匹配一个。我们也还可以通过 minimum_should_match 来限制它匹配更多个。
GET /product_index/product/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"product_name": "java"
}
},
{
"match": {
"product_name": "程序员"
}
},
{
"match": {
"product_name": "书"
}
},
{
"match": {
"product_name": "推荐"
}
}
],
"minimum_should_match": 3
}
}
}
boost 用法
在搜索精准度的控制上,还有一种需求,比如搜索:PHILIPS toothbrush,要比:Braun toothbrush 更加优先,我们可以这样:
GET /product_index/product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"product_name": "toothbrush"
}
}
],
"should": [
{
"match": {
"product_name": {
"query": "PHILIPS",
"boost": 4
}
}
},
{
"match": {
"product_name": {
"query": "Braun",
"boost": 3
}
}
}
]
}
}
}
通配符搜索(性能较差,扫描所有倒排索引)
GET /product_index/product/_search
{
"query": {
"wildcard": {
"product_name": {
"value": "ipho*"
}
}
}
}
正则搜索(性能较差,扫描所有倒排索引)
GET /product_index/product/_search
{
"query": {
"regexp": {
"product_name": "iphone[0-9].+"
}
}
}
range用法
range用于查询数值,时间区间
GET /product_index/product/_search
{
"query": {
"range": {
"price": {
"gte": 30.00
}
}
}
}
Elasticsearch基本语法的更多相关文章
- ElasticSearch 查询语法
ElasticSearch是基于lucene的开源搜索引擎,它的查询语法关键字跟lucene一样,如下: 分页:from/size 字段:fields 排序:sort 查询:query 过滤:filt ...
- elasticsearch 基础 语法总结
1. es 使用 restful 风格的 api 备注: es 的 api 格式 基本是这个样 请求方式 /索引名/文档名/id?参数 ,但是 还有 很多不是这样的 请求,比如 ...
- elasticSearch curl 语法总结
#创建索引a.put创建curl -XPUT http://localhost:9200/shb01/student/1-d'{"name":"jack",&q ...
- elasticsearch基本概念与查询语法
序言 后面有大量类似于mysql的sum, group by查询 elk === elk总体架构 https://www.elastic.co/cn/products Beat 基于go语言写的轻量型 ...
- Elasticsearch基本CURD操作语法讲解
当我们的ES集群搭建完成以后,我怎么能看到集群中各个节点状态以及主节点和健康情况呢,如下讲解使用curl命令来与ES集群进行交互.分别有查询主节点情况.集群状态.以及创建索引查看索引.查看分片以及对E ...
- Elasticsearch之java的基本操作一
摘要 接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...
- Func<T,T>应用之Elasticsearch查询语句构造器的开发
前言 之前项目中做Elasticsearch相关开发的时候,虽然借助了第三方的组件PlainElastic.Net,但是由于当时不熟悉用法,而选择了自己拼接查询语句.例如: string queryG ...
- Elasticsearch java api 基本搜索部分详解
文档是结合几个博客整理出来的,内容大部分为转载内容.在使用过程中,对一些疑问点进行了整理与解析. Elasticsearch java api 基本搜索部分详解 ElasticSearch 常用的查询 ...
- Elasticsearch Docker环境下安装
Elasticsearch Docker环境下安装 Daemon镜像配置的是https://registry.docker-cn.com Linux:vi /etc/docker/daemon.jso ...
随机推荐
- codevs——3372 选学霸(背包)
题目等级 : 大师 Master 时间限制: 1 s 空间限制: 128000 KB 题解 题目描述 Description 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实 ...
- Mysql乱码问题解决历程
可能是因为看了太多网上的关于这个问题的解决办法,可能当时是我自己没有看明白也或许是情况不一样,反正都没有解决我当初遇到的问题,现在想想可能是自己当初太无知了,第二个原因是原来大多数情况下是在windo ...
- 使用nginx实现的灰度发布思路研究(待实践)
灰度发布也叫 A/B 测试,原理是一套系统在实现了负载均衡,全国节点都部署了系统之后,可以在新功能上线后,让一小部分用户先使用,从中收集使用信息来做对比和发现bug,及时调整,最终分发到全国的节点. ...
- windows 下使用github客户端报错:Failed to publish this branch
在windows系统下使用github客户端同步的时候报错“Failed to publish this branch”,查找原因,发现结果是安装vscode的时候没有检查到git,然后安装git后库 ...
- linux 学习解决归档管理器打开rar和zip中文文件名乱码问题
在ubunut下打开windows下压缩的rar文件和zip压缩文件出现中文文件名乱码的问题真的很头疼.文件名乱码其实也没有什么关系是不?至少重命名再改回来或者是使用英文命名都可以克服.不巧的是,如此 ...
- hdu 1541Stars
题意:定义在某颗星星左下方的星星的个数表示该星星的水平,求出水平分别为为0...n-1的星星个数. 首先题目是按照y坐标升序输入的,设第第1,2...n个星星的横坐标依次为x1,x2,...xn.显然 ...
- mac 安装 office
1.下载word2016,可以网上搜一下,很多资源 当然这里将我用的版本提供给你: 链接: https://pan.baidu.com/s/1mix07lY 密码: asgc 一直默认下一步进行安 ...
- OpenStack 安装教程(使用Fuel )
OpenStack Fuel 安装教程 1介绍 OpenStack 是由 Rackspace 和 NASA 共同开发的云计算平台,帮助服务商和企业内部实现类似于 Amazon EC2 和 S3 的云基 ...
- HttpWebRequest.Proxy属性
HttpWebRequest.Proxy默认使用IE代理,可以设置“Internet选项-Internet属性-连接-局域网设置-代理服务器”.
- jquery实现图片的依次加载图片
css代码: ul#portfolio{margin:0;padding:0;} ul#portfolio li{float:left;margin:0 5px 0 0;width:250px;hei ...