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檢索語言實現(xiàn)低散,它通過自身的query參數(shù)指定具體的DSL查詢語句俯邓。
- search api
- 具體語法見https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html#search-multi-index-type。
- 主要關(guān)注帶request body的api調(diào)用形式熔号,因為DSL必須在這種形式下才可以使用稽鞭。
curl -XGET 'http://host:port/index/doc/_search' -d ' { # request body
...
"query": {DSL_QUERY_CLAUSE} # query 參數(shù)
...
}'
- DSL語法
DSL_QUERY_CLAUSE:
{
QUERY_NAME:
{
FIELD_NAME:
{
ARGUMENT: VALUE,
ARGUMENT: VALUE
...
}
}
}
-
DSL 上下文
- query context(按匹配程度篩選結(jié)果)
- 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(按匹配程度篩選結(jié)果)
-
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
- 比較重要,因為需要使用復(fù)合查詢的場景可能比較多
- 通過復(fù)合查詢跨嘉,可以指定子查詢是query context還是filter context
- https://www.elastic.co/guide/en/elasticsearch/reference/current/compound-queries.html
- eg