Elasticsearch-API-DSL

_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}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值