简介
https://www.elastic.co/guide/en/elasticsearch/reference/7.3/query-dsl.html
Elasticsearch提供了基于JSON的完整查询DSL(Domain Specific Language 特定域的语言)来定义查询。将查询DSL视为查询的AST(抽象语法树),它由两种子句组成:
- 叶子查询子句
叶子查询子句 在特定域中寻找特定的值,如 match,term或 range查询。
- 复合查询子句
复合查询子句包装其他叶子查询或复合查询,并用于以逻辑方式组合多个查询(例如 bool或 dis_max查询),或更改其行为(例如 constant_score查询)
- 支持跨索引查询
```json# 跨index只需要在index中写上index名字,用逗号间隔,或者用通配符
post index1,index2,logs*/_search
profile代表查看具体怎么执行的
POST /索引库名/_search { “profile”: true, “query”: { “查询类型”: { “查询条件”: “查询条件值” } }, “sort”: [ { “_score”: { “order”: “desc” } } ] }
这里的query代表一个查询对象,里面可以有不同的查询属性
- 查询类型:
例如: match_all , match , term , range 等等
- 查询条件:
查询条件会根据类型的不同,写法也有差异
<a name="BcPCe"></a>
# 查所有
例子
```json
POST /lagou-company-index/_search
{
"query": {
"match_all": {}
}
}
返回指定字段
POST /blogs/_search?pretty
{
"_source":{
"includes":["body"],
"excludes":["id"]
},
"query": {
"match_all": {}
}
}
GET /book/_search?filter_path=hits.total
{
"query":{
"match_all": {}
}
}
准备数据
PUT /book
{
"settings": {},
"mappings": {
"properties": {
"description": {
"type": "text",
"analyzer": "ik_max_word"
},
"name": {
"type": "text",
"analyzer": "ik_max_word"
},
"price": {
"type": "float"
},
"timestamp": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
PUT /book/_doc/1
{
"name": "lucene",
"description": "Lucene Core is a Java library providing powerful indexing and search features, as well as spellchecking, hit highlighting and advanced analysis/tokenization capabilities. The PyLucene sub project provides Python bindings for Lucene Core. ",
"price": 100.45,
"timestamp": "2020-08-21 19:11:35"
}
PUT /book/_doc/2
{
"name": "solr",
"description": "Solr is highly scalable, providing fully fault tolerant distributed indexing, search and analytics. It exposes Lucenes features through easy to use JSON/HTTP interfaces or native clients for Java and other languages.",
"price": 320.45,
"timestamp": "2020-07-21 17:11:35"
}
PUT /book/_doc/3
{
"name": "Hadoop",
"description": "The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models.",
"price": 620.45,
"timestamp": "2020-08-22 19:18:35"
}
PUT /book/_doc/4
{
"name": "ElasticSearch",
"description": "Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力 的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条 款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜 索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elasticsearch是最受欢 迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。",
"price": 999.99,
"timestamp": "2020-08-15 10:11:35"
}
PUT /lagou-property
{
"settings": {},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"images": {
"type": "keyword"
},
"price": {
"type": "float"
}
}
}
}
POST /lagou-property/_doc
{
"title": "小米电视4A",
"images": "https://image.lagou.com/12479122.jpg",
"price": 4288
}
POST /lagou-property/_doc
{
"title": "小米手机",
"images": "https://image.lagou.com/12479622.jpg",
"price": 2699
}
POST /lagou-property/_doc
{
"title": "华为手机",
"images": "https://image.lagou.com/12479922.jpg",
"price": 5699
}
DELETE employees
PUT /employees/_bulk
{ "index" : { "_id" : "1" } }
{ "name" : "Emma","age":32,"job":"Product Manager","gender":"female","salary":35000 }
{ "index" : { "_id" : "2" } }
{ "name" : "Underwood","age":41,"job":"Dev Manager","gender":"male","salary": 50000}
{ "index" : { "_id" : "3" } }
{ "name" : "Tran","age":25,"job":"Web Designer","gender":"male","salary":18000 }
{ "index" : { "_id" : "4" } }
{ "name" : "Rivera","age":26,"job":"Web Designer","gender":"female","salary": 22000}
{ "index" : { "_id" : "5" } }
{ "name" : "Rose","age":25,"job":"QA","gender":"female","salary":18000 }
{ "index" : { "_id" : "6" } }
{ "name" : "Lucy","age":31,"job":"QA","gender":"female","salary": 25000}
{ "index" : { "_id" : "7" } }
{ "name" : "Byrd","age":27,"job":"QA","gender":"male","salary":20000 }
{ "index" : { "_id" : "8" } }
{ "name" : "Foster","age":27,"job":"Java Programmer","gender":"male","salary": 20000}
{ "index" : { "_id" : "9" } }
{ "name" : "Gregory","age":32,"job":"Java Programmer","gender":"male","salary":22000 }
{ "index" : { "_id" : "10" } }
{ "name" : "Bryant","age":20,"job":"Java Programmer","gender":"male","salary": 9000}
{ "index" : { "_id" : "11" } }
{ "name" : "Jenny","age":36,"job":"Java Programmer","gender":"female","salary":38000 }
{ "index" : { "_id" : "12" } }
{ "name" : "Mcdonald","age":31,"job":"Java Programmer","gender":"male","salary": 32000}
{ "index" : { "_id" : "13" } }
{ "name" : "Jonthna","age":30,"job":"Java Programmer","gender":"female","salary":30000 }
{ "index" : { "_id" : "14" } }
{ "name" : "Marshall","age":32,"job":"Javascript Programmer","gender":"male","salary": 25000}
{ "index" : { "_id" : "15" } }
{ "name" : "King","age":33,"job":"Java Programmer","gender":"male","salary":28000 }
{ "index" : { "_id" : "16" } }
{ "name" : "Mccarthy","age":21,"job":"Javascript Programmer","gender":"male","salary": 16000}
{ "index" : { "_id" : "17" } }
{ "name" : "Goodwin","age":25,"job":"Javascript Programmer","gender":"male","salary": 16000}
{ "index" : { "_id" : "18" } }
{ "name" : "Catherine","age":29,"job":"Javascript Programmer","gender":"female","salary": 20000}
{ "index" : { "_id" : "19" } }
{ "name" : "Boone","age":30,"job":"DBA","gender":"male","salary": 30000}
{ "index" : { "_id" : "20" } }
{ "name" : "Kathy","age":29,"job":"DBA","gender":"female","salary": 20000}
指定超时
比如: 正常来说我们可以在10s内获取2000条数据,但是指定了timeout,发生超时后我们可以获取10ms中获取到的 100条数据。
GET /_search?timeout=10ms
返回值
{
"took" : 63,// 耗费的时间 (以毫秒为单位)
// 是否超时了,默认情况下不存在time_out,比如你的搜索耗时1分钟,它就等1分钟,但是不超时
// 在发送搜索请求时可以指定超时时间
// 比如你指定了10ms超时,它就会把这10ms内获得的数据返回给你
"timed_out" : false,
"_shards" : { // 你的搜索请求打到了几个shard上面去。
// Primary Shard可以承接读、写流量。Replica Shard会承接读流量。
// 因为我是默认配置,有五个primary shard。
// 所以它的搜索请求会被打到5个分片上去,并且都成功了
"total" : 5,
"successful" : 5,
"skipped" : 0,// 跳过了0个
"failed" : 0 // 失败了0个
},
"hits" : {//命中的情况
"total" : 1000,// 命中率 1000个
// _score 全文检索时使用,这个相关性得分越高,说明doc和检索的内容的越相关、越匹配
// max_score就是最大的 _score
"max_score" : null,
// 默认查询前10条,直接返回每个doc的完整数据
"hits" : [ {
"_index" : "bank",// 索引
"_type" : "_doc",// type
"_id" : "0",// id
"sort": [0],
"_score" : null,// 相关性得分
// _source里面存放的是doc的具体数据
"_source" : {"account_number":0,
"balance":16623,
"firstname":"Bradshaw",
"lastname":"Mckenzie",
"age":29,
"gender":"F",
"address":"244 Columbus Place",
"employer":"Euron",
"email":"bradshawmckenzie@euron.com",
"city":"Hobucken",
"state":"CO"}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "1",
"sort": [1],
"_score" : null,
"_source" : {"account_number":1,
"balance":39225,
"firstname":"Amber",
"lastname":"Duke",
"age":32,
"gender":"M",
"address":"880 Holmes Lane",
"employer":"Pyrami",
"email":"amberduke@pyrami.com",
"city":"Brogan",
"state":"IL"}
}, ...
]
}
}