ElasticSearch 8.x 查询 DSL 案例
match_all:查询全部数据
是一种简单的查询,匹配索引中的所有文档
shell
GET /shuofeng_shop_v1/_search
{
"query": {
"match_all": {}
}
}match:有条件查询数据
match,对查询内容进行分词, 然后进行查询,多个词条之间是 or的关系,然后在与文档里面的分词进行匹配,匹配度越高分数越高越前面
shell
GET /shuofeng_shop_v1/_search
{
"query": {
"match": {
"summary": "Spring"
}
}
}
#包括多个词
GET /shuofeng_shop_v1/_search
{
"query": {
"match": {
"summary": "Spring Java"
}
}
}term:完整关键词查询
term 查询,不会将查询条件分词,直接与文档里面的分词进行匹配,虽然 match 也可以完成,但是 match 查询会多一步进行分词,浪费资源
shell
#keyword类型字段,ES不进行分词
GET /shuofeng_shop_v1/_search
{
"query": {
"term": {
"title": {
"value": "Spring Boot"
}
}
}
}获取指定字段
某些情况场景下,不需要返回全部字段,太废资源,可以指定 source 返回对应的字段
shell
GET /shuofeng_shop_v1/_search
{
"_source":["price","title"],
"query": {
"term": {
"title": {
"value": "Spring Boot"
}
}
}
}总结
- match 在匹配时会对所查找的关键词进行分词,然后分词匹配查找;term 会直接对关键词进行查找。
- 一般业务里面需要模糊查找的时候,更多选择 match,而精确查找时选择 term 查询
朔风