ElasticSearch安装使用

29 篇文章 2 订阅


ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

安装

下载

官网下载:
https://www.elastic.co/downloads/elasticsearch
官网资料:
https://www.elastic.co/guide/index.html
https://www.elastic.co/guide/cn/index.html【简体中文,但版本陈旧】
安装参考:
https://www.elastic.co/guide/en/elastic-stack-get-started/current/get-started-elastic-stack.html

解压运行

本次安装windows版本,解压文件后,进入bin目录,运行elasticsearch.bat
为防止出现中文乱码,建议在dos中运行chcp 65001,更改字符集为UTF-8
run

查看

浏览器录入http://127.0.0.1:9200 可以看到节点名称、版本等信息
默认情况下,Elastic 只允许本机访问,如果需要远程访问,可以修改 Elastic 安装目录的config/elasticsearch.yml文件,去掉network.host的注释,将它的值改成0.0.0.0,然后重新启动 Elastic。
exploer

命令使用

创建索引

新建 Index

可以直接向 Elastic 服务器发出 PUT 请求。
下面的例子是新建一个名叫weather的 Index
curl -X PUT http://localhost:9200/weather
D:>curl -X PUT http://localhost:9200/weather
{“acknowledged”:true,“shards_acknowledged”:true,“index”:“weather”}

查看所有索引

curl localhost:9200/_cat/indices?v

附:查看集群状态
curl localhost:9200/_cat/health?v
附:查询节点的列表:
curl localhost:9200/_cat/nodes?v

分词器语法

新建一个 Index,指定需要分词的字段。这一步根据数据结构而异,基本上,凡是需要搜索的中文字段,都要单独设置。
语法如下:
POST _analyze
{
“analyzer”: “分词器类型”,
“text”: “我是一个中国人,张姓在中国是大姓。”
}
标准分词器:standard
简单分词器:simple
空格分词器:whitespace
语言分词器:chinese,效果类似标准分词器
analyzer
simple

中文分词器

下载安装

中文分词器https://github.com/medcl/elasticsearch-analysis-ik/releases下载后,解压到安装elastic的plugin目录下,然后重启服务
ik
setupik

使用说明

官网https://github.com/medcl/elasticsearch-analysis-ik
Analyzer: ik_smart , ik_max_word
Tokenizer: ik_smart , ik_max_word

示例一

请求:{
“analyzer”: “ik_max_word”,
“text”: “我是一个中国人,张姓在中国是大姓。”
}
响应:{
“tokens”: [
{
“token”: “我”,
“start_offset”: 0,
“end_offset”: 1,
“type”: “CN_CHAR”,
“position”: 0
},
{
“token”: “是”,
“start_offset”: 1,
“end_offset”: 2,
“type”: “CN_CHAR”,
“position”: 1
},
{
“token”: “一个中国”,
“start_offset”: 2,
“end_offset”: 6,
“type”: “CN_WORD”,
“position”: 2
},
{
“token”: “一个”,
“start_offset”: 2,
“end_offset”: 4,
“type”: “CN_WORD”,
“position”: 3
},
{
“token”: “一”,
“start_offset”: 2,
“end_offset”: 3,
“type”: “TYPE_CNUM”,
“position”: 4
},
{
“token”: “个中”,
“start_offset”: 3,
“end_offset”: 5,
“type”: “CN_WORD”,
“position”: 5
},
{
“token”: “个”,
“start_offset”: 3,
“end_offset”: 4,
“type”: “COUNT”,
“position”: 6
},
{
“token”: “中国人”,
“start_offset”: 4,
“end_offset”: 7,
“type”: “CN_WORD”,
“position”: 7
},
{
“token”: “中国”,
“start_offset”: 4,
“end_offset”: 6,
“type”: “CN_WORD”,
“position”: 8
},
{
“token”: “国人”,
“start_offset”: 5,
“end_offset”: 7,
“type”: “CN_WORD”,
“position”: 9
},
{
“token”: “张”,
“start_offset”: 8,
“end_offset”: 9,
“type”: “CN_CHAR”,
“position”: 10
},
{
“token”: “姓”,
“start_offset”: 9,
“end_offset”: 10,
“type”: “CN_CHAR”,
“position”: 11
},
{
“token”: “在”,
“start_offset”: 10,
“end_offset”: 11,
“type”: “CN_CHAR”,
“position”: 12
},
{
“token”: “中国”,
“start_offset”: 11,
“end_offset”: 13,
“type”: “CN_WORD”,
“position”: 13
},
{
“token”: “国是”,
“start_offset”: 12,
“end_offset”: 14,
“type”: “CN_WORD”,
“position”: 14
},
{
“token”: “大姓”,
“start_offset”: 14,
“end_offset”: 16,
“type”: “CN_WORD”,
“position”: 15
}
]
}

示例二

新建一个名称为accounts的 Index,里面有一个名称为person的 Type。person有三个字段。User、title、desc这三个字段都是中文,而且类型都是文本(text),所以需要指定中文分词器,不能使用默认的英文分词器。
analyzer是字段文本的分词器,search_analyzer是搜索词的分词器
curl -X PUT ‘localhost:9200/accounts’ -d ’
{
“mappings”: {
“person”: {
“properties”: {
“user”: {
“type”: “text”,
“analyzer”: “ik_max_word”,
“search_analyzer”: “ik_max_word”
},
“title”: {
“type”: “text”,
“analyzer”: “ik_max_word”,
“search_analyzer”: “ik_max_word”
},
“desc”: {
“type”: “text”,
“analyzer”: “ik_max_word”,
“search_analyzer”: “ik_max_word”
}
}
}
}
}’
account

新增记录

向指定的索引 /Index/Type 发送 PUT 请求,就可以在 Index 里面新增一条记录。
比如,向/accounts/person发送请求,就可以新增一条人员记录。

$ curl -X PUT ‘localhost:9200/accounts/person/1’ -d ’
{
“user”: “张三”,
“title”: “工程师”,
“desc”: “数据库管理”
}’
newadd
请求路径是/accounts/person/1,最后的1是该条记录的 Id。它不一定是数字,任意字符串(比如abc)都可以。新增记录的时候,也可以不指定 Id,这时要将PUT改成 POST 请求。
$ curl -X POST ‘localhost:9200/accounts/person’ -d ’
{
“user”: “李四”,
“title”: “工程师”,
“desc”: “系统管理”
}’
adds

查看记录

向/Index/Type/Id发出 GET 请求,就可以查看这条记录。

$ curl ‘localhost:9200/accounts/person/1?pretty=true’
query
found字段表示查询成功,_source字段返回原始记录

更新记录

更新记录就是使用 PUT 请求,重新发送一次数据。

$ curl -X PUT ‘localhost:9200/accounts/person/1’ -d ’
{
“user” : “张三”,
“title” : “工程师”,
“desc” : “数据库管理,软件开发”
}’
{
“_index”: “accounts”,
“_type”: “person”,
“_id”: “1”,
“_version”: 2,
“result”: “updated”,
“_shards”: {
“total”: 2,
“successful”: 1,
“failed”: 0
},
“_seq_no”: 1,
“_primary_term”: 1
}

返回所有记录

使用 GET 方法,直接请求【语法:/Index/Type/_search】,就会返回所有记录
示例:http://127.0.0.1:9200/accounts/person/_search
{
“took”: 99,
“timed_out”: false,
“_shards”: {
“total”: 5,
“successful”: 5,
“skipped”: 0,
“failed”: 0
},
“hits”: {
“total”: 2,
“max_score”: 1,
“hits”: [
{
“_index”: “accounts”,
“_type”: “person”,
“_id”: “1”,
“_score”: 1,
“_source”: {
“user”: “张三”,
“title”: “工程师”,
“desc”: “数据库管理,软件开发”
}
},
{
“_index”: “accounts”,
“_type”: “person”,
“_id”: “xO-0jmkBNbAgXcUy_b8l”,
“_score”: 1,
“_source”: {
“user”: “李四”,
“title”: “工程师”,
“desc”: “系统管理”
}
}
]
}
}
took字段表示该操作的耗时(单位为毫秒),timed_out字段表示是否超时,hits字段表示命中的记录

全文搜索

Elastic 的查询非常特别,使用自己的查询语法,要求 GET 请求带有数据体。

$ curl ‘localhost:9200/accounts/person/_search’ -d ’
{
“query” : { “match” : { “desc” : “软件” }}
}’
使用 Match 查询,指定的匹配条件是desc字段里面包含"软件"这个词
https://www.elastic.co/guide/en/elasticsearch/reference/6.6/query-dsl.html
https://www.elastic.co/guide/en/elasticsearch/reference/6.6/query-dsl-match-query.html
Elastic 默认一次返回10条结果,可以通过size字段改变这个设置
{
“query” : { “match” : { “desc” : “管理” }},
“size”: 1
}
通过from字段,指定位移。
示例:从位置1开始(默认是从位置0开始),只返回一条结果
{
“query” : { “match” : { “desc” : “管理” }},
“from”: 1,
“size”: 1
}
match

逻辑运算

如果有多个搜索关键字, Elastic 认为它们是or关系。

$ curl ‘localhost:9200/accounts/person/_search’ -d ’
{
“query” : { “match” : { “desc” : “软件 系统” }}
}’
上面代码搜索的是软件 or 系统。
如果要执行多个关键词的and搜索,必须使用布尔查询。
{
“query”: {
“bool”: {
“must”: [
{ “match”: { “desc”: “软件” } },
{ “match”: { “desc”: “系统” } }
]
}
}
}

删除记录

删除记录就是发出 DELETE 请求。$ curl -X DELETE ‘localhost:9200/accounts/person/1’

API使用

https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安装使用Elasticsearch,您可以按照以下步骤进行操作: 1. 下载并安装Java:Elasticsearch是用Java编写的,因此您需要先安装Java。您可以从Oracle官方网站下载并安装适合您操作系统的Java版本。 2. 下载Elasticsearch:在Elasticsearch官方网站(https://www.elastic.co/downloads/elasticsearch)上下载适合您操作系统的Elasticsearch版本。 3. 解压缩Elasticsearch:将下载的文件解压缩到您选择的目录中。 4. 配置Elasticsearch:进入解压缩后的Elasticsearch目录,找到config文件夹。在该文件夹中,您可以编辑elasticsearch.yml文件以进行配置。您可以根据需要更改默认配置,例如集群名称、网络绑定地址等。 5. 启动Elasticsearch:在命令行中进入Elasticsearch目录,并运行bin/elasticsearch命令来启动Elasticsearch。请确保在启动之前Java已正确配置,并且没有其他进程在占用所需的端口。 6. 验证安装使用curl或浏览器访问http://localhost:9200,您应该能够看到Elasticsearch的版本信息。如果您能成功访问,表示安装成功。 一旦您安装Elasticsearch,您可以使用其RESTful API进行索引、搜索和管理数据。您还可以使用Kibana等工具来可视化和分析数据。请记住,Elasticsearch是一个功能强大而复杂的工具,您可能需要学习更多关于其配置和使用的知识。可以查阅Elasticsearch官方文档以获取更多详细信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值