ElasticSearch7.6入门学习笔记-KuangStudy-文章
启动闪退问题
解决:使用自带的jdk
在 elasticsearch-env.bat文件里面加一句
set JAVA_HOME=D:/zuomian/elasticsearch-7.6.1/jdk
修改启动内存
启动
解决跨域问题
在elasticsearch.yml下添加
http.cors.enabled: true
http.cors.allow-origin: "*"
启动elasticsearch-head-master
cd elasticsearch-head
# 安装依赖
npm install
# 启动
npm run start
# 访问
http://localhost:9100/
启动kibana
1、使用kibana测试
ik_smart
:最少切分
ik_max_word
:最细粒度划分(穷尽词库的可能)
基本Rest命令说明:
method | url地址 | 描述 |
---|---|---|
PUT(创建,修改) | localhost:9200/索引名称/类型名称/文档id | 创建文档(指定文档id) |
POST(创建) | localhost:9200/索引名称/类型名称 | 创建文档(随机文档id) |
POST(修改) | localhost:9200/索引名称/类型名称/文档id/_update | 修改文档 |
DELETE(删除) | localhost:9200/索引名称/类型名称/文档id | 删除文档 |
GET(查询) | localhost:9200/索引名称/类型名称/文档id | 查询文档通过文档ID |
POST(查询) | localhost:9200/索引名称/类型名称/文档id/_search | 查询所有数据 |
字段数据类型
- 字符串类型
- text、keyword
- text:支持分词,全文检索,支持模糊、精确查询,不支持聚合,排序操作;text类型的最大支持的字符长度无限制,适合大字段存储;
- keyword:不进行分词,直接索引、支持模糊、支持精确匹配,支持聚合、排序操作。keyword类型的最大支持的长度为——32766个UTF-8类型的字符,可以通过设置ignore_above指定自持字符长度,超过给定长度后的数据将不被索引,无法通过term精确匹配检索返回结果。
- text、keyword
- 数值型
- long、Integer、short、byte、double、float、half float、scaled float
- 日期类型
- date
- te布尔类型
- boolean
- 二进制类型
- binary
- 等等…
索引的基本操作
创建一个索引,添加
put /索引名/类型名/文档id
PUT /test1/type1/1
{
"name": "星晨",
"age": 18
}
3、指定字段的类型(使用PUT)
类似于建库(建立索引和字段对应类型),也可看做规则的建立
PUT /test2
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age":{
"type": "long"
},
"birthday":{
"type": "date"
}
}
}
}
PUT /xc/user/1
{
"name": "星晨",
"age": 18,
"desc": "放弃不难,但坚持一定很酷!",
"tags": ["技术宅","阳光","乐观"]
}
获取建立的规则
修改索引
POST /test3/_doc/1/_update
{
"doc":{
"name": "法外狂徒张三"
}
}
删除索引
DELETE test1
条件查询
GET xc/user/_search?q=name:星晨
构建查询(推荐使用)
指定字段查询
GET xc/user/_search
{
"query": {
"match": {
"name": "星晨"
}
},
"_source": ["name","desc"]
}
排序
GET xc/user/_search
{
"query": {
"match": {
"name": "星晨"
}
},
"sort": [
{
"age": {
"order": "asc"
}
}
]
}
分页
GET xc/user/_search
{
"query": {
"match": {
"name": "星晨"
}
},
"sort": [
{
"age": {
"order": "asc"
}
}
],
"from": 0,
"size": 1
}
多条件查询
must(and) 都需要满足否则查不到 where id=1 and age=xx
GET xc/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "星晨"
}
},
{
"match": {
"age": 18
}
}
]
}
}
}
should(or) 符合其中一个就能查出 where id=1 or age=18
GET xc/user/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "星晨"
}
},
{
"match": {
"age": 18
}
}
]
}
}
}GET xc/user/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"age": 18
}
},
{
"term": {
"age": 21
}
}
]
}
}
}
must_not(not)
过滤
GET xc/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "星晨"
}
}
],
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
}
}
}
匹配多个条件
精确查询
term
直接通过 倒排索引 指定词条查询
GET xc/user/_search
{
"query": {
"term": {
"tags": "男"
}
}
}
关于分词
text和keyword
- text:
- 支持分词,全文检索、支持模糊、精确查询,不支持聚合,排序操作;
- text类型的最大支持的字符长度无限制,适合大字段存储;
- keyword:
- 不进行分词,直接索引、支持模糊、支持精确匹配,支持聚合、排序操作。
- keyword类型的最大支持的长度为——32766个UTF-8类型的字符,可以通过设置ignore_above指定自持字符长度,超过给定长度后的数据将不被索引,无法通过term精确匹配检索返回结果。
高亮
GET xc/user/_search
{
"query": {
"match": {
"name": "星晨"
}
},
"highlight": {
"fields": {
"name":{}
}
}
}
自定义高亮
GET xc/user/_search
{
"query": {
"match": {
"name": "星晨"
}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"name":{}
}
}
}