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" : [ ]
    }
  }
}

文章到此就结束了,有问题能够在下方评论,技术问题能够私聊我

相关文章
相关标签/搜索