Windows下 Elasticsearch 学习
下载启动
下载地址 https://www.elastic.co/cn/downloads/elasticsearch
下载好后进入bin目录 执行命令,安装中文分词插件ik
elasticsearch-plugin.bat install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.8.0/elasticsearch-analysis-ik-7.8.0.zip
版本号7.8.0需要与es版本号一致, 我这里为es7.8.0
执行elasticsearch.bat文件即可启动,
请求9200可得到以下页面
新建index
创建一个名为test_data的index,并设置分词引擎。
{
"mappings": {
"properties": {
"name": {
"type":"text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"desc": {
"type":"text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}
插入数据
{
"name":"张三",
"desc":"张三是一名帅哥"
}
查询数据
根据插入时返回的id查询
更新数据
根据id更新
更新后还有其他附带值也会改变.
查询index中所有数据
上面代码中,返回结果的 took字段表示该操作的耗时(单位为毫秒),timed_out字段表示是否超时,hits字段表示命中的记录,里面子字段的含义如下。
total:返回记录数,本例是2条。
max_score:最高的匹配程度,本例是1.0。
hits:返回的记录组成的数组。
全文检索
get请求并带有请求体
{
"query" : { "match" : { "desc" : "帅哥" }}
}
{
"query" : { "match" : { "desc" : "帅哥" }},
"size": 1,
"from": 1
}
增加size参数可设置返回条数
from指定位移
类似from与size一起用 mysql limit。
多关键字搜索
会认为是or关系
{
"query" : { "match" : { "desc" : "帅哥 屌丝" }}
}
and 关系
{
"query": {
"bool": {
"must": [
{ "match": { "desc": "帅哥" } },
{ "match": { "desc": "一" } }
]
}
}
}
es 文档 https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html