ES 之快速入门

Docker安装ES参考:
https://blog.csdn.net/const_/article/details/106035130

初步检索

_cat:

# 查看所有节点
http://192.168.56.11:9200/_cat/nodes
# 查看 es 健康状况
http://192.168.56.11:9200/_cat/health
# 查看主节点
http://192.168.56.11:9200/_cat/master
# 查看所有索引 show databases
http://192.168.56.11:9200/_cat/indices

快速使用:

1、索引一个文档(保存)
在 customer 索引下的 external 类型下保持 1 号数据
PUT http://192.168.56.11:9200/customer/external/1
{
    "name": "John Doe"
}
第一次请求,创建
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}
第二次请求,更新
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

2、查询文档
GET http://192.168.56.11:9200/customer/external/1
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 2,
    "_seq_no": 1,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "John Doe"
    }
}
3、删除文档
DELETE http://192.168.56.11:9200/customer/external/1

批量API:

POST customer/external/_bulk
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }

进阶检索

样本测试数据:

在 bank 索引下的 account 类型下批量索引数据
POST bank/account/_bulk
测试数据

在这里插入图片描述

检索信息:

GET bank/_search?q=*&sort=account_number:asc
响应结果解释:
took - Elasticsearch 执行搜索的时间(毫秒)
time_out - 告诉我们搜索是否超时
_shards - 告诉我们多少个分片被搜索了,以及统计了成功/失败的搜索分片
hits - 搜索结果
hits.total - 搜索结果
hits.hits - 实际的搜索结果数组(默认为前 10 的文档)
sort - 结果的排序 key(键)(没有则按 score 排序)
score 和 max_score –相关性得分和最高得分(全文检索用)


query 定义如何查询:match_all 查询类型【代表查询所有的所有】,es 中可以在 query 中组合非常多的查
询类型完成复杂查询。
from+size 限定,完成分页功能。
sort 排序。
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 2,
  "sort": [
    {
      "account_number": {
        "order": "desc"
      }
    }
  ]
}
返回:
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "999",
        "_score" : null,
        "_source" : {
          "account_number" : 999,
          "balance" : 6087,
          "firstname" : "Dorothy",
          "lastname" : "Barron",
          "age" : 22,
          "gender" : "F",
          "address" : "499 Laurel Avenue",
          "employer" : "Xurban",
          "email" : "dorothybarron@xurban.com",
          "city" : "Belvoir",
          "state" : "CA"
        },
        "sort" : [
          999
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "998",
        "_score" : null,
        "_source" : {
          "account_number" : 998,
          "balance" : 16869,
          "firstname" : "Letha",
          "lastname" : "Baker",
          "age" : 40,
          "gender" : "F",
          "address" : "206 Llama Court",
          "employer" : "Dognosis",
          "email" : "lethabaker@dognosis.com",
          "city" : "Dunlo",
          "state" : "WV"
        },
        "sort" : [
          998
        ]
      }
    ]
  }
}


返回部分字段:
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 2,
  "_source": [
    "age",
    "balance"
  ]
}
返回:
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "balance" : 39225,
          "age" : 32
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "balance" : 5686,
          "age" : 36
        }
      }
    ]
  }
}


match【匹配查询】:
基本类型(非字符串),精确匹配。match 返回 account_number=20 的数据。
GET bank/_search
{
  "query": {
    "match": {
      "account_number": "20"
    }
  }
}
字符串,全文检索。查询出 address 中包含 mill 单词的所有记录。
GET bank/_search
{
  "query": {
    "match": {
      "address": "mill"
    }
  }
}


match_phrase【短语匹配】:
查出 address 中包含 mill road 的所有记录。
GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "mill road"
    }
  }
}


multi_match【多字段匹配】:
查询 state 或者 address 字段包含 mill 的数据。
GET bank/_search
{
  "query": {
    "multi_match": {
      "query": "mill",
      "fields": [
        "state",
        "address"
      ]
    }
  }
}

IK分词器

安装ik分词器:
https://github.com/infinilabs/analysis-ik

# 安装
docker exec -it elasticsearch ./bin/elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-ik/7.4.2
# 重启容器
docker restart elasticsearch
# 查看是否安装成功
curl -X GET "localhost:9200/_cat/plugins?v&s=component&h=name,component,version"
name         component   version
a6322eeab7b1 analysis-ik 7.4.2

安装ik分词器(用这个):
下载对应的版本,然后解压缩到plugins目录中。
在这里插入图片描述
检查是否安装成功:

docker exec -it elasticsearch /bin/bash
cd ./bin
elasticsearch-plugin list

在这里插入图片描述
测试分词器:

# 使用默认分词器
POST _analyze
{
  "text": "我是中国人"
}

# 使用 ik 分词器
POST _analyze
{
  "analyzer": "ik_smart",
  "text": "我是中国人"
}

# 
POST _analyze
{
  "analyzer": "ik_max_word",
  "text": "我是中国人"
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
自定义词库:
安装nginx:

# 随便启动一个 nginx 实例,只是为了复制出配置
docker run -p 80:80 --name nginx -d nginx:1.27.2

# 将容器内的配置文件拷贝到当前目录
mkdir -p /home/docker/nginx
cd /home/docker/nginx
docker container cp nginx:/etc/nginx ./conf
docker container cp nginx:/usr/share/nginx/html ./html

# 删除原容器
docker rm -f nginx

# 创建新的 nginx
# 给 nginx 的 html 下面放的所有资源可以直接访问
docker run -p 80:80 --name nginx \
-v /home/docker/nginx/html:/usr/share/nginx/html \
-v /home/docker/nginx/logs:/var/log/nginx \
-v /home/docker/nginx/conf:/etc/nginx \
-d nginx:1.27.2

docker exec -it nginx /bin/bash

在Nginx中创建对应的词库文件:
在这里插入图片描述
然后在ik分词器的插件配置文件中修改远程词库的地址:
重启ElasticSearch服务。
在这里插入图片描述
验证自定义分词:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值