ubuntu安装elasticsearch&简单使用
2015-12-21 18:15
447 查看
先安装一下jdk8
点击下载 官网下载地址
2.解压
3.启动
4.简单使用:
$ sudo add-apt-repository ppa:webupd8team/java $ sudo apt-get update $ sudo apt-get install oracle-java8-installer $ java -version
点击下载 官网下载地址
$ wget 'https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/zip/elasticsearch/2.1.1/elasticsearch-2.1.1.zip'
2.解压
unzip elasticsearch-2.1.1.zip
3.启动
bin/elasticsearch
4.简单使用:
require "elasticsearch" require "hashie" $client = Elasticsearch::Client.new hosts: [ 'localhost:9200' ], randomize_hosts: true #创建索引 def create_index $client.indices.create index: 'weibo', body: { settings: { index: { number_of_shards: 10, number_of_replicas: 1 } }, mappings: { weibo: { _all: { analyzer: "ik_max_word", search_analyzer: "ik_max_word", }, properties: { content: { type: 'string', analyzer: 'ik_max_word', search_analyzer: 'ik_max_word' }, tags: { type: 'string', index: "not_analyzed" }, site_user: { type: 'string', index: 'not_analyzed' }, created_time: { type: 'integer' }, created_date: { type: 'string', index: 'not_analyzed' }, } } } } end #添加单条数据 def test_single_index $client.index index: 'weibo', type: 'weibo', id: '1', body: { content: '主持人说,最近周杰伦宣传期接受了一些采访,你知道吗,他说“没有人会不喜欢你.”昆凌听后还在哭[泪] 祝福你和杰伦能永远幸福下去!倒计时14天~', tags: [ '周杰伦', '昆凌' ], site_user: 'BettyHu-', created_time: 1420435348, created_date: '20150105' } end #添加多条数据 def test_bulk_index $client.bulk body:[ { index: {_index: 'weibo', _type: 'weibo', _id: '5', data: { content: '记住,任何人都没有权利剥夺或者讽刺一个人喜欢一个偶像的权利。 ', tags: [ '周杰伦', '偶像' ], site_user: '慕君', created_time: 1420436338, created_date: '20150105' } } }, { index: {_index: 'weibo', _type: 'weibo', _id: '6', data: { content: '周杰伦签售会再三强调歌迷要注意安全,为歌迷连续七八个小时签了一万多本 ', tags: [ '周杰伦', '偶像', '歌迷' ], site_user: '橙_', created_time: 1420436594, created_date: '20150105' } } }, { index: {_index: 'weibo', _type: 'weibo', _id: '7', data: { content: '“听妈妈的话,快快长大。。。”', tags: [ '周杰伦' ], site_user: '眼角飞过', created_time: 1420436058, created_date: '20150105' } } } # { update: { _index: 'weibo', _type: 'weibo', _id: 2, data: { doc: { site_user: '陌_1' } } } }, # { delete: { _index: 'weibo', _type: 'weibo', _id: 3 } } # ... # ... ] end #查询 def test_search # multi index "index1, index2, indexN.." response = $client.search index: 'weibo', body: { query: { match: {content:'周杰伦'} # bool:{ # must: [ # { term: {tags: '周杰伦'}}, # { match: {content: '周杰伦'}} # ] }, aggregations: { tags: { terms: {field: 'tags'} }, min_time: { min: {field: 'created_time'} }, stats_time:{ stats: {field:'created_time'} }, value_count: { value_count: {field: 'created_time'} } } } mash = Hashie::Mash.new response # puts mash.hits.hits.first._source.site_user # puts puts mash.aggregations.tags.buckets.collect{|x| [x['key'],x['doc_count']]}.to_h end
相关文章推荐
- 命令行下操作常用语句
- Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules
- 数据包结构
- 右键git bash here和git gui here失效恢复
- Java进阶(五十一)Could not create the view: An unexpected exception was thrown
- 非法终端接入管控的三部曲:控、查、导
- 非法终端接入管控的三部曲:控、查、导
- Java进阶(五十一)Could not create the view: An unexpected exception was thrown
- Centos7源码安装Emacs24.5
- JAVA如何随机生成一个汉字
- Java API —— Map接口
- [Cryptography] Network Security Part II
- 最近写了一些东西,可以共同学习
- Swift 设置按钮圆角 代码&Xib
- 3D Touch开发初体验
- 时间序列分析
- 分析app和wap手机网站的不同
- Linux 学习001
- online_judge_1131
- Screwturn搭建企业内部wiki