赞
踩
目录
中文手册:https://elasticsearch.cn ElasticSearch 搜索引擎及索引建立引擎 kibana 可视化管理工具
下载:5.5.3版本 解压即可。
执行bin/elasticsearch
即可。如果想后端运行,那么执行bin/elasticserach -d
。
###
让我们在集群中唯一一个空节点上创建一个叫做blogs的索引。默认情况下,一个索引被分配5个主分片,但是为了演示的目的,我们只分配3个主分片和一个复制分片(每个主分片都有一个复制分片):
- PUT /blogs
- {
- "settings" : {
- "number_of_shards" : 3,
- "number_of_replicas" : 1
- }
- }
中文分词插件 IK analyzer 插件官网 安装步骤: ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-6.3.0.zip
参考:Elasticsearch5.x安装IK分词器以及使用
拼音分词器插件 插件官网 安装步骤: ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v5.5.3/elasticsearch-analysis-pinyin-5.5.3.zip
测试: 5.X版本:
- POST http://localhost:9200/_analyze?analyzer=pinyin&pretty=true
- BOYD {"text":"这里是好记性不如烂笔头感叹号的博客们"}
6.x版本:6.0版本移除了_analyze的analyzer参数支持,analyzer测试需要在Body中关键字指定 参考:6.3 Testing analyzers
- POST http://localhost:9200/_analyze
- BOYD
- {
- "analyzer": "pinyin",
- "text": "这里是好记性不如烂笔头感叹号的博客们"
- }
测试结果:
- {
- "tokens": [
- {
- "token": "zhe",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 0
- },
- {
- "token": "zlshjxbrlbtgthdb",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 0
- },
- {
- "token": "li",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 1
- },
- {
- "token": "shi",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 2
- },
- {
- "token": "hao",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 3
- },
- {
- "token": "ji",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 4
- },
- {
- "token": "xing",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 5
- },
- {
- "token": "bu",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 6
- },
- {
- "token": "ru",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 7
- },
- {
- "token": "lan",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 8
- },
- {
- "token": "bi",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 9
- },
- {
- "token": "tou",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 10
- },
- {
- "token": "gan",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 11
- },
- {
- "token": "tan",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 12
- },
- {
- "token": "hao",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 13
- },
- {
- "token": "de",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 14
- },
- {
- "token": "bo",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 15
- },
- {
- "token": "ke",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 16
- },
- {
- "token": "men",
- "start_offset": 0,
- "end_offset": 0,
- "type": "word",
- "position": 17
- }
- ]
- }
索引
创建索引
- #请求
- PUT twitter
- #响应
- {
- "acknowledged": true,
- "shards_acknowledged": true,
- "index": "test-base"
- }
- #默认配置的索引的配置如下
- {
- "twitter": {
- "aliases": {},
- "mappings": {},
- "settings": {
- "index": {
- "creation_date": "1539050228177",
- "number_of_shards": "5",
- "number_of_replicas": "1",
- "uuid": "x-yRLPprS_mMqe_yXcoibQ",
- "version": {
- "created": "6030299"
- },
- "provided_name": "twitter"
- }
- }
- }
- }
- #请求
- PUT twitter
- {
- "settings" : {
- "index" : {
- "number_of_shards" : 3,
- "number_of_replicas" : 2
- }
- }
- }
- #响应
- {
- "acknowledged": true,
- "shards_acknowledged": true,
- "index": "twitter"
- }
- #请求
- PUT twitter
- {
- "settings" : {
- "number_of_shards" : 3,
- "number_of_replicas" : 2
- }
- }
- #请求
- PUT test
- {
- "settings" : {
- "number_of_shards" : 1
- },
- "mappings" : {
- "type1" : {
- "properties" : {
- "field1" : { "type" : "text" }
- }
- }
- }
- }
- #结果
- {
- "test": {
- "aliases": {},
- "mappings": {
- "type1": {
- "properties": {
- "field1": {
- "type": "text"
- }
- }
- }
- },
- "settings": {
- "index": {
- "creation_date": "1539051572751",
- "number_of_shards": "1",
- "number_of_replicas": "1",
- "uuid": "ioXgmR5ISiy76HLrZCjc0w",
- "version": {
- "created": "6030299"
- },
- "provided_name": "test"
- }
- }
- }
- }
查询索引
- #请求
- GET twitter
- #响应
- {
- "twitter": {
- "aliases": {},
- "mappings": {},
- "settings": {
- "index": {
- "creation_date": "1539050228177",
- "number_of_shards": "5",
- "number_of_replicas": "1",
- "uuid": "x-yRLPprS_mMqe_yXcoibQ",
- "version": {
- "created": "6030299"
- },
- "provided_name": "twitter"
- }
- }
- }
- }
删除索引
- #请求
- DELETE twitter
- #响应
- {
- "acknowledged": true
- }
检测索引是否存在
- #请求
- HEAD twitter
- #响应
- ##存在
- 200 - OK
- ##不存在
- 404 - Not Found
-
关闭/打开索引
- #请求
- POST /my_index/_open
- #响应
- {
- "acknowledged": true,
- "shards_acknowledged": true
- }
- #请求
- POST /my_index/_close
- #响应
- {
- "acknowledged": true
- }
对于已经关闭的索引在集群上基本没有开销(除了维护元数据),已经关闭的索引不允许进行读写操作。
可以打开或关闭多个索引,如果有索引不存在,则会报错,可以通过使用ignore_unavailable=true
参数忽略索引不存在的异常。
- #请求
- POST /my_index2,my_index/_close?ignore_unavailable=true
修改索引
- PUT /my_source_index/_settings
- {
- "settings": {
- "index.blocks.write": true
- }
- }
- # 之后的写请求的返回
- {
- "error": {
- "root_cause": [
- {
- "type": "cluster_block_exception",
- "reason": "blocked by: [FORBIDDEN/8/index write (api)];"
- }
- ],
- "type": "cluster_block_exception",
- "reason": "blocked by: [FORBIDDEN/8/index write (api)];"
- },
- "status": 403
- }
- PUT /test/_settings
- {
- "settings": {
- "index.blocks.write": false
- }
- }
收缩索引 参考:Shrink Index
拆分索引 参考: Split Index
设置索引的Mappings
只允许新增字段到索引Mappings或者修改已有的字段的搜索设置。
对于Object类型的字段,可以添加新的子字段。
参考: PUT Mappings properties Mapping parameters Field datatypes
索引的配置信息
参考:index-modules 索引的配置分为两部分,一部分为静态的,一部分为动态的。
注:更改已关闭索引上的静态或动态索引设置可能会导致不正确的设置,如果不删除并重新创建索引,则无法纠正这些设置。
执行安装目录下的bin/kibana
命令即可。
注意:必须在ElasticSearch 启动之后才能正常访问Kibana,否则提示无法登录。
python main.py Kibana目录
调研问题列表
Suggestion
实现,参考search-suggesters-completion,详情见笔记:ElasticSearch 的使用-关键字补全Highlighter
实现,参考search-request-highlighting,详情见笔记:ElasticSearch 的使用-高亮查询。目前采用修改前先读取doc的全部数据,再在程序里更新为最终的数据,最后通过新增/替换命令更新整个doc。
long
scaled_float
并设置scaling_factor
值为100
即保留2位小数。text
,不分词使用keyword
nested
date
object
字段本身
使用标准解析器分词字段.raw
不分词字段.chinese
索引使用中文最大分词,查询使用中文智能分词raw
和chinese
子字段。"yyyy/MM/dd HH:mm:ss.SSSSSS||yyyy/MM/dd||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd HH:mm:ss.SSS||epoch_millis"
格式。调研问题列表
调研问题列表
Fielddata is disabled on text fields by default. Set fielddata=true on [interests] in order to load
问题原因,是indeed的mapping字段没有设置
fielddata=true
。处理方法如下: 执行以下请求:
PUT megacorp/_mapping/employee { "employee": { "properties": { "interests": { "type": "text", "fielddata": true } } } }
在Linux上不允许以root用户启动
_doc
,参考删除映射类型良好的ES性能的关键是将数据去规范化为文档。JOIN字段会带来额外的性能开销。参考parent_join_and_performance
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。