ElasticSearch——DSL语言查询
1 文档批量操作
批量获取文档数据是通过_mget的API来实现的。
请求地址:_mget
请求方式:GET
功能说明:可以通过ID批量获取不同index和type的数据
请求参数: docs : 文档数组参数 _index : 指定index _type : 指定type _id : 指定id _source : 指定要查询的字段 请求示例: GET _mget { "docs": [{ "_index": "es_db", "_type": "_doc", "_id": 1 }, { "_index": "es_db", "_type": "_doc", "_id": 2 }] }
响应示例:
{
"docs" : [
{
"_index" : "es_db",
"_type" : "_doc",
"_id" : "1",
"found" : false
},
{
"_index" : "es_db",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "李四",
"sex" : 1,
"age" : 28,
"address" : "广州荔湾大厦",
"remark" : "java assistant"
}
}
]
}
请求地址:/{{indexName}}/_mget
请求方式:GET
功能说明:可以通过ID批量获取不同index和type的数据
请求参数: docs : 文档数组参数 _index : 指定index _type : 指定type _id : 指定id _source : 指定要查询的字段 请求示例: GET /es_db/_mget { "docs": [{ "_type": "_doc", "_id": 1 }, { "_type": "_doc", "_id": 2 }] }
请求地址:/{{indexName}}/{{typeName}}/_mget
请求方式:GET
功能说明:可以通过ID批量获取不同index和type的数据
请求参数: docs : 文档数组参数 _index : 指定index _type : 指定type _id : 指定id _source : 指定要查询的字段 请求示例: GET /es_db/_doc/_mget { "docs": [{ "_id": 1 }, { "_id": 2 }] }
1.2 批量操作文档数据
请求示例: POST /article/_bulk {"create":{"_id":3}} {"id":3,"title":"潇斌老师1","content":"白起老师666","tags":["java","面向对象"],"create_time":1554015482530} {"create":{"_id":4}} {"id":4,"title":"潇斌老师2","content":"白起老师NB","tags":["java", "面向对象"],"create_time":1554015482530}
1.2.2 普通创建或全量替换index
请求示例:
POST _bulk
{"index":{"_index":"article", "_type":"_doc", "_id":3}}
{"id":3,"title":"小斌老师(一)","content":"小斌老师666","tags":["java", "面向对象"],"create_time":1554015482530}
{"index":{"_index":"article", "_type":"_doc", "_id":4}}
{"id":4,"title":"小斌老师(二)","content":"小斌老师NB","tags":["java", "面向对象"],"create_time":1554015482530}
说明:
如果原文档不存在,则是创建。
如果原文档存在,则是替换(全量修改原文档)。
请求示例: POST _bulk {"delete":{"_index":"article", "_type":"_doc", "_id":3}} {"delete":{"_index":"article", "_type":"_doc", "_id":4}}
1.2.4 批量修改update
请求示例: POST _bulk {"update":{"_index":"article", "_type":"_doc", "_id":3}} {"doc":{"title":"ES大法必修内功"}} {"update":{"_index":"article", "_type":"_doc", "_id":4}} {"doc":{"create_time":1554018421008}}
2 DSL语言高级查询
2.1 无条件查询
GET /es_db/_doc/_search { "query":{ "match_all":{} } }
2.2 有条件查询
2.2.1 叶子条件查询
(1) 模糊匹配
示例: POST /es_db/_doc/_search { "query": { "term": { "name": "admin" } } }
说明:根据备注信息模糊查询 match, match会根据该字段的分词器,进行分词查询 示例: POST /es_db/_doc/_search { "from": 0, "size": 2, "query": { "match": { "address": "广州" } } }
#多字段模糊匹配查询与精准查询 multi_match
示例:
POST /txb/_doc/_search
{
"query": {
"multi_match": {
"query": "潇",
"fields": [
"address",
"name"
]
}
}
}
#未指定字段条件查询 query_string , 含 AND 与 OR 条件
示例: POST /es_db/_doc/_search { "query":{ "query_string":{ "query":"广州 OR 长沙" } } }
#指定字段条件查询 query_string , 含 AND 与 OR 条件
示例: POST /es_db/_doc/_search { "query":{ "query_string":{ "query":"admin OR 长沙", "fields":["name","address"] } } }
#分页、输出字段、排序综合查询 示例: POST /es_db/_doc/_search { "query" : { "range" : { "age" : { "gte":25, "lte":28 } } }, "from": 0, "size": 2, "_source": ["name", "age", "book"], "sort": {"age":"desc"} }
term : 单个条件相等
terms : 单个字段属于某个值数组内的值
range : 字段属于某个范围内的值
exists : 某个字段的值是否存在
ids : 通过ID批量查询
2.2.2 组合条件查询(多条件查询)
bool : 各条件之间有and,or或not的关系
must : 各个条件都必须满足,即各条件是and的关系
should : 各个条件有一个满足即可,即各条件是or的关系
must_not : 不满足所有条件,即各条件是not的关系
filter : 不计算相关度评分,它不计算_score即相关度评分,效率更高
constant_score : 不计算相关度评分
示例: POST /es_db/_doc/_search { "query" : { "bool" : { "filter" : { "term":{ "age":25 } } } } }
2.3 总结
GET /es_db/_search { "query": { "match": { "remark": "java developer" } } }
GET /es_db/_search { "query": { "match": { "remark": { "query": "java developer", "operator": "and" } } } }
GET /es_db/_search { "query": { "match": { "remark": { "query": "java architect assistant", "minimum_should_match": "68%" } } } }
GET /es_db/_search { "query": { "bool": { "should": [ { "match": { "remark": "java" } }, { "match": { "remark": "developer" } }, { "match": { "remark": "assistant" } } ], "minimum_should_match": 2 } } }
3.2 match 的底层转换
GET /es_db/_search { "query": { "match": { "remark": "java developer" } } } 转换后是: GET /es_db/_search { "query": { "bool": { "should": [ { "term": { "remark": "java" } }, { "term": { "remark": { "value": "developer" } } } ] } } }
GET /es_db/_search { "query": { "match": { "remark": { "query": "java developer", "operator": "and" } } } } 转换后是: GET /es_db/_search { "query": { "bool": { "must": [ { "term": { "remark": "java" } }, { "term": { "remark": { "value": "developer" } } } ] } } }
GET /es_db/_search { "query": { "match": { "remark": { "query": "java architect assistant", "minimum_should_match": "68%" } } } } 转换后为: GET /es_db/_search { "query": { "bool": { "should": [ { "term": { "remark": "java" } }, { "term": { "remark": "architect" } }, { "term": { "remark": "assistant" } } ], "minimum_should_match": 2 } } }
GET /es_db/_search { "query": { "bool": { "must": [ { "match": { "remark": "java" } } ], "should": [ { "match": { "remark": { "query": "developer", "boost" : 1 } } }, { "match": { "remark": { "query": "architect", "boost" : 3 } } } ] } } }
3.4 基于dis_max实现best fields策略进行多字段搜索
GET /es_db/_search { "query": { "dis_max": { "queries": [ { "match": { "name": "rod" } }, { "match": { "remark": "java developer" } } ] } } }
3.5 基于tie_breaker参数优化dis_max搜索效果
GET /es_db/_search { "query": { "dis_max": { "queries": [ { "match": { "name": "rod" } }, { "match": { "remark": "java developer" } } ], "tie_breaker":0.5 } } }
3.6 使用multi_match简化dis_max+tie_breaker
GET /es_db/_search { "query": { "dis_max": { "queries": [ { "match": { "name": "rod" } }, { "match": { "remark": { "query": "java developer", "boost" : 2, "minimum_should_match": 2 } } } ], "tie_breaker": 0.5 } } } #使用multi_match语法为:其中type常用的有best_fields和most_fields。^n代表权重,相当于"boost":n。 GET /es_db/_search { "query": { "multi_match": { "query": "rod java developer", "fields": ["name", "remark^2"], "type": "best_fields", "tie_breaker": 0.5, "minimum_should_match" : "50%" } } }
3.7 cross fields搜索
GET /es_db/_search { "query": { "multi_match": { "query": "java developer", "fields": ["name", "remark"], "type": "cross_fields", "operator" : "and" } } }
{ "category_name" : "手机", "product_name" : "一加6T手机", "price" : 568800, "sell_point" : "国产最好的Android手机", "tags": ["8G+128G", "256G可扩展"], "color" : "红色", "keyword" : "手机 一加6T手机 国产最好的Android手机" }
PUT /es_db/_mapping { "properties": { "provice" : { "type": "text", "analyzer": "standard", "copy_to": "address" }, "city" : { "type": "text", "analyzer": "standard", "copy_to": "address" }, "street" : { "type": "text", "analyzer": "standard", "copy_to": "address" }, "address" : { "type": "text", "analyzer": "standard" } } }
GET /test_a/_search { "query": { "fuzzy": { "f" : { "value" : "word", "fuzziness": 2 } } } }
3.10 match phrase短语搜索
GET _search { "query": { "match_phrase": { "remark": "java assistant" } } }
3.10.1 match phrase原理 -- term position
GET _analyze { "text": "hello world, java spark", "analyzer": "standard" }
{ "tokens": [ { "token": "hello", "start_offset": 0, "end_offset": 5, "type": "<ALPHANUM>", "position": 0 }, { "token": "world", "start_offset": 6, "end_offset": 11, "type": "<ALPHANUM>", "position": 1 }, { "token": "java", "start_offset": 13, "end_offset": 17, "type": "<ALPHANUM>", "position": 2 }, { "token": "spark", "start_offset": 18, "end_offset": 23, "type": "<ALPHANUM>", "position": 3 } ] }
GET _analyze { "text": "hello world, java spark", "analyzer": "standard" } POST /test_a/_doc/3 { "f" : "hello world, java spark" } GET /test_a/_search { "query": { "match_phrase": { "f" : { "query": "hello spark", "slop" : 2 } } } } GET /test_a/_search { "query": { "match_phrase": { "f" : { "query": "spark hello", "slop" : 4 } } } }
中文:
GET _analyze { "text": "中国,一个世界上最强的国家", "analyzer": "ik_max_word" } POST /test_a/_doc/1 { "f" : "中国,一个世界上最强的国家" } GET /test_a/_search { "query": { "match_phrase": { "f" : { "query": "中国最强", "slop" : 5 } } } } GET /test_a/_search { "query": { "match_phrase": { "f" : { "query": "最强中国", "slop" : 9 } } } }
3.11 前缀搜索 prefix search
GET /test_a/_search { "query": { "prefix": { "f.keyword": { "value": "J" } } } }
GET /test_a/_search { "query": { "wildcard": { "f.keyword": { "value": "?e*o*" } } } }
GET /test_a/_search { "query": { "regexp" : { "f.keyword" : "[A-z].+" } } }
GET /test_a/_search { "query": { "match_phrase_prefix": { "f": { "query": "java s", "slop" : 10, "max_expansions": 10 } } } }
POST /test_a/_doc/3 { "f" : "hello, java is very good, spark is also very good" } POST /test_a/_doc/4 { "f" : "java and spark, development language " } POST /test_a/_doc/5 { "f" : "Java Spark is a fast and general-purpose cluster computing system. It provides high-level APIs in Java, Scala, Python and R, and an optimized engine that supports general execution graphs." } POST /test_a/_doc/6 { "f" : "java spark and, development language " } GET /test_a/_search { "query": { "match": { "f": "java spark" } } } GET /test_a/_search { "query": { "bool": { "must": [ { "match": { "f": "java spark" } } ], "should": [ { "match_phrase": { "f": { "query": "java spark", "slop" : 50 } } } ] } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?