Query DSL 语法和应用
什么是Query DSL
- Query DSL(Domain-Specific Language)是一种用于构建搜索查询的强大的领域特定查询语言
- 类似我们关系性数据库的 SQL 查询语法
- ES 中用 JSON 结构化的方式定义和执行各种查询操作,在 ES 中进行高级搜索和过滤
基本语法
shell
GET /索引库名/_search
{
"query":{
"查询类型":{
}
}常见 Query DSL 查询语句和功能
match 查询
用于执行全文搜索,它会将搜索查询与指定字段中的文本进行匹配
shell
{
"query": {
"match": {
"title": "elasticsearch"
}
}
}term 查询
用于精确匹配一个指定字段的关键词,不进行分词处理。
shell
{
"query": {
"term": {
"category": "books"
}
}
}总结
- Query DSL提供了更多种类的查询和过滤语句,以满足不同的搜索需求。
- 可以根据具体的业务需求和数据结构,结合不同的查询方式来构建复杂的搜索和过滤操作
数据准备
创建索引
shell
PUT /shuofeng_shop_v1
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"title": {
"type": "keyword"
},
"summary": {
"type": "text"
},
"price": {
"type": "float"
}
}
}
}导入数据
shell
PUT /shuofeng_shop_v1/_bulk
{ "index": { "_index": "shuofeng_shop_v1" } }
{ "id": "1", "title": "Spring Boot","summary":"this is a summary Spring Boot video", "price": 9.99 }
{ "index": { "_index": "shuofeng_shop_v1" } }
{ "id": "2", "title": "java","summary":"this is a summary java video", "price": 19.99 }
{ "index": { "_index": "shuofeng_shop_v1" } }
{ "id": "3", "title": "Spring Cloud","summary":"this is a summary Spring Cloud video", "price": 29.99 }
{ "index": { "_index": "shuofeng_shop_v1" } }
{ "id": "4", "title": "Spring_Boot", "summary":"this is a summary Spring_Boot video","price": 59.99 }
{ "index": { "_index": "shuofeng_shop_v1" } }
{ "id": "5", "title": "SpringBoot","summary":"this is a summary SpringBoot video", "price": 0.99 }查询数据
shell
GET /shuofeng_shop_v1/_search
朔风