前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch常用查询

Elasticsearch常用查询

作者头像
用户3467126
发布2020-03-11 15:59:07
5910
发布2020-03-11 15:59:07
举报
文章被收录于专栏:爱编码爱编码

前言

本文使用的Elasticsearch版本为6.5.4,上文主要介绍ES的各种查询,以满足更多的需求。ES的主要功能其实就是体现在这搜索查询上,所以我就总结一下,方便以后直接拿来就用。

Query查询

代码语言:javascript
复制
# 首先做一个数据准备,创建一个索引
PUT /lib3
{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 0
    },
    "mappings": {
        "user": {
            "properties": {
                "name": {"type": "text"},
                "address": {"type": "text"},
                "age": {"type": "integer"},
                "interests": {"type": "text"},
                "birthday": {"type": "date"}
            }
        }
    }
}

# 添加一些文档
PUT /lib3/user/1
{
    "name": "zhaoliu",
    "address": "hei long jiang sheng tie ling shi",
    "age": 50,
    "birthday": "1970-12-12",
    "interests": "xi huan hejiu, duanlian,lvyou"
}

PUT /lib3/user/2
{
    "name": "zhaoming",
    "address": "bei jing hai dian qu qing he zhen",
    "age": 20,
    "birthday": "1998-10-12",
    "interests": "xi huan hejiu, duanlian,changge"
}

PUT /lib3/user/3
{
    "name": "lisi",
    "address": "bei jing hai dian qu qing he zhen",
    "age": 23,
    "birthday": "1998-10-12",
    "interests": "xi huan hejiu, duanlian,changge"
}

PUT /lib3/user/4
{
    "name": "wangwu",
    "address": "bei jing hai dian qu qing he zhen",
    "age": 26,
    "birthday": "1995-10-12",
    "interests": "xi huan biancheng, tingyinyue,lvyou"
}

PUT /lib3/user/5
{
    "name": "zhangsan",
    "address": "bei jing chao yang qu",
    "age": 29,
    "birthday": "1988-10-12",
    "interests": "xi huan tingyinyue,changge,tiaowu"
}

# 简单查询
GET /lib3/user/_search?q=name:lisi
# 返回结果
{
    "took": 9,  			# took:当前搜索所用时间"time_out": false, 		# time_out:是否超时"_shards": {  			# shards:分片信息"total": 3,  		# 总分片数"successful": 3, 	# 请求**成功**的分片数"skipped": 0, 		# 请求**跳过**的分片数"failed": 0 		# 请求**失败**的分片数
    },
    "hits": {
        "total": 1,  		# 查询出的文档数"max_score": 0.6931472, # 相关度分数(即查出的文档和搜索条件的匹配度,完全匹配为最大值1)"hits": [
            {
                "_index": "lib3",
                "_type": "user",
                "_id": "3",
                "_score": 0.6931472,
                "_source": {
                    "name": "lisi",
                    "address": "bei jing hai dian qu qing he zhen",
                    "age": 23,
                    "birthday": "1998-10-12",
                    "interests": "xi huan he jiu, duan lian,chang ge"
                }
            }
        ]
    }
}

# 简单查询# 查询兴趣是changge的人,并对age进行降序排列
GET /lib3/user/_search?q=interests:changge&sort=age:desc
# 返回结果
{
    "took": 17,
    "time_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": null,
        "hits": [
            {
                "_index": "lib3",
                "_type": "user",
                "_id": "5",
                "_score": null,
                "_source": {
                    "name": "zhangsan",
                    "address": "bei jing chao yang qu",
                    "age": 29,
                    "birthday": "1988-10-12",
                    "interests": "xi huan tingyinyue,changge,tiaowu"
                },
                "sort": [
                    29
                ]
            },
            {
                "_index": "lib3",
                "_type": "user",
                "_id": "5",
                "_score": null,
                "_source": {
                    "name": "lisi",
                    "address": "bei jing hai dian qu qing he zhen",
                    "age": 23,
                    "birthday": "1998-10-12",
                    "interests": "xi huan hejiu,duanlian,cahngge"
                },
                "sort": [
                    23
                ]
            },
            {
                "_index": "lib3",
                "_type": "user",
                "_id": "5",
                "_score": null,
                "_source": {
                    "name": "zhaoming",
                    "address": "bei jing hai dian qu qing he zhen",
                    "age": 20,
                    "birthday": "1998-10-12",
                    "interests": "xi huan hejiu,duanlian,changge"
                },
                "sort": [
                    20
                ]
            }
        ]
    }
}

term查询和terms查询

term query回去倒排索引中寻找确切的term(即精准查询),它并不知道分词器的存在。这种查询适合keyword,numeric,date。

term:查询某个字段里含有某个关键词的文档 terms:查询某个字段里含有多个关键字的文档

代码语言:javascript
复制
GET /lib3/user/_search
{
    "query": {
        "term": {
            "interests": "changge"
        }
    }
}

GET /lib3/user/_search
{
    "query": {
        "terms": {
            "interests": ["hejiu", "changge"] # 这里多个条件是或(即or)的关系
        }
    }
}

match查询

match query知道「分词器的存在」,会对filed进行分词操作,然后在查询

match_all:查询所有文档 multi_match:可以指定多个字段 match_phrase:短语匹配查询,Elasticsearch引擎首先分析(analyze)查询字符串,从分析后的文本中构建短语查询,这意味着必须匹配短语中的所有分词,并且保证各个分词的相对位置不变;

代码语言:javascript
复制
# 分词的体现# 匹配的条件越多,相似度的值将会越高
GET /lib3/user/_search
{
    "query": {
        "match": {
            "name": "zhaoliu zhaoming"
        }
    }
}

GET /lib3/user/_search
{
    "query": {
        "match": {
            "age": 20
        }
    }
}
GET /lib3/user/_search
{
    "query": {
        "multi_match": {
            "query": "lvyou",
            "fields": [
                "interests",
                "name"
            ]
        }
    }
}


GET /lib3/user/_search
{
    "query": {
        "match_phrase": {
            "interests": "duanlian, shuoxiangsheng"
        }
    }
}

指定返回的字段

_source:可在_source中设置想返回的字段

代码语言:javascript
复制
GET /lib3/user/_search
{
    "_source": [
        "address",
        "name"
    ],
    "query": {
        "match": {
            "interests": "changge"
        }
    }
}

控制加载的字段

includes:包含某些字段,可以使用通配符进行查询 excludes:排除某些字段,可以使用通配符进行查询

代码语言:javascript
复制
GET /lib3/user/_search
{
    "_source": {
        "includes": ["name", "address"],
        "excludes": ["age", "birthday"]
    },
    "query": {
        "match_all": {}
    }
}

# 使用通配符查询
GET /lib3/user/_search
{
    "_source": {
        "includes": ["addr*"],
        "excludes": ["name", "bir*"]
    },
    "query": {
        "match_all": {}
    }
}

排序

使用sort实现排序:desc降序 asc升序

代码语言:javascript
复制
GET /lib3/user/_search
{
    "sort": [
        {
            "age": {
                "order": "asc"
            }
        }
    ],
    "query": {
        "match_all": {}
    }
}

GET /lib3/user/_search
{
    "sort": [
        {
            "age": {
                "order": "desc"
            }
        }
    ],
    "query": {
        "match_all": {}
    }
}

控制查询返回的数量

from:从哪一个文档开始 size:需要的个数

代码语言:javascript
复制
GET /lib3/user/_search
{
	"from": 0,
	"size": 2,
    "query": {
        "terms": {
            "interests": ["hejiu", "changge"]
        }
    }
}

范围查询

range:实现范围查询 参数:from、to、include_lower、include_upper、boost

include_lower:是否包含范围的左边界,默认是true include_upper:是否包含范围的右边界,默认是true

代码语言:javascript
复制
GET /lib3/user/_search
{
    "query": {
        "range": {
            "birthday": {
                "from": "1990-10-10",
                "to": "2018-05-01"
            }
        }
    }
}

GET /lib3/user/_search
{
    "query": {
        "range": {
            "age": {
                "from": 20,
                "to": 25,
                "include_lower": true,
                "include_upper": false
            }
        }
    }
}

wildcard查询

允许使用通配符 * 和 ? 来进行查询

*:代表0个或者多个字符 ?:代表任意一个字符

代码语言:javascript
复制
GET /lib3/user/_search
{
    "query": {
        "wildcard": {
            "name": "zhao*"
        }
    }
}

GET /lib3/user/_search
{
    "query": {
        "wildcard": {
            "name": "li?i"
        }
    }
}

fuzzy实现模糊查询

value:查询的关键字 boost:查询的权值,默认值是1.0 min_similarity:设置匹配的最小相似度,默认值为0.5,对于字符串,取值为0-1(包含0和1);对于数值,取值可能大于1;对于日期类型取值为1d,1m等,1d就代表1天 prefix_length:指明区分词项的共同前缀长度,默认是0 max_expansions:查询中的词项可以扩展的数目,默认可以无限大

代码语言:javascript
复制
GET /lib3/user/_search
{
    "query": {
        "fuzzy": {
            "interests": "changge"
        }
    }
}

GET /lib3/user/_search
{
    "query": {
        "fuzzy": {
            "interests": {
                "value": "chagge"# 此处changge写成了chagge,但因为模糊查询依然可以查出结果
            }
        }
    }
}

高亮搜索结果

代码语言:javascript
复制
GET /lib3/user/_search
{
    "query": {
        "match": {
            "interests": "changge"
        }
    },
    "highlight": {
        "fields": {
            "interests": {}
        }
    }
}

返回版本号

version:当设置version为true时,查询的文档中将返回版本信息,默认情况是false(即不设置的version时)

代码语言:javascript
复制
GET /lib3/user/_search
{
	"version": true,
    "query": {
        "terms": {
            "interests": ["hejiu", "changge"]
        }
    }
}

正则表达式查询

代码语言:javascript
复制
GET /lib3/user/_search
{
    "query": {
         "regexp" : {
            "address" : "b[a-z]*u"
        }
    },
    "highlight": {
        "fields": {
            "interests": {}
        }
    }
}

Filter查询

代码语言:javascript
复制
GET /lib7/items/_search
{
    "query":{
        "bool":{
            "filter":[
                {"terms":{"price":[25,40]}}
            ]
        }
    }
}

关于filter查询以及组合查询可以参考文章 https://blog.csdn.net/qq_25834767/article/details/84642051

https://www.cnblogs.com/javasl/p/11405405.html

参考文章

https://blog.csdn.net/zx711166/article/details/81811548

https://www.cnblogs.com/UUUz/p/11170833.html

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-02-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 爱编码 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • Query查询
  • term查询和terms查询
  • match查询
  • 指定返回的字段
  • 控制加载的字段
  • 排序
  • 控制查询返回的数量
  • 范围查询
  • wildcard查询
  • fuzzy实现模糊查询
  • 高亮搜索结果
  • 返回版本号
  • 正则表达式查询
  • Filter查询
  • 参考文章
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档