Elasticsearch搜索經常使用API(利用Kibana來操做)

上面咱們已經介紹了Elasticsearch的一些基本操做,這篇文章屬於進階篇,咱們一塊兒來學習。elasticsearch

前面咱們建立了sdb和user文檔,如今咱們來看如何查詢user中全部的文檔呢?post

GET /sdb/user/_search學習

此時輸出入下:this

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "username" : "小麗麗",
          "age" : 16,
          "gender" : "女",
          "about" : "this is my info",
          "addrs" : [
            "北京",
            "江西",
            "香港"
          ]
        }
      },
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "username" : "張三",
          "age" : 16,
          "gender" : "男",
          "about" : "this is my info",
          "addrs" : [
            "甘肅",
            "陝西",
            "蘭州"
          ]
        }
      },
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "username" : "秦雪",
          "age" : 16,
          "gender" : "女",
          "about" : "this is my student",
          "addrs" : [
            "甘肅",
            "陝西",
            "天津"
          ]
        }
      }
    ]
  }
}

接下來咱們來查詢姓名爲秦雪的人url

GET /sdb/user/_search?q=username:%e7%a7%a6%e9%9b%aa (這裏須要注意,username:是urlencoding事後的字符串,若是是中文,kibana dev tools會報錯)rest

執行結果以下:code

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "3",
        "_score" : 0.5753642,
        "_source" : {
          "username" : "秦雪",
          "age" : 16,
          "gender" : "女",
          "about" : "this is my student",
          "addrs" : [
            "甘肅",
            "陝西",
            "天津"
          ]
        }
      }
    ]
  }
}

能夠看到,咱們檢索出了名字爲秦雪的人。blog

下面咱們介紹如何使用Query String的形式來查詢ip

GET /sdb/user/_search
{
    "query" : {
        "match" : {
            "username" : "秦雪"
        }
    }
}

咱們能夠看到,檢索結果以下:文檔

下面咱們來看看更爲複雜的檢索

例如要查詢addr在甘肅的同窗

GET /sdb/user/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "addrs": "甘肅"
        }}
      ]
    }
  }
}

結果以下:

{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "username" : "張三",
          "age" : 16,
          "gender" : "男",
          "about" : "this is my info",
          "addrs" : [
            "甘肅",
            "陝西",
            "蘭州"
          ]
        }
      },
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "3",
        "_score" : 0.5753642,
        "_source" : {
          "username" : "秦雪",
          "age" : 16,
          "gender" : "女",
          "about" : "this is my student",
          "addrs" : [
            "甘肅",
            "陝西",
            "天津"
          ]
        }
      }
    ]
  }
}

檢索包含甘肅可是不在包含天津的同窗

GET /sdb/user/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "addrs": "甘肅"
        }}
      ],
      "must_not": [
        {"match": {
          "addrs": "天津"
        }}
      ]
      
    }
  }
}

結果以下:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "username" : "張三",
          "age" : 16,
          "gender" : "男",
          "about" : "this is my info",
          "addrs" : [
            "甘肅",
            "陝西",
            "蘭州"
          ]
        }
      }
    ]
  }
}

接下來爲你們介紹es裏邊的短語搜索

首先使用match_all來顯示全部文檔
GET /sdb/user/_search
{
  "query": {
   "match_all": {}
  }
}

結果以下:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "username" : "小麗麗",
          "age" : 16,
          "gender" : "女",
          "about" : "this is my info",
          "addrs" : [
            "北京",
            "江西",
            "香港"
          ]
        }
      },
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "username" : "張三",
          "age" : 16,
          "gender" : "男",
          "about" : "this is my info",
          "addrs" : [
            "甘肅",
            "陝西",
            "蘭州"
          ]
        }
      },
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "username" : "秦雪",
          "age" : 16,
          "gender" : "女",
          "about" : "this is my student",
          "addrs" : [
            "甘肅",
            "陝西",
            "天津"
          ]
        }
      }
    ]
  }
}

接着咱們查詢

GET /sdb/user/_search
{
  "query": {
   "match_phrase": {
     "about": "my student"
   }
  }
}

結果以下:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "3",
        "_score" : 0.5753642,
        "_source" : {
          "username" : "秦雪",
          "age" : 16,
          "gender" : "女",
          "about" : "this is my student",
          "addrs" : [
            "甘肅",
            "陝西",
            "天津"
          ]
        }
      }
    ]
  }
}

咱們發現,查詢到的帶有my student的記錄只有一個,說明查詢是正確的。

下面咱們說一下關鍵詞高亮,這個在搜索顯示的地方用的比較多

GET /sdb/user/_search
{
  "query": {
   "match_phrase": {
     "about": "my student"
   }
  },
  "highlight": {
    "fields": {
      "about": {}
    }
  }
}

輸出結果以下:

{
  "took" : 233,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "3",
        "_score" : 0.5753642,
        "_source" : {
          "username" : "秦雪",
          "age" : 16,
          "gender" : "女",
          "about" : "this is my student",
          "addrs" : [
            "甘肅",
            "陝西",
            "天津"
          ]
        },
        "highlight" : {
          "about" : [
            "this is <em>my</em> <em>student</em>"
          ]
        }
      }
    ]
  }
}

可能有不少同窗會問,我不想用em標籤,我想用其餘的,那怎麼辦?不用着急,elasticsearch已經爲咱們想到了,請看下面

GET /sdb/user/_search
{
  "query": {
    "match_phrase": {
      "about": "my student"
    }
  },
  "highlight": {
    "fields": {
      "about": {}
    },
    "pre_tags" : ["<color>"],
    "post_tags" : ["</color>"]
  }
}

結果以下:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "3",
        "_score" : 0.5753642,
        "_source" : {
          "username" : "秦雪",
          "age" : 16,
          "gender" : "女",
          "about" : "this is my student",
          "addrs" : [
            "甘肅",
            "陝西",
            "天津"
          ]
        },
        "highlight" : {
          "about" : [
            "this is <color>my</color> <color>student</color>"
          ]
        }
      }
    ]
  }
}

接下來咱們來看看分析

GET /sdb/user/_search
{
  "aggs": {
    "all_interests": {
      "terms": {"field": "interests"}
    }
  }
}

以上寫法是死的,結果以下:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "username" : "小麗麗",
          "age" : 16,
          "gender" : "女",
          "about" : "this is my info",
          "addrs" : [
            "北京",
            "江西",
            "香港"
          ]
        }
      },
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "username" : "張三",
          "age" : 16,
          "gender" : "男",
          "about" : "this is my info",
          "addrs" : [
            "甘肅",
            "陝西",
            "蘭州"
          ]
        }
      },
      {
        "_index" : "sdb",
        "_type" : "user",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "username" : "秦雪",
          "age" : 16,
          "gender" : "女",
          "about" : "this is my student",
          "addrs" : [
            "甘肅",
            "陝西",
            "天津"
          ]
        }
      }
    ]
  },
  "aggregations" : {
    "all_interests" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ ]
    }
  }
}

文章到此就結束了,有問題能夠在下方評論,技術問題能夠私聊我

相關文章
相關標籤/搜索