999 - Elasticsearch Query DSL 01

Query DSL 简介

  • Elasticsearch提供了基于JSON的、完整的Query DSL(Domain Specific Language,领域特定语言)。
  • 把Query DSL想象成AST(Abstract Syntax Tree,抽象语法树),这样就有叶子子句复合(容器)子句
    • 叶子子句:叶子子句可以单独使用,一般用来查找指定字段的指定值,例如matchtermrange
    • 复合子句:组合叶子子句或其他复合子句,一般用来逻辑组合多查询(例如bool)或改变行为(例如constant_score)。

Query Context | Filter Context

  • Query Context
    • 除了匹配文档,还会计算匹配程度(相关性算分),分数越高匹配程度越高。
  • Filter Context
    • Filter Context只进行过滤(是|否),不计算匹配程度(相关性算分)。
    • ELasticsearch会自动缓存常用的过滤,以提高性能。
  • 总之,把需要计算匹配程度的条件放到Query Context下,其余尽量放到Filter Context下。放到Query Context下算分,放到Filter Context缓存,提高性能。

在Query DSL这一部分的示例中,我都会使用这份样例数据

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

你可能感兴趣的:(999 - Elasticsearch Query DSL 01)