Elasticsearch专栏-4.es基本用法-查询api

es基本用法-查询api

说明

es对数据的检索,总结下来就是两部分,即查询和处理。查询指的是查找符合条件的数据,包括查询所有、匹配查询、布尔查询、范围查询、模糊查询等等。处理指的是对查询到的数据做进一步处理,包括是否分页、是否排序、是否聚合、是否分组、是否只返回部分字段等等。es的api就是对这两部分的不同组合。举例如下,数据集使用上一篇创建的bank索引。

查询所有

c 复制代码
GET /bank/_search
{
  "query":{
    "match_all": {}
  }
}

某一字段匹配查询

这块的查询已经涉及到了分词,指的是查询address中包含Place或National的文档。针对分词查询后面会进一步做深入讲解。

c 复制代码
GET /bank/_search
{
	"query": {
		"match": {
			"address": "Place National"
		}
	}
}

多字段查询

multi_match用在多字段查询中,下面的语句表示,只要address或city中,包含mill和urie其中的一个,就算是命中查询。用sql语句表示: select * from bank where (address like '%mill%' or like '%urie%') or (citylike '%mill%' or like '%urie%')

c 复制代码
GET /bank/_search
{
  "query":{
    "multi_match": {
      "query": "mill urie",
      "fields": ["address","city"]
    }
  }
}

bool查询

bool查询主要用到三个关键字:must、must_not、should。在mysql中,就是and、not、or的概念。

其中,must表示必须包含,must_not表示一定不能包含,should表示可以包含,也可以不包含。包含的话,排名要比不包含的靠前。

c 复制代码
GET /bank/_search
{
	"query": {
		"bool": {
			"must": [
				{
					"match": {
						"gender": "M"
					}
				},
				{
					"match": {
						"address": "mill"
					}
				}
			],
			"must_not": [
				{
					"match": {
						"age": "18"
					}
				}
			],
			"should": [
				{
					"match": {
						"lastname": "Wallace"
					}
				}
			]
		}
	}
}

范围查询

在范围查询中,使用的关键词有range、filter,以及表示大于小于的gt、lt、gte、lte。

关键字 含义
gt 大于
lt 小于
gte 大于等于
lte 小于等于
  1. rang方式
c 复制代码
GET /bank/_search
{
	"query": {
	  "range": {
		"age": {
		  "gte": "20",
		  "lte": "25"
		  }
	  }
	}
}
  1. filter方式
c 复制代码
GET /bank/_search
{
	"query": {
		"bool": {
			"must": [
				{
					"match": {
						"gender": "M"
					}
				},
				{
					"match": {
						"address": "mill"
					}
				}
			],
			"filter": [
				{
					"range": {
						"age": {
							"gte": 10,
							"lte": 50
						}
					}
				},
				{
					"range": {
						"balance": {
							"gte": 9812,
							"lte": 9813
						}
					}
				}
			]
		}
	}
}

精确查询

精确查询可以简单理解为完全匹配查询,用term关键字。在数值型查询中经常用到,而在文本中查询使用,是表示查询时不进行分词,刚好和分词查询关键字match对立。

1.查询单个

c 复制代码
GET /bank/_search
{
	"query": {
		"match": {
			"age": 33
		}
	}
}

2.查询多个,此时用terms

c 复制代码
GET /bank/_search
{
	"query": {
		"terms": {
		  "balance": [
		    "34487",
		    "29104"
		  ]
		}
	}
}

2.查询多个,也可以用另外一种方式:should+term

c 复制代码
GET /bank/_search
{
	"query": {
	  "bool": {
	    "should": [
	      {
	        "term": {
	          "balance": "34487"
	        }
	      },
	      {
	        "term": {
	          "balance": "29104"
	        }
	      }
	    ]
	  }
	}
}

正则匹配

c 复制代码
GET /bank/_search
{
  "query": {
    "wildcard": {
        "firstname": "*amber"
    }
  }
}

模糊查询

c 复制代码
GET /bank/_search
{
  "query": {
    "fuzzy": {
        "firstname": "hol"
    }
  }
}

上述只罗列了常用的查询,除此之外,还有很多其他查询,这里不做演示,感兴趣的小伙伴可以自己查找下。

结果处理

开头我们也说了,es对数据的处理就两部分,查询和处理。上面介绍了查询,现在我们介绍下基本的处理:分页、排序、返回部分字段。

关键字 含义
sort desc/asc
from 页数,从0开始
size 每页大小
_source 只返回需要的字段,可以罗列字段,也可以用通配符
c 复制代码
GET /bank/_search
{
	"query": {
		"match": {
			"address": "Hendrickson"
		}
	},
	"sort": [
		{
			"balance": "desc"
		}
	],
	"from": 0,
	"size": 5,
	"_source": [
		"balance",
		"account_number",
		"address"
	]
}
c 复制代码
GET /bank/_search
{
	"query": {
		"match": {
			"address": "Hendrickson"
		}
	},
	"sort": [
		{
			"balance": "asc"
		}
	],
	"from": 0,
	"size": 5,
	"_source":{
     "includes": "addr*",
     "excludes": ["name","bir*"]
  }
}
相关推荐
亲爱的非洲野猪13 小时前
基于ElasticSearch的法律法规检索系统架构实践
大数据·elasticsearch·系统架构
从零开始学习人工智能18 小时前
Doris 与 Elasticsearch:谁更适合你的数据分析需求?
大数据·elasticsearch·数据分析
代码搬运媛21 小时前
ES Modules 与 CommonJS 的核心区别详解
大数据·elasticsearch·搜索引擎
数据智能老司机1 天前
Elastic 向量搜索实战指南——Elastic中的模型管理与向量相关考量
elasticsearch·搜索引擎·llm
不爱学英文的码字机器1 天前
[Git] 标签管理
大数据·git·elasticsearch
异常君1 天前
Elasticsearch 与机器学习结合:实现高效模型推理的方案(上)
java·elasticsearch·机器学习
异常君1 天前
Elasticsearch 与机器学习结合:实现高效模型推理的方案(下)
java·elasticsearch·机器学习
Elastic 中国社区官方博客1 天前
使用 OpenTelemetry 和 Elastic 简化公共部门的可观察性
大数据·elasticsearch·搜索引擎·全文检索·可用性测试·opentelemetry
异常君1 天前
Elasticsearch 集群滚动升级实战 - 保障业务零停机
java·elasticsearch
异常君1 天前
FST 在 Elasticsearch 中的核心应用与性能优化实践
java·elasticsearch·性能优化