ElasticSearch初窥

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放到浏览器中访问,就能看到结果。

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

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

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

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow 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 datetime
from 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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值