Elasticsearch 全文搜索引擎
分布式 多用户 RESTful接口 java开发服务 支持中文分词搜索 默认配置文件内存4G
安装 Elasticsearch
1.安装java环境(JDK)
$yum install -y java //yum 安装JDK
$java -version //测试安装
2.下载和安装Elasticsearch 官网找RPM包
$wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.0-x86_64.rpm
$rpm -ivh elasticsearch-7.6.0-x86_64.rpm
$systemctl start elasticsearch.service //启动——时间比较长 or $service elasticsearch start
$curl localhost:9200 //启动检测
$netstat -apn | grep 9200 //检测端口
$server elasticsearch status //查看操作状态
$cd /etc/elasticsearch/jvm.options //配置文件 出错应添加 -Xms256m 分配内存大小 --XX:AssumeMP
3.下载安装kibana 图示化操作 Elasticsearch
$wget https://artifacts.elastic.co/downloads/kibana/kibana-7.6.0-x86_64.rpm
$rpm -ivh kibana-7.6.0-x86_64.rpm //安装
$vim /etc/kibana/kibana.yml //修改kibana相关配置
server.host "0.0.0.0" //任意主机访问
lasticsearch: "http://127.0.0.1:9200"
$/usr/share/kibana/bin/kibana //启动kibana 启动比较慢
通过 http://域名:5610访问
4.基本概念
index(类似数据库) document(json数据) type即将废弃(对document分组)
kibana->Dev Toohs->console 中操作
GET /_cat/health?v //集群状况
GET /_cat/nodes?v //节点状态
GET /_cat/indices?v //列出所有索引
5.创建索引
put /goods //创建索引
delete /goods //删除索引
6.Document文档操作
创建
PUT /customer/_doc/1?pretty{"name":"zhangsan"} // 手动指定ID customer索引,_doc type,1为ID,pretty返回结果json格式
POST /customer/_doc/{"name":"lisi"} //生成随机ID,唯一
查询
GET /customer/_doc/1?pretty
修改
PUT /customer/_doc/1{"name":"zhangshan1"} //完整字段更新,否则数据丢失
POST /customer/_doc/1/_update{"doc":{"name","zhangsan2"}} //只更新提交的字段
删除
DELETE /customer/_doc/1
批量添加
POST /customer/_doc/_bulk{"index":{"_id":2}}{"name":"wangwu"}{"index":{"_id":3}}{"name":"wangwu1"}
批量修改
POST /customer/_doc/_buck{"update:{"_id":2}"}{"doc":"name":"zhaoku"}{"delete":{"_id":3}}
获取多条数据
GET /customer/_doc/_mget{"ids":[1,2]}
搜索数据
全部商品
GET /goods/_search
条件搜索
GET /goods/_search?q=title:xiaomi5
请求体查询 完全匹配
GET /goods/_search{"query":{"match":{"title":"xiaomi5"}}}
高级搜索
同时满足
GET /goods/_search{"query"{"bool":{"must":[{"match":{"des":}}]}}}
满足一个
GET /goods/_search{"query"{"bool":{"should":[{"match":{"des":}}]}}}
7.安装中文分词插件 IK 标准分词器:把中文的词分成字匹配 出现使用问题
网址: https://github.com/medcl/elasticsearch-analysis-ik
$find / -name elasticsearch-plugin //查找elasticsearch-plugin安装路径
下载及安装同时(注意对应版本) $路径/elasticsearch-plugin install https://github.com/elasticsearch-analysis-ik-6.3.0
$service elasticsearch restart //重启服务
8.中文分词插件 设置查询索引
"analyzer":"ik_max_word"
"search_analyzer":"ik_max_word"
GET /tv/_analyze{"text":["索尼电视"]} //查看分词器情况
GET /tv/_analyze{
"tokenizer":"ik_max_word" //查询指定分词器 默认粒度按字查询
"text":["索尼电视"]
}
//分词匹配 索尼 电视 完全匹配
GET /tv/_search
{
"query":{
"match_phrase":{
"name":{
"query":"索尼电视", // 索尼 电视 匹配
"slop":20 //间隔多少查询两个词的匹配
}
}
}
}
9.laravel5.5 使用elasticsearch.yml 可以修改配置设置访问
laravel composer安装
composer require elasticsearch/elasticsearch:~6.0
1.初始化操作创建索引
$host = ['192.168.1.168:9200'];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$params = [
'index'=>'product',
'body'=>[
'setings'=>[
'number_of_shards'=>1,
'number_of_replicas'=>0
],
'mappings'=>[
'_doc'=>[
'_source'=>[
'enable'=>true
],
'properties'=>[
'title'=>[
'type'=>'text',
'analyzer'=>'ik_max_word',
'search_analyzer'=>'ik_max_word',
],
'description'=>[
'type'=>'text',
'analyzer'=>'ik_max_word',
'search_analyzer'=>'ik_max_word',
]
]
]
]
]
]
];
$response = $client->index($params);
2.添加商品
$host = ['192.168.1.168:9200'];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$params = [
'index'=>'product',
'type'=>'_doc',
'id'=>$id,
'body'=>[
'title'=>$title,
'description'=>$description
]
];
$response = $client->index($params);
3.搜索商品
$where['description'] = [
'query'=>$description
'slop'=>20
];
$params = [
'index'=>'product',
'type'=>'_doc',
'body'=>[
'query'=>[
'match_phrase'=>$where
]
]
];
$host = ['192.168.1.168:9200'];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$client->search($params);
网友评论