_template
如果用户每次新建一个索引的时候都需要手动创建mapping非常麻烦,Elasticsearch内部维护了template,template定义好了mapping,只要index的名称被template匹配到,那么该index的mapping就按照template中定义的mapping自动创建
# 创建模板
PUT _template/temp
{
"order": 0,
"index_patterns": ["te*", "bar*"],
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"_doc": {
"_source": {
"enabled": true
}
}
}
}
# 添加文档及索引创建
POST /test/_doc?pretty
{
"user": "elastic",
"message": "Hello World"
}
GET /_template/temp?pretty
DELETE /_template/temp
基于索引的ID从索引获取类型化的JSON文档
curl -X GET "localhost:9200/user/_doc/1?pretty"
Source filtering
默认情况下,
_source
除非使用了stored_fields
参数或_source
禁用了该字段,否则get操作将返回该字段的内容。您可以_source
使用以下_source
参数关闭检索curl -X GET "localhost:9200/user/_doc/2?_source=false&pretty"
如果只需要完整字段中的一两个字段,则
_source
可以使用_source_includes
和_source_excludes
参数来包含或过滤出所需的部分。这对于大型文档特别有用,在大型文档中,部分检索可以节省网络开销。这两个参数均采用逗号分隔的字段或通配符表达式列表:GET twitter/_doc/0?_source_includes=*.id&_source_excludes=entities
如果只想指定包含,则可以使用较短的符号:
GET twitter/_doc/0?_source=*.id,retweeted
Getting the _source
directly
Use the
/{index}/{type}/{id}/_source #
仅获取_source
文档的字段curl -X GET "localhost:9200/user/_doc/2/_source?pretty"
您也可以使用相同的源过滤参数来控制
_source
将返回的哪些部分:GET twitter/_doc/1/_source?_source_includes=*.id&_source_excludes=entities curl -I "localhost:9200/user/_doc/2/_source?pretty"
注意:_source端点还有一个HEAD变体,如果现有文档在mapping被禁用,则将没有_source : 可开启,
PUT user { "mappings": { "_doc": { "_source": { "enabled": true } } } }
Multi Get API:
GET /_mget { "docs" : [ { "_index" : "user", "_type" : "_doc", "_id" : "1", "_source" : false }, { "_index" : "test", "_type" : "_doc", "_id" : "2", "_source" : ["field3", "field4"] } ] }
GET /test/_doc/_mget { "docs" : [ { "_id" : "1" }, { "_id" : "2" } ] }
GET /test/_doc/_mget { "ids" : ["1", "2"] }
Query DSL
##### Query and filter context 查询和过滤上下文 bool, filter ##### Compound queries 复合查询: Boolean(布尔查询) ##### Full text queries 全文查询: match(匹配查询), match_phrase(匹配词组查询), match_phrase_prefix(匹配词组前缀查询), multi_match(允许多字段查询) query_string(查询字符串查询), simple_query_string(简单查询字符串查询) ##### Match all 匹配所有查询 match_all ##### Term-level queries Exists(存在查询,查找缺少索引值的文档) Fuzzy(模糊匹配) wildcard(通配符查询) IDs Prefix(前缀查询) Range(范围查询) Regexp(正则表达式查询) term(返回在提供的字段中包含确切术语的文档) terms(返回在提供的字段中包含一个或多个确切术语的文档) term和terms 不会对查询对字段进行分词处理,适合于price,productId,username,date、num、id等确切数据进行搜索 terms_set
kibana:blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];
curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:9200/_all/_settings -d '{"index.blocks.read_only_allow_delete": null}'
{"acknowledged":true}