Query DSL 簡介
- Elasticsearch提供了基于JSON的吉拳、完整的
Query DSL
(Domain Specific Language楷拳,領域特定語言)。
- 把Query DSL想象成
AST
(Abstract Syntax Tree,抽象語法樹)幅疼,這樣就有葉子子句
和復合(容器)子句
晓勇。
- 葉子子句:葉子子句可以單獨使用堂飞,一般用來查找指定字段的指定值,例如
match
绑咱、term
绰筛、range
。
- 復合子句:組合葉子子句或其他復合子句描融,一般用來邏輯組合多查詢(例如
bool
)或改變行為(例如constant_score
)铝噩。
Query Context | Filter Context
-
Query Context
- 除了匹配文檔,還會計算匹配程度(相關性算分)窿克,分數(shù)越高匹配程度越高骏庸。
-
Filter Context
- Filter Context只進行過濾(是|否),不計算匹配程度(相關性算分)年叮。
- ELasticsearch會自動緩存常用的過濾具被,以提高性能。
- 總之只损,把需要計算匹配程度的條件放到Query Context下一姿,其余盡量放到Filter Context下。放到Query Context下算分跃惫,放到Filter Context緩存啸蜜,提高性能。
在Query DSL這一部分的示例中辈挂,我都會使用這份樣例數(shù)據(jù)
DELETE /examples
PUT /examples
{
"settings": {
"number_of_replicas": 1,
"number_of_shards": 5
}
}
POST /examples/_doc/_bulk?pretty
{"index":{"_id":1}}
{"name":"Mr. Zhang","age":22,"birth":"1997-03-26","address":"China Jiangsu Xuzhou","handsome":true}
{"index":{"_id":2}}
{"name":"A B","age":22,"birth":"1997-03-26","address":"China Jiangsu","handsome":true}
{"index":{"_id":3}}
{"name":"C D","age":23,"birth":"1996-03-26","address":"China Zhejiang","handsome":true}
{"index":{"_id":4}}
{"name":"E F","age":24,"birth":"1995-03-26","address":"China Chongqing","handsome":true}
{"index":{"_id":5}}
{"name":"G","age":25,"birth":"1994-03-26","address":"Jiangsu","handsome":false}
GET /examples/_search
GET /examples/_mapping
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者