Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎。不管在开源仍是专有领域,Lucene能够被认为是迄今为止最早进、性能最好的、功能最全的搜索引擎库。linux
下面展现了在关系型数据库中和ElasticSearch中对应的存储字段:git
Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices -> Types -> Documents -> Fieldsgithub
这里的索引和传统的关系型数据库中索引有些不一样,在ElasticSearch中,一个索引就像是传统关系型数据库中的数据库。web
在ElasticSearch和Lucene中,使用的是一种叫倒排索引的数据结构加速检索。chrome
因为机器有限,因此我采用的是使用虚拟机安装ElasticSearch。数据库
实验环境以下:npm
能够在ElasticSearch官网下载最新版的ElasticSearch。json
下载地址:https://www.elastic.co/downloads/elasticsearchvim
若是使用Windows则下载zip包,使用linux则下载tar.gz包。centos
安装以前须要安装JDK1.7以上版本。
在Windows下方法比较简单,解压zip包后,经过DOS命令行切换到bin目录,运行elasticsearch便可,能够经过查看localhost:9200端口,看是否启动成功。
在Linux中安装与使用
在linux中新建一个目录,名字为elasticsearch,使用Xftp或者WinSCP等工具将刚才下载的安装包传输到该目录下。
tar -zxvf elasticsearch-5.2.2.tar.gz解压文件。
进入到解压后的文件,输入.bin/elasticsearch便可打开elasticsearch。
可能出现的问题:
访问 ip + 9200,结果以下,ElasticSearch开启成功。
elasticsearch-head是一个很好的管理工具,能够可视化看到集群的情况。
下载地址:https://github.com/mobz/elasticsearch-head
在elasticsearch5.0后,废弃了使用plugin安装的方式,可使用git先clone代码,使用npm安装。
访问 ip + 9100,记得开放端口。
在chrome或者火狐浏览器中可能会出现跨域问题而没法链接,可使用IE链接。
在官网能够查看使用多种语言的API,在这里使用最简单的基于HTTP协议的API,使用JSON格式数据。
在linux中,输入:
curl -XPUT 'http://192.168.91.129:9200/website/blog/1' -d '{"title":"liuyang", "text":"太阳在头顶之上", "date":"2017-4-16"}'
这样就能够插入一条数据,在插入多条数据后,在浏览器输入:
http://192.168.91.129:9200/_search?pretty
在结果中能够看到有多条记录。pretty的做用是为了让JSON优化排版,也能够直接使用curl -XGET获取数据。
查询字符串
在浏览器输入:
http://192.168.91.129:9200/_search?q=太阳
在q后面加上本身想要查找的字符串,就能够进行匹配了。
_score字段为匹配程度,越高的结果就会越在前面。
格式化结果后:
{ "took": 292, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 3, "max_score": 0.56008905, "hits": [ { "_index": "website", "_type": "blog", "_id": "2", "_score": 0.56008905, "_source": { "title": "title2", "text": "小路弯弯,太阳正在头顶上", "date": "2017-4-16" } }, { "_index": "website", "_type": "blog", "_id": "3", "_score": 0.5446649, "_source": { "title": "title3", "text": "最美的太阳", "date": "2017-4-16" } }, { "_index": "website", "_type": "blog", "_id": "1", "_score": 0.48515025, "_source": { "title": "liuyang", "text": "太阳在头顶之上", "date": "2017-4-16" } } ] } }
全文搜索
在linux中输入:
curl -XGET 'http://192.168.91.129:9200/_search' -d '{"query":{"match":{"text":"太阳"}}}'
后面的JSON为搜索的具体field,这样就能够作到在关系型数据库中很难作到的全文检索。
从输出结果中能够看到,每个document都有对应的_score字段,这就是对此document的相关性评分。
格式化输出:
{ "took": 44, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 3, "max_score": 0.5716521, "hits": [ { "_index": "website", "_type": "blog", "_id": "1", "_score": 0.5716521, "_source": { "title": "liuyang", "text": "太阳在头顶之上", "date": "2017-4-16" } }, { "_index": "website", "_type": "blog", "_id": "3", "_score": 0.5649868, "_source": { "title": "title3", "text": "最美的太阳", "date": "2017-4-16" } }, { "_index": "website", "_type": "blog", "_id": "2", "_score": 0.48515025, "_source": { "title": "title2", "text": "小路弯弯,太阳正在头顶上", "date": "2017-4-16" } } ] } }
经过对比两种搜索方式的结果可知,使用全文搜索的匹配更加知足咱们的预期。