Elasticsearch基于Luence,是一个受到广泛使用的收索引擎。像Github、Wikipedia这样的大型网站都在使用Elasticsearch用作其站内搜索。
怀着好奇心,自己就装了装Elasticsearch来玩玩儿。
下载安装Elasticsearch
http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_installing_elasticsearch.html
当Server启动以后,可以使用如下的命令查看Server是否正常运行:
curl 'http://localhost:9200/?pretty'如果正常,将会看到这样的结果:
{
"status": 200,
"name": "Shrunken Bones",
"version": {
"number": "1.1.0",
"lucene_version": "4.7"
},
"tagline": "You Know, for Search"
}
Indexing
在Elasticsearch中,文档(document)有其归属的类型(type),而类型又归属于索引(index)
一个简单的传统型关系数据库与elasticsearch的大致对应关系如下:
Relational DB | Databases |Tables|Rows |Columns
Elasticsearch | Indices |Types |Documents|Fields
Elasticsearch使用的是RESTFul API。如果将一个文档进行索引,其实意思大概可以理解为将一个文档插入到elasticsearch中去。如:
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
其中,megacorp指的是index名称,employee是类型名称,1指的是这个document的id。
查询
当我们将信息index到Elasticsearch以后,我们就可以进行查询了。
比如我们想要查找megacorp中employee得last_name为Smith的时候,使用如下命令就可以
GET /megacorp/employee/_search?q=last_name:Smith返回结果大致如下:
{Elasticsearch还支持文本搜索和语义搜索,这里就不赘述了。
...
"hits": {
"total": 2,
"max_score": 0.30685282,
"hits": [
{
...
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
},
{
...
"_source": {
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests": [ "music" ]
}
}
]
}
}
更多的查询相关信息可以参考:http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_search_lite.html
参考资料
http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/getting-started.html