Elasticsearch7.7 基础教程 1

Elasticsearch7.7 基础教程 1

以下简称es7.7

es7.7的安装:

1 官网下载 https://www.elastic.co/cn/downloads/elasticsearch
2 解压文件
3 在安装文件夹下的bin目录下启动windows 批处理文件
(elasticsearch-7.7.0-windows-x86_64\elasticsearch-7.7.0\bin)
4 启动后访问http://localhost:9200/ 若看见对应的页面出现:

{
  "name" : "LAPTOP-0V6FD18V",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "epUEbKDwRAKVZqVIx9boSw",
  "version" : {
    "number" : "7.7.0",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "81a1e9eda8e6183f5237786246f6dced26a10eaf",
    "build_date" : "2020-05-12T02:01:37.602180Z",
    "build_snapshot" : false,
    "lucene_version" : "8.5.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

即安装成功

注意

启动的时候可能会出现种种问题 看安装目录下的日志文件 解决问题
我遇到的问题
1 java版本不对应
2 堆栈异常
前者将jdk 由11 调整到 14成功解决
后者重启解决

图形化界面head的安装

由于es没有图形化操作界面 所以要安装head图形化界面
1 下载位置:https://github.com/mobz/elasticsearch-head/tree/v5.0.0
2 下载之后开始解压缩到任意的目录 但是要和es7.7 的安装目录分开
3 下载node.js 并安装
4 node -v 出现版本号 即安装成功node.js
5 head界面 需要依赖于 grunt 需要npm全局安装grant
6 全局安装grunt的命令: npm install -g grunt-cil 安装速度太慢记得换源
7 进入elasticsearch-head-master 目录中 安装相关的依赖 npm install
8 启动 grunt server
9 地址 http://localhost:9100/
10 在es7.7 的安装目录下的H:\elasticsearch-7.7.0-windows-x86_64\elasticsearch-7.7.0\config 下的 elasticsearch.yml 文件 在文件末追加

http.cors.enabled: true
http.cors.allow-origin: "*"

这两行 作用是: 允许 跨域连接
11 在 http://localhost:9100/ 的连接框中输入 http://localhost:9200/ (es7.7 的地址)

es中的几个名词解释

index (索引) 相当于数据库中的 database
type (类型) 相当于数据库中的table
document (文档) 相当于数据库中的row (记录)
fields (字段) 相当于数据库中的column (字段)
mapping 对处理数据的方式和规则做一些限制
分片和复制 (分布式中将数据进行分片以及备份)

安装postman

目的是方便进行发送各种请求

下载位置https://www.postman.com/downloads/ 傻瓜式安装 没有难度

创建index+index下的type请求:

url(post)

http://localhost:9200/blog1?include_type_name=true

url 的含义是 创建blog1 这个index

put请求包含的json文件

{
	"mappings": {
		"article": {
			"properties": {
				"id": {
					"type": "long",
					"store": true,
					"index": false
				},
				"title": {
					"type": "text",
					"store": true,
					"index": true,
					"analyzer": "standard"
				},
				"content": {
					"type": "text",
					"store": true,
					"index": true,
					"analyzer": "standard"
				}
			}
		}		
	}
}

json文件的含义是 创建一个article type 里面包含字段 id title content

为type添加一个document

url(post)

http://localhost:9200/blog1/article/3
url的含义是 为 blog1 (index) 中的 article (type)
添加主键为3 的一条document (数据库中的记录)

json

{
	"id":3,
	"title":"孔小格",
	"content":"孔小格"
}

json 中的id 实际上指的是 定义字段的id 和上面的主键要分开

删除一条document 就是选中一条document 通过postman 发送 del请求即可

修改一条document 就是创建一条document 只要将主键对应起来 会自动覆盖

对某一个type(table)进行搜索(三种搜索方式)

1 关键字搜索

url(post)
http://localhost:9200/blog/hello/_search
json
{
	"query":{
		"term":{
			"title":"孔"
		}
	}
}

2 id直接查找

url(get)
http://localhost:9200/blog1/article/1

3 query_string 查找

query的特点:对条件进行先分词 例如“小孔” 会先分词为“小”和“孔” 进行命中

url(post)
http://localhost:9200/blog1/article/_search
json
{
	"query":{
		"query_string":{
			"default_field":"title",
			"query":"今天小孔很开心"
		}
	}
}

es7中的分析器(standard)

url(post)

http://localhost:9200/_analyze

json

{
  "analyzer": "standard", 
  "text":"小孔"
}

显示结果

{
    "tokens": [
        {
            "token": "小",
            "start_offset": 0,
            "end_offset": 1,
            "type": "<IDEOGRAPHIC>",
            "position": 0
        },
        {
            "token": "孔",
            "start_offset": 1,
            "end_offset": 2,
            "type": "<IDEOGRAPHIC>",
            "position": 1
        }
    ]
}

es7 集成 IK 分析器

集成步骤:

1 下载ik的zip 下载地址
https://github.com/medcl/elasticsearch-analysis-ik/releases
2 解压缩 放到es安装下的plugins 文件夹下
3 将文件夹改名为 ik_analyzer (这一步并没有实质性的用途 只是为了方便辨析这是ik分析器)

使用步骤

集成好了之后,使用方法和 standard 一样 只不过把 json 中的 “analyzer”: “standard”, 改为
“analyzer”: “ik_smart”,
或者
“analyzer”: “ik_max_word”,
这是两种不同。0算法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值