美文网首页
elasticsearch + Kinaba + elastic

elasticsearch + Kinaba + elastic

作者: 茶艺瑶 | 来源:发表于2017-11-04 15:16 被阅读388次

    Elasticsearch 在公司的使用越来越广,很多同事之前并没有接触过 Elasticsearch,所以,最近在公司准备了一次关于 Elasticsearch 的分享,整理成此文。此文面向 Elasticsearch 新手,老司机们可以撤了。

    Elasticsearch 是一个分布式的搜索和分析引擎,可以用于全文检索、结构化检索和分析,并能将这三者结合起来。Elasticsearch 基于 Lucene 开发,现在是使用最广的开源搜索引擎之一,Wikipedia、Stack Overflow、GitHub 等都基于 Elasticsearch 来构建他们的搜索引擎。

    在国内还有一个xunsearch占用资源比较低,他只有搜索的功能,分析引擎要自己写。在国内用的企业也比较多,比如小米等一些手机官网都用这个,比较使用的起来比较简单。可以使用mysql
    Elasticsearch 就比较吃资源了,如果你的电脑没有2G的话,拿就不好意思了,我在虚拟上运行也要开2G才能运行这个程序
    还有本文中的 Elasticsearch 使用的是Elasticsearch 5 你的JAVA SDK 必须为 JAVA SDK 8
    下面的全是笔记

    流程图

    github elasticsearch-rtf
    github elasticsearch-head
    
    elasticsearch配置
    编辑 config/elasticsearch.yml
    http.cors.enabled: true
    http.cors.allow-origin: "*"
    http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
    http.cors.allow-headers: "X-Requested-With,Content-Type,Content-Length,X-User"
    
    #安装kibana
    

    集群:一个或者多个节点组织在一起

    节点:一个节点是集群中的一个名字来标识,默认是一个随机的漫画角色的名字

    分片:将索引划分为多份的能力,允许水平分割和扩展容量,多个分片相应请求,提高性能和吞吐量

    副本:创建分片的一份或者多份的能力,在一个节点失败其余节点可以顶上

    倒排索引待解决的问题

    1. 大小写转换问题,入python和PYTHON应该为一个词
    2. 词干抽取,looking和look应该为处理的一词
    3. 分词,若屏蔽系统 应该分词为“屏蔽”、“系统”还是为了“屏蔽系统”
    4. 倒排索引文件过大 - 压缩编码

    安装elasticsearch-rtf(中文集成包)

    
    git clone https://github.com/medcl/elasticsearch-rtf
    #具体操作请看 https://github.com/medcl/elasticsearch-rtf
    
    #注意elasticsearch默认是不支持使用root运行的
    so you must to do this:
        groupadd elsearch
        useradd elsearch -g elsearch -p elasticsearch
        
    next:
        su elsearch
    

    看到此图就为成功,我已经是做了nginx 的方向代理了,在本机使用host访问到虚拟机中

    Paste_Image.png

    安装elasticsearch-head(elasticsearch一个管理工具)

    具体操作看
        https://github.com/mobz/elasticsearch-head
    主意的是:这里要使用到node.js 最好使用node.js 6 左右的版本 很多linux的包都是4的,node 4 的api 比较弱
    
    还有就是一定要用cnpm 下载 不是在下载 phantomjs的时候超级慢,npm yarn 都卡在哪里
    
    还有就是使用docker 安装啦,最简单最安全
    
    注意!!!
        如果你在linux上面请不要第一种方法!
        有依赖不能在linux上运行,和laravel的前端开发框架一样 fsevents
    
    Paste_Image.png Paste_Image.png

    Kibana 安装 (elasticsearch 一个操作工具)

    必须对应elasticsearch的版本,怎么看版本 回去上面看看
    
    
    wget https://artifacts.elastic.co/downloads/kibana/kibana-5.1.2-linux-x86_64.tar.gz
    
    tar -xvf 解压
    cd 进去
    /bin/kibana
    运行就好了
    
    如果觉得慢的话
    可以直接使用百度云下载喔
    链接:http://pan.baidu.com/s/1kVxLaMr 密码:7ufu
    然后用FZ工具上传上去解压就好了
    
    Paste_Image.png

    Kibana 对 elasticsearch的操作

    #es  de 的文档、索引的CRUD操作
    #索引初始化操作
    #指定分片和副本的数量
    #shards一旦设置不能修改
    PUT lagou
    {
        "settings":{
            "index":{
                "number_of_shards":5,
                "number_of_replicas":1
            }
        }
    }
    
    GET lagou/_settings
    GET _all/_settings
    GET .kibana,lagou/_settings
    GET _setting
    
    #更新settings
    PUT lagou/_settings{
        "number_of_replicas":2
    }
    
    #获取全部的索引
    GET _all
    GET lagou
    
    #保存文档
    PUT lagou/job/
    {
        "title":"python分布式爬虫开放",
        "salary_min":15000,
        "city":"北京",
        "company":{
            "name":"百度",
            "company_addr":"北京市软件园"
        },
        "publish_date":"2017-4-16",
        "comments":15
    }
    
    GET lagou/job/1
    GET lagou/job/1?_source=title
    GET lagou/job/1?_source=title,city
    
    POST lagou/job/1/_update{
        "doc":{
            "comments":20
        }
    }
    
    #删除
    DELETE lagou/job/1
    DELETE lagou/job
    DELETE lagou
    
    

    批量操作

    GET _mget
    
    GET testdb/job1/_mget
    {
        "docs":[
            {
                "_id":1
            },{
                "_id":2
            }
        ]
    }
    
    GET testdb/_mget{
        "docs":{
            "_id":[1,2]
        }
    }
    
    #bulk批量操作
    POST _bulk 指明数据
    

    映射(mapping)

    作用:会让索引建立的更加完善
    类型:静态映射和动态映射
    string:text ,keyword(string类型在es5开始已经废除)

    数字类型:long,integer,short,byte,double,float
    日期:date
    bool:boolean
    binary类型:binary
    obiject,nested
    geo-opingt,geo-shape
    ip,competion
    

    属性
    store
    index
    null_value
    analyzer
    include_in_all
    format

    搜索

    elasticsearch-ik 索引要有 ik_max_word
    
    #match查询
    GET lagou/_search
    {
        "query":{
            "match":{
                "title":"python"
            }
        }
    }
    
    #term查询 不分词
    GET lagou/_search
    {
        "query":{
            "term":{
                "title":"python"
            }
        }
    }
    
    #terms查询 不分词
    GET lagou/_search
    {
        "query":{
            "terms":{
                "title":["python","工程师","系统","django"]
            }
        }
    }
    
    #控制查询返回数量
    GET lagou/_search
    {
        "query":{
            "match":{
                "title":"python"
            },
            "from":0,
            "size":2
        }
    }
    
    #match_all 查询
    
    GET lagou/_search
    {
        "query":{
            "match_all":{},
            "from":0,
            "size":2
        }
    }
    
    

    基本教程这里结束了

    最好要解决的就是怎么同步mysql 的数据到 elasticsearch,为什么这么说呢!我一开始也不知道的,后面谷歌了之后才发现原理elasticsearch也就是一个数据库的存在

    笔者还没有试过 github 使用 elasticsearch-jdbc比较多
    http://www.cnblogs.com/zhongshengzhen/p/elasticsearch_mysql.html
    这里是传送门

    然后就看你们的程序语言对elasticsearch的支持程度
    在laravel(PHP) 上laravel-search 是mmanos出的

    composer require mmanos/laravel-search dev-master
    

    使用传送门

    http://laravelacademy.org/post/3781.html
    

    相关文章

      网友评论

          本文标题:elasticsearch + Kinaba + elastic

          本文链接:https://www.haomeiwen.com/subject/nqgfmxtx.html