Why am I writing this shit, beacause the official documentation really really sucks, though you might find this article about es search api and query dsl sucks more.
Elasticsearch可以通过search api提供对文档的精细检索。Elasticsearch的search api对文档的精细检索主要靠Elasticsearch的DSL检索语言实现,它通过自身的query参数指定具体的DSL查询语句。
- search api
- 具体语法见https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html#search-multi-index-type。
- 主要关注带request body的api调用形式,因为DSL必须在这种形式下才可以使用。
curl -XGET 'http://host:port/index/doc/_search' -d ' { # request body
...
"query": {DSL_QUERY_CLAUSE} # query 参数
...
}'
- DSL语法
DSL_QUERY_CLAUSE:
{
QUERY_NAME:
{
FIELD_NAME:
{
ARGUMENT: VALUE,
ARGUMENT: VALUE
...
}
}
}
-
DSL 上下文
- query context(按匹配程度筛选结果)
- A query clause used in query context answers the question “How well does this document match this query clause?”
- filter context(类似精确匹配)
- In filter context, a query clause answers the question “Does this document match this query clause?”
- query context(按匹配程度筛选结果)
-
Query clause 类型
- leave clause
- compound clause
- wrap other leaf or compound queries and used to combine multiple queries in a logical fashion(quote from official documentation).
-
compound queries
- 比较重要,因为需要使用复合查询的场景可能比较多
- 通过复合查询,可以指定子查询是query context还是filter context
- https://www.elastic.co/guide/en/elasticsearch/reference/current/compound-queries.html
- eg