Elasticsearch入门

557 阅读5分钟

安装Elasticsearch

Elasticsearch 8.5.2 点此下载

点此查看安装向导

本机win10系统,下载解压后,直接双击bin/elasticsearch.bat 启动,启动时会显示用户名和密码,以及Kibana初始配置所使用的token.

-> Elasticsearch security features have been automatically configured!
-> Authentication is enabled and cluster connections are encrypted.

->  Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):
  CrkpwVRUn5u2do5IgZ9C

->  HTTP CA certificate SHA-256 fingerprint:
  4d096912ddfa36cc6b57efece319bc597865e6c820c158aeea2922534773184e

->  Configure Kibana to use this cluster:
* Run Kibana and click the configuration link in the terminal when Kibana starts.
* Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes):
  eyJ2ZXIiOiI4LjQuMyIsImFkciI6WyIxOTguMTguMC4xOjkyMDAiXSwiZmdyIjoiNGQwOTY5MTJkZGZhMzZjYzZiNTdlZmVjZTMxOWJjNTk3ODY1ZTZjODIwYzE1OGFlZWEyOTIyNTM0NzczMTg0ZSIsImtleSI6IlB4NWR3WVFCYi14OHVoNGt0Tmd5OlQ4LUs4ZWFjUWJLLVpiTTNIUmpVb3cifQ==

->  Configure other nodes to join this cluster:
* On this node:
  - Create an enrollment token with `bin/elasticsearch-create-enrollment-token -s node`.
  - Uncomment the transport.host setting at the end of config/elasticsearch.yml.
  - Restart Elasticsearch.
* On other nodes:
  - Start Elasticsearch with `bin/elasticsearch --enrollment-token <token>`, using the enrollment token that you generated.

这玩意只有第一次启动时会打印,如果忘记用户名密码,可以通过工具重置。

D:\tools\elasticsearch-8.5.2\bin>elasticsearch-reset-password.bat -u elastic
warning: ignoring JAVA_HOME=D:\tools\JDK\17; using bundled JDK
This tool will reset the password of the [elastic] user to an autogenerated value.
The password will be printed in the console.
Please confirm that you would like to continue [y/N]y


Password for the [elastic] user successfully reset.
New value: QC68LA4-9Au5Ks_5lXl1

Kibana 安装配置

Kibana 8.5.2 点此下载

下载解压即可,为了让界面显示中文,需要在config/kibana.yml文件中添加一行配置:i18n.locale: "zh-CN"

双击bin/kibana.bat 启动Kibana.

首次启动时会生成带code的链接,复制该链接到浏览器打开,填写elasticsearch启动时生成的token,即可完成配置。

image.png

点开左边的菜单栏,向下划动,选择Management->开发工具(dev tools)。即可进入开发调试页面,调用Elasticsearch API进行测试。 image.png

移动到某一行命令时,当前行右上解会显示三角形按钮,点击按钮可执行当前的命令。

image.png

使用Postman访问Elasticsearch

Postman也是一种常用来测试Elasticsearch API的方式,使用Postman调用Elasticsearch的rest api需要进行身份认证。点开Postman的Authorization栏选择使用Basic Auth,设置Elasticsearch用户名/密码。如下图所示:

image.png

使用https://127.0.0.1:9200 获取版本信息,可以看到调用成功。

image.png

中文分词插件

默认情况下,Elasticsearch中文分词是一个字一个字的拆分的。尝试下面命令

GET _analyze
{
  "text": "上海东方明珠",
  "tokenizer": "standard"
}

得到的结果:

 上、海、东、方、明、珠

这没有任任意义,想要得到有意义的分词结果,需要安装中文分词插件,目前使用最广泛的中文分词插件为IK.点此下载 v8.4.3

  • 下载后解压将目录拷贝到elasticsearch的plugin目录下
  • 修改plugin-descriptor.properties文件,将elasticsearch.version配置项改成你所使用的版本,我这里是8.5.2
  • 重启elasticsearch即可生效

tokenizer支持两种类型:ik_smart , ik_max_word

  • ik_max_word:尽量匹配更多的词组,会穷尽各种可能的组合,适合Term Query
  • ik_smart:智能分析应该提取哪些有意义的词语,适合Phrase查询

测试一下效果:

GET _analyze
{
  "text": "上海东方明珠",
  "tokenizer": "ik_smart"
}

返回结果:

上海、东方明珠
GET _analyze
{
  "text": "上海东方明珠",
  "tokenizer": "ik_max_word"
}

返回结果 :

上海、海东、东方明珠、东方、明珠

使用API管理索引

创建索引

POST my_index/_doc/1
{
  "user_id":"1",
  "user_name":"cody",
  "city":"shanghai",
  "province":"shanghai",
  "country":"china"
}

查询索引:

GET /my_index/_doc/1

根据主键修改索引

PUT my_index/_doc/1
{
  "user_id":"1",
  "user_name":"andy",
  "city":"shanghai",
  "province":"shanghai",
  "country":"china"
}

通过查询修改索引

POST /my_index/_update_by_query
{
  "query": {
    "match": {
      "user_name": "andy"
    }
  },
  "script": {
    "source": "ctx._source.city=params.city;ctx._source.province=params.province;ctx._source.country=params.country",
    "lang": "painless",
    "params": {
        "city":"上海",
        "province":"上海",
        "country":"中国"
    }
  }
}

列出所有索引

GET /_cat/indices
yellow open my_index Syr-XvB-QCm0bAabXg-t7Q 1 1 1 2 32.2kb 32.2kb

删除索引

DELETE my_index

批量插入索引

POST _bulk
{"index":{"_index":"my_index"}}
{"user_id":"1","user_name":"上海浦东刘一","age":"24","city":"上海","province":"上海","country":"中国","address":"上海市浦东新区东方明珠","location":{"lat":"13.234321","lon":"141.234867"}}
{"index":{"_index":"my_index"}}
{"user_id":"1","user_name":"上海浦东陈二","age":"32","city":"上海","province":"上海","country":"中国","address":"上海市浦东新区世纪公园","location":{"lat":"13.256321","lon":"141.345121"}}
{"index":{"_index":"my_index"}}
{"user_id":"1","user_name":"上海黄浦张三","age":"36","city":"上海","province":"上海","country":"中国","address":"上海市黄浦区人民广场","location":{"lat":"13.247311","lon":"141.423126"}}
{"index":{"_index":"my_index"}}
{"user_id":"2","user_name":"湖南株洲李四","age":"27","city":"株洲","province":"湖南","country":"中国","address":"湖南省株洲市荷塘区流芳公园","location":{"lat":"14.0642","lon":"61.234809"}}
{"index":{"_index":"my_index"}}
{"user_id":"2","user_name":"湖南长沙王五","age":"45","city":"长沙","province":"湖南","country":"中国","address":"湖南省长沙市岳麓区橘子洲","location":{"lat":"17.345003","lon":"67.346821"}}
{"index":{"_index":"my_index"}}
{"user_id":"2","user_name":"湖南长沙赵六","age":"51","city":"长沙","province":"湖南","country":"中国","address":"湖南省长沙市芙蓉区马王堆","location":{"lat":"16.245307","lon":"71.234569"}}

查询所有数据

GET my_index/_search

查看索引结构定义mapping

GET my_index/_mapping

由查询的结果可知,插入索引时自动生成的mapping中,location的类型并不是地理信息类型,所以我们需要自定义mapping。

自定义索引结构mapping

将location设置为geo_point类型。

PUT my_index
PUT my_index/_mapping
{
  "properties": {
    "location": {
      "type": "geo_point"
    },
    "address": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "uid": {
      "type": "long"
    },
    "age": {
      "type": "short"
    },
    "city": {
      "type": "keyword"
    },
    "province": {
      "type": "keyword"
    },
    "country": {
      "type": "keyword"
    }
  }
}

索引简单查询

GET my_index/_search
{
  "query":{
    "match":{
      "city":"上海"
    }
  }
}

查询得到三条数据。

逻辑与(and)查询:

GET my_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "province": "湖南"
          }
        },
        {
          "match": {
            "city": "长沙"
          }
        }
      ]
    }
  }
}

查询得到两条数据。

逻辑或(or)查询:

GET my_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "city": "株洲"
          }
        },
        {
          "match": {
            "city": "长沙"
          }
        }
      ]
    }
  }
}

逻辑非(not)查询:

GET my_index/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "city": "株洲"
          }
        }
      ]
    }
  }
}

范围查询

查询年龄30到50之间的记录

GET my_index/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 30,
        "lte": 50
      }
    }
  }
}

地理位置按距离查询

距离指定地址5KM以内只有一条记录

GET my_index/_search
{
  "query":{
    "match":{
      "city":"上海"
    }
  },
  "post_filter": {
    "geo_distance": {
      "distance": "5km",
      "location": {
        "lat": 13.245,
        "lon": 141.254
      }
    }
  }
}
上海浦东刘一

距离指定地址20KM以内有三条记录

GET my_index/_search
{
  "query":{
    "match":{
      "city":"上海"
    }
  },
  "post_filter": {
    "geo_distance": {
      "distance": "20km",
      "location": {
        "lat": 13.245,
        "lon": 141.254
      }
    }
  }
}
上海浦东刘一、上海浦东陈二、上海黄浦张三

按距离排序

按距离倒序排列,看看谁距离最远

GET my_index/_search
{
  "query": {
    "match": {
      "city": "上海"
    }
  },
  "post_filter": {
    "geo_distance": {
      "distance": "20km",
      "location": {
        "lat": 13.245,
        "lon": 141.254
      }
    }
  },
  "sort": [
    {"_geo_distance": {
      "location": {
        "lat": 13.245,
        "lon": 141.254
      },
      "order": "desc"
    }}
  ]
}
上海黄浦张三

聚合统计

按年龄区间统计

GET my_index/_search
{
  "size":0,
  "aggs":{
    "age":{
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 20,
            "to": 30
          },{
            "from": 30,
            "to": 40
          },{
            "from": 40,
            "to": 50
          }
        ]
      }
    }
  }
}

湖南省按城市分组统计

GET my_index/_search
{
  "query": {
    "match": {
      "province": "湖南"
    }
  },
  "size":0,
  "aggs":{
    "city_aggs":{
      "terms": {
        "field": "city",
        "size": 10
      }
    }
  }
}