全文检索ElasticSearch
jianfly.com 2019-01-31 2728次浏览
下载rpm包,yum安装 yum -y install 包的位置
配置: vim /etc/elasticsearch/elasticsearch.yml
cluster.name: yii2-search #集群名称
node.name: master-1 #节点名称
#path.data #数据存储位置
#path.logs #日志存储位置
netword.host:192.168.199.112 #服务器位置
http.port: 9200 #端口号
启动: service elasticsearch start
浏览器可打开192.168.199.112:9200 查看界面返回状态信息
安装elasticsearch-analysis-ik插件,用于中文分词(git)
有zip包后,放到/use/share/elasticsearch/plugins/目录下
unzip解压到/use/share/elasticsearch/plugins/ik/目录下
重启: service elasticsearch restart
测试
curl -XPOST “http://192.168.119.112:9200/_analyze?analyzer=standard&pretty” -d ‘这是一个商品的标题’
curl -XPOST “http://192.168.119.112:9200/_analyze?analyzer=ik&pretty” -d ‘这是一个商品的标题’
curl -XPOST “http://192.168.119.112:9200/_analyze?analyzer=ik_smart&pretty” -d ‘这是一个商品的标题’
curl -XPOST “http://192.168.119.112:9200/_analyze?analyzer=ik_max_word&pretty” -d ‘这是一个商品的标题’
创建一个索引
createindex.json
{ "settings": { "refresh_interval": "5s", //5s后刷新生效 “number_of_shards”: 1, "number_of_replicas": 0 }, "mappings": { "_default_": { "_all": { “enabled”: true //所有字段生效 } }, "products": { //索引类型 "dynamic": false, //新的不认识的字段忽略 "properties": { //指定哪些字段 "productid": { "type": "long", //长整型 }, "title": { "type": "string", //字符串 "index": "analyzed", //索引类型 "analyzer":"ik" //分词器 }, "descr": { "type": "string", //字符串 "index": "analyzed", //索引类型 "analyzer":"ik" //分词器 } } } } }
curl -XPUT “http://192.168.199.112:9200/imooc_shop” -d ‘@createindex.json’
向索引中添加数据
curl -XPUT “http://192.168.199.112:9200/imooc_shop/products/1” -d ‘{“productid”:1, “title”:”这是一个商品的标题”, “descr”:”这是一个商品的描述”}’
查询
search.json
"query": { "multi_match": { "query": "苹果", "fields": ["title", "descr"] } }, "highlight": { //高亮 “pre_tags”: ["<i class=\"highlight\">"], "post_tags": ["</i>"], "fields": { //高亮内容 "title": {}, "descr": {} } } }
curl _XPOST “http://192.168.199.112:9200/imooc_shop/_search?pretty” id ‘@search.json’
elasticsearch-jdbc 批量导入工具
yii2安装ES拓展
gihub搜索yii2-elasticsearch
php composer.phar require –prefer-dist yiisoft/yii2-elasticsearch
/config/web.php中
'elasticsearch' => [ 'class' => 'yii\elasticsearch\Connection', 'nodes' => [ ['http_address' => '192.168.199.112:9200'], ] ],
product/search方法
public function actionSearch() { $this->layout = "layout2"; $keyword = htmlspecialchars(Yii::$app->request->get("keyword")); //搜索关键词 $highlight = [ "pre_tags" => ["<em>"], "post_tags" => ["</em>"], "fields" => [ "title" => new \stdClass(), //空对象 "descr" => new \stdClass(); ] ]; //高亮 $searchModel = ProductSearch::find()->query([ "multi_match" => [ "query" => $keyword, "fields" => ["title", "descr"] //查询的字段 ], ]); $count = $searchModel->count(); $pageSize = Yii::$app->params['pageSize']['frontproduct']; $pager = new Pagination(['totalCount' => $count, 'pageSize' => $pageSize]); $res = $searchModel->highlight($highlight)->offset($pager->offset)->limit($pager->limit)->all(); $products = []; foreach($res as $result) { $product = Product::findOne($result->productid); $product->title = !empty($result->highlight['title'][0]) ? $result->highlight['title'][0] : $product->title; //替换高亮 $product->descr= !empty($result->highlight['descr'][0]) ? $result->highlight['descr'][0] : $product->descr; $products[] = $product; } return $this->render("index", ['all' => $products, 'pager' => $pager, 'count' => $count]); }
model类
namespace app\models; use yii\elasticsearch\ActiveRecode; class ProductSearch extends ActiceRecode { public function attributes() //定义属性 { return ["productid", "title", "descr"]; } public static function index() //定义索引名称 { return "imooc_shop"; } public static function type() //定义索引类型 { return "products"; } }
增量数据
数据库表中要有创建时间和更新时间两个字段
model类中使用behaviors
use yii\behaviors\TimestampBehavior; public function behaviors() { return [ [ 'class' => TimestampBehavior::className(), 'createdAttribute' => 'createtime', 'updatedAttribute' => 'updatetime', 'attributes' => [ ActiveRecode::EVENT_BEFORE_INSERT => ['createtime', 'updatetime'], ActiveRecode::EVENT_BEFORE_UPDATE => ['updatetime'], ] ] ]; }
jdbc导入工具制作计划任务
删除:将一个字段标识为删除
- 上一篇:基于Redis的邮件异步发送工具
- 下一篇:rbac权限管理