elasticsearch學習筆記(二十六)——Elasticsearch query DSL搜索實戰

下面先解釋一下ES的GET+request body這種模式
通常咱們知道HTTP協議通常是不容許get請求帶上request body,可是由於get更加適合描述查詢數據的操做,所以仍是這麼用了。
碰巧當前不少瀏覽器或是服務器也都支持GET+request body模式
若是遇到不支持的場景,也能夠用POST+request body模式.web

一、 一個例子讓你明白什麼是query DSL

GET /website/_search
{
  "query": {
    "match_all": {}
  }
}

二、query DSL的基本語法

GET /{index}/_search
{
    "各類條件"
}

示例:瀏覽器

GET /website/_search
{
  "query": {
    "match": {
      "title": "article"
    }
  }
}
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.13353139,
    "hits" : [
      {
        "_index" : "website",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.13353139,
        "_source" : {
          "post_date" : "2017-01-01",
          "title" : "my first article",
          "content" : "this is my first article in this website",
          "author_id" : 11400
        }
      },
      {
        "_index" : "website",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.13353139,
        "_source" : {
          "post_date" : "2017-01-02",
          "title" : "my second article",
          "content" : "this is my second article in this website",
          "author_id" : 11400
        }
      },
      {
        "_index" : "website",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.13353139,
        "_source" : {
          "post_date" : "2017-01-03",
          "title" : "my third article",
          "content" : "this is my third article in this website",
          "author_id" : 11400
        }
      }
    ]
  }
}

三、組合多個搜索條件

搜索需求:title必須包含first,content能夠包含website也能夠不包含,author_id必須不爲111服務器

GET /website/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "first"
          }
        }
      ],
      "should": [
        {
          "match": {
            "content": "website"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "author_id": 111
          }
        }
      ]
    }
  }
}
{
  "took" : 21,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.1143606,
    "hits" : [
      {
        "_index" : "website",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.1143606,
        "_source" : {
          "post_date" : "2017-01-01",
          "title" : "my first article",
          "content" : "this is my first article in this website",
          "author_id" : 11400
        }
      }
    ]
  }
}

下面附上一個更復雜的組合查詢,以後能夠做爲思考的模板:post

GET /website/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "first"
          }
        }
      ],
      "should": [
        {
          "match": {
            "content": "website"
          }
        },
        {
          "bool": {
            "must": [
              {
                "match": {
                  "content": "first"
                }
              }
            ],
            "must_not": [
              {
                "match": {
                  "title": "second"
                }
              }
            ]
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "author_id": 111
          }
        }
      ]
    }
  }
}
相關文章
相關標籤/搜索