全文检索elasticsearch和IK中文分词器

全文检索elasticsearch(已包含分布式) 运行elasticsearch服务器,项目中引入架包,set数据,get数据。其实elasticsearch就是这么简单,许多牛人能写出好几本书那是很有水平的,我就会把这些复杂的,抽丝剥茧成简单的,再深入分析。

https://www.elastic.co/downloads/elasticsearch下载完成之后解压文件

tar -zxvf elasticsearch-2.3.3.tar.gz 
 
 
  • 1

    启动ElasticSearch命令:

    ./elasticsearch-2.3.3/bin/elasticsearch
     
     
    • 1
    • 1
    默认情况下, Elasticsearch 使用 9200 来提供对其 REST API 的访问。 访问http://127.0.0.1:9200/
    {
      "name" : "Obliterator",
      "cluster_name" : "elasticsearch",
      "version" : {
        "number" : "2.3.3",
        "build_hash" : "218bdf10790eef486ff2c41a3df5cfa32dadcfde",
        "build_timestamp" : "2016-05-17T15:40:04Z",
        "build_snapshot" : false,
        "lucene_version" : "5.5.0"
      },
      "tagline" : "You Know, for Search"
    }
      
      
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    安装head 插件,在浏览器输入地址:http://localhost:9200/_plugin/head/ 即可调用 head 插件 查看集群状态、节点信息、做查询等等。

    安装head插件命令:

    ./elasticsearch-2.3.3/bin/plugin install mobz/elasticsearch-head
        
        
    • 1
    • 1

    安装IK分词器

    (a).首先使用Git clone命令下载IK分词器源码

    git clone https://github.com/medcl/elasticsearch-analysis-ik.git
     
     
    • 1
    • 1

    也可以直接访问github地址(https://github.com/medcl/elasticsearch-analysis-ik)点击右侧Clone or download按钮,然后Download ZIP直接下载.

    (b.)解压下载的elasticsearch-analysis-ik-master.zip.

    unzip elasticsearch-analysis-ik-master.zip
     
     
    • 1
    • 1

    (c.)使用maven打包 
    确保系统已经安装maven,使用mvn -version命令查看是否已经安装maven.如果没有安装,可以根据系统选择安装方法,比如mac OS系统可以使用brew install maven命令完成安装. 
    进入ik分词器的下载目录,运行命令:

    mvn package
     
     
    • 1
    • 1

    打包完成以后可以看到根目录下多出一个target文件夹. 
    (d.) 配置Ik插件 
    elasticsearch-2.3.3/plugins/目录下新建名为ik的文件夹.把elasticsearch-analysis-ik-master/target/releases 
    /elasticsearch-analysis-ik-1.9.3.zip解压,把解压后的所有文件拷贝到elasticsearch-2.3.3/plugins/ik/目录下. 
    重新启动es,如果配置正确,不会有异常信息输出。 

    测试Ik分词器 
    1.首先创建一个索引用于测试:

    curl -XPUT localhost:9200/index
      
      
    • 1
    • 1

    2.为索引index创建mapping:

    curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
    {
        "fulltext": {
                 "_all": {
                "analyzer": "ik"
            },
            "properties": {
                "content": {
                    "type" : "string",
                    "boost" : 8.0,
                    "term_vector" : "with_positions_offsets",
                    "analyzer" : "ik",
                    "include_in_all" : true
                }
            }
        }
    }'
    
      
      
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3.测试:

    curl 'http://localhost:9200/index/_analyze?analyzer=ik&pretty=true' -d '
    {
    "text":"中国有13亿人口"
    }'
      
      
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4

    显示结果如下:

    {
      "tokens" : [ {
        "token" : "中国",
        "start_offset" : 0,
        "end_offset" : 2,
        "type" : "CN_WORD",
        "position" : 0
      }, {
        "token" : "国有",
        "start_offset" : 1,
        "end_offset" : 3,
        "type" : "CN_WORD",
        "position" : 1
      }, {
        "token" : "13",
        "start_offset" : 3,
        "end_offset" : 5,
        "type" : "ARABIC",
        "position" : 2
      }, {
        "token" : "亿",
        "start_offset" : 5,
        "end_offset" : 6,
        "type" : "CN_WORD",
        "position" : 3
      }, {
        "token" : "人口",
        "start_offset" : 6,
        "end_offset" : 8,
        "type" : "CN_WORD",
        "position" : 4
      } ]
    }

    索引、类型、文档、字段

    • 索引是ElasticSearch存放数据的地方,可以理解为关系型数据库中的一个数据库。
    • 类型用于区分同一个索引下不同的数据类型,相当于关系型数据库中的表
    • 文档是ElasticSearch中存储的实体,类比关系型数据库,每个文档相当于数据库表中的一行数据。
    • 文档由字段组成,相当于关系数据库中列的属性,不同的是ES的不同文档可以具有不同的字段集合。 
      对比关系型数据库:
    Relational DB -> Databases -> Tables -> Rows -> Columns
    Elasticsearch -> Indices   -> Types  -> Documents -> Fields
     
     
    • 1
    • 2

    创建文档

    以博客内容管理为例,索引名为blog,类型为article,新加一个文档:

    curl -XPUT http://localhost:9200/blog/article/1 -d '
    {
          "id": "1",
          "title": "New version of Elasticsearch released!",
          "content": "Version 1.0 released today!",
          "priority": 10,
          "tags": ["announce", "elasticsearch", "release"]
    }'
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    检索文档

    http://localhost:9200/blog/article/1?pretty
     
     
    • 1
    • 1

    更新文档

    curl -XPOST http://localhost:9200/blog/article/1/_update -d '{
      "script": "ctx._source.content = \"new content\""
    }'
     
     
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3

    删除文档

    curl -XDELETE http://localhost:9200/blog/article/1 


    节点与集群

    ElasticSearch是一个分布式全文搜索引擎,既可以做为一个独立的搜索服务器工作,也可以使用多台服务器同时运行,这样就构成了一个集群(cluster),集群的每一个服务器称为一个节点(node).

    ElasticSearch的强大之处:可以多开几个终端,运行起来集群名相同的节点都在一个集群中。

    分片

    当数据量比较大的时候,受RAM、硬盘容量的限制,同时一个节点的计算能力有限。可以将数据切分,每部分是一个单独的lucene索引,成为分片(shard)。每个分片可以被存储在集群的不同节点上。当需要查询由多个分片构成的索引时,ElasticSearch将查询发送到每个相关的分片,之后将查询结果合并。过程对应用透明,无须知道分片的存在。

    副本

    副本是对原始分片的一个精确拷贝,原始分片成为主分片。对索引的所有操作都直接作用在主分片上,每个主分片可以有零个或多个副分片。主分片丢失,集群可以将一个副分片提升为主的新分片。


    中文api:

    https://es.xiaoleilu.com

    英文api:

    https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html



    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值