写点什么

Elasticsearch Query DSL 概述

用户头像
escray
关注
发布于: 2021 年 02 月 19 日
Elasticsearch Query DSL 概述

Elasticsearch Query DSL, 文字内容来自 B 站中华石杉 Elasticsearch 顶尖高手系列课程核心知识篇,英文内容来自官方文档。

Query DSL


Elasticsearch provides a full Query DSL (Domain Specific Language) based on JSON to define queries.


By default, Elasticsearch sorts matching search results by relevance score, which measures how well each document matches a query.


In the query context, a query clause answers the question "How well does this document match this query clause?"


In a filter context, a query clause answers the question "Does this document match this query clause?"


GET /_search{  # The query parameter indicates query context  "query": {    # The bool and two match clauses are used in query context, which means that they are used to score how well each document matches.    "bool": {      "must": [        { "match": { "title": "search" }},        { "match": { "content": "Elasticsearch" }}        ],      # The filter parameter indicates filter context. It's term and range clauses are used in filter context. They will filter out documents which do not match, but they will not affect the score for matching documents      "filter": [        { "term": { "status": "published" }},        { "range": { "publish_date": { "gte": "2015-01-01" }}}        ]    }  }}
复制代码



一个例子让你明白什么是 Query DSL


GET /_search{    "query": {        "match_all": {}    }}
{ "took" : 36, "timed_out" : false, "_shards" : { "total" : 49, "successful" : 49, "skipped" : 0, "failed" : 0 },...
复制代码
Query DSL 基本语法


{    QUERY_NAME: {        ARGUMENT: VALUE,        ARGUMENT: VALUE,...    }}
{    QUERY_NAME: {        FIELD_NAME: {            ARGUMENT: VALUE,            ARGUMENT: VALUE,...        }    }}
复制代码


示例:


GET /test_index/_search {  "query": {    "match": {      "test_field": "test"    }  }}
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" },
复制代码
如何组合多个搜索条件


搜索需求:title 必须包含 elasticsearch,content 可以包含 elasticsearch 也可以不包含,author_id 必须不为 111


GET /website/_search{  "query": {    "bool": {      "must": [        {          "match": {            "title": "elasticsearch"          }        }        ],      "should": [        {          "match": {            "content": "elasticsearch"          }        }        ],      "must_not": [        {          "match": {            "author_id": 111          }        }        ]    }  }}
复制代码


官方示例:


GET /test_index/_search{  "query": {    "bool": {      "must": { "match": { "name": "tom" }},      "should": [        {"match": {"hired": true }},        {"bool": {          "must": {"match": {"personality": "good" }},          "must_not": {"match": {"rude":true }}        }}        ],      "minimum_should_match": 1    }  }}
复制代码


发布于: 2021 年 02 月 19 日阅读数: 32
用户头像

escray

关注

Let's Go 2017.11.19 加入

在学 Elasticsearch 的项目经理

评论

发布
暂无评论
Elasticsearch Query DSL 概述