es 一 ---,es---
环境:
系统:centos6.4
elasticsearch:1.5.2
ik:1.5
1.安装elasticsearch
[html] view plain copy
- curl -L -O https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.5.2.tar.gz
- tar -xvf elasticsearch-1.5.2.tar.gz
- cd elasticsearch-1.5.2/bin
2.elasticsearch插件安装
elasticsearch-koaf 通过koaf,可以查看集群几乎所有信息,还能进行简单的搜索查询,观察自动恢复的情况等等。
安装完成之后,在浏览器输入:http://192.168.199.159:9200/_plugin/kopf ,可以查看显示效果
**一定是bin的上级目录执行,要有bin/plugin
[html] view plain copy
- ./elasticsearch/bin/plugin --install lmenezes/elasticsearch-kopf/1.5.4
3.安装ik分词
https://github.com/medcl/elasticsearch-analysis-ik
http://davidbj.blog.51cto.com/4159484/1604393
编译源码成zip压缩包
elasticsearch-analysis-ik-1.3.0.zip
[html] view plain copy
- `git clone https://github.com/medcl/elasticsearch-analysis-ik`
- `cd elasticsearch-analysis-ik`
- `mvn compile`
- `mvn package`
在elasticsearch/plugins目录下创建目录analysis_ik
[html] view plain copy
- cd elasticsearch-1.5.2/plugins
- mkdir analysis-ik
- cd analysis-ik
- unzip elasticsearch-analysis-ik-1.3.0.zip
- rm elasticsearch-analysis-ik-1.3.0.zip
https://github.com/davidbj/elasticsearch-rtf/archive/master.zip
unzip master.zip
将master的解压目录config/ik文件夹压缩成zip
复制到elasticsearch-1.5.2/config目录下
[html]
view plain
copy
- cd elasticsearch-1.5.2/config
- unzip ik.zip
- rm ik.zip
配置elasticsearch.yml的分词
[html] view plain copy
- vim elasticsearch.yml
- cluster.name: boluofan
- index:
- analysis:
- analyzer:
- ik:
- alias: [ik_analyzer]
- type: org.elasticsearch.index.analysis.IkAnalyzerProvider
- ik_max_word:
- type: ik
- use_smart: false
- ik_smart:
- type: ik
- use_smart: true
4.启动 & 关闭 -d 后台运行 -p pid的文件位置
[html] view plain copy
- ./elasticsearch -d -p /tmp/es.pid
- kill `cat /tmp/es.pid`
***如果只启动一个实例的话,那么es只会启动 主分片,不会启动 复制分片,这种情况下可以使用 下列指令查看status是yellow状态的,这就说明不会启动 复制分片
[html] view plain copy
- curl -i -XGET 'http://localhost:9200/_cluster/health'
[html] view plain copy
- status字段提供一个综合的指标来表示集群的的服务状况。三种颜色各自的含义:
green |
所有主要分片和复制分片都可用 |
red |
不是所有的主要分片都可用 |
yellow |
所有主要分片可用,但不是所有复制分片都可用 |
***想要启动复制分片的方法很简单,只需要直接再启动一次,不管是在本地机器还是另一个ip的机器上
只要第二个节点与第一个节点有相同的cluster.name(请看./config/elasticsearch.yml文件),它就能自动发现并加入第一个节点所在的集群。
如果没有,检查日志找出哪里出了问题。这可能是网络广播被禁用,或者防火墙阻止了节点通信。
[html] view plain copy
- ./elasticsearch -d -p /tmp/es_share.pid
**这个时候再使用命令查看es的健康状态。就会发现status变成green
[html] view plain copy
- curl -i -XGET 'http://localhost:9200/_cluster/health'
5.es概念,具体参考elasticsearch权威指南:http://es.xiaoleilu.com/010_Intro/25_Tutorial_Indexing.html
[html] view plain copy
- Index
- 可以把 ElasticSearch 的 Index 理解为 MySQL 的一个数据库。
- Type
- 可以理解为 MySQL 中的一个表
- Document
- ElasticSearch 中的一个 document 相当于 MySQL 某个表中的一条记录。它是 JSON 结构。
- Mapping
- 可以理解为 MySQL 的表结构(table schema)
- Cluster
- 很多个 ElasticSearch 的节点(node)可以构成一个分布式的集群(cluster)。
- Shard
- 每个 index 的数据默认切分到 5 个 shards 中。
- 有点像 MongoDB,也有点像 MySQL 表的水平分割。
6.测试: 1.创建索引 [html] view plain copy
- curl -XPUT http://localhost:9200/index
2.创建type映射
[html] view plain copy
- curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
- {
- "fulltext": {
- "_all": {
- "indexAnalyzer": "ik",
- "searchAnalyzer": "ik",
- "term_vector": "no",
- "store": "false"
- },
- "properties": {
- "content": {
- "type": "string",
- "store": "no",
- "term_vector": "with_positions_offsets",
- "indexAnalyzer": "ik",
- "searchAnalyzer": "ik",
- "include_in_all": "true",
- "boost": 8
- }
- }
- }
- }'
3.创建索引记录document
[html] view plain copy
- curl -XPOST http://localhost:9200/index/fulltext/1 -d'
- {"content":"美国留给伊拉克的是个烂摊子吗"}
- '
- curl -XPOST http://localhost:9200/index/fulltext/2 -d'
- {"content":"公安部:各地校车将享最高路权"}
- '
- curl -XPOST http://localhost:9200/index/fulltext/3 -d'
- {"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
- '
- curl -XPOST http://localhost:9200/index/fulltext/4 -d'
- {"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
- '
4.查询并且高亮关键字
[html] view plain copy
- curl -XPOST http://localhost:9200/index/fulltext/_search -d'
- {
- "query" : { "term" : { "content" : "中国" }},
- "highlight" : {
- "pre_tags" : ["<tag1>", "<tag2>"],
- "post_tags" : ["</tag1>", "</tag2>"],
- "fields" : {
- "content" : {}
- }
- }
- }
- '
5。查询结果
[html] view plain copy
- {
- "took": 14,
- "timed_out": false,
- "_shards": {
- "total": 5,
- "successful": 5,
- "failed": 0
- },
- "hits": {
- "total": 2,
- "max_score": 2,
- "hits": [
- {
- "_index": "index",
- "_type": "fulltext",
- "_id": "4",
- "_score": 2,
- "_source": {
- "content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
- },
- "highlight": {
- "content": [
- "<tag1>中国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 "
- ]
- }
- },
- {
- "_index": "index",
- "_type": "fulltext",
- "_id": "3",
- "_score": 2,
- "_source": {
- "content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
- },
- "highlight": {
- "content": [
- "均每天扣1艘<tag1>中国</tag1>渔船 "
- ]
- }
- }
- ]
- }
- }