使用ElasticSearch我们可以构建一个功能完备的搜索服务器.
这一切实现起来都很简单,本文将花五分钟向你介绍如何实现.
安装和运行Elasticsearch这篇文章的操作环境是Linux或者Mac,在安装ElasticSearch之前,确保你的系统上已经安装好JDK6或者以上版本.
wgethttps://download.
elastic.
co/elasticsearch/elasticsearch/elasticsearch-1.
7.
2.
tar.
gztar-zxvfelasticsearch-1.
7.
2.
tar.
gzcdelasticsearch-1.
7.
2bin/elasticsearch然后你将在终端看到如下输出:[2015-09-1415:32:52,278][INFO][node][BigMan]version[1.
7.
2],pid[10907],build[e43676b/2015-09-14T09:49:53Z][2015-09-1415:32:52,279][INFO][node][BigMan]initializing.
.
.
[2015-09-1415:32:52,376][INFO][plugins][BigMan]loaded[],sites[][2015-09-1415:32:52,426][INFO][env][BigMan]using[1]datapaths,mounts[[/(/dev/sdc1)]],netusable_space[8.
7gb],nettotal_space[219.
9gb],types[ext3]JavaHotSpot(TM)ServerVMwarning:Youhaveloadedlibrary/tmp/es/elasticsearch-1.
7.
2/lib/sigar/libsigar-x86-linux.
sowhichmighthavedisabledstackguard.
TheVMwilltrytofixthestackguardnow.
It'shighlyrecommendedthatyoufixthelibrarywith'execstack-c',orlinkitwith'-znoexecstack'.
[2015-09-1415:32:55,294][INFO][node][BigMan]initialized[2015-09-1415:32:55,294][INFO][node][BigMan]starting.
.
.
[2015-09-1415:32:55,411][INFO][transport][BigMan]bound_address{inet[/0:0:0:0:0:0:0:0:9300]},publish_address{inet[/192.
168.
43.
172:9300]}[2015-09-1415:32:55,428][INFO][discovery][BigMan]elasticsearch/VKL1HQmyT_KRtmTGznmQyg[2015-09-1415:32:59,210][INFO][cluster.
service][BigMan]new_master[BigMan][VKL1HQmyT_KRtmTGznmQyg][Happy][inet[/192.
168.
43.
172:9300]],reason:zen-disco-join(elected_as_master)[2015-09-1415:32:59,239][INFO][http][BigMan]bound_address{inet[/0:0:0:0:0:0:0:0:9200]},publish_address{inet[/192.
168.
43.
172:9200]}[2015-09-1415:32:59,239][INFO][node][BigMan]started1/5[2015-09-1415:32:59,284][INFO][gateway][BigMan]recovered[0]indicesintocluster_state现在你的系统上成功运行了Elasticsearch!
你可以在浏览器里面访问http://localhost:9200,这时候浏览器里面会返回以下内容:{"status":200,"name":"BigMan","cluster_name":"elasticsearch","version":{"number":"1.
7.
2","build_hash":"e43676b1385b8125d647f593f7202acbd816e8ec","build_timestamp":"2015-09-14T09:49:53Z","build_snapshot":false,"lucene_version":"4.
10.
4"},"tagline":"YouKnow,forSearch"}索引数据现在我们往ElasticSearch里面构建一些数据,我们假设有一个博客系统,里面有一些文章和评论相关的数据,现在将这些信息添加到ElasticSearch里面:curl-XPUT'http://localhost:9200/blog/user/dilbert'-d'{"name":"DilbertBrown"}'curl-XPUT'http://localhost:9200/blog/post/1'-d'{"user":"dilbert","postDate":"2011-12-15","body":"Searchishard.
Searchshouldbeeasy.
","title":"Onsearch"}'curl-XPUT'http://localhost:9200/blog/post/2'-d'{"user":"dilbert","postDate":"2011-12-12","body":"Distributionishard.
Distributionshouldbeeasy.
",2/5"title":"Ondistributedsearch"}'curl-XPUT'http://localhost:9200/blog/post/3'-d'{"user":"dilbert","postDate":"2011-12-10","body":"Loremipsumdolorsitamet,consectetueradipiscingelit,seddiamnonummynibheuismodtinciduntutlaoreetdoloremagnaaliquameratvolutpat","title":"Loremipsum"}'上面的每一步请求,你都会接受到一个响应,里面会证明你的请求成功,如下:{"ok":true,"_index":"blog","_type":"post","_id":"1","_version":1}现在我们来看看上面的请求是否正的成功了:curl-XGET'http://localhost:9200/blog/user/dilbertpretty=true'curl-XGET'http://localhost:9200/blog/post/1pretty=true'curl-XGET'http://localhost:9200/blog/post/2pretty=true'curl-XGET'http://localhost:9200/blog/post/3pretty=true'注意:往ElasticSearch里面添加数据主要有两种方式:通过HTTP发送Json数据;内置提供的客户端(Nativeclient).
搜索我们来看看我们是否可以检索我们刚刚通过搜索添加的文档,下面的例子是找出所有Dilbert作者的文章:curl'http://localhost:9200/blog/post/_searchq=user:dilbert&pretty=true'3/5上面的请求将会返回以下的Json结果:{"took":85,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":1,"hits":[{"_index":"blog","_type":"post","_id":"1","_score":1,"_source":{"user":"dilbert","postDate":"2011-12-15","body":"Searchishard.
Searchshouldbeeasy.
","title":"Onsearch"}},{"_index":"blog","_type":"post","_id":"2","_score":0.
30685282,"_source":{"user":"dilbert","postDate":"2011-12-12","body":"Distributionishard.
Distributionshouldbeeasy.
","title":"Ondistributedsearch"}},{"_index":"blog","_type":"post","_id":"3","_score":0.
30685282,"_source":{"user":"dilbert","postDate":"2011-12-10",4/5"body":"Loremipsumdolorsitamet,consectetueradipiscingelit,seddiamnonummynibheuismodtinciduntutlaoreetdoloremagnaaliquameratvolutpat","title":"Loremipsum"}}]}}下面例子搜索标题不含search的文章curl'http://localhost:9200/blog/post/_searchq=-title:search&pretty=true'下面例子搜索标题包含search但是不包含distributed的文章:curl'http://localhost:9200/blog/post/_searchq=+title:search%20-title:distributed&pretty=true&fields=title'下面例子通过postDate字段搜索一定时间范围内的文章:curl-XGET'http://localhost:9200/blog/_searchpretty=true'-d'{"query":{"range":{"postDate":{"from":"2011-12-10","to":"2011-12-12"}}}}'好了,现在我们已经学习了ElasticSearch的一些基本的使用情况.
本博客文章除特别声明,全部都是原创!
原创文章版权归过往记忆大数据(过往记忆)所有,未经许可不得转载.
本文链接:【】()PoweredbyTCPDF(www.
tcpdf.
org)5/5
wordpress简洁英文主题,wordpress简洁通用大气的网站风格设计 + 更适于欧美国外用户操作体验,完善的外贸企业建站功能模块 + 更好的移动设备特色模块支持,更高效实用的后台自定义设置 + 标准高效的代码程序功能结构,更利于Goolge等国际搜索引擎的SEO搜索优化和站点收录排名。点击进入:wordpress简洁通用型高级外贸主题主题价格:¥3980 特 惠 价:¥1280安装环境:运...
青云互联怎么样?青云互联是一家成立于2020年6月的主机服务商,致力于为用户提供高性价比稳定快速的主机托管服务,目前提供有美国免费主机、香港主机、香港服务器、美国云服务器,让您的网站高速、稳定运行。美国cn2弹性云主机限时8折起,可选1-20个IP,仅15元/月起,附8折优惠码使用!点击进入:青云互联官方网站地址青云互联优惠码:八折优惠码:ltY8sHMh (续费同价)青云互联活动方案:美国洛杉矶...
Sharktech荷兰10G带宽的独立服务器月付319美元起,10Gbps共享带宽,不限制流量,自带5个IPv4,免费60Gbps的 DDoS防御,可加到100G防御。CPU内存HDD价格购买地址E3-1270v216G2T$319/月链接E3-1270v516G2T$329/月链接2*E5-2670v232G2T$389/月链接2*E5-2678v364G2T$409/月链接这里我们需要注意,默...
博客系统为你推荐
天玑1000plus和骁龙865哪个好哪种5G手机是联发科天玑1000芯片?闪迪和金士顿哪个好u盘是金士顿好还是闪迪好?浏览器哪个好用浏览器哪个好朗逸和速腾哪个好大众速腾与朗逸哪个好浮动利率和固定利率哪个好房贷须知:固定还是浮动利率好无纺布和熔喷布口罩哪个好表层水刺布,中间层pp无纺布+熔喷布,里层pp无纺布口罩好吗?手机管家哪个好手机管家和腾讯手机管家哪个好用电动牙刷哪个好电动牙刷哪个牌子比较好,不要那么贵的qq空间登录电脑求助,怎么登陆电脑版的qq空间qq空间登录网址开通QZONe
中国十大域名注册商 Oray域名注册服务商 冰山互联 荷兰服务器 香港vps99idc hostgator 安云加速器 视频存储服务器 360抢票助手 国内加速器 空间服务商 linux空间 国外免费全能空间 seednet idc资讯 169邮箱 双11秒杀 能外链的相册 吉林铁通 太原联通测速 更多