安装
Elastic 需要 Java 8 环境。
安装完 Java,就可以跟着官方文档安装 Elastic。直接下载压缩包比较简单。
[root@localhost] wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gz
[root@localhost] tar -zxvf elasticsearch-6.2.3.tar.gz
[root@localhost] mv elasticsearch-6.2.3 /usr/local/es
接着,进入 es 的目录,运行下面的命令,启动 es。
[root@localhost] ./bin/elasticsearch
es 默认本地访问,正式环境可以改成内网。 为方便测试可以先改成 0.0.0.0 允许公网访问。
vi elasticsearch.yml
network.host: 0.0.0.0
错误解决
1.root 启动错误
can not run elasticsearch as root
问题原因:es 默认不允许 root 账号启动
解决方法:
方案一:在执行 elasticSearch 时加上参数-Des.insecure.allow.root=true,完整命令如下
./bin/elasticsearch -Des.insecure.allow.root=true
方案二:编辑 elasicsearch 文件,增加
ES_JAVA_OPTS="-Des.insecure.allow.root=true"
最好不要用 root 用户启动
2.用户权限错误
main ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
步骤一: 可以看下 config 文件夹是否属于启动 es 的用户
步骤二: chown -R 775 config(给 config 文件夹读写权限)
步骤三: 检查启动命令,不要在 bin 文件夹内启动 es。已这样的路径运行 ./bin/elasticsearch
3.ulimit 错误
[1]: max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
解决方法:修改 ulimit 打开文件数
4.进程数错误
[1]: max number of threads [2048] for user [es] is too low, increase to at least [4096]
解决方法:修改系统的打开进程数 vi /etc/security/limits.d/90-nproc.conf
5.SecComp 检查错误
[1]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
问题原因:因为 Centos6 不支持 SecComp
解决方法:在 elasticsearch.yml 中配置 bootstrap.system_call_filter 为 false,注意要在 Memory 下面可以加在底部
使用
Elastic 就会在默认的 9200 端口运行。这时,打开另一个命令行窗口,请求该端口,会得到说明信息。
{
"name": "qv81z4g",
"cluster_name": "elasticsearch",
"cluster_uuid": "oBS8HzHgRC6kcfs_Uh_Luw",
"version": {
"number": "6.2.3",
"build_hash": "c59ff00",
"build_date": "2018-03-13T10:06:29.741383Z",
"build_snapshot": false,
"lucene_version": "7.2.1",
"minimum_wire_compatibility_version": "5.6.0",
"minimum_index_compatibility_version": "5.0.0"
},
"tagline": "You Know, for Search"
}
索引创建
创建索引 articles _type:article 增加两个映射
title 关键字类型
create_time long 类型
curl -X PUT 'http://localhost:9200/articles/' -d '
{
"mappings": {
"article": {
"properties": {
"title": {
"type": "keyword"
},
"create_time": {
"type": "long"
}
}
}
}
}'
Response:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "articles"
}
索引查询
curl http://localhost:9200/articles/
Response:
{
"articles": {
"aliases": {},
"mappings": {
"article": {
"properties": {
"create_time": {
"type": "long"
},
"title": {
"type": "keyword"
}
}
}
},
"settings": {
"index": {
"creation_date": "1523172764380",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "iiQlEh5ARhihKWn2NF-JcA",
"version": {
"created": "6020399"
},
"provided_name": "articles"
}
}
}
}
创建文档
curl -X PUT 'http://localhost:9200/articles/' -d '{"title":"画江湖之不良人?","create_time":1523173408}'
Response:
{
"_index": "articles",
"_type": "article",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
单条查询
curl http://localhost:9200/articles/article/1
Response:
{
"_index": "articles",
"_type": "article",
"_id": "1",
"_version": 3,
"found": true,
"_source": {
"title": "画江湖之不良人?",
"create_time": 1523123408
}
}
简单分页查询
size: 每页大小
from: 开始位置
curl http://localhost:9200/articles/article/_search?size=20&from=0
Response:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "articles",
"_type": "article",
"_id": "1",
"_score": 1,
"_source": {
"title": "画江湖之不良人?",
"create_time": 1523173408
}
}
]
}
}
简单精准匹配
curl -X POST 'localhost:9200/articles/article/_search' -d '
{
"query": {
"match": {
"title": "画江湖之不良人?"
}
}
}'
Response:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "articles",
"_type": "article",
"_id": "1",
"_score": 0.2876821,
"_source": {
"title": "画江湖之不良人?",
"create_time": 1523173408
}
}
]
}
}
通配符匹配
curl -X POST 'localhost:9200/articles/article/_search' -d '
{
"query": {
"wildcard": {
"title": "*之*"
}
}
}'
Response:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "articles",
"_type": "article",
"_id": "1",
"_score": 1,
"_source": {
"title": "画江湖之不良人?",
"create_time": 1523173408
}
}
]
}
}
简单字段排序
curl -X POST 'localhost:9200/articles/article/_search' -d '
{
"sort": { "create_time": { "order": "desc" }}
}'
Response:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "articles",
"_type": "article",
"_id": "3",
"_score": null,
"_source": {
"title": "画江湖之不良人3?",
"create_time": 1523175608
},
"sort": [
1523175608
]
},
{
"_index": "articles",
"_type": "article",
"_id": "2",
"_score": null,
"_source": {
"title": "画江湖之不良人2?",
"create_time": 1523173608
},
"sort": [
1523173608
]
},
{
"_index": "articles",
"_type": "article",
"_id": "1",
"_score": null,
"_source": {
"title": "画江湖之不良人?",
"create_time": 1523173408
},
"sort": [
1523173408
]
}
]
}
}
官方文档
es 十分强大更多功能请查看官方文档介绍。
https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
中文分词
可以安装 ik 插件
https://github.com/medcl/elasticsearch-analysis-ik
按步骤安装即可
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于