elasticsearch环境搭建与使用

2 篇文章 0 订阅

转载:http://www.cnblogs.com/cswuyg/p/5651620.html

http://shensuqiao.iteye.com/blog/2251674

https://es.xiaoleilu.com/010_Intro/10_Installing_ES.html


Elasticsearch是一个基于Lucene构建的开源、分布式、RESTful的搜索引擎,能够实现近实时(NRT)搜索,稳定、可靠、安装方便。性能不错、水平扩展、文档齐全、社区火爆,这几点很重要。

如果之前已经了解过分布式存储系统、query切词、检索相关性策略,Elasticsearch的使用学习起来会很快。

1 基础概念

Elasticsearch是一个近实时的系统,从你写入数据到数据可以被检索到,一般会有1秒钟的延时。Elasticsearch是基于Lucene的,Lucene的读写是两个分开的句柄,往写句柄写入的数据刷新之后,读句柄重新打开,这才能读到新写入的数据。

名词解释:

Cluster:集群。

Index:索引,Index相当于关系型数据库的DataBase。

Type:类型,这是索引下的逻辑划分,一般把有共性的文档放到一个类型里面,相当于关系型数据库的table。

Document:文档,Json结构,这点跟MongoDB差不多。

Shard、Replica:分片,副本。

分片有两个好处,一个是可以水平扩展,另一个是可以并发提高性能。在网络环境下,可能会有各种导致分片无法正常工作的问题,所以需要有失败预案。ES支持把分片拷贝出一份或者多份,称为副本分片,简称副本。副本有两个好处,一个是实现高可用(HA,High Availability),另一个是利用副本提高并发检索性能。

分片和副本的数量可以在创建index的时候指定,index创建之后,只能修改副本数量,不能修改分片。

2 搭建ElasticSearch

首先安装java,设置好JAVA_HOME环境变量(export JAVA_HOME=.../java8),然后安装Elasticsearch。 可选用elasticsearch-2.3.0对应jdk7

参考官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/_installation.html

设置配置的时候,ES可能因为各种原因不能自动找到集群,所以把地址也设置上,如:

discovery.zen.ping.unicast.hosts: ["host_name...:9301", "host_name_xxx:port_yyy"...]

安装head插件:拉取 https://github.com/mobz/elasticsearch-head 代码,将其放到./plugins/head 目录下。

启动之前设置ES使用的内存:export ES_HEAP_SIZE=10g。

elasticsearch-.yml(中文配置详解)

mater例子入下

cluster.name: my-application    ##cluster名
node.name: "node-1"    ##节点名称
node.master: true   ##是否是master节点
node.data: true   ##该节点上是否保存数据
index.number_of_replicas: 1   ##备份的数量,这里设为1
path.data: /opt/esdata    ##该节点上数据存储的path
transport.tcp.port: 9300   ##tcp的端口号
http.port: 9200    ##http的端口号
discovery.zen.minimum_master_nodes: 1
discovery.zen.ping.timeout: 3s    ##节点间自动发现的响应时间
discovery.zen.ping.unicast.hosts: ["localhost"]    ##节点间自动发现,master节点为localhost


node2

cluster.name: my-application     ##cluster名,注意和master节点保持一致
node.name: "node-2"
node.master: false   ##不是master节点
node.data: true
index.number_of_replicas: 1
path.data: /opt/esdata
transport.tcp.port: 9301
http.port: 9201
discovery.zen.minimum_master_nodes: 1
discovery.zen.ping.timeout: 3s    ##节点间自动发现的响应时间
discovery.zen.ping.unicast.hosts: ["localhost:9300"]  


elasticsearch -d 以守护进程方式启动,启动之后,就可以在浏览器里使用head插件看到集群信息,如:

http://host_name_xxx:port_yyy/_plugin/head/


3 测试Elasticsearch使用

先创建一个index:

POST http://127.0.0.1:9300/test_index

查看创建的index:

GET  http://127.0.0.1:9300/_cat/indices?v

写入数据:

查询数据:

(1)使用id直接查:
GET http://xxxhost:9300/qa_xx2/qa_xx3/1235
(2)DSL查询:

往查询url POST数据即可:
URL格式:http://xxxhost:9300/qa_xx2/qa_xx3/_search

a. 查询title中包含有cswuyg字段的文档。Highlight设置高亮命中的词。POST方法的body:

{
    "query": {
        "match": {
            "title": {
                "query": "cswuyg "
            }
        }
    },
    "highlight": {
        "fields": {
            "title": {
                
            }
        }
    }
}

b. bool组合查询,命中的文档的title字段必须能命中“餐厅”、“好吃”、“深圳”,可以是完全命中,也可以是名字其中的个别字。“便宜”则是可选命中。
POST方法的body:

{
    "query": {
        "bool": {
            "must": [{
                "match": {
                    "title": {
                        "query": "餐厅"
                    }
                }
            },
            {
                "match": {
                    "title": {
                        "query": "好吃"
                    }
                }
            },
            {
                "match": {
                    "title": {
                        "query": "深圳"
                    }
                }
            }],
            "should": [{
                "match": {
                    "title": "便宜"
                }
            }]
        }
    },
    "highlight": {
        "fields": {
            "title": {
                
            }
        }
    }
}
c. 给查询词设置权重(boost)。POST方法的body:

{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "title": {
                        "query": "好吃的餐厅",
                        "boost": 1
                    }
                }
            },
            "must": {
                "match": {
                    "title": {
                        "query": "深圳湾",
                        "boost": 100
                    }
                }
            },
            "should": [{
                "match": {
                    "title": "便宜"
                }
            }]
        }
    },
    "highlight": {
        "fields": {
            "title": {
                
            }
        }
    }
}




 4 使用ik中文切词插件

Elasticsearch默认的中文切词插件是单字切词,这不能满足我们要求,需要安装中文切词插件。

插件github地址:https://github.com/medcl/elasticsearch-analysis-ik
源码安装:编译时需要联网,可以在windows下编译完之后,把elasticsearch-analysis-ik-1.9.3.zip拷贝到linux机器的./plugin/head目录下解压。
配置:在配置文件./config/elasticsearch.yml末尾添加配置: index.analysis.analyzer.ik.type: "ik"

测试ik切词:http://host_name_xx:port_yyy/qa_pair/_analyze?analyzer=ik&pretty=true&text=我是中国人"

 部分学习资料

官方入门文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html

使用ik中文分词插件:http://blog.csdn.net/liuzhenfeng/article/details/39404435

提升性能:http://zhousheng29.iteye.com/blog/2101905

Elasticsearch有哪些用户:https://www.elastic.co/use-cases

6 同步MYSQL数据 部学习资料

http://blog.csdn.net/laoyang360/article/details/51694519

http://www.cnblogs.com/phpshen/p/6098333.html

http://www.cnblogs.com/mignet/p/MySQL_ElasticSearch_sync_By_logstash_jdbc.html

http://www.cnblogs.com/phpshen/p/6098333.html

http://blog.csdn.net/kingice1014/article/details/53492917


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值