您的位置:首页 > 其它

安装和使用 elasticSearch、LogStash、IK(二)

2018-01-08 18:18 281 查看
ELK的安装和使用
-IK中文分词
IK的安装

安装 maven

使用IK例子

-IK使用
新增记录

查看

删除

更新

数据查询 accountsperson_search 查询 account person type 的所有结果

全文搜索

逻辑运算

原文链接:https://www.leon0204.com/article/93.html

ELK的安装和使用

-IK中文分词

1. IK的安装

直接使用
plugins
安装 对应版本的
ik


$ ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.1/elasticsearch-analysis-ik-5.5.1.zip 
-> Downloading https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.1/elasticsearch-analysis-ik-5.5.1.zip [=================================================] 100%
-> Installed analysis-ik

# 重启启动 ES ,会自动加载,如果报错
$  /data/elasticsearch-5.5.1$ ./bin/elasticsearch
Exception in thread "main" org.elasticsearch.bootstrap.BootstrapException: java.nio.file.AccessDeniedException: /data/elasticsearch-5.5.1/config/analysis-ik
Likely root cause: java.nio.file.AccessDeniedException: /data/elasticsearch-5.5.1/config/analysis-ik

#是新装的插件文件权限问题, chmod 之后即可解决


2. 安装 maven

apt-get install aptitude
sudo aptitude install maven

$ find / -name "IKAnalyzer.cfg.xml"
/data/elasticsearch-5.5.1/config/analysis-ik/IKAnalyzer.cfg.xml


3. 使用IK例子

这里新建了一个名为 accounts 的 Index ,里面有个名称为 person 的 type ,有3个字段

user    用户名
title   标题
desc    描述
这三个中文字段,指定了中文分词, analyzer。
analyzer是字段文本的分词器
search_analyzer是搜索词的分词器
ik_max_word分词器是插件ik提供的,可以对文本进行最大数量的分词。

root@b0c170e13e44:~# 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"
>         }
>       }
>     }
>   }
> }'

# 返回结果
{"acknowledged":true,"shards_acknowledged":true}


-IK使用

本页的代码复制使用时,请把注释去掉,会影响执行

1 新增记录

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

curl -X PUT 'localhost:9200/accounts/person/1' -d '
{
"user": "张三",
"title": "工程师",
"desc": "数据库管理"
}'

#返回结果
{
"_index": "accounts",
"_type": "person",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}


这里如果 Index 不存在,那么也会执行,会创建Index

root@b0c170e13e44:~# curl -X POST 'localhost:9200/accounts/person' -d '
> {
>   "user": "李四",
>   "title": "工程师",
>   "desc": "系统管理"
> }'

#返回结果
{
"_index": "accounts",
"_type": "person",
"_id": "AWDFSjYm4DfKqktV2Xfm",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}


2 查看

root@b0c170e13e44:~# curl 'localhost:9200/accounts/person/1?pretty=true'
{
"_index" : "accounts",
"_type" : "person",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"user" : "张三",
"title" : "工程师",
"desc" : "数据库管理"
}
}
# id 不对 查不到  found 字段为 false
root@b0c170e13e44:~# curl 'localhost:9200/accounts/person/10?pretty=true'
{
"_index" : "accounts",
"_type" : "person",
"_id" : "10",
"found" : false
}


3 删除

curl -X DELETE 'localhost:9200/accounts/person/1’


4 更新

$    curl -X PUT 'localhost:9200/accounts/person/1' -d '
{
"user" : "leon0204",
"title" : "工程师",
"desc" : "数据库管理,软件开发"
}'

{
"_index": "accounts",
"_type": "person",
"_id": "1",
"_version": 4,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}


5 数据查询 /accounts/person/_search 查询 account person type 的所有结果

root@b0c170e13e44:~# curl 'localhost:9200/accounts/person/_search'
{
"took": 46, # 花费的时间
"timed_out": false, # 花费时间是否超时
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
# 返回匹配结果
"hits": {
"total": 2, # 总条数
"max_score": 1,#最高的匹配条数
"hits": [#返回的记录组成的数组
{
"_index": "accounts",
"_type": "person",
"_id": "AWDFSjYm4DfKqktV2Xfm",
"_score": 1,# 表示匹配程度,默认按这个字段降序排列
"_source": {
"user": "李四",
"title": "工程师",
"desc": "系统管理"
}
},
{
"_index": "accounts",
"_type": "person",
"_id": "1",
"_score": 1,
"_source": {
"user": "leon0204",
"title": "工程师",
"desc": "数据库管理,软件开发"
}
}
]
}
}


6 全文搜索

root@b0c170e13e44:~# curl 'localhost:9200/accounts/person/_search'  -d '
{
"query" : { "match" : { "desc" : "软件" }}
} '

# 查询含有软件的 默认返回10条
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.6189728,
"hits": [
{
"_index": "accounts",
"_type": "person",
"_id": "1",
"_score": 0.6189728,
"_source": {
"user": "leon0204",
"title": "工程师",
"desc": "数据库管理,软件开发"
}
}
]
}
}

root@b0c170e13e44:~# curl 'localhost:9200/accounts/person/_search'  -d '
{
"query" : { "match" : { "desc" : "软件" }},
"from": 1,
"size": 1
} '


7 逻辑运算

默认是认为是or 的也就是 或

curl 'localhost:9200/accounts/person/_search'  -d '
{
"query" : { "match" : { "desc" : "软件 系统" }}
}’


查询软件 或者 系统 的 记录Document

desc

系统管理

数据库管理,软件开发

user

李四

leon0204

curl 'localhost:9200/accounts/person/_search'  -d '
{
"query": {
"bool": {
"must": [
{ "match": { "desc": "软件" } },
{ "match": { "desc": "系统" } }
]
}
}
}

很明显 空的
#换一个 与 的
curl 'localhost:9200/accounts/person/_search'  -d '
{
"query": {
"bool": {
"must": [
{ "match": { "desc": "软件" } },
{ "match": { "desc": "数据" } }
]
}
}
}’
查到了 数据库管理,软件开发
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.2379456,
"hits": [
{
"_index": "accounts",
"_type": "person",
"_id": "1",
"_score": 1.2379456,
"_source": {
"user": "leon0204",
"title": "工程师",
"desc": "数据库管理,软件开发"
}
}
]
}
}


参考:阮老师的博客
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  IK ElasticSearch