当前位置: 首页 > 工具软件 > sql4es > 使用案例 >

Elasticsearch语句—SQL详解

慎芷阳
2023-12-01

目录

es是什么?

数据类型:

关键字:

bool查询总结:

ES详细查询的两种方式

一、查询字符串搜索

二、结构化查询(单字段查询,不能多字段组合查询)

1、match系列之match_all (查询全部)

2、match系列之match_phrase(短语查询)

3、match系列之match_phrase_prefix(最左前缀查询)智能搜索–以什么开头

4、match系列之multi_match(多字段查询)

5、ES的排序查询

6、ES的分页查询

7、ES的bool查询 (must、should)


es是什么?


es是基于Apache Lucene的开源分布式(全文)搜索引擎,,提供简单的RESTful API来隐藏Lucene的复杂性。
es除了全文搜索引擎之外,还可以这样描述它:

1、分布式的实时文件存储,每个字段都被索引并可被搜索
2、分布式的实时分析搜索引擎
3、可以扩展到成百上千台服务器,处理PB级结构化或非结构化数据。

说明:
类型默认为“_doc”,可自定义名称,在elasticsearch 7.0.0版本必须使用单index,单type,多type结构则会完全移除。


数据类型:

1.text:字符串类型,用于全文索引的字段,例如一篇文章或评论,大小写不敏感。
2.long, integer, short, byte, double, float, half_float, scaled_float:数字类型
3.data:时间类型 :常用的时间类型 format 有:epoch_millis(时间戳,精确到毫秒)、epoch_second(时间戳,精确到秒)
4.boolean:布尔类型
5.binary:二进制类型
6.array:json 中的数组,里面可以包含 string、integers、array、objects,如果是 array object,里面包含的对象或数组不会被索引。
7.object:对象
8.nested:array 对象,里面的包含的对象字段会被索引。
9.keyword:精确匹配关键字,大小写敏感

关键字:

1、match:返回所有匹配的分词。

2、match_all:查询全部。

3、match_phrase:短语查询,在match的基础上进一步查询词组,可以指定slop分词间隔。

4、match_phrase_prefix:前缀查询,根据短语中最后一个词组做前缀匹配,可以应用于搜索提示,但注意和max_expanions搭配。其实默认是50…

5、multi_match:多字段查询,使用相当的灵活,可以完成match_phrase和match_phrase_prefix的工作。

bool查询总结:

must:与关系,相当于关系型数据库中的 and。

should:或关系,相当于关系型数据库中的 or。

must_not:非关系,相当于关系型数据库中的 not。

filter:过滤条件。

range:条件筛选范围。

gt:大于,相当于关系型数据库中的 >。

gte:大于等于,相当于关系型数据库中的 >=。

lt:小于,相当于关系型数据库中的 <。

lte:小于等于,相当于关系型数据库中的 <=。

ES详细查询的两种方式


一、查询字符串搜索

GET test/doc/_search?q=name:wangfei


二、结构化查询(单字段查询,不能多字段组合查询)

GET test/doc/_search
{
“query”:{
“match”:{
“name”:“wang”
}
}
}


1、match系列之match_all (查询全部)

GET test/doc/_search
{
“query”:{
“match_all”: {
}
}
}


2、match系列之match_phrase(短语查询)

GET test1/doc/_search
{
“query”:{
“match_phrase”: {
“title”: “中国”
}
}
}


3、match系列之match_phrase_prefix(最左前缀查询)智能搜索–以什么开头

查询语句

GET test2/doc/_search
{
“query”: {
“match_phrase_prefix”: {
“desc”: “bea”
}
}
}

查询短语
GET test2/doc/_search
{
“query”: {
“match_phrase_prefix”: {
“desc”: “you are bea”
}
}
}


4、match系列之multi_match(多字段查询)


multi_match是要在多个字段中查询同一个关键字 除此之外,mulit_match甚至可以当做match_phrase和match_phrase_prefix使用,只需要指定type类型即可

GET test2/doc/_search
{
“query”: {
“multi_match”: {
“query”: “beautiful”,
“fields”: [“title”,“desc”]
}
}
}

GET test2/doc/_search
{
“query”: {
“multi_match”: {
“query”: “bea”,
“fields”: [“desc”],
“type”: “phrase_prefix”
}
}
}

5、ES的排序查询


es 6.8.4版本中,需要分词的字段不可以直接排序,比如:text类型,如果想要对这类字段进行排序,需要特别设置:对字段索引两次,一次索引分词(用于搜索)一次索引不分词(用于排序),es默认生成的text类型字段就是通过这样的方法实现可排序的。
text类型字段排序问题

倒叙排序

GET test/doc/_search
{
“query”: {
“match_all”: {}
},
“sort”: [
{
“age”: {
“order”: “desc”
}
}
]
}
升序排序

GET test/doc/_search
{
“query”: {
“match_all”: {}
},
“sort”: [
{
“age”: {
“order”: “asc”
}
}
]
}


6、ES的分页查询


from:从哪开始查 size:返回几条结果

GET test/doc/_search
{
“query”: {
“match_phrase_prefix”: {
“name”: “wang”
}
},
“from”: 0,
“size”: 1
}


7、ES的bool查询 (must、should)


must (must字段对应的是个列表,也就是说可以有多个并列的查询条件,一个文档满足各个子条件后才最终返回)

单条件查询
GET test/doc/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“name”: “wangfei”
}
}
]
}
}
}

多条件组合查询


GET test/doc/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“name”: “wanggfei”
}
},{
“match”: {
“age”: 25
}
}
]
}
}
}

should (只要符合其中一个条件就返回)

GET test/doc/_search
{
“query”: {
“bool”: {
“should”: [
{
“match”: {
“name”: “wangjifei”
}
},{
“match”: {
“age”: 27
}
}
]
}
}
}


must_not 顾名思义

GET test/doc/_search
{
“query”: {
“bool”: {
“must_not”: [
{
“match”: {
“name”: “wangjifei”
}
},{
“match”: {
“age”: 27
}
}
]
}
}
}


filter(条件过滤查询,过滤条件的范围用range表示gt表示大于、lt表示小于、gte表示大于等于、lte表示小于等于)

GET test/doc/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“name”: “wangjifei”
}
}
],
“filter”: {
“range”: {
“age”: {
“gte”: 10,
“lt”: 27
}
}
}
}
}
}



 

 类似资料: