使用ElasticSearch索引和搜索记录。
kinto-elasticsearch的Python项目详细描述
kinto elasticsearch将记录转发到elasticsearch并提供一个/search 查询索引数据的终结点。
安装
pip install kinto-elasticsearch
设置
在Kinto设置中:
kinto.includes=kinto_elasticsearchkinto.elasticsearch.hosts=localhost:9200
默认情况下,ElasticSearch是智能的,索引不会在每次更改时刷新。 您可以强制执行此操作(在性能上有一定的缺陷):
kinto.elasticsearch.force_refresh=true
默认情况下,索引名称的前缀是kinto-。您可以使用:
kinto.elasticsearch.index_prefix=myprefix
运行elasticsearch
用docker在localhost:9200上运行elasticsearch的本地安装非常简单:
sudo docker run -p 9200:9200 elasticsearch
它也可以手动安装在ubuntu上:
sudo apt-get install elasticsearch
更多信息请参见official docs。
用法
创建新记录:
$ echo '{"data": {"note": "kinto"}}' | http POST http://localhost:8888/v1/buckets/example/collections/notes/records --auth token:alice-token
现在应该可以使用ElasticSearch API搜索它了。
例如,使用快速查询字符串搜索:
$ http "http://localhost:8888/v1/buckets/example/collections/notes/search?q=note:kinto"--auth token:alice-token
或使用请求正文进行高级搜索:
$ echo '{"query": {"match_all": {}}}' | http POST http://localhost:8888/v1/buckets/example/collections/notes/search --auth token:alice-token
HTTP/1.1200OKAccess-Control-Expose-Headers:Retry-After, Content-Length, Alert, BackoffContent-Length:333Content-Type:application/json; charset=UTF-8Date:Wed, 20 Jan 2016 12:02:05 GMTServer:waitress{"_shards":{"failed":0,"successful":5,"total":5},"hits":{"hits":[{"_id":"453ff779-e967-4b08-99b9-5c16af865a67","_index":"example-assets","_score":1.0,"_source":{"id":"453ff779-e967-4b08-99b9-5c16af865a67","last_modified":1453291301729,"note":"kinto"},"_type":"example-assets"}],"max_score":1.0,"total":1},"timed_out":false,"took":20}
自定义索引映射
默认情况下,elasticsearch从索引记录推断数据类型。
但是可以从集合元数据定义索引映射(即模式)。 在index:schema属性中:
$ echo'{ "data": { "index:schema": { "properties": { "id": {"type": "keyword"}, "last_modified": {"type": "long"}, "build": { "properties": { "date": {"type": "date", "format": "strict_date"}, "id": {"type": "keyword"} } } } } } }'| http PATCH "http://localhost:8888/v1/buckets/blog/collections/builds" --auth token:admin-token --verbose
有关映射的更多信息,请参阅ElasticSearch官方文档。
另请参见domapping一个用于将json模式转换为elasticsearch映射的cli工具。
运行测试
$ make tests
更改日志
0.3.1(2018-04-11)
错误修复
- 修复reindex get_paginated_records函数。(修复61)
0.3.0(2017-09-12)
新功能
- 添加statsd计时器以测量e/s指数化(修复54)
0.2.1(2017-06-14)
错误修复
- 在查询中指定时修复结果数(ref 45)
0.2.0(2017-06-13)
错误修复
- 限制默认返回的结果数(修复程序45)
- 修复搜索分析异常时的崩溃(修复44)
0.1.0(2017-05-26)
新功能
- 刷新服务器时刷新索引(修复了4)
- 批量执行插入和删除以提高效率(修复5)
- 添加更改时强制索引刷新的设置(修复6)
- 添加心跳(修复3)
- 删除存储桶和集合时删除索引(修复21)
- 支持从querystring快速搜索(修复程序34)
- 返回有关请求正文中无效查询的详细信息(修复程序23)
- 支持从集合元数据中的index:schema属性定义映射(ref 8)
错误修复
- 只有在提交了存储事务时才索引记录(修复了15)
- 如果集合或存储桶没有读取权限,则不允许搜索(修复了7)
- 修复在集合创建后启用插件时的空结果响应(ref 20)
内部更改
- 创建集合时创建索引(修复27)