ElasticSearch初窥

来源:互联网 发布:主题模型 知乎 编辑:程序博客网 时间:2024/05/19 03:17

ElasticSearch介绍

  • 基于Lucene
  • 开源,分布式,可扩展,搜索引擎
  • 实时搜索、分析
  • 大数据
  • 全文搜索+结构化搜索

搜索引擎的核心:倒排索引

Elasticsearch使用一种叫做倒排索引(inverted index)的结构来做快速的全文搜索。

倒排索引是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射。

例如,我们有两个文档,每个文档content字段包含:

  • (1) The quick brown fox jumped over the lazy dog
  • (2) Quick brown foxes leap over lazy dogs in summer

我们为其构建一个字典,记录这些词在哪些文档中出现过:

["Quick", "The", "brown", "dog", "dogs", "fox", "foxes", "in", "jumped", "lazy", "leap", "over", "quick", "summer", "the"]

现在,如果我们想搜索”quick brown”,我们只需要找到每个词在文档中出现次数:

  • (1) [1,1]
  • (2) [0,1]

两个文档都匹配,但是第一个比第二个有更多的匹配项。 如果我们加入简单的相似度算法(similarity algorithm),计算匹配单词的数目,这样我们就可以说第一个文档比第二个匹配度更高——对于我们的查询具有更多相关性。

更多细节,请查看ES的倒排索引。

ES安装

我在Win-10下安装配置的过程如下:

  • 下载JDK8,安装,配置环境变量
  • 下载elasticsearch-5.4.0.zip,解压
  • 在CMD中,运行如下命令,启动ES
elasticsearch-5.4.0\bin>elasticsearch.bat
  • 在浏览器访问http://localhost:9200/(用GET访问ES的API),如果看到如下提示信息,表示ES成功启动
{  "name" : "buMOTc0",  "cluster_name" : "elasticsearch",  "cluster_uuid" : "9sOwApMzRNqsstGhCXXE5Q",  "version" : {    "number" : "5.4.0",    "build_hash" : "780f8c4",    "build_date" : "2017-04-28T17:43:27.229Z",    "build_snapshot" : false,    "lucene_version" : "6.5.0"  },  "tagline" : "You Know, for Search"}

ES基本API

可以用ES的RESTFul API查询ES的基本信息。为了方便我们做实验,下面给出URL,如果是GET访问的,直接将URL放到浏览器中访问,就能看到结果。

  • (1) Cluster健康状况

    • 查看集群信息:http://localhost:9200/_cat/health?v
    • 查看结点信息:http://localhost:9200/_cat/nodes?v
  • (2) 查看所有索引

    • http://localhost:9200/_cat/indices?v
  • (3) 创建一个索引(索引名为customer

    • PUT http://localhost:9200/customer?pretty

用POSTMAN的PUT请求这个URL。得到如下回复:

{  "acknowledged": true,  "shards_acknowledged": true}

然后,查看索引(2中有讲解),就能看到创建的这个索引:

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.sizeyellow open   customer r45CR8ZKTO64ze8I-tgWpw   5   1          0            0       650b           650b
  • (4) 对索引中添加一个document

将下面这样一个key-value的document添加到customer索引中,肯定使用PUT请求。

{  "name": "John Doe"}

在POSTMAN中,用PUT请求访问http://localhost:9200/customer/external/1?pretty(这里将ID设置为1),如下所示:

[PIC-1]

    1. 查询某个索引中的document

用GET http://localhost:9200/customer/external/1?pretty,能查询名为customer的document中,id为1的那个document内容。具体内容如下:

{  "_index" : "customer",  "_type" : "external",  "_id" : "1",  "_version" : 1,  "found" : true,  "_source" : {    "name" : "John Doe"  }}

可以看到上例中的key-value内容(”name” : “John Doe”)。

用python访问ES

pip安装elasticsearch,即可运行如下代码,代码功能见注释。

from datetime import datetimefrom elasticsearch import Elasticsearch# 生成es实例es = Elasticsearch([{'host':'localhost', 'port':9200}], timeout=60000)# document示例doc = {    'author': 'kimchy',    'text': 'Elasticsearch: cool. bonsai cool.',    'timestamp': datetime.now(),}# 创建一个索引(索引名为`test-index`)res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)print(res['created'])# return True# 获取索引‘test-index’中,id为1的document内容res = es.get(index="test-index", doc_type='tweet', id=1)print(res['_source'])# {'text': 'Elasticsearch: cool. bonsai cool.', 'author': 'kimchy', 'timestamp': '2017-05-08T14:55:57.232983'}es.indices.refresh(index="test-index")# 查询res = es.search(index="test-index", body={"query": {"match_all": {}}})print("Got %d Hits:" % res['hits']['total'])for hit in res['hits']['hits']:    print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])    # 2017-05-08T14:55:57.232983 kimchy: Elasticsearch: cool. bonsai cool.
0 0
原创粉丝点击